-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_Registry.ahk
237 lines (184 loc) · 5.38 KB
/
class_Registry.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
#NoEnv
; MsgBox, % Registry.prop[ "cd" ]
; ExitApp
/**
* Window Registry
*/
class Registry {
static prop := []
static void := Registry._init()
__New() {
throw Exception( "Registry is a static class, don't instant it!", -1 )
}
_init() {
Registry.prop[ "cd" ] := A_ScriptDir
Registry.prop[ "cdWin" ] := RegExReplace( A_ScriptDir, "\\", "\\" )
Registry.prop[ "cdUnix" ] := RegExReplace( A_ScriptDir, "\\", "/" )
}
getProp( key ) {
return Registry.prop[ key ]
}
setProp( key, value ) {
Registry.prop[ key ] := value
}
/**
* Write Registry from file
*
* @param file {String} filePath contains data formatted Windows Registry
*/
write( file ) {
SetRegView 32
Registry._setRegistry( file, prop )
SetRegView 64
Registry._setRegistry( file, prop )
}
/**
* Write Registry from file
*
* @param file {String} filePath contains data formatted Windows Registry
* @param properties {Array} properties to bind
*/
_setRegistry( file, properties ) {
regKey := ""
readNextLine := false
isHex := true
Loop, Read, %file%
{
line := Trim( A_LoopReadLine )
if RegExMatch(line, "^Windows Registry Editor" ) {
continue
} else if ( StrLen(line) == 0 ) {
Continue
} else if RegExMatch(line, "^\[.*\]" ) {
regKey := RegExReplace( line, "^\[(.*)\]", "$1" )
continue
} else if ( regKey == "" ) {
continue
}
if ( readNextLine == true ) {
regVal := regVal line
} else {
regName := RegExReplace( line, "^(@|"".+?"")=.*$", "$1" )
regName := RegExReplace( regName, "^""(.+?)""$","$1" )
regName := RegExReplace( regName, "\\""", """" )
regName := Registry._bindValue( regName, properties )
regVal := RegExReplace( line, "^(@|"".*?"")=(.*)$", "$2" )
regVal := RegExReplace( regVal, "\\""", """" )
regType := "REG_SZ"
if ( regName == "@" ) {
regName := ""
}
if RegExMatch( regVal, "^"".*""$" ) {
regType := "REG_SZ"
regVal := Registry._bindValue( RegExReplace( regVal, "^""(.*)""$", "$1" ), properties )
isHex := false
} else if RegExMatch( regVal, "^dword:" ) {
regType := "REG_DWORD"
regVal := RegExReplace( regVal, "^dword:(.*)$", "$1" )
isHex := false
} else if RegExMatch( regVal, "^hex\(b\):" ) {
regType := "REG_QWORD"
regVal := RegExReplace( regVal, "^hex\(b\):(.*)$", "$1" )
isHex := true
} else if RegExMatch( regVal, "^hex\(7\):" ) {
regType := "REG_MULTI_SZ"
regVal := RegExReplace( regVal, "^hex\(7\):(.*)$", "$1" )
isHex := true
} else if RegExMatch( regVal, "^hex\(2\):" ) {
regType := "REG_EXPAND_SZ"
regVal := RegExReplace( regVal, "^hex\(2\):(.*)$", "$1" )
isHex := true
} else if RegExMatch( regVal, "^hex:" ) {
regType := "REG_BINARY"
regVal := RegExReplace( regVal, "^hex:(.*)$", "$1" )
isHex := true
}
}
if ( RegExMatch(line, "^.*\\$") ) {
readNextLine := true
continue
} else {
readNextLine := false
}
if ( isHex == true ) {
regVal := RegExReplace( regVal, "[\\\t ]", "" )
}
if ( regType == "REG_DWORD" ) {
regVal := "0x" regVal
} else if ( regType == "REG_QWORD" ) {
regVal := "0x" Registry._toNumberFromHex( regVal )
} else if( regType == "REG_MULTI_SZ" ) {
regVal := Registry._toStringFromHex( regVal )
} else if( regType == "REG_EXPAND_SZ" ) {
regVal := Registry._toStringFromHex( regVal )
} else if( regType == "REG_BINARY" ) {
StringReplace, regVal, regVal, % ",", , All
}
regName := Registry._bindValue( regName, properties )
; debug( "[" regKey "] " regName " - " regType ":" regVal )
; if it needs to run as admin, restart itself
if ( ! RegExMatch(regKey, "^(HKEY_CURRENT_USER|HKEY_USERS)\\.*$") ) {
Registry._restartAsAdmin()
}
RegWrite, % regType, % regKey, % regName, % regVal
}
}
_bindValue( value, properties ) {
For key, val in properties
value := StrReplace( value, "#{" key "}", val )
return value
}
_toStringFromHex( hexValue ) {
if ! hexValue
return 0
array := StrSplit( hexValue, "," )
if ( mod( array.MaxIndex(), 2 ) != 0 ) {
array.Insert( "00" )
}
result := ""
for i, element in array
{
if ( mod(i,2) == 0 )
Continue
result := result chr( "0x" array[i + 1] array[i] )
}
return result
}
_toNumberFromHex( hexValue ) {
if ! hexValue
return 0
array := StrSplit( hexValue, "," )
if ( mod( array.MaxIndex(), 2 ) != 0 ) {
array.Insert( "00" )
}
result := ""
for i, element in array
{
if ( mod(i,2) == 0 )
Continue
result := array[i + 1] array[i] result
}
;return "0x" result
return "0x0000000c"
}
_convertBase( fromBase, toBase, number ) {
static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
static v := A_IsUnicode ? "_i64tow" : "_i64toa"
VarSetCapacity(s, 65, 0)
value := DllCall("msvcrt.dll\" u, "Str", number, "UInt", 0, "UInt", fromBase, "CDECL Int64")
DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", toBase, "CDECL")
return s
}
_restartAsAdmin() {
if not (A_IsAdmin) {
try ; leads to having the script re-launching itself as administrator
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
}
}