-
Notifications
You must be signed in to change notification settings - Fork 115
/
Directives.ahk
359 lines (339 loc) · 12.2 KB
/
Directives.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
;
; File encoding: UTF-8 with BOM
;
#Include <VersionRes>
ProcessDirectives(ExeFile, Module, Directives, PriorLines, IcoFile, VerInfo)
{ state := { ExeFile: ExeFile, Module: Module, resLang: 0x409, VerInfo: {}
, IcoFile: IcoFile, PostExec:[], PostExec0:[], PostExec1:[], PostExec2:[] }
state.VerInfo := VerInfo
for k, Cmd in Directives
{ DerefIncludeVars.A_PriorLine := state.PriorLine := PriorLines[k]
while SubStr(wk := DerefIncludePath(Directives[k+A_Index]
, DerefIncludeVars, 1), 1, 4) ~= "i)^Cont$|^Nop $"
Cmd .= SubStr(wk, 1, 4) = "Cont" ? SubStr(wk, 6) : ""
Util_Status("Processing directive: " (SubStr(Cmd,1,11) = "AddResource"
&& SubStr(PriorLines[k],1,1) = Chr(127) ? SubStr(PriorLines[k],2) : Cmd))
state.Cmd := Cmd := DerefIncludePath(Cmd, DerefIncludeVars, 1)
if !RegExMatch(Cmd, "^(\w+)(?:\s+(.+))?$", o)
Util_Error("Error: Invalid directive: (D1)", 0x63, Cmd)
args := [], nargs := 0
StringReplace, o2, o2, ```,, `n, All
Loop, Parse, o2, `,, %A_Space%%A_Tab%
{
StringReplace, ov, A_LoopField, `n, `,, All
StringReplace, ov, ov, ``n, `n, All
StringReplace, ov, ov, ``r, `r, All
StringReplace, ov, ov, ``t, `t, All
StringReplace, ov, ov,````, ``, All
args.Insert(ov), nargs++
}
fn := Func("Directive_" o1)
if !fn
Util_Error("Error: Invalid directive: (D2)" , 0x63, Cmd)
if (!fn.IsVariadic && (fn.MinParams-1 > nargs || nargs > fn.MaxParams-1))
Util_Error("Error: Wrongly formatted directive: (D1)", 0x64, Cmd)
fn.(state, args*)
}
if IcoFile := state.IcoFile
{ Util_Status("Changing the main icon...")
if !FileExist(IcoFile)
Util_Error("Error changing icon: File does not exist.", 0x35, IcoFile)
if !AddOrReplaceIcon(Module, IcoFile, ExeFile, 159)
Util_Error("Error changing icon: Unable to read icon or icon was of the wrong format.", 0x42, IcoFile)
}
return state
}
; ---------------------------- Handle Directives -------------------------------
Directive_ConsoleApp(state)
{ state.ConsoleApp := true
}
Directive_Cont(state, txt*)
{ ; Handled above
}
Directive_Debug(state, txt*)
{ for k, v in txt
wk .= k=1 ? v : ", " v
Util_Error( "Debug: " wk, 0)
}
Directive_ExeName(state, txt)
{ global ExeFileG, StopCDExe
if !StopCDExe
{ SplitPath ExeFileG,, gdir,,gname
SplitPath txt ,, idir,,iname
ExeFileG := (idir ? idir : gdir) "\" (iname ? iname : gname) ".exe"
} }
Directive_Let(state, txt*)
{ for k in txt
{ wk := StrSplit(txt[k], "=", "`t ", 2)
if (wk.Length() != 2)
Util_Error("Error: Wrongly formatted directive: (D2)", 0x64, state.Cmd)
DerefIncludeVars[(wk.1 ~= "i)^U_" ? "" : "U_") wk.1] := wk.2
} }
Directive_Nop(state, txt*)
{ ; Do nothing
}
Directive_Obey(state, name, txt, extra:=0)
{ global AhkPath, AhkSw, SilentMode
IfExist %AhkPath%
{ if !(extra ~= "^[0-9]$")
Util_Error("Error: Wrongly formatted directive: (D3)", 0x64, state.Cmd)
wk := Util_TempFile(, "Obey~")
FileAppend % (txt~="^=" ? name ":" : "") txt "`nFileOpen(""" wk 0
. """,""W"",""UTF-8"").Write(" name ")", %wk%, UTF-8
Loop % extra
FileAppend % "`nFileOpen(""" wk A_Index
. """,""W"",""UTF-8"").Write(" name A_Index ")", %wk%, UTF-8
if SilentMode
{ ErrorData := RunCMD("""" AhkPath """ " AhkSw " /ErrorStdOut """ wk """")
if ErrorLevel
{ FileDelete %wk%*
Util_Error("Error: 'Obey' directive cannot be executed.",0x68,ErrorData)
} } else RunWait "%AhkPath%" %AhkSw% "%wk%",,Hide
Loop % extra + 1
{ FileRead result, % "*p65001 " wk (cnt := A_Index - 1)
DerefIncludeVars[(name~="i)^U_"?"":"U_") name (cnt ? cnt : "")] := result
}
FileDelete %wk%*
} }
Directive_PostExec(state, txt, when="", WorkingDir="", Hidden=0, IgnoreErrors=0)
{ if !({"":1,0:1,1:1,2:1}[when] && {"":1,0:1,1:1}[Hidden]
&& {"":1,0:1,1:1}[IgnoreErrors])
Util_Error("Error: Wrongly formatted directive: (D4)", 0x64, state.Cmd)
state["PostExec" when].Push([txt, WorkingDir, Hidden, IgnoreErrors])
}
Directive_ResourceID(state, txt)
{ state.ResourceID := txt
}
Directive_Set(state, name, txt)
{ state.VerInfo[name] := txt
}
Directive_SetCompanyName(state, txt)
{ state.VerInfo.CompanyName := txt
}
Directive_SetCopyright(state, txt)
{ state.VerInfo.LegalCopyright := txt
}
Directive_SetDescription(state, txt)
{ state.VerInfo.FileDescription := txt
}
Directive_SetFileVersion(state, txt)
{ state.VerInfo.FileVersion := txt
}
Directive_SetInternalName(state, txt)
{ state.VerInfo.InternalName := txt
}
Directive_SetLanguage(state, txt)
{ state.VerInfo.Language := txt
}
Directive_SetLegalTrademarks(state, txt)
{ state.VerInfo.LegalTrademarks := txt
}
Directive_SetMainIcon(state, txt := "")
{ global StopCDIco
if !StopCDIco
state.IcoFile := txt
}
Directive_SetName(state, txt)
{ state.VerInfo.InternalName := state.VerInfo.ProductName := txt
}
Directive_SetOrigFilename(state, txt)
{ state.VerInfo.OriginalFilename := txt
}
Directive_SetProductName(state, txt)
{ state.VerInfo.ProductName := txt
}
Directive_SetProductVersion(state, txt)
{ state.VerInfo.ProductVersion := txt
}
Directive_SetVersion(state, txt)
{ state.VerInfo.FileVersion := state.VerInfo.ProductVersion := txt
}
Directive_UpdateManifest(state, admin, name = "", version = "", uiaccess = "")
{ xml := ComObjCreate("Msxml2.DOMDocument")
xml.async := false
xml.setProperty("SelectionLanguage", "XPath")
xml.setProperty("SelectionNamespaces"
, "xmlns:v1='urn:schemas-microsoft-com:asm.v1' "
. "xmlns:v3='urn:schemas-microsoft-com:asm.v3'")
if !xml.load("res://" state.ExeFile "/#24/#1") ; Load current manifest
Util_Error("Error: Error opening destination file. (D2)", 0x31, state.Cmd)
node := xml.selectSingleNode("/v1:assembly/v1:assemblyIdentity")
if !node ; Not AutoHotkey v1.1?
Util_Error("Error: Error opening destination file. (D3)", 0x31, state.Cmd)
(version && node.setAttribute("version", version))
(name && node.setAttribute("name", name))
node := xml.selectSingleNode("/v1:assembly/v3:trustInfo/v3:security"
. "/v3:requestedPrivileges/v3:requestedExecutionLevel")
if !node ; Not AutoHotkey v1.1?
Util_Error("Error: Error opening destination file. (D4)", 0x31, state.Cmd)
(admin=1 && node.setAttribute("level", "requireAdministrator"))
(admin=2 && node.setAttribute("level", "highestAvailable"))
(uiaccess && node.setAttribute("uiAccess", "true"))
xml := RTrim(xml.xml, "`r`n")
VarSetCapacity(data, data_size := StrPut(xml, "utf-8") - 1)
StrPut(xml, &data, "utf-8")
if !DllCall("UpdateResource", "ptr", state.Module, "ptr", 24, "ptr", 1
, "ushort", 1033, "ptr", &data, "uint", data_size, "uint")
Util_Error("Error changing the version information. (D2)", 0x67, state.Cmd)
}
Directive_UseResourceLang(state, resLang)
{
if resLang is not integer
Util_Error("Error: Resource language must be an integer between 0 and 0xFFFF.", 0x65, state.Cmd)
if resLang not between 0 and 0xFFFF
Util_Error("Error: Resource language must be an integer between 0 and 0xFFFF.", 0x65, state.Cmd)
state.resLang := resLang+0
}
Directive_AddResource(state, rsrc, resName := "")
{
resType := "" ; auto-detect
if RegExMatch(rsrc, "^\*(\w+)\s+(.+)$", o)
resType := o1, rsrc := o2
resFile := Util_GetFullPath(rsrc)
if !FileExist(rsrc)
Util_Error("Error: specified resource does not exist:", 0x36
, SubStr(state.PriorLine,1,1)=Chr(127)?SubStr(state.PriorLine,2):state.Cmd)
SplitPath, resFile, resFileName,, resExt
if !resName
resName := SubStr(state.PriorLine,1,1) = Chr(127) ? rsrc : resFileName
, defResName := 1
StringUpper, resName, resName
if resType =
{
; Auto-detect resource type
if resExt in bmp,dib
resType := 2 ; RT_BITMAP
else if resExt = ico
resType := 14 ; RT_GROUP_ICON
else if resExt = cur
Util_Error("Error: Cursor resource adding is not supported yet!", 0x27, state.Cmd)
else if resExt in htm,html,mht
resType := 23 ; RT_HTML
else if resExt = manifest
{
resType := 24 ; RT_MANIFEST
if defResName
resName := 1
} else
resType := 10 ; RT_RCDATA
}
if resType = 14
{
if resName is not integer
resName := 0
AddOrReplaceIcon(state.Module, resFile, state.ExeFile, resName)
return
}
typeType := "str"
nameType := "str"
if resType is integer
if resType between 0 and 0xFFFF
typeType := "ptr"
resName := resName ~= "^#\d+$" ? SubStr(resName, 2) : resName
if resName is integer
if resName between 0 and 0xFFFF
nameType := "ptr"
if resType in 4,5,6,9,23,24 ; Deref text-type resources
{ FileRead fData, %resFile%
fData1 := DerefIncludePath(fData, DerefIncludeVars, 1)
VarSetCapacity(fData, fSize := StrPut(fData1, "utf-8") - 1)
StrPut(fData1, &fData, "utf-8")
}
else if (resExt = "ahk" && resType = 10 ; Process AutoHotkey scripts
&& SubStr(state.PriorLine,1,1) != Chr(127)) ; But not from FileInstall
{ OldA_s := [], OldA_s.Push(DerefIncludeVars.A_ScriptFullPath)
OldA_s.Push(DerefIncludeVars.A_ScriptName)
OldA_s.Push(DerefIncludeVars.A_ScriptDir)
SplitPath, resFile, ScriptName, ScriptDir
DerefIncludeVars.A_ScriptFullPath := resFile
DerefIncludeVars.A_ScriptName := ScriptName
DerefIncludeVars.A_ScriptDir := ScriptDir
tempWD := new CTempWD(ScriptDir)
PreprocessScript(fData1 := "", resFile, Directives := [], PriorLines := [])
dirState := ProcessDirectives(state.ExeFile, state.Module
, Directives, PriorLines, "", state.VerInfo)
if dirState.ConsoleApp ; Pass any ConsoleApp up chain
state.ConsoleApp := dirState.ConsoleApp
for k, v in ["PostExec", "PostExec0", "PostExec1", "PostExec2"]
state[v].Push(dirState[v]*) ; Pass any PostExec up chain
DerefIncludeVars.A_ScriptDir := OldA_s.Pop()
DerefIncludeVars.A_ScriptName := OldA_s.Pop()
DerefIncludeVars.A_ScriptFullPath := OldA_s.Pop()
VarSetCapacity(fData, fSize := StrPut(fData1, "utf-8") - 1)
StrPut(fData1, &fData, "utf-8")
}
else
{ FileGetSize, fSize, %resFile%
VarSetCapacity(fData, fSize)
FileRead, fData, *c %resFile%
}
pData := &fData
if resType = 2
{ ; Remove BM header in order to make it a valid bitmap resource
if fSize < 14
Util_Error("Error: Impossible BMP file!", 0x66, state.Cmd)
pData += 14, fSize -= 14
}
if !DllCall("UpdateResource", "ptr",state.Module, typeType,resType, nameType
, resName, "ushort",state.resLang, "ptr",pData, "uint",fSize, "uint")
Util_Error("Error adding resource:", 0x46, state.Cmd)
VarSetCapacity(fData, 0)
}
ChangeVersionInfo(ExeFile, hUpdate, VerInfo)
{
hModule := DllCall("LoadLibraryEx", "str", ExeFile, "ptr", 0, "ptr", 2, "ptr")
if !hModule
Util_Error("Error: Error opening destination file. (D1)", 0x31)
hRsrc := DllCall("FindResource", "ptr", hModule, "ptr", 1, "ptr", 16, "ptr") ; Version Info\1
hMem := DllCall("LoadResource", "ptr", hModule, "ptr", hRsrc, "ptr")
vi := new VersionRes(DllCall("LockResource", "ptr", hMem, "ptr"))
DllCall("FreeLibrary", "ptr", hModule)
ffi := vi.GetDataAddr()
props := SafeGetViChild(SafeGetViChild(vi, "StringFileInfo"), "040904b0")
for k,v in VerInfo
{ if (!v)
props.DeleteChild(k) ; Remove any unwanted version info
else
{ if !(k = "Language")
SafeGetViChild(props, k).SetText(v) ; All properties, but not language
if k in FileVersion,ProductVersion
{ ver := VersionTextToNumber(v)
hiPart := (ver >> 32)&0xFFFFFFFF, loPart := ver & 0xFFFFFFFF
if (k = "FileVersion")
NumPut(hiPart, ffi+8, "UInt"), NumPut(loPart, ffi+12, "UInt")
else NumPut(hiPart, ffi+16, "UInt"), NumPut(loPart, ffi+20, "UInt")
} } }
VarSetCapacity(newVI, 16384) ; Should be enough
viSize := vi.Save(&newVI)
if (wk := VerInfo.Language) ; Change language?
{ NumPut(VerInfo.Language, newVI, viSize-4, "UShort")
}
DllCall("UpdateResource", "ptr", hUpdate, "ptr", 16, "ptr", 1
, "ushort", 0x409, "ptr", 0, "uint", 0, "uint") ; Delete lang 0x409
if !DllCall("UpdateResource", "ptr", hUpdate, "ptr", 16, "ptr", 1, "ushort"
, wk ? wk : 0x409, "ptr", &newVI, "uint", viSize, "uint") ; Add new language
Util_Error("Error changing the version information. (D1)", 0x67)
}
VersionTextToNumber(v)
{
r := 0, i := 0
while i < 4 && RegExMatch(v, "O)^(\d+).?", o)
{
StringTrimLeft, v, v, % o.Len
val := o[1] + 0
r |= (val&0xFFFF) << ((3-i)*16)
i ++
}
return r
}
SafeGetViChild(vi, name)
{
c := vi.GetChild(name)
if !c
{
c := new VersionRes()
c.Name := name
vi.AddChild(c)
}
return c
}