Skip to content

Instantly share code, notes, and snippets.

@marth8880
Last active August 1, 2018 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marth8880/1f7d11126899267b44bd1614e6bfd995 to your computer and use it in GitHub Desktop.
Save marth8880/1f7d11126899267b44bd1614e6bfd995 to your computer and use it in GitHub Desktop.
SWBF2 directory auto-detect for Inno Setup Compiler
// USAGE: Paste the following code in your setup script's [Code] section. Then, in your [Setup] section,
// use {code:GetDirName} for the value of DefaultDirName.
function ParseSteamConfig(FileName: string; var Paths: TArrayOfString): Boolean;
var
Lines: TArrayOfString;
I: Integer;
Line: string;
P: Integer;
Key: string;
Value: string;
Count: Integer;
BaseInstallFolderKeyPrefix: string;
begin
BaseInstallFolderKeyPrefix := 'BaseInstallFolder_';
Result := LoadStringsFromFile(FileName, Lines);
Count := 0;
// source: https://stackoverflow.com/a/37019690/3639133
// parse Steam's config.vdf file and extract the Steam library directory paths
for I := 0 to GetArrayLength(Lines) - 1 do
begin
Line := Trim(Lines[I]);
if Copy(Line, 1, 1) = '"' then
begin
Delete(Line, 1, 1);
P := Pos('"', Line);
if P > 0 then
begin
Key := Trim(Copy(Line, 1, P - 1));
Delete(Line, 1, P);
Line := Trim(Line);
//Log(Format('Found key "%s"', [Key]));
if (CompareText(Copy(Key, 1, Length(BaseInstallFolderKeyPrefix)), BaseInstallFolderKeyPrefix) = 0) and
(Line[1] = '"') then
begin
//Log(Format('Found base install folder key "%s"', [Key]));
Delete(Line, 1, 1);
P := Pos('"', Line);
if P > 0 then
begin
Value := Trim(Copy(Line, 1, P - 1));
StringChange(Value, '\\', '\');
Log(Format('Found base install folder "%s"', [Value]));
Count := Count + 1;
SetArrayLength(Paths, Count);
Paths[Count - 1] := Value;
end;
end;
end;
end;
end;
end;
function GetDirName(const Value: string): string;
var
InstallPath: string;
SteamPath: string;
SteamConfigFileArray: TArrayOfString;
SteamLibs: TArrayOfString;
SteamConfigFilePath: string;
I: integer;
begin
// initialize default path, which will be returned when the following registry
// key queries fail due to missing keys or for some different reason
Result := ExpandConstant('{pf}\LucasArts\Star Wars Battlefront II\GameData\');
// query the first registry value; if this succeeds, return the obtained value
if RegQueryStringValue(HKLM, 'SOFTWARE\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
Result := InstallPath
// otherwise the first registry key query failed, so query the second registry value; if it succeeds, return the obtained value
else if RegQueryStringValue(HKLM, 'SOFTWARE\Wow6432Node\LucasArts\Star Wars Battlefront II\1.0', 'ExePath', InstallPath) then
Result := InstallPath
else
// if the registry values aren't found, check if the game is through Steam
begin
SteamPath := ExpandConstant('{reg:HKLM\SOFTWARE\Valve\Steam,SteamPath}');
if SteamPath = '' then
SteamPath := ExpandConstant('{reg:HKCU\SOFTWARE\Valve\Steam,SteamPath}');
if SteamPath = '' then
SteamPath := ExpandConstant('{pf}\Steam');
Log(Format('Found SteamPath: %s', [SteamPath]));
SteamConfigFilePath := SteamPath + '\config\config.vdf';
if FileExists(SteamConfigFilePath) then
begin
if LoadStringsFromFile(SteamConfigFilePath, SteamConfigFileArray) then
begin
// get the user's Steam library paths
ParseSteamConfig(SteamConfigFilePath, SteamLibs);
// look for the game directory in each Steam library
for I := 0 to GetArrayLength(SteamLibs) - 1 do
begin
if FileExists(SteamLibs[i] + '\steamapps\common\Star Wars Battlefront II\GameData\BattlefrontII.exe') then
begin
Result := SteamLibs[i] + '\steamapps\common\Star Wars Battlefront II\GameData\BattlefrontII.exe';
break;
end;
end;
end;
end;
end;
Log(Format('Result is %s', [ExtractFilePath(Result)]));
// return only the path
Result := ExtractFilePath(Result);
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment