inno-setup

Choose between source paths for all files


I automatically run an obfuscator when I compile my project in visual studio. The obfuscated .dll files are saved with the same filename but in subfolders.

Folder structure

FileA_Secure (subfolder)
    FileA.dll
FileA.dll

Question: Can I make sure that Inno Setup compiles FileA.dll from the subfolder FileA_Secure and not the FileA.dll from the main folder if the "secured" FileA.dll exists?


Solution

  • You can use Inno Setup preprocessor to conditionally select a source. Particularly, use the #ifexist directive:

    [Files]
    #ifexist "MyClass_Secure\MyClass.dll"
    Source: "MyClass_Secure\MyClass.dll"; DestDir: "{app}"
    #else
    Source: "MyClass.dll"; DestDir: "{app}"
    #endif
    

    Add SaveToFile to the end of your .iss, to see what is the final script like:

    #expr SaveToFile(AddBackslash(SourcePath) + "Preprocessed.iss")
    

    If you need to do this for many files, you can define a macro:

    #define GetDllSource(Name) \
        FileExists(Name + "_Secure\" + Name + ".dll") ? \
            Name + "_Secure\" + Name + ".dll" : Name + ".dll"
    
    [Files]
    Source: {#GetDllSource("MyClass")}; DestDir: "{app}"
    Source: {#GetDllSource("MyClass2")}; DestDir: "{app}"
    

    If you want to do this for all files in a folder and its subfolders, it gets way more complicated:

    #pragma parseroption -p-
    
    ; For given DLL, return path to secured DLL, if exists,
    ; otherwise return the DLL itself
    #define GetDllSource(FileName) \
        Local[0] = ExtractFileName(FileName), \
        Local[1] = Pos(".", Local[0]), \
        Local[2] = Local[1] > 0 ? Copy(Local[0], 1, Local[1] - 1) : Local[0], \
        Local[3] = ExtractFilePath(FileName), \
        Local[4] = Local[3] + "\\" + Local[2] + "_Secure\\" + Local[0], \
        FileExists(Local[4]) ? Local[4] : FileName
    
    ; For DLLs, returns [Files] section entry, skip other files
    #define FileEntry(Source) \
        (ExtractFileExt(Source) != "dll") ? \
            "" : "Source: " + GetDllSource(Source) + "; DestDir: {app}\n"
    
    ; If the directory entry is folder, call ProcessFolder.
    ; If it is a file, call FileEntry
    #define ProcessFile(Source, FindResult, FindHandle) \
        FindResult \
            ? \
                Local[0] = FindGetFileName(FindHandle), \
                Local[1] = Source + "\\" + Local[0], \
                (Local[0] != "." && Local[0] != ".." \
                    ? (DirExists(Local[1]) \
                        ? ProcessFolder(Local[1]) : FileEntry(Local[1])) \
                    : "") + \
                ProcessFile(Source, FindNext(FindHandle), FindHandle) \
            : \
                ""
    
    ; If the folder is not _Secure, process its files and subfolders
    #define ProcessFolder(Source) \
        (Pos("_Secure", ExtractFileName(Source)) > 0) ? \
            "" : \
            (Local[0] = FindFirst(Source + "\\*", faAnyFile), \
            ProcessFile(Source, Local[0], Local[0]))
    
    #pragma parseroption -p+
    
    [Files]
    ; Process this folder for DLLs and subfolders (can be changed for a real path)
    #emit ProcessFolder(".")