-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathbgmod-uninstaller.lpr
More file actions
795 lines (717 loc) · 28.9 KB
/
Copy pathbgmod-uninstaller.lpr
File metadata and controls
795 lines (717 loc) · 28.9 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
program bgmod_uninstaller;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, IniFiles, Process, BaseUnix, Unix;
function GetBGModPath: string; forward;
function CopyFile(const Src, Dst: string): Boolean;
var
SrcStream, DstStream: TFileStream;
begin
Result := False;
try
SrcStream := TFileStream.Create(Src, fmOpenRead or fmShareDenyWrite);
try
DstStream := TFileStream.Create(Dst, fmCreate);
try
DstStream.CopyFrom(SrcStream, SrcStream.Size);
Result := True;
finally
DstStream.Free;
end;
finally
SrcStream.Free;
end;
except
// ignore copy failures
end;
end;
function setenv(name: PChar; value: PChar; overwrite: Integer): Integer; cdecl; external 'c' name 'setenv';
function execvp(file_: PChar; argv: PPChar): Integer; cdecl; external 'c' name 'execvp';
procedure SetEnvironmentVariable(const Name, Value: string);
begin
setenv(PChar(Name), PChar(Value), 1);
end;
var
GameDir: string;
CentralLogDir: string;
CentralLogFile: string;
UninstallerPath: string;
procedure UpdateCentralLogPaths;
var
DataHome, GameDirName: string;
begin
if GameDir = '' then Exit;
GameDirName := ExtractFileName(ExcludeTrailingPathDelimiter(GameDir));
if GameDirName = '' then Exit;
if CentralLogFile = '' then
begin
DataHome := GetEnvironmentVariable('HOST_XDG_DATA_HOME');
if DataHome = '' then
DataHome := GetEnvironmentVariable('XDG_DATA_HOME');
if DataHome = '' then
DataHome := GetUserDir + '.local/share';
CentralLogDir := IncludeTrailingPathDelimiter(DataHome) + 'goverlay' + PathDelim + 'logs' + PathDelim + GameDirName;
CentralLogFile := IncludeTrailingPathDelimiter(CentralLogDir) + 'bgmod-uninstaller.log';
end;
end;
procedure Log(const Msg: string);
var
F: TextFile;
LogMsg: string;
begin
if (GameDir <> '') and (CentralLogFile = '') then
UpdateCentralLogPaths;
LogMsg := FormatDateTime('yyyy-MM-dd hh:nn:ss', Now) + ' - ' + Msg;
WriteLn(LogMsg);
// Append to /tmp/bgmod-uninstaller.log
try
AssignFile(F, '/tmp/bgmod-uninstaller.log');
if FileExists('/tmp/bgmod-uninstaller.log') then
Append(F)
else
Rewrite(F);
WriteLn(F, LogMsg);
CloseFile(F);
except
// ignore logging failures
end;
// Append to game directory bgmod-uninstaller.log if resolved
if GameDir <> '' then
begin
try
AssignFile(F, IncludeTrailingPathDelimiter(GameDir) + 'bgmod-uninstaller.log');
if FileExists(IncludeTrailingPathDelimiter(GameDir) + 'bgmod-uninstaller.log') then
Append(F)
else
Rewrite(F);
WriteLn(F, LogMsg);
CloseFile(F);
except
// ignore logging failures
end;
end;
// Append to central GOverlay logs directory if resolved
if CentralLogFile <> '' then
begin
try
if not DirectoryExists(CentralLogDir) then
ForceDirectories(CentralLogDir);
AssignFile(F, CentralLogFile);
if FileExists(CentralLogFile) then
Append(F)
else
Rewrite(F);
WriteLn(F, LogMsg);
CloseFile(F);
except
// ignore logging failures
end;
end;
end;
function GetCommandOutput(const Cmd: string): string;
var
Proc: TProcess;
List: TStringList;
begin
Result := '';
Proc := TProcess.Create(nil);
List := TStringList.Create;
try
Proc.Executable := '/bin/sh';
Proc.Parameters.Add('-c');
Proc.Parameters.Add(Cmd);
Proc.Options := [poUsePipes, poWaitOnExit];
Proc.Execute;
List.LoadFromStream(Proc.Output);
Result := Trim(List.Text);
except
on E: Exception do
Log('Error running shell command: ' + E.Message);
end;
List.Free;
Proc.Free;
end;
function FindUEShippingExe(const BaseDir: string; Depth: Integer): string;
var
SR: TSearchRec;
SearchPath, Res: string;
begin
Result := '';
if Depth > 4 then Exit;
if DirectoryExists(IncludeTrailingPathDelimiter(BaseDir) + 'Binaries' + PathDelim + 'Win64') then
begin
SearchPath := IncludeTrailingPathDelimiter(BaseDir) + 'Binaries' + PathDelim + 'Win64' + PathDelim + '*.exe';
if FindFirst(SearchPath, faAnyFile, SR) = 0 then
begin
try
repeat
if (SR.Attr and faDirectory) = 0 then
begin
Result := IncludeTrailingPathDelimiter(BaseDir) + 'Binaries' + PathDelim + 'Win64';
Break;
end;
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
if Result <> '' then Exit;
end;
end;
SearchPath := IncludeTrailingPathDelimiter(BaseDir) + '*';
if FindFirst(SearchPath, faDirectory, SR) = 0 then
begin
try
repeat
if (SR.Name <> '.') and (SR.Name <> '..') and ((SR.Attr and faDirectory) <> 0) then
begin
if (UpperCase(SR.Name) <> 'ENGINE') and
(UpperCase(SR.Name) <> 'BUGREPORTCLIENT') and
(UpperCase(SR.Name) <> 'CRASHREPORTCLIENT') then
begin
Res := FindUEShippingExe(IncludeTrailingPathDelimiter(BaseDir) + SR.Name, Depth + 1);
if Res <> '' then
begin
Result := Res;
Break;
end;
end;
end;
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
end;
end;
procedure ResolveGameDirectory;
var
Arg, ExePath, LutrisId, Cmd: string;
i, j, PosPipe: Integer;
LauncherIni: TIniFile;
LauncherList: TStringList;
KeyLine, KeyName, EntryVal, TargetSub, Repl, CleanKey, ConfPath: string;
begin
GameDir := '';
// 1. Check command line arguments for .exe
for i := 1 to ParamCount do
begin
Arg := ParamStr(i);
if LowerCase(ExtractFileExt(Arg)) = '.exe' then
begin
// Game launcher replacements
LauncherList := TStringList.Create;
LauncherIni := nil;
try
ConfPath := ExtractFilePath(ParamStr(0)) + 'bgmod.conf';
if not FileExists(ConfPath) then
ConfPath := IncludeTrailingPathDelimiter(GetBGModPath) + 'bgmod.conf';
if FileExists(ConfPath) then
begin
LauncherIni := TIniFile.Create(ConfPath);
LauncherIni.ReadSectionValues('Launchers', LauncherList);
end;
if LauncherList.Count = 0 then
begin
LauncherList.Add('Cyberpunk 2077=REDprelauncher.exe|bin/x64/Cyberpunk2077.exe');
LauncherList.Add('Witcher 3=REDprelauncher.exe|bin/x64_dx12/witcher3.exe');
LauncherList.Add('Baldurs Gate 3=Launcher/LariLauncher.exe|bin/bg3_dx11.exe');
LauncherList.Add('Baldurs Gate 3 Alt=Launcher\LariLauncher.exe|bin/bg3_dx11.exe');
LauncherList.Add('HITMAN 3=Launcher.exe|Retail/HITMAN3.exe');
LauncherList.Add('HITMAN World of Assassination=Launcher.exe|Retail/HITMAN3.exe');
LauncherList.Add('SYNCED=Launcher/sop_launcher.exe|SYNCED.exe');
LauncherList.Add('2KLauncher=2KLauncher/LauncherPatcher.exe|DoesntMatter.exe');
LauncherList.Add('Warhammer 40,000 DARKTIDE=launcher/Launcher.exe|binaries/Darktide.exe');
LauncherList.Add('Warhammer Vermintide 2=launcher/Launcher.exe|binaries_dx12/vermintide2_dx12.exe');
LauncherList.Add('Satisfactory=FactoryGameSteam.exe|Engine/Binaries/Win64/FactoryGameSteam-Win64-Shipping.exe');
LauncherList.Add('FINAL FANTASY XIV Online=boot/ffxivboot.exe|game/ffxiv_dx11.exe');
LauncherList.Add('DuneAwakening=Launcher/FuncomLauncher.exe|DuneSandbox/Binaries/Win64/DuneSandbox-Win64-Shipping.exe');
end;
for j := 0 to LauncherList.Count - 1 do
begin
KeyLine := LauncherList[j];
PosPipe := Pos('=', KeyLine);
if PosPipe > 0 then
begin
KeyName := Trim(Copy(KeyLine, 1, PosPipe - 1));
CleanKey := KeyName;
if Pos(' Alt', CleanKey) > 0 then
CleanKey := Copy(CleanKey, 1, Pos(' Alt', CleanKey) - 1);
if Pos(' alt', CleanKey) > 0 then
CleanKey := Copy(CleanKey, 1, Pos(' alt', CleanKey) - 1);
EntryVal := Trim(Copy(KeyLine, PosPipe + 1, MaxInt));
PosPipe := Pos('|', EntryVal);
if PosPipe > 0 then
begin
TargetSub := Trim(Copy(EntryVal, 1, PosPipe - 1));
Repl := Trim(Copy(EntryVal, PosPipe + 1, MaxInt));
if (Pos(CleanKey, Arg) > 0) and (Pos(TargetSub, Arg) > 0) then
begin
Arg := StringReplace(Arg, TargetSub, Repl, [rfReplaceAll, rfIgnoreCase]);
Break;
end;
end;
end;
end;
finally
if Assigned(LauncherIni) then
LauncherIni.Free;
LauncherList.Free;
end;
GameDir := ExtractFilePath(Arg);
Log('Resolved GameDir from argument: ' + GameDir);
Break;
end;
end;
// 2. Check command line arguments for Lutris game run ID
if GameDir = '' then
begin
for i := 1 to ParamCount do
begin
Arg := ParamStr(i);
if Pos('lutris:rungameid/', Arg) = 1 then
begin
LutrisId := Copy(Arg, Length('lutris:rungameid/') + 1, MaxInt);
Log('Detected Lutris game ID: ' + LutrisId);
Cmd := 'lutris_id=' + LutrisId + '; slug=$(lutris --list-games --json 2>/dev/null | jq -r ".[] | select(.id == $lutris_id) | .slug"); [ -n "$slug" ] && config_file=$(find ~/.config/lutris/games/ -iname "${slug}-*.yml" | head -1); [ -n "$config_file" ] && grep -E "^\s*exe:" "$config_file" | sed "s/.*exe:[[:space:]]*//"';
ExePath := GetCommandOutput(Cmd);
if ExePath <> '' then
begin
GameDir := ExtractFilePath(ExePath);
Log('Resolved Lutris GameDir: ' + GameDir);
end
else
Log('Failed to resolve Lutris slug or game configuration file');
Break;
end;
end;
end;
// 3. Check STEAM_COMPAT_INSTALL_PATH fallback
if GameDir = '' then
begin
GameDir := GetEnvironmentVariable('STEAM_COMPAT_INSTALL_PATH');
if GameDir <> '' then
Log('Resolved GameDir from STEAM_COMPAT_INSTALL_PATH: ' + GameDir);
end;
// 4. Unreal Engine subfolder resolution
if (GameDir <> '') and DirectoryExists(IncludeTrailingPathDelimiter(GameDir) + 'Engine') then
begin
Log('UE Engine folder detected, searching for shipping executable...');
ExePath := FindUEShippingExe(GameDir, 1);
if ExePath <> '' then
begin
GameDir := ExePath;
Log('Adjusted UE GameDir: ' + GameDir);
end;
end;
end;
procedure SafeDeleteFile(const Path: string);
begin
if not FileExists(Path) then Exit;
try
if DeleteFile(Path) then
Log('Cleaned up file: ' + Path)
else
Log('Failed to delete file: ' + Path);
except
on E: Exception do
Log('Exception deleting ' + Path + ': ' + E.Message);
end;
end;
procedure SafeDeleteDirectory(const Path: string);
var
SR: TSearchRec;
FileP: string;
begin
if not DirectoryExists(Path) then Exit;
if FindFirst(IncludeTrailingPathDelimiter(Path) + '*', faAnyFile, SR) = 0 then
begin
try
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
FileP := IncludeTrailingPathDelimiter(Path) + SR.Name;
if (SR.Attr and faDirectory) <> 0 then
SafeDeleteDirectory(FileP)
else
SafeDeleteFile(FileP);
end;
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
end;
try
if RemoveDir(Path) then
Log('Removed directory: ' + Path)
else
Log('Failed to remove directory: ' + Path);
except
on E: Exception do
Log('Exception removing directory ' + Path + ': ' + E.Message);
end;
end;
// Marker-based ownership check.
// A proxy DLL is GOverlay-owned when its name is a known GOverlay proxy DLL
// (per IsProxyDllName) and a goverlay.vars marker file exists in the same
// directory. The goverlay.vars marker is written by the bgmod install flow
// (SafeCopyFile(ConfigDir + 'goverlay.vars', ...)) and is therefore a
// channel-agnostic signature that GOverlay placed the DLLs there, regardless
// of whether OptiScaler was installed on the stable or bleeding-edge channel.
// This replaces the previous file-size comparison against the global pristine
// bgmod/renames/<name>.dll / bgmod/OptiScaler.dll, which only matched the
// stable template and silently failed for bleeding-edge installs whose DLL
// has a different size.
function IsProxyDllName(const FileName: string): Boolean; forward;
function IsGOverlayProxyFile(const TargetDir, FileName: string): Boolean;
begin
Result := False;
if not IsProxyDllName(FileName) then Exit;
Result := FileExists(IncludeTrailingPathDelimiter(TargetDir) + 'goverlay.vars');
end;
function IsProxyDllName(const FileName: string): Boolean;
begin
Result := SameText(FileName, 'dxgi.dll') or
SameText(FileName, 'winmm.dll') or
SameText(FileName, 'dbghelp.dll') or
SameText(FileName, 'version.dll') or
SameText(FileName, 'wininet.dll') or
SameText(FileName, 'winhttp.dll');
end;
procedure CleanDirectory(const SrcDir, DestDir: string);
var
SR: TSearchRec;
SrcFile, DestFile: string;
begin
if not DirectoryExists(SrcDir) or not DirectoryExists(DestDir) then Exit;
if FindFirst(IncludeTrailingPathDelimiter(SrcDir) + '*', faAnyFile, SR) = 0 then
begin
try
repeat
if (SR.Name <> '.') and (SR.Name <> '..') then
begin
SrcFile := IncludeTrailingPathDelimiter(SrcDir) + SR.Name;
DestFile := IncludeTrailingPathDelimiter(DestDir) + SR.Name;
if (SR.Attr and faDirectory) <> 0 then
begin
CleanDirectory(SrcFile, DestFile);
RemoveDir(DestFile);
end
else
begin
if FileExists(DestFile) then
SafeDeleteFile(DestFile);
end;
end;
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
end;
end;
// Restore an original DLL from the per-game backup folder, or fall through to
// the marker-based delete if no backup exists. The in-GameDir `<file>.b`
// mechanism is gone — backups now live in BackupsDir (outside GameDir) so
// they cannot be corrupted by reinstalls or Steam "Verify integrity".
procedure SafeCleanOrRestore(const TargetDir, BackupsDir, FileName: string; IsOriginalGameFile: Boolean);
var
FullFile, FullBackup: string;
begin
FullFile := IncludeTrailingPathDelimiter(TargetDir) + FileName;
FullBackup := IncludeTrailingPathDelimiter(BackupsDir) + FileName;
if FileExists(FullBackup) then
begin
try
if FileExists(FullFile) then
DeleteFile(FullFile);
if CopyFile(FullBackup, FullFile) then
begin
Log('Restored original ' + FileName + ' from ' + BackupsDir);
DeleteFile(FullBackup);
end
else
Log('Failed to restore ' + FileName + ' from backup');
except
on E: Exception do
Log('Exception restoring ' + FileName + ': ' + E.Message);
end;
end
else if not IsOriginalGameFile then
begin
if IsProxyDllName(FileName) then
begin
if IsGOverlayProxyFile(TargetDir, FileName) then
SafeDeleteFile(FullFile)
else
Log('Skipping deletion of third-party proxy DLL: ' + FullFile);
end
else
SafeDeleteFile(FullFile);
end;
end;
function GetBGModPath: string;
var
DataHome: string;
Ini: TIniFile;
IsStable: Boolean;
ChannelFolder: string;
begin
IsStable := True;
if FileExists(IncludeTrailingPathDelimiter(UninstallerPath) + 'bgmod.conf') then
begin
Ini := TIniFile.Create(IncludeTrailingPathDelimiter(UninstallerPath) + 'bgmod.conf');
try
IsStable := Ini.ReadInteger('Config', 'OPT_CHANNEL', 0) <> 1;
finally
Ini.Free;
end;
end;
if IsStable then
ChannelFolder := 'optiscaler-stable'
else
ChannelFolder := 'optiscaler-edge';
DataHome := GetEnvironmentVariable('XDG_DATA_HOME');
if DataHome = '' then
DataHome := GetUserDir + '.local/share';
Result := IncludeTrailingPathDelimiter(DataHome) + 'goverlay' + PathDelim + ChannelFolder;
end;
var
TempStr, Key, Val, Line, CurrentOverrides: string;
i, StartArgIdx: Integer;
Args: array of PChar;
ArgsStrings: array of string;
IsGlobalUninstall: Boolean;
BackupsDir: string;
{$if defined(CPUAARCH64) and defined(LINUX)}
procedure Dummy_libc_csu_init; cdecl; public name '__libc_csu_init';
begin
end;
procedure Dummy_libc_csu_fini; cdecl; public name '__libc_csu_fini';
begin
end;
{$endif}
begin
GameDir := '';
UninstallerPath := ExtractFilePath(ParamStr(0));
// Original-DLL backup folder lives in the per-game GOverlay config dir
// (the parent of this binary's location for per-game installs, or
// bgmod/backups/ when the uninstaller binary was invoked from the global
// pristine bgmod/ folder). Backups are only restored from here; never
// written by the uninstaller.
BackupsDir := UninstallerPath + 'backups' + PathDelim;
// Resolve central GOverlay log path
CentralLogDir := '';
CentralLogFile := '';
if UninstallerPath <> '' then
begin
TempStr := ExcludeTrailingPathDelimiter(UninstallerPath);
Key := ExtractFileName(TempStr); // GameName or 'bgmod'
Val := ExtractFilePath(TempStr); // Parent folder path (e.g. gameconfig/ or share/goverlay/)
if Val <> '' then
begin
Line := ExtractFileName(ExcludeTrailingPathDelimiter(Val));
CurrentOverrides := ExtractFilePath(ExcludeTrailingPathDelimiter(Val));
if LowerCase(Line) = 'gameconfig' then
begin
CentralLogDir := IncludeTrailingPathDelimiter(CurrentOverrides) + 'logs' + PathDelim + Key;
CentralLogFile := IncludeTrailingPathDelimiter(CentralLogDir) + 'bgmod-uninstaller.log';
end
else if LowerCase(Key) = 'bgmod' then
begin
CentralLogDir := IncludeTrailingPathDelimiter(Val) + 'logs';
CentralLogFile := IncludeTrailingPathDelimiter(CentralLogDir) + 'bgmod-uninstaller.log';
end;
end;
end;
IsGlobalUninstall := False;
for i := 1 to ParamCount do
begin
if (ParamStr(i) = '--global') or (ParamStr(i) = '-g') then
begin
IsGlobalUninstall := True;
Break;
end;
end;
if IsGlobalUninstall then
begin
Log('========================= bgmod global uninstall =========================');
// Get goverlay base folder (parent of cache folders)
// ExtractFileDir of GetBGModPath is e.g. ~/.local/share/goverlay
TempStr := IncludeTrailingPathDelimiter(ExtractFileDir(ExcludeTrailingPathDelimiter(GetBGModPath)));
// Delete stable cache
if DirectoryExists(TempStr + 'optiscaler-stable') then
begin
Log('Removing stable cache directory: ' + TempStr + 'optiscaler-stable');
SafeDeleteDirectory(TempStr + 'optiscaler-stable');
end;
// Delete edge cache
if DirectoryExists(TempStr + 'optiscaler-edge') then
begin
Log('Removing edge cache directory: ' + TempStr + 'optiscaler-edge');
SafeDeleteDirectory(TempStr + 'optiscaler-edge');
end;
// Delete dlssenabler cache
if DirectoryExists(TempStr + 'dlssenabler-stable') then
begin
Log('Removing dlssenabler stable cache directory: ' + TempStr + 'dlssenabler-stable');
SafeDeleteDirectory(TempStr + 'dlssenabler-stable');
end;
if DirectoryExists(TempStr + 'dlssenabler-edge') then
begin
Log('Removing legacy dlssenabler edge cache directory: ' + TempStr + 'dlssenabler-edge');
SafeDeleteDirectory(TempStr + 'dlssenabler-edge');
end;
// Delete bgmod template folder
if DirectoryExists(TempStr + 'bgmod') then
begin
Log('Removing global bgmod template directory: ' + TempStr + 'bgmod');
SafeDeleteDirectory(TempStr + 'bgmod');
end;
Exit;
end;
// Resolve the game folder directory
ResolveGameDirectory;
Log('========================= bgmod uninstaller initialization =========================');
Log('Uninstaller location: ' + UninstallerPath);
Log('Game directory: ' + GameDir);
if GameDir <> '' then
begin
if not DirectoryExists(GameDir) then
Log('Error: Resolved game directory does not exist: ' + GameDir)
else if fpAccess(PChar(GameDir), W_OK) <> 0 then
Log('Error: No write permission to game directory: ' + GameDir)
else
begin
Log('Starting uninstallation from game directory...');
// Delete legacy in-GameDir <file>.b backups (untrusted post-bug).
// The new isolated-folder backup mechanism does not use these; any
// leftover .b files come from the previous buggy SafeBackupFile that
// could store a previous GOverlay proxy as the "original". Removing
// them is unconditional so this uninstall cannot restore a proxy via
// the old .b path (which no longer exists in SafeCleanOrRestore anyway,
// but we delete the breadcrumbs so they don't linger in GameDir).
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'd3dcompiler_47.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'amd_fidelityfx_dx12.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'amd_fidelityfx_loader_dx12.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'amd_fidelityfx_framegeneration_dx12.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'amd_fidelityfx_upscaler_dx12.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'amd_fidelityfx_vk.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'libxess.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'libxess_dx11.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'libxess_fg.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'libxell.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'nvngx.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'nvngx_dlss.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'nvngx_dlssd.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'nvngx_dlssg.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'dxgi.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'winmm.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'dbghelp.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'version.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'wininet.dll.b');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'winhttp.dll.b');
Log('Cleaned legacy in-GameDir .b breadcrumbs (if any).');
// Original game files (ONLY restore if backup exists, do NOT delete if no backup)
SafeCleanOrRestore(GameDir, BackupsDir, 'd3dcompiler_47.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'amd_fidelityfx_dx12.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'amd_fidelityfx_framegeneration_dx12.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'amd_fidelityfx_upscaler_dx12.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'amd_fidelityfx_vk.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'libxess.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'libxess_dx11.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'libxess_fg.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'libxell.dll', True);
// Copied proxy/supporting files (restore if backup exists, otherwise safe to delete)
SafeCleanOrRestore(GameDir, BackupsDir, 'OptiScaler.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dxgi.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'winmm.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dbghelp.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'version.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'wininet.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'winhttp.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'd3d12.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'OptiScaler.ini', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'amd_fidelityfx_loader_dx12.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'OptiScaler.log', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'OptiScaler.asi', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlssg_to_fsr3_amd_is_better.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlssg_to_fsr3.ini', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlssg_to_fsr3.log', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'nvapi64.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'fakenvapi.ini', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'fakenvapi.log', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'fakenvapi.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'nvngx.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'nvngx.ini', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'nvngx_dlss.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'nvngx_dlssd.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'nvngx_dlssg.dll', True);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlss-enabler.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlss-enabler-upscaler.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlss-enabler.log', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlss-enabler.ini', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'nvngx-wrapper.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, '_nvngx.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'dlssg_to_fsr3_amd_is_better-3.0.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.common.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.dlss.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.dlss_g.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.interposer.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.nis.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.reflex.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.template.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.reflex_vk.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.pcl.dll', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.common.log', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'sl.interposer.log', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'MangoHud.conf', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'vkBasalt.conf', False);
SafeCleanOrRestore(GameDir, BackupsDir, 'vkSumi.conf', False);
// Remove plugins folder (only files matching global plugins)
CleanDirectory(IncludeTrailingPathDelimiter(GetBGModPath) + 'plugins', IncludeTrailingPathDelimiter(GameDir) + 'plugins');
RemoveDir(IncludeTrailingPathDelimiter(GameDir) + 'plugins');
SafeDeleteDirectory(IncludeTrailingPathDelimiter(GameDir) + 'OptiScaler');
SafeDeleteDirectory(IncludeTrailingPathDelimiter(GameDir) + 'D3D12_OptiScaler');
// Remove wrappers and script configs
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'bgmod');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'fgmod');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'bgmod-uninstaller.sh');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'bgmod-remover.sh');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'bgmod.conf');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'bgmod.log');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'MangoHud.conf');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'vkBasalt.conf');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'vkSumi.conf');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'bgmod-uninstaller');
SafeDeleteFile(IncludeTrailingPathDelimiter(GameDir) + 'goverlay.vars');
Log('Uninstallation from game directory completed.');
end;
end;
// Execute the game/original command if requested
StartArgIdx := 1;
while (StartArgIdx <= ParamCount) and ((ParamStr(StartArgIdx) = '--') or (ParamStr(StartArgIdx) = '--global') or (ParamStr(StartArgIdx) = '-g')) do
Inc(StartArgIdx);
if StartArgIdx > ParamCount then
begin
Log('bgmod uninstaller done.');
Exit;
end;
TempStr := ParamStr(StartArgIdx);
Log('Game Executable: ' + TempStr);
SetLength(ArgsStrings, ParamCount - StartArgIdx + 1);
SetLength(Args, ParamCount - StartArgIdx + 2);
ArgsStrings[0] := TempStr;
Args[0] := PChar(ArgsStrings[0]);
for i := StartArgIdx + 1 to ParamCount do
begin
ArgsStrings[i - StartArgIdx] := ParamStr(i);
Args[i - StartArgIdx] := PChar(ArgsStrings[i - StartArgIdx]);
Log('Arg ' + IntToStr(i - StartArgIdx) + ': ' + ArgsStrings[i - StartArgIdx]);
end;
Args[ParamCount - StartArgIdx + 1] := nil;
Log('------------------------------------------------------------------------');
Log('Launching subprocess: ' + ArgsStrings[0]);
Log('------------------------------------------------------------------------');
execvp(Args[0], @Args[0]);
// If we reach here, execvp failed
Log('Error: execvp failed');
Halt(127);
end.