2

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?

Johan
  • 502
  • 4
  • 18

1 Answers1

2

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(".")
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks, but if I don't want to list all files one by one and make this check would it still be possible. Like Source * . * but for each file X check if X_Secure\X exists if so choose that file instead. – Johan Aug 30 '17 at 20:50
  • Maybe you should show us your script to make clear what you actually do – Martin Prikryl Aug 30 '17 at 20:51
  • I can post it tomorrow, but I basically just do this now (and manually have to replace all files that have an obfuscated version before compiling and removing the secured subfolders). My hope would be to automate that part. Source: "* . *"; DestDir: "{app}" Flags: recursesubdirs – Johan Aug 30 '17 at 20:56
  • `recursesubdirs` will find both obfuscated and unobfuscated version, won't? Do you really have dlls in subfolders? – Martin Prikryl Aug 30 '17 at 20:57
  • I will try your macro tomorrow. If that can be called with * . * then it is the answer. – Johan Aug 30 '17 at 21:01
  • No it cannot. But it can be made to allow that. But we need to know, if you really need the `recursesubdirs` - and if you need, how it should handle the `_Secure` subfolders. I assume it would have to skip those, right? – Martin Prikryl Aug 30 '17 at 21:03
  • Yes I have other subfolders that should be compiled but indeed I need to exclude all subfolders ending with _Secured from the recursive search – Johan Aug 30 '17 at 21:06
  • Great....yes it was certainly a bit more complicated. Will try it out tomorrow morning and let you know. Thanks a lot for this extremely fast and great support! – Johan Aug 30 '17 at 21:14