-
Notifications
You must be signed in to change notification settings - Fork 2
/
npl.py
executable file
·603 lines (570 loc) · 16.5 KB
/
npl.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/usr/bin/env python
#Copyright (C) 2009-2010 Johan Grip
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import array
import struct
import sys
##
## Statics
if sys.version_info < (2, 6):
TRANSTAB = ''.join(chr(i) for i in range(256))
else:
TRANSTAB = None
##
## Opcodes
##
OP_LOAD = 0 # Load a constand
OP_COMP = 1 # Comparisons
OP_FUNC = 2 # Function calls
OP_LABL = 4 # Label definition
OP_GOTO = 8 # Goto
##
## Compare operations
##
COMPARES = {
4: "?",
5: ">",
6: "<",
7: "=",
10: "!="
}
##
## Functions
##
FUNCS = {
0:"Stop",
1:"True",
2:"False",
3:"Null",
4:"GetRandom",
5:"GetRandom10",
6:"GetRandom100",
7:"GetRandomTrueFalse",
8:"SetLevelCompleted",
9:"SetLevelFail",
10:"SetGameCompleted",
11:"SetGameFail",
12:"GetCrystalsPickedUp",
13:"GetCrystalsCurrentlyStored",
14:"GetCrystalsUsed",
15:"GetCrystalsStolen",
16:"SetMessage",
17:"GetR0",
18:"GetR1",
19:"GetR2",
20:"GetR3",
21:"GetR4",
22:"GetR5",
23:"GetR6",
24:"GetR7",
25:"SetR0",
26:"SetR1",
27:"SetR2",
28:"SetR3",
29:"SetR4",
30:"SetR5",
31:"SetR6",
32:"SetR7",
33:"AddR0",
34:"AddR1",
35:"AddR2",
36:"AddR3",
37:"AddR4",
38:"AddR5",
39:"AddR6",
40:"AddR7",
41:"SubR0",
42:"SubR1",
43:"SubR2",
44:"SubR3",
45:"SubR4",
46:"SubR5",
47:"SubR6",
48:"SubR7",
49:"SetMessagePermit",
50:"GetObjectiveShowing",
51:"GetTeleportIconClicked",
52:"GetMiniFiguresOnLevel",
53:"GetBuildIconClicked",
54:"SetBuildIconClicked",
55:"SetTeleportIconClicked",
56:"SetToolStoreIconClicked",
57:"GetToolStoreIconClicked",
58:"FlashToolStoreIcon",
59:"FlashBuildIcon",
60:"FlashTeleportIcon",
61:"SetGoBackIconClicked",
62:"GetGoBackIconClicked",
63:"FlashGoBackIcon",
64:"SetDigIconClicked",
65:"GetDigIconClicked",
66:"FlashDigIcon",
67:"GetTimer0",
68:"GetTimer1",
69:"GetTimer2",
70:"GetTimer3",
71:"SetTimer0",
72:"SetTimer1",
73:"SetTimer2",
74:"SetTimer3",
75:"GetBarracksBuilt",
76:"GetOreRefineriesBuilt",
77:"GetToolStoresBuilt",
78:"GetStudCount",
79:"GetTeleportsBuilt",
80:"GetMiniFigureSelected",
81:"GetSmallDiggerSelected",
82:"GetMiniFigureinSmallDigger",
83:"GetTrainFlags",
84:"SetTrainFlags",
85:"SetIconPos",
86:"SetIconSpace",
87:"SetIconWidth",
88:"SetMessageWait",
89:"GetMountIconClicked",
90:"SetMountIconClicked",
91:"FlashMountIcon",
92:"SetTutorialPointer",
93:"GetTutorialFlags",
94:"SetTutorialFlags",
95:"SetRockMonster",
96:"GetOrePickedUp",
97:"GetOreCurrentlyStored",
98:"GetOreUsed",
99:"GetOreStolen",
100:"GetCrystalRefineriesBuilt",
101:"FlashLayPathIcon",
102:"SetLayPathIconClicked",
103:"GetLayPathIconClicked",
104:"GetTeleportPadIconClicked",
105:"SetTeleportPadIconClicked",
106:"FlashTeleportPadIcon",
107:"GetMessageTimer",
108:"SetMessageTimerValues",
109:"GetTutorialBlockClicks",
110:"SetTutorialBlockClicks",
111:"GetTutorialCrystals",
112:"SetTutorialCrystals",
113:"GetPathsBuilt",
114:"GetTutorialBlockIsGround",
115:"SetCameraGotoTutorial",
116:"FlashDynamiteIcon",
117:"GetDynamiteClicked",
118:"SetDynamiteClicked",
119:"AddPoweredCrystals",
120:"GetGraniteGrinderSelected",
121:"GetChromeCrusherSelected",
122:"GetTutorialBlockIsPath",
123:"GetGunstationIconClicked",
124:"SetGunstationIconClicked",
125:"FlashGunstationIcon",
126:"GetGunstationsBuilt",
127:"SetOreAtIconPositions",
128:"GetVehicleTeleportsBuilt",
129:"GetVehicleTransportIconClicked",
130:"SetVehicleTransportIconClicked",
131:"FlashVehicleTransportIcon",
132:"GetUpgradeStationIconClicked",
133:"SetUpgradeStationIconClicked",
134:"FlashUpgradeStationIcon",
135:"GetUpgradeStationsBuilt",
136:"SetTutorialBlockIsGround",
137:"SetTutorialBlockIsPath",
138:"CameraLockOnObject",
139:"CameraUnlock",
140:"CameraZoomIn",
141:"CameraZoomOut",
142:"CameraRotate",
143:"GetCameraAtTutorial",
144:"GetSelectedRecordObject",
145:"GetSmallHelicopterSelected",
146:"GetRapidRiderSelected",
147:"GetMiniFigureinRapidRider",
148:"GetDismountIconClicked",
149:"SetDismountIconClicked",
150:"FlashDismountIcon",
151:"GetGetToolIconClicked",
152:"SetGetToolIconClicked",
153:"FlashGetToolIcon",
154:"GetAnyKeyPressed",
155:"SetPauseGame",
156:"GetGetLaserIconClicked",
157:"SetGetLaserIconClicked",
158:"FlashGetLaserIcon",
159:"SetRockMonsterAtTutorial",
160:"GetCallToArmsButtonClicked",
161:"GetRockMonstersDestroyed",
162:"SetGetPusherIconClicked",
163:"GetGetPusherIconClicked",
164:"FlashGetPusherIcon",
165:"GetRockMonsterRunningAway",
166:"SetCallToArms",
167:"FlashPowerStationIcon",
168:"SetPowerstationIconClicked",
169:"GetPowerstationIconClicked",
170:"GetPowerstationsBuilt",
171:"SetAttackDefer",
172:"SetRockMonsterPainThreshold",
173:"SetRockMonsterHealth",
174:"SetGameSpeed",
175:"FlashBarracksIcon",
176:"SetBarracksIconClicked",
177:"GetBarracksIconClicked",
178:"GetRecordObjectAtTutorial",
179:"GetHiddenObjectsFound",
180:"SetHiddenObjectsFound",
181:"GetOxygenLevel",
182:"FlashGeodomeIcon",
183:"GetGeodomeIconClicked",
184:"SetGeodomeIconClicked",
185:"GetGeodomeBuilt",
186:"AddStoredOre",
187:"GenerateSlug",
188:"GetSlugsOnLevel",
189:"GetMonstersOnLevel",
190:"SetCongregationAtTutorial",
191:"SetObjectiveSwitch",
192:"GetObjectiveSwitch",
193:"GetGraniteGrindersOnLevel",
194:"GetSmallDiggersOnLevel",
195:"GetDocksBuilt",
196:"GetRapidRidersOnLevel",
197:"GetUnitAtBlock",
198:"GetSmallHelicoptersOnLevel",
199:"GetRecordObjectAmountAtTutorial",
200:"AdvanceMessage",
201:"AllowCameraMovement",
202:"ClickOnlyObjects",
203:"ClickOnlyMap",
204:"ClickOnlyIcon",
205:"DisallowAll",
206:"SupressArrow",
207:"GetMiniFigureinGraniteGrinder",
208:"GetMiniFigureinChromeCrusher",
209:"GetMessagesAreUpToDate",
210:"SetCrystalPriority",
211:"MakeSomeoneOnThisBlockPickUpSomethingOnThisBlock",
212:"SetTrainIconClicked",
213:"GetTrainIconClicked",
214:"FlashTrainIcon",
215:"SetTrainDriverIconClicked",
216:"GetTrainDriverIconClicked",
217:"FlashTrainDriverIcon",
218:"SetTrainPilotIconClicked",
219:"GetTrainPilotIconClicked",
220:"FlashTrainPilotIcon",
221:"SetTrainSailorIconClicked",
222:"GetTrainSailorIconClicked",
223:"FlashTrainSailorIcon",
224:"GetSmallTruckSelected",
225:"GetMiniFigureinSmallTruck",
226:"GetMiniFigureinSmallHelicopter",
227:"SetBarracksLevel",
228:"SetDocksLevel",
229:"SetGeoDomeLevel",
230:"SetPowerstationLevel",
231:"SetToolStoreLevel",
232:"SetGunstationLevel",
233:"SetTeleportPadLevel",
234:"SetSuperTeleportLevel",
235:"SetUpgradeStationLevel",
236:"GetBarracksSelected",
237:"GetDocksSelected",
238:"GetGeoDomeSelected",
239:"GetPowerstationSelected",
240:"GetToolStoreSelected",
241:"GetGunstationSelected",
242:"GetTeleportPadSelected",
243:"GetSuperTeleportSelected",
244:"GetUpgradeStationSelected",
245:"SetUpgradeBuildingIconClicked",
246:"GetUpgradeBuildingIconClicked",
247:"FlashUpgradeBuildingIcon",
248:"GetBuildingsTeleported",
249:"SetBuildingsTeleported",
250:"CameraLockOnMonster",
251:"SetMonsterAttackPowerstation",
252:"ClickOnlyCalltoarms",
253:"FlashCallToArmsIcon",
254:"SetRecordObjectPointer",
255:"GetGetSonicBlasterIconClicked",
256:"SetGetSonicBlasterIconClicked",
257:"FlashGetSonicBlasterIcon",
258:"GetDropSonicBlasterIconClicked",
259:"SetDropSonicBlasterIconClicked",
260:"FlashDropSonicBlasterIcon",
261:"GetMonsterAtTutorial",
262:"SetMonsterAttackNowt",
263:"GetPlaceFenceIconClicked",
264:"SetPlaceFenceIconClicked",
265:"FlashPlaceFenceIcon",
266:"GetLevel1BarracksBuilt",
267:"GetLevel1DocksBuilt",
268:"GetLevel1GeodomeBuilt",
269:"GetLevel1PowerstationsBuilt",
270:"GetLevel1ToolStoresBuilt",
271:"GetLevel1GunstationsBuilt",
272:"GetLevel1TeleportsBuilt",
273:"GetLevel1VehicleTeleportsBuilt",
274:"GetLevel1UpgradeStationsBuilt",
275:"GetLevel2BarracksBuilt",
276:"GetLevel2DocksBuilt",
277:"GetLevel2GeodomeBuilt",
278:"GetLevel2PowerstationsBuilt",
279:"GetLevel2ToolStoresBuilt",
280:"GetLevel2GunstationsBuilt",
281:"GetLevel2TeleportsBuilt",
282:"GetLevel2VehicleTeleportsBuilt",
283:"GetLevel2UpgradeStationsBuilt",
284:"GetPoweredBarracksBuilt",
285:"GetPoweredDocksBuilt",
286:"GetPoweredGeodomeBuilt",
287:"GetPoweredPowerstationsBuilt",
288:"GetPoweredToolStoresBuilt",
289:"GetPoweredGunstationsBuilt",
290:"GetPoweredTeleportsBuilt",
291:"GetPoweredVehicleTeleportsBuilt",
292:"GetPoweredUpgradeStationsBuilt"
}
def disassemble(data):
"Decompiles a NPL bytecode stream"
import collections
BYTECODE = struct.Struct('2H')
result = ""
bytecode = []
labels = {}
Instruction = collections.namedtuple("Instruction","address opcode operand")
# Unpack instruction stream
unpack = array.array('H',data)
code = zip(unpack[1::2], unpack[::2])
for address, data in enumerate(code):
bytecode.append(Instruction(address,data[0],data[1]))
# bytecode now contains the instruction stream
# Pass one, collect the labels
for op in bytecode:
if op.opcode == OP_LABL:
labels[op.address] = op.operand
# Pass two, decompile
for op in bytecode:
label = ""
if op.opcode == OP_LOAD:
opcode = "LOAD"
operand = str(op.operand)
elif op.opcode == OP_COMP:
opcode = "COMP"
operand = COMPARES[op.operand]
elif op.opcode == OP_FUNC:
opcode = "FUNC"
operand = FUNCS[op.operand]
elif op.opcode == OP_LABL:
label = "label%d:" % (op.operand)
opcode = operand = ""
elif op.opcode == OP_GOTO:
opcode = "GOTO"
operand = "label%d" % (labels[op.operand])
else:
opcode = "***Unknown opcode***"
result += "%s\t%s %s\n" % (label, opcode, operand)
# Done, return text
return result
def assemble(source):
"Compiles a bytecode assembly file into bytecode"
BYTECODE = struct.Struct('2H')
bytecode = array.array('H')
# Pass one, massage the input
cleansource = ""
for line in source.splitlines():
# Strip comments
if ';' in line:
line = line.partition(';')[0]
# Remove whitespace
line = line.strip()
# Split labels into separate lines
if ':' in line:
label, colon, rest = line.partition(':')
if rest != "":
line = '%s:\n%s' % (label, rest.strip())
# No blank lines:
if line == "":
continue
cleansource += line + "\n"
# Pass two, compile into bytecode
labelidx = 0
labels = {}
gotos = {}
for line in cleansource.splitlines():
# Check for a label
if ":" in line:
label, colon, rest = line.partition(':')
label = label.lower() # Labels are case insensitive
labels[label] = len(bytecode) // 2
bytecode.append(labelidx)
bytecode.append(OP_LABL)
labelidx += 1
continue
opcode = line[0:4]
operand = line[5:].strip()
if opcode == "LOAD":
bytecode.append(int(operand))
bytecode.append(OP_LOAD)
elif opcode == "COMP":
if operand in COMPARES.values():
for idx, op in COMPARES.iteritems():
if op == operand:
bytecode.append(idx)
bytecode.append(OP_COMP)
else:
raise ValueError("unknown compare function %s" % operand)
elif opcode == "FUNC":
if operand.lower() in [name.lower() for name in FUNCS.itervalues()]:
found = False
for idx, op in FUNCS.iteritems():
if op.lower() == operand.lower():
bytecode.append(idx)
bytecode.append(OP_FUNC)
found = True
if not found:
raise ValueError("Could not find function index for %s" % (operand))
else:
raise ValueError("Calling unknown function %s" % operand)
elif opcode == "GOTO":
gotos[len(bytecode)] = operand
bytecode.append(0)
bytecode.append(OP_GOTO)
else:
raise ValueError("Unknown opcode encountered: %s" % opcode)
# Pass three, fixup the GOTOs
for addr, label in gotos.items():
bytecode[addr] = labels[label.lower()]
return bytecode.tostring()
def compile(source):
"Compile .nrM source into bytecode"
import re
cleansource = ""
datestamp = re.compile('"\d\d:\d\d:\d\d":')
# Pass one, massage input
for line in source.splitlines():
# Remove whitespace
line = line.strip()
# Strip comments
if ';' in line:
line = line.partition(';')[0]
if line.startswith('//'):
continue
# Ugly hack for that timestamp in the original files
if datestamp.match(line):
cleansource += "__init__:\n"
continue
# Remove brackets
line = line.translate(TRANSTAB,'{}')
# No blank lines:
if line == "":
continue
cleansource += line + "\n"
asmsource = ""
# Pass two, tokenize and generate asm
for token in cleansource.split():
# Loading a constant
if token.isdigit():
opcode = "LOAD"
operand = token
# Label or goto
elif ':' in token:
if token.startswith(':'):
# A goto
opcode = "GOTO"
operand = token[1:]
else:
# Label definition
opcode = token
operand = ''
# Comparison
elif token in COMPARES.values():
opcode = "COMP"
operand = token
# Function call
elif token.lower() in [name.lower() for name in FUNCS.itervalues()]:
opcode = "FUNC"
operand = token
else:
raise ValueError("Something wierd found: %s" % (token))
# Add to asm source
asmsource += "%s %s\n" % (opcode, operand)
# Pass three, feed to compiler
return assemble(asmsource)
if __name__ == "__main__":
import optparse
import os
import sys
parser = optparse.OptionParser(version="%prog 0.3")
parser.add_option('-d', dest="disfile",
help="Disassemble a npl file")
parser.add_option('-a', dest="asfile",
help="Assemble a npl file")
parser.add_option('-c', dest="cfile",
help="Compile a nrM file")
parser.add_option('-o', dest="output",
help="Specify output file. Defaults to console when disassembling, <asfile>.npl when assembling")
options, args = parser.parse_args()
# Check arguments
if options.disfile is None and options.asfile is None and options.cfile is None:
parser.print_help()
sys.exit()
if options.disfile is not None and options.asfile is not None and options.cfile is not None:
print "Specify only one of -d, -a and -c."
sys.exit()
# Read source
if options.asfile:
data = open(options.asfile).read()
elif options.disfile:
data = open(options.disfile,'rb').read()
elif options.cfile:
if options.cfile == '-':
data = sys.stdin.read()
else:
data = open(options.cfile).read()
# Check output
if options.disfile is not None:
# Disassembling
if options.output is not None:
outfile = open(options.output,'w')
else:
outfile = sys.stdout
if options.asfile is not None:
# Assembling
if options.output is not None:
outfile = open(options.output,'wb')
else:
outfilename = options.asfile + ".npl"
outfile = open(outfilename,'wb')
if options.cfile is not None:
# Compiling
if options.output is not None:
outfile = open(options.output,'wb')
else:
outfilename = options.cfile + ".npl"
outfile = open(outfilename,'wb')
# Do the work
if options.asfile:
result = assemble(data)
outfile.write(result)
if options.disfile:
result = disassemble(data)
outfile.write(result)
if options.cfile:
result = compile(data)
outfile.write(result)
# Done