-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathgames_tab.pas
More file actions
3256 lines (2889 loc) · 102 KB
/
Copy pathgames_tab.pas
File metadata and controls
3256 lines (2889 loc) · 102 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit games_tab;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, process, Forms, Controls, Graphics, Dialogs, ExtCtrls, Math,
unix, BaseUnix, StdCtrls, Spin, ComCtrls, Buttons, ActnList, Menus,
LCLtype, Clipbrd, LCLIntf, IniFiles, FileUtil, StrUtils, Types, fpjson,
jsonparser, themeunit, systemdetector, constants, bgmod_resources, hintsunit,
configmanager, IntfGraphics, Grids, overlayunit, overlay_config, apputils, overlay_utils;
const
CARD_W = 150;
CARD_H = 215;
CARD_IMG_H = 215;
GRAD_H = 55;
CARD_MARGIN = 8;
SEL_EXPAND = 3;
type
TNonSteamCoverItem = record
GameName: string;
CachePath: string;
CardIndex: Integer;
end;
TPortMapping = record
PortName: string;
GameName: string;
end;
TCoverDownloadThread = class(TThread)
private
FAppIDs: TStringList;
FImages: TList;
FCacheDir: string;
FForm: Tgoverlayform;
FCurrentImage: TImage;
FCurrentPath: string;
procedure DoUpdateImage;
procedure DoGenerateFallback;
protected
procedure Execute; override;
public
constructor Create(AAppIDs: TStringList; AImages: TList;
const ACacheDir: string; AForm: Tgoverlayform);
destructor Destroy; override;
end;
TNonSteamCoverThread = class(TThread)
private
FItems: array of TNonSteamCoverItem;
FForm: Tgoverlayform;
FCurrentCardIdx: Integer;
FCurrentPath: string;
FCurrentIsFallback: Boolean;
procedure DoUpdateImage;
procedure DoGenerateFallback;
protected
procedure Execute; override;
public
constructor Create(const AItems: array of TNonSteamCoverItem;
AForm: Tgoverlayform);
end;
TGamesTabHelper = class
private
FForm: Tgoverlayform;
public
constructor Create(AForm: Tgoverlayform);
procedure InitGamesTab;
procedure LoadSteamGames;
procedure LoadNonSteamFolders(var ACardIndex: Integer; const ACardsPerRow, ARowMargin: Integer);
procedure CoverThreadTerminated(Sender: TObject);
procedure DrawCardRibbon(Bmp: TBitmap; BadgeMask: Integer);
function SearchSteamStoreGame(const AGameName: string; out AAppId: string): Boolean;
function DownloadSteamCover(const AAppId, ACachePath: string): Boolean;
function SearchWebCover(const AGameName, ACachePath: string): Boolean;
procedure RefreshGameCardsAsync(Data: PtrInt);
procedure LoadGlobalThumb;
procedure ShowGameThumb(ACard: TPanel);
procedure ApplyCardBrightness(ACard: TPanel; BrightFactor: Integer);
procedure ApplyAllCardsDim;
procedure GameCardClick(Sender: TObject);
procedure GameCardUninstallClick(Sender: TObject);
procedure GameCardMouseEnter(Sender: TObject);
procedure GameCardMouseLeave(Sender: TObject);
procedure GameCardMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ActionPanelPaint(Sender: TObject);
procedure ActionPanelMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure ActionPanelClick(Sender: TObject);
function GetCardPanel(AControl: TControl): TPanel;
procedure CreateActionPanel(CardPanel: TPanel);
procedure GameHoverFolderClick(Sender: TObject);
procedure GameHoverPrefixClick(Sender: TObject);
procedure GameHoverUninstallClick(Sender: TObject);
procedure GameCardOpenFolderClick(Sender: TObject);
procedure GameCardOpenPrefixClick(Sender: TObject);
procedure AddNonSteamFolderClick(Sender: TObject);
procedure RemoveFolderMenuItemClick(Sender: TObject);
procedure ShowRemoveFoldersMenu(Sender: TObject; X, Y: Integer);
procedure GamesScrollBoxResize(Sender: TObject);
procedure GamesEmptySpaceClick(Sender: TObject);
procedure RefreshGameCards;
procedure ReflowGamesGrid;
end;
procedure ProcessCoverBitmap(Bmp: TBitmap; GradH: Integer);
procedure GenerateFallbackCover(const APath: string; AForm: Tgoverlayform);
function NormalizeAppImageName(const AFileName: string): string;
function ResolveUnofficialPortName(const AGameName: string): string;
implementation
function NormalizeAppImageName(const AFileName: string): string;
var
BaseName: string;
i, p: Integer;
LowerBase: string;
begin
BaseName := ChangeFileExt(AFileName, ''); // Strip extension (e.g. .appimage)
LowerBase := LowerCase(BaseName);
// Find common architecture/platform suffixes
p := Pos('-linux', LowerBase);
if p > 0 then BaseName := Copy(BaseName, 1, p - 1);
LowerBase := LowerCase(BaseName);
p := Pos('_linux', LowerBase);
if p > 0 then BaseName := Copy(BaseName, 1, p - 1);
LowerBase := LowerCase(BaseName);
p := Pos('-x86_64', LowerBase);
if p > 0 then BaseName := Copy(BaseName, 1, p - 1);
LowerBase := LowerCase(BaseName);
p := Pos('_x86_64', LowerBase);
if p > 0 then BaseName := Copy(BaseName, 1, p - 1);
LowerBase := LowerCase(BaseName);
p := Pos('-x64', LowerBase);
if p > 0 then BaseName := Copy(BaseName, 1, p - 1);
// Strip trailing version suffixes like -v1.4.1 or -1.4.1
LowerBase := LowerCase(BaseName);
for i := Length(LowerBase) downto 2 do
begin
if (LowerBase[i] in ['0'..'9']) and (LowerBase[i-1] = '.') then
Continue;
if (LowerBase[i] = '.') and (LowerBase[i-1] in ['0'..'9']) then
Continue;
if (LowerBase[i] in ['0'..'9']) and (LowerBase[i-1] = 'v') and (i >= 3) and (LowerBase[i-2] in ['-', '_']) then
begin
BaseName := Copy(BaseName, 1, i - 3);
Break;
end;
if (LowerBase[i] in ['0'..'9']) and (LowerBase[i-1] in ['-', '_']) then
begin
BaseName := Copy(BaseName, 1, i - 2);
Break;
end;
end;
Result := Trim(BaseName);
if Result = '' then
Result := ChangeFileExt(AFileName, ''); // Fallback to raw filename without extension if empty
end;
const
STATIC_PORT_MAPPINGS: array[0..38] of TPortMapping = (
(PortName: 'zelda3'; GameName: 'The Legend of Zelda: A Link to the Past'),
(PortName: 'ship of harkinian'; GameName: 'The Legend of Zelda: Ocarina of Time'),
(PortName: '2 ship 2 harkinian'; GameName: 'The Legend of Zelda: Majora''s Mask'),
(PortName: 'zelda64recomp'; GameName: 'The Legend of Zelda: Ocarina of Time'),
(PortName: 'dusklight'; GameName: 'The Legend of Zelda: Twilight Princess'),
(PortName: 'courage reborn'; GameName: 'The Legend of Zelda: Twilight Princess'),
(PortName: 'smw'; GameName: 'Super Mario World'),
(PortName: 'sm'; GameName: 'Super Metroid'),
(PortName: 'mariokart64recomp'; GameName: 'Mario Kart 64'),
(PortName: 'perfect_dark'; GameName: 'Perfect Dark'),
(PortName: 'opengoal'; GameName: 'Jak and Daxter'),
(PortName: 'spacecadetpinball'; GameName: '3D Pinball for Windows - Space Cadet'),
(PortName: 'cannonball'; GameName: 'Outrun'),
(PortName: 'dethrace'; GameName: 'Carmageddon'),
(PortName: 'openmw'; GameName: 'The Elder Scrolls III: Morrowind'),
(PortName: 'openrct2'; GameName: 'RollerCoaster Tycoon 2'),
(PortName: 'daggerfall-unity'; GameName: 'The Elder Scrolls II: Daggerfall'),
(PortName: 'sa2'; GameName: 'Sonic Advance 2'),
(PortName: 'tmc'; GameName: 'The Legend of Zelda: The Minish Cap'),
(PortName: 'forest'; GameName: 'Animal Crossing'),
(PortName: 'pokeplatinum'; GameName: 'Pokémon Platinum'),
(PortName: 'metaforce'; GameName: 'Metroid Prime'),
(PortName: 'psydoom'; GameName: 'Doom'),
(PortName: 'pt for pc'; GameName: 'PT'),
(PortName: 'rec98'; GameName: 'Touhou 1 - The Highly Responsive to Prayers'),
(PortName: 'rsdkv5-decompilation'; GameName: 'Sonic Mania'),
(PortName: 'sonic 3: angel island revisited'; GameName: 'Sonic 3 & Knuckles'),
(PortName: 'wipeout phantom edition'; GameName: 'Wipeout'),
(PortName: 'wipeout rewrite'; GameName: 'Wipeout'),
(PortName: 'star fox 64 recomp'; GameName: 'Star Fox 64'),
(PortName: 'superman 64 recomp'; GameName: 'Superman: The New Superman Adventures'),
(PortName: 'dk64 recompiled'; GameName: 'Donkey Kong 64'),
(PortName: 'banjorecomp'; GameName: 'Banjo-Kazooie'),
(PortName: 'lighthouse'; GameName: 'Banjo-Kazooie'),
(PortName: 'fable2recomp'; GameName: 'Fable 2'),
(PortName: 'openmbu'; GameName: 'Marble Blast Ultra'),
(PortName: 'openmbg'; GameName: 'Marble Blast Gold'),
(PortName: 'openmohaa'; GameName: 'Medal of Honor: Allied Assault'),
(PortName: 'aleph one'; GameName: 'Marathon')
);
function ResolveUnofficialPortName(const AGameName: string): string;
var
LowerName: string;
i: Integer;
begin
Result := AGameName;
LowerName := Trim(LowerCase(AGameName));
if LowerName = '' then Exit;
for i := Low(STATIC_PORT_MAPPINGS) to High(STATIC_PORT_MAPPINGS) do
begin
if STATIC_PORT_MAPPINGS[i].PortName = LowerName then
begin
Result := STATIC_PORT_MAPPINGS[i].GameName;
Exit;
end;
end;
end;
constructor TGamesTabHelper.Create(AForm: Tgoverlayform);
begin
FForm := AForm;
end;
constructor TCoverDownloadThread.Create(AAppIDs: TStringList; AImages: TList;
const ACacheDir: string; AForm: Tgoverlayform);
begin
inherited Create(True);
FAppIDs := AAppIDs;
FImages := AImages;
FCacheDir := ACacheDir;
FForm := AForm;
FreeOnTerminate := True;
OnTerminate := @FForm.CoverThreadTerminated;
end;
destructor TCoverDownloadThread.Destroy;
begin
FAppIDs.Free;
FImages.Free;
inherited;
end;
procedure TCoverDownloadThread.DoUpdateImage;
var
ScaledBmp: TBitmap;
CardPanel: TPanel;
CardIdx: Integer;
begin
if not Assigned(FForm) or (FForm.FCoverThread <> Self) then Exit;
if not Assigned(FCurrentImage) or not FileExists(FCurrentPath) then Exit;
// Safety: verify the image's parent panel is still in the active card list
if not Assigned(FCurrentImage.Parent) or not (FCurrentImage.Parent is TPanel) then Exit;
if Assigned(FForm) and Assigned(FForm.FCardPanels) and
(FForm.FCardPanels.IndexOf(FCurrentImage.Parent) < 0) then Exit;
try
FCurrentImage.Picture.LoadFromFile(FCurrentPath);
if (FCurrentImage.Picture.Graphic = nil) or
(FCurrentImage.Picture.Graphic.Width = 0) then Exit;
ScaledBmp := TBitmap.Create;
try
ScaledBmp.SetSize(CARD_W, CARD_H);
ScaledBmp.Canvas.StretchDraw(
Rect(0, 0, CARD_W, CARD_H), FCurrentImage.Picture.Graphic);
ProcessCoverBitmap(ScaledBmp, GRAD_H);
FCurrentImage.Picture.Bitmap.Assign(ScaledBmp);
// Update FOrigCovers so hover brightness uses the processed image
if Assigned(FForm) and Assigned(FForm.FCardPanels) and
Assigned(FForm.FOrigCovers) and (FCurrentImage.Parent is TPanel) then
begin
CardPanel := TPanel(FCurrentImage.Parent);
CardIdx := FForm.FCardPanels.IndexOf(CardPanel);
if (CardIdx >= 0) and (CardIdx < FForm.FOrigCovers.Count) then
begin
if FForm.FOrigCovers[CardIdx] <> nil then
TLazIntfImage(FForm.FOrigCovers[CardIdx]).Free;
FForm.FOrigCovers[CardIdx] := ScaledBmp.CreateIntfImage;
end;
end;
finally
ScaledBmp.Free;
end;
// Dim cover to match the default un-hovered state
if Assigned(FForm) and (FCurrentImage.Parent is TPanel) then
FForm.ApplyCardBrightness(TPanel(FCurrentImage.Parent), 100);
except
end;
end;
procedure TCoverDownloadThread.DoGenerateFallback;
begin
GenerateFallbackCover(FCurrentPath, FForm);
end;
procedure GenerateFallbackCover(const APath: string; AForm: Tgoverlayform);
var
Bmp: TBitmap;
Png: TPortableNetworkGraphic;
IconPath: string;
DestRect: TRect;
IconSize: Integer;
begin
if not Assigned(AForm) then Exit;
IconPath := AForm.GetAppBaseDir + 'data/icons/128x128/goverlay.png';
if not FileExists(IconPath) then
IconPath := '/usr/share/icons/hicolor/128x128/apps/goverlay.png';
if not FileExists(IconPath) then
IconPath := '/usr/share/icons/hicolor/128x128/apps/io.github.benjamimgois.goverlay.png';
WriteLn(StdErr, '[CoverThread] GenerateFallbackCover APath="', APath, '" IconPath="', IconPath, '" exists=', FileExists(IconPath));
Bmp := TBitmap.Create;
try
Bmp.SetSize(CARD_W, CARD_H);
Bmp.Canvas.Brush.Color := $252525; // Dark background
Bmp.Canvas.FillRect(Rect(0, 0, CARD_W, CARD_H));
if FileExists(IconPath) then
begin
Png := TPortableNetworkGraphic.Create;
try
Png.LoadFromFile(IconPath);
IconSize := 96; // 96x96 centered inside 150x215
DestRect := Rect(
(CARD_W - IconSize) div 2,
(CARD_H - IconSize) div 2,
(CARD_W - IconSize) div 2 + IconSize,
(CARD_H - IconSize) div 2 + IconSize
);
Bmp.Canvas.StretchDraw(DestRect, Png);
finally
Png.Free;
end;
end;
with TJPEGImage.Create do
try
Assign(Bmp);
SaveToFile(APath);
with TStringList.Create do
try
SaveToFile(APath + '.fallback');
finally
Free;
end;
WriteLn(StdErr, '[CoverThread] GenerateFallbackCover saved successfully to ', APath);
except
on E: Exception do
WriteLn(StdErr, '[CoverThread] Error saving fallback cover: ', E.Message);
end;
finally
Bmp.Free;
end;
end;
procedure TCoverDownloadThread.Execute;
var
i, j, k, m, n: Integer;
AppID, GameName, OutPath, Url, TmpFile, JsonStr: string;
Proc: TProcess;
CdnUrls, S: TStringList;
begin
ForceDirectories(FCacheDir);
WriteLn(StdErr, '[CoverThread] Execute started. FAppIDs.Count=', FAppIDs.Count);
for i := 0 to FAppIDs.Count - 1 do
begin
if Terminated then Break;
AppID := FAppIDs.Names[i];
GameName := FAppIDs.ValueFromIndex[i];
if AppID = '' then
begin
AppID := FAppIDs[i];
GameName := '';
end;
OutPath := FCacheDir + AppID + '.jpg';
WriteLn(StdErr, '[CoverThread] AppID=', AppID, ' GameName="', GameName, '" OutPath="', OutPath, '"');
if FileExists(OutPath) and (FileSize(OutPath) > 0) then
begin
WriteLn(StdErr, '[CoverThread] Cached file exists, using it');
FCurrentImage := TImage(FImages[i]);
FCurrentPath := OutPath;
Synchronize(@DoUpdateImage);
Continue;
end;
// Try multi-CDN candidate URLs
CdnUrls := TStringList.Create;
try
CdnUrls.Add('https://shared.akamai.steamstatic.com/store_item_assets/steam/apps/' + AppID + '/library_600x900.jpg');
CdnUrls.Add('https://cdn.cloudflare.steamstatic.com/steam/apps/' + AppID + '/library_600x900.jpg');
CdnUrls.Add('https://cdn.akamai.steamstatic.com/steam/apps/' + AppID + '/library_600x900.jpg');
CdnUrls.Add('https://shared.akamai.steamstatic.com/store_item_assets/steam/apps/' + AppID + '/header.jpg');
CdnUrls.Add('https://cdn.cloudflare.steamstatic.com/steam/apps/' + AppID + '/header.jpg');
CdnUrls.Add('https://cdn.akamai.steamstatic.com/steam/apps/' + AppID + '/header.jpg');
for j := 0 to CdnUrls.Count - 1 do
begin
Url := CdnUrls[j];
Proc := TProcess.Create(nil);
try
Proc.Executable := 'curl';
Proc.Parameters.Add('-s');
Proc.Parameters.Add('-L');
Proc.Parameters.Add('--connect-timeout');
Proc.Parameters.Add('3');
Proc.Parameters.Add('--max-time');
Proc.Parameters.Add('6');
Proc.Parameters.Add('--fail');
Proc.Parameters.Add('-o');
Proc.Parameters.Add(OutPath);
Proc.Parameters.Add(Url);
Proc.Options := [poWaitOnExit, poNoConsole];
try Proc.Execute; except end;
finally
Proc.Free;
end;
if FileExists(OutPath) and (FileSize(OutPath) > 0) then
begin
WriteLn(StdErr, '[CoverThread] CDN download successful from ', Url);
Break;
end
else
DeleteFile(OutPath);
end;
finally
CdnUrls.Free;
end;
// Fallback to Steam Store API details if direct CDN URLs failed
if (not FileExists(OutPath)) or (FileSize(OutPath) = 0) then
begin
TmpFile := GetTempDir + 'goverlay_steam_api_' + AppID + '_' + IntToStr(GetProcessID) + '.json';
Proc := TProcess.Create(nil);
try
Proc.Executable := 'curl';
Proc.Parameters.Add('-s');
Proc.Parameters.Add('-L');
Proc.Parameters.Add('--connect-timeout');
Proc.Parameters.Add('3');
Proc.Parameters.Add('--max-time');
Proc.Parameters.Add('6');
Proc.Parameters.Add('-o');
Proc.Parameters.Add(TmpFile);
Proc.Parameters.Add('https://store.steampowered.com/api/appdetails?appids=' + AppID);
Proc.Options := [poWaitOnExit, poNoConsole];
try Proc.Execute; except end;
finally
Proc.Free;
end;
if FileExists(TmpFile) then
begin
JsonStr := '';
S := TStringList.Create;
try
S.LoadFromFile(TmpFile);
JsonStr := S.Text;
finally
S.Free;
DeleteFile(TmpFile);
end;
k := Pos('"header_image":"', JsonStr);
if k = 0 then k := Pos('"capsule_image":"', JsonStr);
if k > 0 then
begin
m := PosEx('http', JsonStr, k);
if m > 0 then
begin
n := m;
while (n <= Length(JsonStr)) and (JsonStr[n] <> '"') do Inc(n);
Url := Copy(JsonStr, m, n - m);
Url := StringReplace(Url, '\/', '/', [rfReplaceAll]);
if Url <> '' then
begin
Proc := TProcess.Create(nil);
try
Proc.Executable := 'curl';
Proc.Parameters.Add('-s');
Proc.Parameters.Add('-L');
Proc.Parameters.Add('--max-time');
Proc.Parameters.Add('10');
Proc.Parameters.Add('--fail');
Proc.Parameters.Add('-o');
Proc.Parameters.Add(OutPath);
Proc.Parameters.Add(Url);
Proc.Options := [poWaitOnExit, poNoConsole];
try Proc.Execute; except end;
finally
Proc.Free;
end;
end;
end;
end;
end;
end;
// Fallback to Web search if Steam CDN fails
if (not FileExists(OutPath)) or (FileSize(OutPath) = 0) then
begin
WriteLn(StdErr, '[CoverThread] CDN failed. Web search fallback for GameName="', GameName, '"');
if GameName <> '' then
begin
DeleteFile(OutPath);
FForm.SearchWebCover(GameName, OutPath);
WriteLn(StdErr, '[CoverThread] Web search result exists=', FileExists(OutPath));
end;
end;
// Fallback to GOverlay Icon on dark background if web search also fails
if (not FileExists(OutPath)) or (FileSize(OutPath) = 0) then
begin
WriteLn(StdErr, '[CoverThread] CDN & Web search failed. Generating GOverlay fallback');
DeleteFile(OutPath);
FCurrentPath := OutPath;
Synchronize(@DoGenerateFallback);
WriteLn(StdErr, '[CoverThread] Fallback cover exists=', FileExists(OutPath));
end;
if FileExists(OutPath) and (FileSize(OutPath) > 0) then
begin
FCurrentImage := TImage(FImages[i]);
FCurrentPath := OutPath;
Synchronize(@DoUpdateImage);
end;
end;
end;
// ============================================================================
// TNonSteamCoverThread implementation
// ============================================================================
constructor TNonSteamCoverThread.Create(
const AItems: array of TNonSteamCoverItem; AForm: Tgoverlayform);
var
i: Integer;
begin
inherited Create(True);
SetLength(FItems, Length(AItems));
for i := 0 to High(AItems) do
FItems[i] := AItems[i];
FForm := AForm;
FreeOnTerminate := True;
OnTerminate := @FForm.CoverThreadTerminated;
end;
// ============================================================================
// Cover check timer: polls for downloaded covers and updates UI from main thread
// ============================================================================
procedure TNonSteamCoverThread.DoUpdateImage;
var
ScaledBmp: TBitmap;
CardPanel: TPanel;
CardImage: TImage;
j: Integer;
begin
if not Assigned(FForm) or (FForm.FNonSteamCoverThread <> Self) then Exit;
if not Assigned(FForm.FCardPanels) or not Assigned(FForm.FOrigCovers) then Exit;
if not FileExists(FCurrentPath) then Exit;
if (FCurrentCardIdx < 0) or (FCurrentCardIdx >= FForm.FCardPanels.Count) then Exit;
CardPanel := TPanel(FForm.FCardPanels[FCurrentCardIdx]);
CardImage := nil;
for j := 0 to CardPanel.ControlCount - 1 do
if CardPanel.Controls[j] is TImage then
begin
CardImage := TImage(CardPanel.Controls[j]);
Break;
end;
if not Assigned(CardImage) then Exit;
try
CardImage.Picture.LoadFromFile(FCurrentPath);
ScaledBmp := TBitmap.Create;
try
ScaledBmp.SetSize(CARD_W, CARD_H);
ScaledBmp.Canvas.StretchDraw(
Rect(0, 0, CARD_W, CARD_H), CardImage.Picture.Graphic);
ProcessCoverBitmap(ScaledBmp, GRAD_H);
CardImage.Picture.Bitmap.Assign(ScaledBmp);
if (FCurrentCardIdx >= 0) and (FCurrentCardIdx < FForm.FOrigCovers.Count) then
begin
if FForm.FOrigCovers[FCurrentCardIdx] <> nil then
TLazIntfImage(FForm.FOrigCovers[FCurrentCardIdx]).Free;
FForm.FOrigCovers[FCurrentCardIdx] := ScaledBmp.CreateIntfImage;
end;
finally
ScaledBmp.Free;
end;
for j := 0 to CardPanel.ControlCount - 1 do
if (CardPanel.Controls[j] is TLabel) and (CardPanel.Controls[j].Tag = 9991) then
begin
CardPanel.Controls[j].Visible := FCurrentIsFallback;
Break;
end;
FForm.ApplyCardBrightness(CardPanel, 100);
except
end;
end;
procedure TNonSteamCoverThread.DoGenerateFallback;
begin
GenerateFallbackCover(FCurrentPath, FForm);
end;
procedure TNonSteamCoverThread.Execute;
var
i: Integer;
AppId: string;
GotCover, IsFallbackCover: Boolean;
SearchName: string;
begin
for i := 0 to High(FItems) do
begin
if Terminated then Break;
// Already cached?
if FileExists(FItems[i].CachePath) then
begin
FCurrentCardIdx := FItems[i].CardIndex;
FCurrentPath := FItems[i].CachePath;
FCurrentIsFallback := FileExists(FItems[i].CachePath + '.fallback');
Synchronize(@DoUpdateImage);
Continue;
end;
WriteLn(StdErr, '[NonSteamCoverThread] Processing GameName="', FItems[i].GameName, '" CachePath="', FItems[i].CachePath, '"');
GotCover := False;
IsFallbackCover := False;
SearchName := ResolveUnofficialPortName(FItems[i].GameName);
// 1st attempt: Steam Store API
if Assigned(FForm) and not FForm.FClosing then
if FForm.SearchSteamStoreGame(SearchName, AppId) then
if FForm.DownloadSteamCover(AppId, FItems[i].CachePath) then
begin
GotCover := True;
DeleteFile(FItems[i].CachePath + '.fallback');
end;
// 2nd attempt: Web image search
if not GotCover and Assigned(FForm) and not FForm.FClosing then
if FForm.SearchWebCover(SearchName, FItems[i].CachePath) then
begin
GotCover := True;
DeleteFile(FItems[i].CachePath + '.fallback');
end;
// 3rd attempt: GOverlay Icon fallback
if not GotCover and (not FileExists(FItems[i].CachePath) or (FileSize(FItems[i].CachePath) = 0)) then
begin
WriteLn(StdErr, '[NonSteamCoverThread] CDN & Web search failed. Generating GOverlay fallback');
DeleteFile(FItems[i].CachePath);
FCurrentPath := FItems[i].CachePath;
Synchronize(@DoGenerateFallback);
GotCover := True;
IsFallbackCover := True;
end;
if GotCover or FileExists(FItems[i].CachePath) then
begin
WriteLn(StdErr, '[NonSteamCoverThread] Updating image card index=', FItems[i].CardIndex, ' path=', FItems[i].CachePath);
FCurrentCardIdx := FItems[i].CardIndex;
FCurrentPath := FItems[i].CachePath;
FCurrentIsFallback := IsFallbackCover or FileExists(FItems[i].CachePath + '.fallback');
Synchronize(@DoUpdateImage);
end;
end;
end;
procedure ProcessCoverBitmap(Bmp: TBitmap; GradH: Integer);
var
IntfImg: TLazIntfImage;
ResultBmp: TBitmap;
Stride, BPP, W, H: Integer;
Row: PByte;
x, y, px, DimPct, Bright: Integer;
begin
W := Bmp.Width;
H := Bmp.Height;
if (W = 0) or (H = 0) then Exit;
IntfImg := TLazIntfImage.Create(W, H);
try
IntfImg.LoadFromBitmap(Bmp.Handle, 0);
Stride := IntfImg.DataDescription.BytesPerLine;
BPP := IntfImg.DataDescription.BitsPerPixel div 8;
if BPP < 3 then Exit;
for y := 0 to H - 1 do
begin
if y < H - GradH then Continue;
DimPct := Round((y - (H - GradH)) / GradH * 88);
if DimPct <= 0 then Continue;
Row := IntfImg.PixelData + PtrUInt(y * Stride);
for x := 0 to W - 1 do
begin
px := x * BPP;
Row[px] := Byte(Integer(Row[px]) * (100 - DimPct) div 100);
Row[px+1] := Byte(Integer(Row[px+1]) * (100 - DimPct) div 100);
Row[px+2] := Byte(Integer(Row[px+2]) * (100 - DimPct) div 100);
end;
end;
ResultBmp := TBitmap.Create;
try
ResultBmp.LoadFromIntfImage(IntfImg);
Bmp.Assign(ResultBmp);
finally
ResultBmp.Free;
end;
finally
IntfImg.Free;
end;
end;
procedure TGamesTabHelper.InitGamesTab;
var
OpenFolderItem: TMenuItem;
UninstallItem: TMenuItem;
GamesBgPB: TPaintBox;
IconPath: string;
Bmp: TBitmap;
IconColor: TColor;
x, y: Integer;
Clr: TColor;
Gray: Byte;
begin
with FForm do
begin
FCardPanels := TList.Create;
FOrigCovers := TList.Create;
// Right-click context menu for game cards
FGameCardMenu := TPopupMenu.Create(FForm);
// Dedicated 16x16 image list for the menu — drawn in greyscale
FGameMenuImgList := TImageList.Create(FForm);
FGameMenuImgList.Width := 16;
FGameMenuImgList.Height := 16;
IconColor := RGBToColor(180, 180, 180);
// --- Icon 0: Folder (greyscale) ---
Bmp := TBitmap.Create;
try
Bmp.SetSize(16, 16);
Bmp.Canvas.Brush.Color := clFuchsia;
Bmp.Canvas.FillRect(0, 0, 16, 16);
Bmp.Canvas.Pen.Color := IconColor;
Bmp.Canvas.Brush.Color := IconColor;
Bmp.Canvas.Rectangle(2, 3, 8, 6); // tab
Bmp.Canvas.Rectangle(2, 5, 14, 13); // body
FGameMenuImgList.AddMasked(Bmp, clFuchsia);
finally
Bmp.Free;
end;
// --- Icon 1: Wine prefix (greyscale copy of iconsImageList[38]) ---
if Assigned(iconsImageList) and (iconsImageList.Count > 38) then
begin
Bmp := TBitmap.Create;
try
Bmp.SetSize(16, 16);
Bmp.Canvas.Brush.Color := clFuchsia;
Bmp.Canvas.FillRect(0, 0, 16, 16);
iconsImageList.Draw(Bmp.Canvas, 0, 0, 38);
// Convert every non-mask pixel to greyscale
for y := 0 to 15 do
for x := 0 to 15 do
begin
Clr := Bmp.Canvas.Pixels[x, y];
if Clr <> clFuchsia then
begin
Gray := (Red(Clr) + Green(Clr) + Blue(Clr)) div 3;
Bmp.Canvas.Pixels[x, y] := RGBToColor(Gray, Gray, Gray);
end;
end;
FGameMenuImgList.AddMasked(Bmp, clFuchsia);
finally
Bmp.Free;
end;
end
else
begin
// Fallback: simple wine-glass silhouette
Bmp := TBitmap.Create;
try
Bmp.SetSize(16, 16);
Bmp.Canvas.Brush.Color := clFuchsia;
Bmp.Canvas.FillRect(0, 0, 16, 16);
Bmp.Canvas.Pen.Color := IconColor;
Bmp.Canvas.Brush.Color := IconColor;
Bmp.Canvas.RoundRect(4, 2, 12, 7, 6, 6); // bowl
Bmp.Canvas.Rectangle(7, 7, 9, 12); // stem
Bmp.Canvas.Rectangle(4, 12, 12, 14); // base
FGameMenuImgList.AddMasked(Bmp, clFuchsia);
finally
Bmp.Free;
end;
end;
// --- Icon 2: Trash / Uninstall (greyscale) ---
Bmp := TBitmap.Create;
try
Bmp.SetSize(16, 16);
Bmp.Canvas.Brush.Color := clFuchsia;
Bmp.Canvas.FillRect(0, 0, 16, 16);
Bmp.Canvas.Pen.Color := IconColor;
Bmp.Canvas.Brush.Color := IconColor;
Bmp.Canvas.Rectangle(3, 2, 13, 4); // lid
Bmp.Canvas.Rectangle(4, 4, 12, 14); // body
Bmp.Canvas.Pen.Color := clFuchsia;
Bmp.Canvas.MoveTo(6, 6); Bmp.Canvas.LineTo(6, 12);
Bmp.Canvas.MoveTo(9, 6); Bmp.Canvas.LineTo(9, 12);
FGameMenuImgList.AddMasked(Bmp, clFuchsia);
finally
Bmp.Free;
end;
FGameCardMenu.Images := FGameMenuImgList;
OpenFolderItem := TMenuItem.Create(FGameCardMenu);
OpenFolderItem.Caption := 'Open install folder';
OpenFolderItem.ImageIndex := 0;
OpenFolderItem.OnClick := @GameCardOpenFolderClick;
FGameCardMenu.Items.Add(OpenFolderItem);
FOpenPrefixMenuItem := TMenuItem.Create(FGameCardMenu);
FOpenPrefixMenuItem.Caption := 'Open prefix folder';
FOpenPrefixMenuItem.ImageIndex := 1;
FOpenPrefixMenuItem.OnClick := @GameCardOpenPrefixClick;
FGameCardMenu.Items.Add(FOpenPrefixMenuItem);
FUninstallMenuItem := TMenuItem.Create(FGameCardMenu);
FUninstallMenuItem.Caption := 'Uninstall changes';
FUninstallMenuItem.ImageIndex := 2;
FUninstallMenuItem.OnClick := @GameCardUninstallClick;
FGameCardMenu.Items.Add(FUninstallMenuItem);
FGamesScrollBox := TScrollBox.Create(FForm);
FGamesScrollBox.Parent := gamesTabSheet;
FGamesScrollBox.Align := alClient;
FGamesScrollBox.AutoScroll := True;
FGamesScrollBox.BorderStyle := bsNone;
FGamesScrollBox.HorzScrollBar.Visible := False;
FGamesScrollBox.Color := RGBToColor(22, 26, 40);
FGamesScrollBox.ParentColor := False;
FGamesScrollBox.OnResize := @GamesScrollBoxResize;
// Navy background paintbox — created before FGamesPanel so it sits behind the cards
GamesBgPB := TPaintBox.Create(FForm);
GamesBgPB.Parent := FGamesScrollBox;
GamesBgPB.Align := alClient;
GamesBgPB.OnPaint := @PresetsBgBoxPaint;
FGamesPanel := TPanel.Create(FForm);
FGamesPanel.Parent := FGamesScrollBox;
FGamesPanel.Caption := '';
FGamesPanel.BevelOuter := bvNone;
FGamesPanel.Color := RGBToColor(22, 26, 40);
FGamesPanel.Left := 0;
FGamesPanel.Top := 0;
FGamesPanel.Width := 800;
FGamesPanel.Height := 100;
FGamesPanel.OnPaint := @PresetsWrapperPaint;
FGamesPanel.OnClick := @GamesEmptySpaceClick;
FGamesScrollBox.OnClick := @GamesEmptySpaceClick;
// Cache badge icons for corner ribbon (loaded once, reused per card)
FMangoIconGfx := TPortableNetworkGraphic.Create;
IconPath := GetAppBaseDir + 'assets/icons/mango-active.png';
if FileExists(IconPath) then try FMangoIconGfx.LoadFromFile(IconPath); except end;
FOptiIconGfx := TPortableNetworkGraphic.Create;
IconPath := GetAppBaseDir + 'assets/icons/scale-up2-active.png';
if FileExists(IconPath) then try FOptiIconGfx.LoadFromFile(IconPath); except end;
// Navy background for the bottom bar
goverlaybarPanel.OnPaint := @PresetsWrapperPaint;
// Quick preview button — icon-only, sits immediately left of popupBitBtn.
FPreviewBtn := TBitBtn.Create(FForm);
FPreviewBtn.Parent := goverlaybarPanel;
// Align height (30) and vertical position (5) with the rest of the bar
FPreviewBtn.SetBounds(684, 5, 28, 30);
FPreviewBtn.Anchors := [akRight, akBottom];
FPreviewBtn.Caption := '▶';
FPreviewBtn.Color := $00445566;
FPreviewBtn.Font.Color := clWhite;
FPreviewBtn.Font.Size := 10;
FPreviewBtn.Font.Style := [fsBold];
FPreviewBtn.Font.Name := 'Noto Sans';
FPreviewBtn.Hint := 'Launch a quick preview cube (pascube / vkcube)';
FPreviewBtn.ShowHint := True;
FPreviewBtn.OnClick := @PreviewBtnClick;
// Re-anchor commandPanel so it stops at the left edge of FPreviewBtn,
// preventing the panel from drawing over the preview button.
commandPanel.AnchorSideRight.Control := FPreviewBtn;
commandPanel.AnchorSideRight.Side := asrLeft;
// Informative hint for the launch-command box
commandPanel.Hint := 'Copy this command and paste it into the game''s Launch Options in Steam.';
commandPanel.ShowHint := True;
end;
end;
procedure TGamesTabHelper.LoadSteamGames;
const
// bit0=Mango(PNG), bit1=vkBasalt(glyph), bit2=OptiScaler(PNG), bit3=Tweaks(glyph)
BADGE_GLYPHS: array[0..3] of string = ('', '', '', '');
BDG_SZ = 18; // icon cell size
BDG_GAP = 5; // vertical gap between icons
BDG_PAD_V = 6; // top/bottom padding inside strip
BDG_FONT = 13; // glyph font size
BDG_W = 26; // strip width (right edge)
var
Libraries: TStringList;
PendingIDs: TStringList;
PendingImages: TList;
CacheDir: string;
i, j, CardX, CardY, CardsPerRow, TotalRows, RowMargin: Integer;
LibPath, AcfContent, AppID, GameName, ImagePath, HomeDir, InstallDir, IconPath: string;
SR: TSearchRec;
AcfFile: TStringList;
CardPanel: TPanel;
CardImage: TImage;
BdgLbl: TLabel;
BdgImg: TImage;
BdgBg: TShape;
NoGamesLabel: TLabel;
LowerName, GameCfgDir: string;
ScaledBmp: TBitmap;
HasMango, HasVkBasalt, HasOptiScaler, HasTweaks: Boolean;
TweakLines: TStringList;
k, BadgeCount, BdgBit, BdgSlot, BdgX, BdgY: Integer;
BdgHint: string;
begin
with FForm do
begin
if not Assigned(FGamesScrollBox) or not Assigned(FGamesPanel) then
Exit;
if IsRunningInFlatpak then
HomeDir := IncludeTrailingPathDelimiter(GetUserDir)
else
HomeDir := IncludeTrailingPathDelimiter(GetEnvironmentVariable('HOME'));
CacheDir := HomeDir + '.cache/goverlay/covers/';
ForceDirectories(CacheDir);
Libraries := TStringList.Create;
PendingIDs := TStringList.Create;
PendingImages := TList.Create;
try
GetSteamLibraries(Libraries);
if Libraries.Count = 0 then
begin