peterx684
peterx684

Reputation: 23

Read installation path from JSON file in Inno Setup

I want to create a script for Inno Setup where the install path would be taken from a file in defined directory - no registry. I suppose it would require writing specific code for it, where would be defined some variable which will contain the value after reading the file. The path and name of the file is always the same for any user so the only value that changes is the install path.

Complete structure, where InstallLocation is the variable:

{
    "FormatVersion": 0,
    "bIsIncompleteInstall": false,
    "AppVersionString": "1.0.1",
    ...
    "InstallLocation": "h:\\Program Files\\Epic Games\\Limbo",
    ...
}

Any ideas for ideal code that would do this?

Thank you

Upvotes: 1

Views: 511

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202118

Implement a scripted constant to provide the value to DefaultDirName directive.

You can use JsonParser library to parse the JSON config file.

[Setup]
DefaultDirName={code:GetInstallLocation}

[Code]

#include "JsonParser.pas"

// Here go the other functions the below code needs.
// See the comments at the end of the post.

const
  CP_UTF8 = 65001;

var
  InstallLocation: string;

<event('InitializeSetup')>
function InitializeSetupParseConfig(): Boolean;
var
  Json: string;
  ConfigPath: string;
  JsonParser: TJsonParser;
  JsonRoot: TJsonObject;
  S: TJsonString;
begin
  Result := True;
  ConfigPath := 'C:\path\to\config.json';
  Log(Format('Reading "%s"', [ConfigPath]));
  if not LoadStringFromFileInCP(ConfigPath, Json, CP_UTF8) then
  begin
    MsgBox(Format('Error reading "%s"', [ConfigPath]), mbError, MB_OK);
    Result := False;
  end
    else
  if not ParseJsonAndLogErrors(JsonParser, Json) then
  begin
    MsgBox(Format('Error parsing "%s"', [ConfigPath]), mbError, MB_OK);
    Result := False;
  end
    else
  begin 
    JsonRoot := GetJsonRoot(JsonParser.Output);
    if not FindJsonString(JsonParser.Output, JsonRoot, 'InstallLocation', S) then
    begin
      MsgBox(Format('Cannot find InstallLocation in "%s"', [ConfigPath]),
        mbError, MB_OK);
      Result := False;
    end
      else
    begin
      InstallLocation := S;
      Log(Format('Found InstallLocation = "%s"', [InstallLocation]));
    end;
    ClearJsonParser(JsonParser);
  end;
end;

function GetInstallLocation(Param: string): string;
begin
  Result := InstallLocation;
end;

The code uses functions from:

Upvotes: 1

Related Questions