-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzeroinit.py
451 lines (382 loc) · 11.6 KB
/
zeroinit.py
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import os
import sys
# Handle function arguments
if len(sys.argv) != 2:
print("usage: {} <GameMaker project dir>".format(sys.argv[0]))
sys.exit(1)
projectdir = sys.argv[1]
# Containers for objects
globals = {}
scripts = {}
objects = {}
builtin = {}
# Auto-populate builtins
builtinsRaw = [
"if",
"else",
"and",
"or",
"xor",
"for",
"while",
"repeat",
"with",
"switch",
"case",
"default",
"break",
"continue",
"argument",
"argument_count",
"argument0",
"argument1",
"argument2",
"argument3",
"argument4",
"argument5",
"argument6",
"argument7",
"argument8",
"argument9",
"argument10",
"argument11",
"argument12",
"argument13",
"argument14",
"argument15",
"var",
"globalvar",
"return",
"gp_face1",
"gp_face2",
"gp_face3",
"gp_face4",
"gp_shoulderl",
"gp_shoulderlb",
"gp_shoulderr",
"gp_shoulderrb",
"gp_select",
"gp_start",
"gp_stickl",
"gp_stickr",
"gp_axislh",
"gp_axislv",
"gp_padu",
"gp_padd",
"gp_padl",
"gp_padr",
"undefined",
"true",
"false",
"mod",
"room_speed",
"x",
"y",
"xprevious",
"yprevious",
"hspeed",
"vspeed",
"speed",
"direction",
"room_width",
"room_height",
"vk_left",
"vk_right",
"vk_up",
"vk_down",
"vk_space",
"vk_enter",
"vk_escape",
"vk_numpad0",
"vk_numpad1",
"vk_numpad2",
"vk_numpad3",
"vk_numpad4",
"vk_numpad5",
"vk_numpad6",
"vk_numpad7",
"vk_numpad8",
"vk_numpad9",
"vk_alt",
"vk_control",
"vk_enter",
"vk_delete",
"vk_insert",
"vk_shift",
"sprite_index",
"image_index",
"object_index",
"gravity",
"bbox_top",
"bbox_bottom",
"bbox_left",
"bbox_right",
"depth",
"room",
"other",
"layerelementtype_background",
"layer",
"alarm",
"visible",
]
for symbol in builtinsRaw:
builtin[symbol] = symbol
# Script asset
class Script:
def __init__(self, name, path):
self.name = name
self.path = path
self.variables = {}
self.scripts = {}
# Object asset
class Object:
def __init__(self, name, path):
self.name = name
self.path = path
self.variables = {}
self.scripts = {}
self.events = []
for fname in os.listdir(path):
pt, ext = os.path.splitext(fname)
if ext == ".gml":
self.events.append(path + "/" + fname)
# Remove comments and strings
def RemoveSpecialChars(string):
pos = 0
while True:
pos = string.find("//", pos)
if pos == -1:
break
removed = 0
while pos < len(string):
if string[pos] == "\n":
break
string = string[0 : pos : ] + string[pos + 1 : :]
removed += 1
pos = 0
while True:
pos = string.find("\"", pos)
if pos == -1:
break
removed = 1
string = string[0 : pos : ] + string[pos + 1 : :]
while pos < len(string):
if string[pos] == "\"":
string = string[0 : pos : ] + string[pos + 1 : :]
break
string = string[0 : pos : ] + string[pos + 1 : :]
removed += 1
pos = 0
while pos < len(string):
pos = string.find("/*", pos)
if pos == -1:
break
removed = 2
while pos < len(string):
if string[pos] == "*":
if string[pos+1] == "/":
string = string[0 : pos : ] + string[pos + 2 : :]
break
string = string[0 : pos : ] + string[pos + 1 : :]
removed += 1
string = string.replace("<", " ")
string = string.replace(">", " ")
string = string.replace("=", " ")
string = string.replace("!", " ")
string = string.replace("|", " ")
string = string.replace("&", " ")
string = string.replace("{", " ")
string = string.replace("}", " ")
string = string.replace("[", " ")
string = string.replace("]", " ")
string = string.replace(")", " ")
string = string.replace("+", " ")
string = string.replace("-", " ")
string = string.replace("*", " ")
string = string.replace("/", " ")
string = string.replace(",", " ")
string = string.replace(";", " ")
string = string.replace(":", " ")
string = string.replace("?", " ")
string = string.replace("^", " ")
string = string.replace("function", " ")
return string
def CharIsNumber(char):
return char == "0" or char == "1" or char == "2" or char == "3" or char == "4" or char == "5" or char == "6" or char == "7" or char == "8" or char == "9" or char == "$"
def IsBuiltin(string):
return string in builtin
def VariableIsInvalid(string):
if string.find(".") != -1:
return True
if string in scripts:
return True
return IsBuiltin(string)
# Get all variable names for a given string
def GetVariableNames(vlist, slist, string):
# Weed out global variables
pos = 0
while pos < len(string):
pos = string.find("global.", pos)
if pos == -1:
break
string = string[0 : pos : ] + string[pos + 7 : :]
substr = ""
while pos < len(string):
if string[pos] == " ":
break
substr += string[pos]
string = string[0 : pos : ] + string[pos + 1 : :]
globals[substr] = substr
# Do the thing
pos = 0
prevvar = ""
vlist_o = vlist
ignoreVars = {}
while pos < len(string):
char = string[pos]
if char == " " or char == "\t" or char == "\n":
pos += 1
continue
substr = ""
while pos < len(string):
if string[pos] == " " or string[pos] == "\t" or string[pos] == "\n":
break
# Refering to other objects
if string[pos] == ".":
psbstr = substr
if substr == "":
substr = prevvar
if not CharIsNumber(substr[0]):
if substr in objects:
vlist = objects[substr].variables
substr = ""
string = string[0 : pos : ] + string[pos + 1 : :]
continue
substr = psbstr
# Weed out functions
if string[pos] == "(":
string = string[0 : pos : ] + string[pos + 1 : :]
if substr != "":
slist[substr] = substr
substr = -1
break
substr += string[pos]
string = string[0 : pos : ] + string[pos + 1 : :]
# Add variable to list maybe
if substr != -1 and substr != "":
if not CharIsNumber(substr[0]):
if prevvar == "var":
ignoreVars[substr] = substr
if substr not in ignoreVars:
vlist[substr] = substr
prevvar = substr
vlist = vlist_o
def CopyVariablesFromScript(vlist, script, visited):
if script not in scripts:
return
if script in visited:
return
for variable in scripts[script].variables:
vlist[variable] = variable
visited[script] = 0
for ns in scripts[script].scripts:
CopyVariablesFromScript(vlist, ns, visited)
# Create script objects
for fname in os.listdir(projectdir + "/scripts"):
scripts[fname] = Script(fname, projectdir + "/scripts/" + fname + "/" + fname + ".gml")
# Create object objects
for fname in os.listdir(projectdir + "/objects"):
objects[fname] = Object(fname, projectdir + "/objects/" + fname)
# Read asset names
for fname in os.listdir(projectdir + "/sprites"):
builtin[fname] = fname
for fname in os.listdir(projectdir + "/rooms"):
builtin[fname] = fname
for fname in os.listdir(projectdir + "/sounds"):
builtin[fname] = fname
for fname in os.listdir(projectdir + "/objects"):
builtin[fname] = fname
# Read script variables
for script in scripts:
script = scripts[script]
file = open(script.path)
string = RemoveSpecialChars(file.read())
print("finding variables used in {}...".format(script.name))
GetVariableNames(script.variables, script.scripts, string)
for variable in script.variables:
# Check variables and convert some to scripts
if variable in scripts:
script.scripts[variable] = variable
# Read object variables
for object in objects:
object = objects[object]
print("finding variables used by {}...".format(object.name))
for event in object.events:
file = open(event)
string = RemoveSpecialChars(file.read())
GetVariableNames(object.variables, object.scripts, string)
# Check variables and convert some to scripts
for variable in object.variables:
if variable in scripts:
object.scripts[variable] = variable
visited = {}
for script in object.scripts:
CopyVariablesFromScript(object.variables, script, visited)
# Prepare to save the output
generated = "This part of the file has been auto-generated by zeroinit.py"
# Print global variables
globalstring = "\n\n//" + generated + "\n//Globals:\n"
for globalvar in globals:
globalstring += "global." + globalvar + " = 0;\n"
# Save global variables
if globalstring != "\n\n//" + generated + "\n//Globals:\n":
print("saving globals...")
path = projectdir + "/scripts/__init_global/"
scrfile = open(path + "__init_global.gml")
scrtext = scrfile.read()
scrfile.close()
os.remove(path + "__init_global.gml")
scrtext += globalstring
scrfile = open(path + "__init_global.gml", "w")
scrfile.write(scrtext)
scrfile.close()
# Print object variables
for object in objects:
object = objects[object]
objectstring = "//" + generated + "\n//" + object.name + ":\n"
path = projectdir + "/objects/" + object.name
oldevent = ""
# Write all the variables to a string
for message in object.variables:
if not VariableIsInvalid(message):
objectstring += message + " = 0;\n"
# Is string empty?
if objectstring == "//" + generated + "\n//" + object.name + ":\n":
continue
# Create event already exists
if "Create_0.gml" in os.listdir(path):
ofile = open(path + "/Create_0.gml")
oldevent = "\n\n//Below are the original contents of the file.\n" + ofile.read()
ofile.close()
os.remove(path + "/Create_0.gml")
# Create event does not exist
else:
print("adding 'create' event for {}...".format(object.name))
# Get object metadata file
yyfile = open(path + "/" + object.name + ".yy")
propfile = yyfile.read()
yyfile.close()
os.remove(path + "/" + object.name + ".yy")
# Find position of event list
pos = propfile.find(r'"eventList": [')
pos = propfile.find("\n", pos)
propfile = propfile[:pos] + "\n " + r'{"resourceType":"GMEvent","resourceVersion":"1.0","name":"","collisionObjectId":null,"eventNum":0,"eventType":0,"isDnD":false,},' + propfile[pos:]
# Add create event to file
yyfile = open(path + "/" + object.name + ".yy", "w")
yyfile.write(propfile)
yyfile.close()
# Save this :)
print("saving variables for {}...".format(object.name))
createfile = open(path + "/Create_0.gml", "w")
createfile.write(objectstring + oldevent)
createfile.close()