2017-12-14 24 views
1

Inno Setup을 사용하여 설치 프로그램을 만들고 싶습니다. 모든 컴퓨터에서 프로그램을 작동 시키려면 도구 디렉토리를 .json -file로 변경해야합니다. 나는 inno-json-config 라이브러리를 사용하여이 문제를 해결하기를 희망Inno 설치 : JSON 배열의 값 변경

{ 
    "commandScriptLinux" : "", 
    "copyToolBehavior" : "once", 
    "deleteWorkingDirectoriesAfterWorkflowExecution" : true, 
    "deleteWorkingDirectoriesKeepOnErrorOnce" : true, 
    "deleteWorkingDirectoriesNever" : true, 
    "documentationFilePath" : "", 
    "enableCommandScriptWindows" : true, 
    "imitationScript" : "", 
    "imitationToolOutputFilename" : "", 
    "launchSettings" : 
    [ 
    { 
     "limitInstallationInstancesNumber" : "1", 
     "limitInstallationInstances" : "false", 
     "toolDirectory" : "%Selected Setup Folder%", 
     "version" : "1.0" 
    } 
    ], 
} 

: 다음은이 파일에서 발췌 한 것입니다. 불행히도, 코드를 실행 한 후에는 줄이 뒤 바뀌며 (마지막 줄이 먼저옵니다) 변경 사항이 적용되지 않습니다.

[Setup] 
AppName=Change_Config 
AppVersion=1.0 
DefaultDirName={userdocs}\Change_Config 

[Files] 
Source: "JSONConfig.dll"; Flags: dontcopy 

[Code] 
function JSONQueryString(FileName, Section, Key, Default: WideString; 
    var Value: WideString; var ValueLength: Integer): Boolean; 
    external '[email protected]:jsonconfig.dll stdcall'; 
function JSONWriteString(FileName, Section, Key, 
    Value: WideString): Boolean; 
    external '[email protected]:jsonconfig.dll stdcall'; 

procedure InitializeWizard; 
var 
    FileName: WideString; 
    IntValue: Int64; 
    StrValue: WideString; 
    StrLength: Integer; 
    BoolValue: Boolean; 
begin 
    FileName := 'c:\configuration.json'; 
    SetLength(StrValue, 16); 
    StrLength := Length(StrValue); 

    if JSONQueryString(
     FileName, 'launchSettings', 'toolDirectory', 'Default', StrValue, StrLength) then 
    MsgBox('Section_1:Key_1=' + StrValue, mbInformation, MB_OK); 

    if not JSONWriteString(FileName, 'launchSettings', 'toolDirectory', 'Test') then 
    MsgBox('JSONWriteString Section_1:Key_1 failed!', mbError, MB_OK); 
end; 

대단히 감사합니다.

감사 알렉스

답변

0

launchSettings 배열이다. inno-json-config 라이브러리가 배열을 지원하지 않는다고 생각합니다.

대신 JsonParser library을 사용할 수 있습니다.

[Code] 

#include "JsonParser.pas" 

function FindJsonValue(
    Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString; 
    var Value: TJsonValue): Boolean; 
var 
    I: Integer; 
begin 
    for I := 0 to Length(Parent) - 1 do 
    begin 
    if Parent[I].Key = Key then 
    begin 
     Value := Parent[I].Value; 
     Result := True; 
     Exit; 
    end; 
    end; 

    Result := False; 
end; 

function FindJsonArray(
    Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString; 
    var Arr: TJsonArray): Boolean; 
var 
    JsonValue: TJsonValue; 
begin 
    Result := 
    FindJsonValue(Output, Parent, Key, JsonValue) and 
    (JsonValue.Kind = JVKArray); 

    if Result then 
    begin 
    Arr := Output.Arrays[JsonValue.Index]; 
    end; 
end; 

{ ... } 

var 
    JsonLines: TStringList; 
    JsonParser: TJsonParser; 
    LaunchSettingsArray: TJsonArray; 
    ToolDirectoryValue: TJsonValue; 
    I: Integer; 
begin 
    { ... } 

    JsonLines := TStringList.Create; 
    JsonLines.LoadFromFile(FileName); 

    ParseJson(JsonParser, JsonLines.Text); 

    if Length(JsonParser.Output.Errors) > 0 then 
    begin 
    Log('Error parsing JSON'); 
    for I := 0 to Length(JsonParser.Output.Errors) - 1 do 
    begin 
     Log(JsonParser.Output.Errors[I]); 
    end; 
    end 
    else 
    begin 
    if FindJsonArray(
     JsonParser.Output, JsonParser.Output.Objects[0], 
     'launchSettings', LaunchSettingsArray) and 
     (GetArrayLength(LaunchSettingsArray) >= 0) and 
     (LaunchSettingsArray[0].Kind = JVKObject) and 
     FindJsonValue(
     JsonParser.Output, 
     JsonParser.Output.Objects[LaunchSettingsArray[0].Index], 'toolDirectory', 
     ToolDirectoryValue) and 
     (ToolDirectoryValue.Kind = JVKString) then 
    begin 
     Log(Format(
     'launchSettings[0]:toolDirectory:%s', [ 
     JsonParser.Output.Strings[ToolDirectoryValue.Index]])); 
     JsonParser.Output.Strings[ToolDirectoryValue.Index] := 'Test'; 
     JsonLines.Clear; 
     PrintJsonParserOutput(JsonParser.Output, JsonLines); 
     JsonLines.SaveToFile(FileName); 
    end; 
    end; 

    ClearJsonParser(JsonParser); 
    JsonLines.Free; 
end; 

여전히 주문을 보존하지는 않지만 (중요하지 않음).


귀하의 경우 JSON을 구문 분석 할 필요는 없지만 단순히 %Selected Setup Folder%을 원하는 값으로 바꿀 수 있습니다.

Replace placeholder in an installed text file with input entered by user을 참조하십시오.