forked from RoadrunnerWMC/zed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
executable file
·219 lines (177 loc) · 6.51 KB
/
interpreter.py
File metadata and controls
executable file
·219 lines (177 loc) · 6.51 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
import os
import os.path
import sys
import ndspy.bmg
import ndspy.rom
class ScriptRunner:
def __init__(self, bmgs):
self.bmgs = bmgs
def runScript(self, scriptID):
"""
Run the specified script.
"""
for bId, b in self.bmgs.items():
if scriptID in b.scripts:
self.nextBmgID = bId
self.nextInstructionIdx = b.scripts[scriptID]
break
else:
print("Sorry, but there's no script with that ID. :/")
return
while self.nextBmgID not in (0xFF, -1) and self.nextInstructionIdx not in (0xFFFF, -1):
self.runInstruction()
print('(End of script.)\n')
def runInstruction(self):
"""
Interpret the next instruction.
"""
bmg = self.bmgs[self.nextBmgID]
instruction = bmg.instructions[self.nextInstructionIdx]
instructionType = instruction & 0xFF
if instructionType == 1:
self.runInstructionType1(instruction)
elif instructionType == 2:
self.runInstructionType2(instruction)
elif instructionType == 3:
self.runInstructionType3(instruction)
else:
raise RuntimeError(f'Found an instruction with type {instructionType}...?')
def runInstructionType1(self, instruction):
"""
Interpret an instruction of type 1.
"""
bmgID = (instruction >> 8) & 0xFF
messageID = (instruction >> 16) & 0xFFFF
nextInstIdx = (instruction >> 32) & 0xFFFF
nextBmgID = (instruction >> 48) & 0xFF
message = self.bmgs[bmgID].messages[messageID]
partsToPrint = []
response1 = []
response2 = []
response3 = []
response4 = []
currentlyPrintingTo = partsToPrint
for i, part in enumerate(message.stringParts):
if isinstance(part, str):
currentlyPrintingTo.append(part)
elif part.type == 0:
if part.data == b'\0\0':
currentlyPrintingTo = response1
elif part.data == b'\1\0':
currentlyPrintingTo = response2
elif part.data == b'\2\0':
currentlyPrintingTo = response3
elif part.data == b'\3\0':
currentlyPrintingTo = response4
else:
currentlyPrintingTo.append(str(part))
stringMessage = ''.join(partsToPrint).rstrip()
print(f'\033[3m"{stringMessage}"\033[0m')
self.response1 = ''.join(response1)
self.response2 = ''.join(response2)
self.response3 = ''.join(response3)
self.response4 = ''.join(response4)
self.nextBmgID = nextBmgID
self.nextInstructionIdx = nextInstIdx
def runInstructionType2(self, instruction):
"""
Interpret an instruction of type 2.
"""
labelsCount = (instruction >> 8) & 0xFF
whatToCheckFor = (instruction >> 16) & 0xFFFF
parameter = (instruction >> 32) & 0xFFFF
baseLabelNumber = (instruction >> 48) & 0xFFFF
n = self.checkCondition(whatToCheckFor, parameter)
while n >= labelsCount:
print(f"Can't branch to label +{n} -- only +0 through +{labelsCount-1} are allowed...")
n = self.checkCondition(whatToCheckFor, parameter)
self.nextBmgID, self.nextInstructionIdx = self.bmgs[self.nextBmgID].labels[
baseLabelNumber + n
]
def runInstructionType3(self, instruction):
"""
Interpret an instruction of type 3.
"""
labelNumber = (instruction >> 16) & 0xFFFF
print('ANIMATION')
self.nextBmgID, self.nextInstructionIdx = self.bmgs[self.nextBmgID].labels[labelNumber]
def checkCondition(self, type, parameter):
"""
Check a condition.
"""
def process(x):
return int(x)
if type in [1, 2, 3]: # Response to a question
responses = [self.response1, self.response2, self.response3, self.response4][: type + 1]
shortcuts = [''] * len(responses)
chars = 0
while len(set(shortcuts)) < len(shortcuts):
chars += 1
shortcuts = [r[:chars].lower() for r in responses]
prompt = ' '.join(f'[{s}: {r}]' for s, r in zip(shortcuts, responses))
def process(x):
if x.lower() in shortcuts:
return shortcuts.index(x)
else:
raise ValueError
elif type == 4: # Checking a flag?
index = parameter >> 5
bit = parameter & 0x1F
prompt = f'Is bit {bit} of global value {index} set? (Y/N): '
def process(x):
if x.lower() == 'y':
return 0
elif x.lower() == 'n':
return 1
else:
raise ValueError
else: # Undocumented
prompt = f'Enter the result of check_{type}({parameter}): '
while True:
try:
return process(input(prompt))
except ValueError:
prompt = 'wat '
def getScriptID():
"""
Get a script ID as input from the user. Return None if the input is
blank or otherwise invalid.
"""
raw = input('Enter a script category and ID (like "104, 9"): ')
if ',' not in raw:
return None
rawspl = raw.split(',')
a, b = int(rawspl[0]), int(rawspl[1])
return a << 16 | b
def main():
if len(sys.argv) > 1:
inpath = sys.argv[1]
else:
inpath = 'Testing/Zelda - Spirit Tracks.nds'
BMGs = {}
if os.path.isfile(inpath):
with open(inpath, 'rb') as f:
romData = f.read()
rom = ndspy.rom.NintendoDSRom(romData)
datasList = []
for filename in rom.filenames['English/Message'].files:
datasList.append(rom.files[rom.filenames['English/Message/' + filename]])
for d in datasList:
bmg = ndspy.bmg.BMG(d)
BMGs[bmg.id] = bmg
else:
for fn in os.listdir(inpath):
fullfn = os.path.join(inpath, fn)
with open(fullfn, 'rb') as f:
d = f.read()
bmg = ndspy.bmg.BMG(d)
BMGs[bmg.id] = bmg
assert len(BMGs) == 30
runner = ScriptRunner(BMGs)
scriptID = getScriptID()
while scriptID is not None:
runner.runScript(scriptID)
scriptID = getScriptID()
print('Closing.')
if __name__ == '__main__':
main()