forked from AutoHotkey/Ahk2Exe
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCompiler.ahk
More file actions
296 lines (260 loc) · 10 KB
/
Compiler.ahk
File metadata and controls
296 lines (260 loc) · 10 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;
; File encoding: UTF-8 with BOM
;
#Include ScriptParser.ahk
#Include IconChanger.ahk
#Include Directives.ahk
AhkCompile(ByRef AhkFile, ExeFile := "", ByRef CustomIcon := "", BinFile := "", UseMPRESS := "", fileCP:="", UseCompression := "", UseInclude := "", UseIncludeResource := "", UsePassword := "AutoHotkey")
{
global ExeFileTmp, ExeFileG
SetWorkingDir %AhkWorkingDir%
SplitPath AhkFile,, Ahk_Dir,, Ahk_Name
SplitPath ExeFile,, Edir,, Ename
ExeFile := (Edir ? Edir : Ahk_Dir) "\" (xe:= Ename ? Ename : Ahk_Name ) ".exe"
ExeFile := Util_GetFullPath(ExeFile)
if (CustomIcon != "")
{ SplitPath CustomIcon,, Idir,, Iname
CustomIcon := (Idir ? Idir : Ahk_Dir) "\" (Iname ? Iname : Ahk_Name ) ".ico"
CustomIcon := Util_GetFullPath(CustomIcon)
}
SetWorkingDir %Ahk_Dir% ; Initial folder for any #Include's
; Get temp file name - remove any invalid "path/" from exe name (/ should be \)
ExeFileTmp := Util_TempFile(, "exe~", RegExReplace(xe,"^.*/"))
if BinFile =
BinFile = %A_ScriptDir%\AutoHotkeySC.bin
Util_DisplayHourglass()
IfNotExist, %BinFile%
Util_Error("Error: The selected AutoHotkeySC binary does not exist. (C1)"
, 0x34, """" BinFile """")
try FileCopy, %BinFile%, %ExeFileTmp%, 1
catch
Util_Error("Error: Unable to copy AutoHotkeySC binary file to destination."
, 0x41, """" ExeFileTmp """")
DerefIncludeVars.Delete("U_", "V_") ; Clear Directives entries
DerefIncludeVars.Delete("A_WorkFileName")
DerefIncludeVars.Delete("A_PriorLine")
BinType := AHKType(ExeFileTmp)
DerefIncludeVars.A_AhkVersion := BinType.Version
DerefIncludeVars.A_PtrSize := BinType.PtrSize
DerefIncludeVars.A_IsUnicode := BinType.IsUnicode
ExeFileG := ExeFile
BundleAhkScript(ExeFileTmp, AhkFile, UseMPRESS, CustomIcon, fileCP, UseCompression, UsePassword)
; the final step...
Util_Status("Moving .exe to destination")
Loop
{ FileMove, %ExeFileTmp%, %ExeFileG%, 1
if !ErrorLevel
break
Util_HideHourglass()
DetectHiddenWindows On
if !WinExist("ahk_exe " ExeFileG)
Util_Error("Error: Could not move final compiled binary file to "
. "destination. (C1)", 0x45, """" ExeFileG """")
else
{ SetTimer Buttons, 50
wk := """" RegExReplace(ExeFileG, "^.+\\") """"
MsgBox 51,Ahk2Exe Query,% "Warning: " wk " is still running, "
. "and needs to be unloaded to allow replacement with this new version."
. "`n`n Press the appropriate button to continue."
. " ('Reload' unloads and reloads the new " wk " without any parameters.)"
IfMsgBox Cancel
Util_Error("Error: Could not move final compiled binary file to "
. "destination. (C2)", 0x45, """" ExeFileG """")
WinClose ahk_exe %ExeFileG%
WinWaitClose ahk_exe %ExeFileG%,,1
IfMsgBox No
Reload := 1
} }
if Reload
run "%ExeFileG%", %ExeFileG%\..
Util_HideHourglass()
Util_Status("")
}
Buttons()
{ IfWinNotExist Ahk2Exe Query
return
SetTimer,, Off
WinActivate
ControlSetText Button1, &Unload
ControlSetText Button2, && &Reload
}
BundleAhkScript(ExeFile, AhkFile, UseMPRESS, IcoFile="", fileCP="", UseCompression := 0, UsePassword := "")
{
global AhkPath := UseAhkPath
if (AhkPath = "")
AhkPath := SubStr(BinFile,"SC.bin") ? SubStr(BinFile,1,-5) ".exe" ? BinFile
if fileCP is space
if SubStr(DerefIncludeVars.A_AhkVersion,1,1) = 2
fileCP := "UTF-8" ; Default for v2 is UTF-8
else fileCP := A_FileEncoding
try FileEncoding, %fileCP%
catch e
Util_Error("Error: Invalid codepage parameter """ fileCP """ was given.", 0x53)
SplitPath, AhkFile,, ScriptDir
ExtraFiles := []
,Directives := PreprocessScript(ScriptBody, AhkFile, ExtraFiles)
,ScriptBody :=Trim(ScriptBody,"`n")
If UseCompression {
FileDelete, %A_AhkDir%\BinScriptBody.ahk
FileAppend, %ScriptBody%, %A_AhkDir%\BinScriptBody.ahk
If SubStr(DerefIncludeVars.A_AhkVersion,1,1) = 2
PID:=DynaRun("
(
UsePassword:=''
buf:=BufferAlloc(bufsz:=10485760,00),totalsz:=0,buf1:=BufferAlloc(10485760)
Loop Read, '" A_AhkDir "\BinScriptBody.ahk'
{
If (A_LoopReadLine=''){
NumPut('Char', 10, buf.Ptr + totalsz)
,totalsz+=1
continue
}
data:=StrBuf(A_LoopReadLine,'UTF-8')
,zip:=UsePassword?ZipRawMemory(data,, '" UsePassword "' ):ZipRawMemory(data)
,CryptBinaryToStringA(zip, zip.size, 0x1|0x40000000, 0, getvar(cryptedsz:=0))
,tosavesz:=cryptedsz
,CryptBinaryToStringA(zip, zip.size, 0x1|0x40000000, buf1, getvar(cryptedsz))
,NumPut('Char', 10, buf1.Ptr+cryptedsz)
if (totalsz+tosavesz>bufsz)
newbuf:=BufferAlloc(bufsz*=2),RtlMoveMemory(newbuf,buf,totalsz),buf:=newbuf
RtlMoveMemory(buf.Ptr + totalsz,buf1,tosavesz)
,totalsz+=tosavesz
}
NumPut('Char', 0, buf.Ptr + totalsz - 1)
If !BinScriptBody := ZipRawMemory(buf.Ptr,totalsz,'" UsePassword "')
ExitApp
f:=FileOpen(A_AhkDir '\..\BinScriptBody.bin','w -rwd'),f.RawWrite(BinScriptBody),f.Close()
)","BinScriptBody","",A_AhkDir "\v2\AutoHotkeyU.exe")
else
PID:=DynaRun("
(
VarSetCapacity(buf,bufsz:=10485760,00),totalsz:=0,VarSetCapacity(buf1,10485760)
Loop, Read, " A_AhkDir "\BinScriptBody.ahk
{
If (A_LoopReadLine=""""){
NumPut(10, &buf, totalsz,""Char"")
,totalsz+=1
continue
}
len:=StrPutVar(A_LoopReadLine,data,""UTF-8"")
,sz:=ZipRawMemory(&data, len, zip, """ UsePassword """)
,DllCall(""crypt32\CryptBinaryToStringA"",""PTR"", &zip,""UInt"", sz,""UInt"", 0x1|0x40000000,""UInt"", 0,""UIntP"", cryptedsz:=0)
,tosavesz:=cryptedsz
,DllCall(""crypt32\CryptBinaryToStringA"",""PTR"", &zip,""UInt"", sz,""UInt"", 0x1|0x40000000,""PTR"", &buf1,""UIntP"", cryptedsz)
,NumPut(10,&buf1,cryptedsz,""Char"")
if (totalsz+tosavesz>bufsz)
VarSetCapacity(buf,bufsz*=2)
RtlMoveMemory((&buf) + totalsz,&buf1,tosavesz)
,totalsz+=tosavesz
}
NumPut(0,&buf,totalsz-1,""Char"")
If !BinScriptBody_Len := ZipRawMemory(&buf,totalsz,BinScriptBody,""" UsePassword """)
ExitApp
f:=FileOpen(A_AhkDir ""\BinScriptBody.bin"",""w -rwd""),f.RawWrite(&BinScriptBody,BinScriptBody_Len),f.Close()
)","BinScriptBody","",DerefIncludeVars.A_IsUnicode ? A_AhkDir "\AutoHotkeyU.exe" : A_AhkDir "\AutoHotkeyA.exe")
Loop {
Process, Exist, %PID%
} Until (!ErrorLevel)
FileRead,BinScriptBody, *c %A_AhkDir%\BinScriptBody.bin
FileGetSize, BinScriptBody_Len, %A_AhkDir%\BinScriptBody.bin
FileDelete, %A_AhkDir%\BinScriptBody.ahk
FileDelete, %A_AhkDir%\BinScriptBody.bin
} else
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. (C1)", 0x31)
SetWorkingDir % ScriptDir
DerefIncludeVars.A_WorkFileName := ExeFile
dirState := ProcessDirectives(ExeFile, module, Directives, IcoFile, UseCompression, UsePassword)
IcoFile := dirState.IcoFile
if outPreproc := dirState.OutPreproc
{
f := FileOpen(outPreproc, "w", "UTF-8-RAW")
f.RawWrite(BinScriptBody, BinScriptBody_Len)
f.Close()
f := ""
}
Util_Status("Adding: Master Script")
if !DllCall("UpdateResource", "ptr", module, "ptr", 10, "str", "E4847ED08866458F8DD35F94B37001C0"
, "ushort", 0x409, "ptr", &BinScriptBody, "uint", BinScriptBody_Len, "uint")
goto _FailEnd
for each,file in ExtraFiles
{
Util_Status("Adding: " file)
StringUpper, resname, file
IfNotExist, %file%
goto _FailEnd2
If UseCompression{
FileRead, tempdata, *c %file%
FileGetSize, tempsize, %file%
If !filesize := ZipRawMemory(&tempdata, tempsize, filedata)
Util_Error("Error: Could not compress the file to: " file, 0x43)
} else {
FileRead, filedata, *c %file%
FileGetSize, filesize, %file%
}
if !DllCall("UpdateResource", "ptr", module, "ptr", 10, "str", resname
, "ushort", 0x409, "ptr", &filedata, "uint", filesize, "uint")
goto _FailEnd2
VarSetCapacity(filedata, 0)
}
gosub _EndUpdateResource
if dirState.ConsoleApp
{
Util_Status("Marking executable as a console application...")
if !SetExeSubsystem(ExeFile, 3)
Util_Error("Could not change executable subsystem!", 0x61)
}
SetWorkingDir %A_ScriptDir%
RunPostExec(dirState)
for k,v in [{MPRESS:"-x"},{UPX:"--all-methods --compress-icons=0"}][UseMPRESS]
{ Util_Status("Compressing final executable with " k " ...")
if FileExist(wk := A_ScriptDir "\" k ".exe")
RunWait % """" wk """ -q " v " """ ExeFile """",, Hide
else Util_Error("Warning: """ wk """ not found.`n`n'Compress exe with " k
. "' specified, but freeware " k ".EXE is not in compiler directory.",0)
, UseMPRESS := 9
}
RunPostExec(dirState, UseMPRESS)
return ; BundleAhkScript() exits here
_FailEnd:
gosub _EndUpdateResource
Util_Error("Error adding script file:`n`n" AhkFile, 0x43)
_FailEnd2:
gosub _EndUpdateResource
Util_Error("Error adding FileInstall file:`n`n" file, 0x44)
_EndUpdateResource:
if !DllCall("EndUpdateResource", "ptr", module, "uint", 0)
{ Util_Error("Error: Error opening the destination file. (C2)", 0
,,"This error may be caused by your anti-virus checker.`n"
. "Press 'OK' to try again, or 'Cancel' to abandon.")
goto _EndUpdateResource
}
return
}
class CTempWD
{
__New(newWD)
{
this.oldWD := A_WorkingDir
SetWorkingDir % newWD
}
__Delete()
{
SetWorkingDir % this.oldWD
}
}
RunPostExec(dirState, UseMPRESS := "")
{ for k, v in dirState["PostExec" UseMPRESS]
{ Util_Status("PostExec" UseMPRESS ": " v.1)
RunWait % v.1, % v.2 ? v.2 : A_ScriptDir, % "UseErrorLevel " (v.3?"Hide":"")
if (ErrorLevel != 0 && !v.4)
Util_Error("Command failed with RC=" ErrorLevel ":`n" v.1, 0x62)
} }
Util_GetFullPath(path)
{
VarSetCapacity(fullpath, 260 * (!!A_IsUnicode + 1))
return DllCall("GetFullPathName", "str", path, "uint", 260, "str", fullpath, "ptr", 0, "uint") ? fullpath : ""
}