-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_pointers.py
194 lines (159 loc) · 7.69 KB
/
find_pointers.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
import os
from rominfo import FdRom, SRC_DISK
from cd_rominfo import CdRom, CD_SRC_DISK
from romtools.dump import BorlandPointer, DumpExcel, PointerExcel
from romtools.disk import Gamefile, Block, Disk
strings_to_skip = ['ポインタが使われました', ' 体', ' 心', 'EMS']
garbage_pointer_values = [0x0, 0xff, 0x8, 0x4, 0x200, 0x400, 0x800, 0xd00, 0x900, 0x1, 0x2, 0x3]
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub) # use start += 1 to find overlapping matches
DUMP_XLS_PATH = 'appareden_sys_dump.xlsx'
Dump = DumpExcel(DUMP_XLS_PATH)
#MSG_DUMP_XLSX_PATH = 'appareden_msg_dump.xlsx'
#MsgDump = DumpExcel(MSG_DUMP_XLSX_PATH)
OriginalAp = Disk(SRC_DISK, dump_excel=Dump)
OriginalCdAp = Disk(CD_SRC_DISK, dump_excel=Dump)
#files_to_search = ['ORFIELD.EXE', 'ORBTL.EXE', 'ORTITLE.EXE']
# TODO: Add ORITTLE back in once I've mapped the CD version.
files_to_search = ['ORFIELD.EXE', 'ORBTL.EXE', 'ORTITLE.EXE']
problem_count = 0
try:
os.remove('appareden_pointer_dump.xlsx')
except WindowsError:
pass
PtrXl = PointerExcel('appareden_pointer_dump.xlsx')
for version in ['FD', 'CD']:
for f in files_to_search:
if version == 'FD':
GF = Gamefile(os.path.join('original', f), disk=OriginalAp)
file_blocks = FdRom.file_blocks[f]
pointer_constant = FdRom.pointer_constant[f]
pointer_tables = FdRom.pointer_tables[f]
pointer_disambiguation = FdRom.pointer_disambiguation
worksheet_name = GF.filename
else:
GF = Gamefile(os.path.join('original_cd', f), disk=OriginalCdAp)
file_blocks = CdRom.file_blocks[f]
pointer_constant = CdRom.pointer_constant[f]
pointer_tables = CdRom.pointer_tables[f]
pointer_disambiguation = CdRom.pointer_disambiguation
worksheet_name = 'CD ' + GF.filename
found_text_locations = []
print(f)
previous_pointer_locations = []
try:
worksheet = PtrXl.add_worksheet(worksheet_name)
except AttributeError:
print("You have the worksheet open. Close it and try again")
worksheet = PtrXl.add_worksheet(worksheet_name)
row = 1
for table in pointer_tables:
print(hex(table[0]), hex(table[1]))
stride = table[2]
table_bytes = GF.filestring[table[0]:table[1]]
pointer_location = table[0]
#print(table_bytes)
while table_bytes:
possible_value = int.from_bytes(table_bytes[0:2], byteorder='little')
if possible_value in garbage_pointer_values:
# Prepare next iteration and skip this one
table_bytes = table_bytes[stride:]
pointer_location += stride
continue
text_location = possible_value + pointer_constant
found_text_locations.append(text_location)
#print(hex(text_location))
if pointer_location in previous_pointer_locations:
print("%s was skipped since it's a duplicate" % hex(pointer_location))
else:
obj = BorlandPointer(GF, pointer_location, text_location)
worksheet.write(row, 0, hex(text_location))
worksheet.write(row, 1, hex(pointer_location))
worksheet.write(row, 2, obj.text())
previous_pointer_locations.append(pointer_location)
row += 1
# Prepare the next iteration of the loop
table_bytes = table_bytes[stride:]
pointer_location += stride
# Now look in the code blocks.
for block in file_blocks:
blk = Block(GF, block)
if version == 'FD':
translations = Dump.get_translations(blk, include_blank=True)
else:
translations = Dump.get_translations(blk, include_blank=True, use_cd_location=True)
for t in translations:
#print(t)
if version == 'CD':
t.location = t.cd_location
if t.location in found_text_locations:
#print("skipping that one")
continue
all_locs = []
pointer_location = '?'
if any([s in t.japanese.decode('shift_jis') for s in strings_to_skip]):
continue
text_location = t.location
look_for_int = t.location - pointer_constant
look_for = look_for_int.to_bytes(2, byteorder='little')
#codeblock_look_for = b'\x68' + look_for
if GF.filestring[:pointer_constant].count(look_for) == 1:
pointer_location = GF.filestring[:pointer_constant].find(look_for)
else:
all_locs = sorted(list(find_all(GF.filestring[:pointer_constant], look_for)))
if text_location in pointer_disambiguation:
pointer_location = pointer_disambiguation[text_location]
else:
for a in all_locs:
if a > pointer_constant:
# Skipping these for now, since they'll hopefully be in a table
continue
if len(all_locs) == 1:
pointer_location = all_locs[0]
#elif last_pointer_location < a < last_pointer_location + 0x100:
else:
pointer_location = a
# Need to test its pointer_location against all other previous pointer_locations to avoid duplicates
#print([hex(t) for t in previous_pointer_locations])
#print(0x26ad8 in previous_pointer_locations)
#print(hex(pointer_location))
if pointer_location in previous_pointer_locations:
try:
print("Duplicate detected at %s, skipping" % hex(pointer_location))
except TypeError:
print("Duplicate detected at %s, skipping" % pointer_location)
continue
obj = BorlandPointer(GF, pointer_location, text_location)
worksheet.write(row, 0, hex(text_location))
try:
worksheet.write(row, 1, hex(pointer_location))
except TypeError:
problem_count += 1
if len(all_locs) == 0:
print("Problem finding %s allegedly at %s, not found" % (t.japanese.decode('shift_jis'), hex(t.location)))
elif len(all_locs) == 1:
print(t.japanese.decode('shift_jis'), 'seems fine')
else:
print("Problem finding %s" % t.japanese.decode('shift_jis'), "multiple found")
worksheet.write(row, 1, '?')
if len(all_locs) > 0:
worksheet.write(row, 3, "%s" % ([hex(a) for a in all_locs]))
worksheet.write(row, 2, obj.text())
row += 1
previous_pointer_locations.append(pointer_location)
PtrXl.workbook.close()
print("%s problems found" % problem_count)
# While we have all these variables, get a count of all the lines in the msg files
#count = 0
#for w in MsgDump.workbook.worksheets:
# rows = list(w.rows)[1:]
# for r in rows:
# if r[0].value is not None:
# count += 1
# #print(w, count)
#print(count)