forked from AutoHotkey/Ahk2Exe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compiler.ahk
156 lines (126 loc) · 4.26 KB
/
Compiler.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#Include ScriptParser.ahk
#Include IconChanger.ahk
AhkCompile(ByRef AhkFile, ExeFile="", ByRef CustomIcon="", BinFile="", UseMPRESS="")
{
global ExeFileTmp
AhkFile := Util_GetFullPath(AhkFile)
if AhkFile =
Util_Error("Error: Source file not specified.")
SplitPath, AhkFile,, AhkFile_Dir,, AhkFile_NameNoExt
if ExeFile =
ExeFile = %AhkFile_Dir%\%AhkFile_NameNoExt%.exe
else
ExeFile := Util_GetFullPath(ExeFile)
ExeFileTmp := ExeFile
if BinFile =
BinFile = %A_ScriptDir%\AutoHotkeySC.bin
Util_DisplayHourglass()
IfNotExist, %BinFile%
Util_Error("Error: The selected AutoHotkeySC binary does not exist.", 1, BinFile)
try FileCopy, %BinFile%, %ExeFile%, 1
catch
Util_Error("Error: Unable to copy AutoHotkeySC binary file to destination.")
BundleAhkScript(ExeFile, AhkFile, CustomIcon)
if FileExist(A_ScriptDir "\mpress.exe") && UseMPRESS
{
Util_Status("Compressing final executable...")
RunWait, "%A_ScriptDir%\mpress.exe" -q -x "%ExeFile%",, Hide
}
Util_HideHourglass()
Util_Status("")
}
BundleAhkScript(ExeFile, AhkFile, IcoFile="")
{
SplitPath, AhkFile,, ScriptDir
ExtraFiles := []
PreprocessScript(ScriptBody, AhkFile, ExtraFiles)
;FileDelete, %ExeFile%.ahk
;FileAppend, % ScriptBody, %ExeFile%.ahk
VarSetCapacity(BinScriptBody, BinScriptBody_Len := StrPut(ScriptBody, "UTF-8"))
StrPut(ScriptBody, &BinScriptBody, "UTF-8")
module := DllCall("BeginUpdateResource", "str", ExeFile, "uint", 0, "ptr")
if !module
Util_Error("Error: Error opening the destination file.")
if IcoFile
{
Util_Status("Changing the main icon...")
if !ReplaceAhkIcon(module, IcoFile, ExeFile, 159)
{
; Error was already displayed
gosub _EndUpdateResource
Util_Error("Error changing icon: Unable to read icon or icon was of the wrong format.")
}
}
Util_Status("Compressing and adding: Master Script")
if !DllCall("UpdateResource", "ptr", module, "ptr", 10, "str", IcoFile ? ">AHK WITH ICON<" : ">AUTOHOTKEY SCRIPT<"
, "ushort", 0x409, "ptr", &BinScriptBody, "uint", BinScriptBody_Len, "uint")
goto _FailEnd
oldWD := A_WorkingDir
SetWorkingDir, %ScriptDir%
for each,resFile in ExtraFiles
{
file := resFile.file
Util_Status("Compressing and adding: " file)
IfNotExist, %file%
goto _FailEnd2
resType := toUpper(ifDefault(trim(resFile.resType),10)) ; default resource-type: RT_RCDATA = 10
resName := toUpper(ifDefault(trim(resFile.resName),file)) ; default resource-name: the file name
resLang := ifDefault(resFile.resLang+0, 0x409) ; default language: english (0x409)
if(file ~= "i)\.ico$" and resType==14){ ; .ico files added to the icon-group section require special handling
ReplaceAhkIcon(module, file, ExeFile, resName, resLang)
}else{
if(file ~= "i)\.ahk$" and resFile.preProcess){ ; if it's a AHK script and pre-processing is enabled then we preprocess it
PreprocessScript(scriptCont, file, [])
VarSetCapacity(filedata, filesize := StrPut(scriptCont, "UTF-8"))
StrPut(scriptCont, &filedata, "UTF-8")
}else{ ; any other file type is added as is
; This "old-school" method of reading binary files is way faster than using file objects.
FileGetSize, filesize, %file%
VarSetCapacity(filedata, filesize)
FileRead, filedata, *c %file%
}
if !DllCall("UpdateResource"
, "ptr", module
, isNum(resType)?"ptr":"str", resType
, isNum(resName)?"ptr":"str", resName
, "ushort", resLang
, "ptr", &filedata
, "uint", filesize, "uint")
goto _FailEnd2
VarSetCapacity(filedata, 0)
}
}
SetWorkingDir, %oldWD%
gosub _EndUpdateResource
return
_FailEnd:
gosub _EndUpdateResource
Util_Error("Error adding script file:`n`n" AhkFile)
_FailEnd2:
gosub _EndUpdateResource
Util_Error("Error adding FileInstall file:`n`n" file)
_EndUpdateResource:
if !DllCall("EndUpdateResource", "ptr", module, "uint", 0)
Util_Error("Error: Error opening the destination file.")
return
}
ifDefault(param,default){
return param=="" ? default : param
}
toUpper(str){
StringUpper str, str
return str
}
isNum(param){
if param is number
return true
return false
}
Util_GetFullPath(path)
{
VarSetCapacity(fullpath, 260 * (!!A_IsUnicode + 1))
if DllCall("GetFullPathName", "str", path, "uint", 260, "str", fullpath, "ptr", 0, "uint")
return fullpath
else
return ""
}