-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.py
163 lines (130 loc) · 5.59 KB
/
dump.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
"""
Generic dumper of Shift-JIS text into an excel spreadsheet.
Meant for quick estimations of how much text is in a game.
"""
import sys
import os
import xlsxwriter
from rominfo import FILE_BLOCKS, FILES, ORIGINAL_ROM_DIR, DUMP_XLS_PATH
COMPILER_MESSAGES = [b'Turbo', b'Borland', b'C++', b'Library', b'Copyright']
ASCII_MODE = 2
# 0 = none
# 1: punctuation and c format strings only (not implemented)
# 2: All ascii
THRESHOLD = 2
def dump(files):
for filename in FILES:
# Keep CEDIT/MAIN.EXE safe
clean_filename = filename.replace('/', '-')
worksheet = workbook.add_worksheet(clean_filename)
worksheet.write(0, 0, 'Offset', header)
worksheet.write(0, 1, 'Japanese', header)
worksheet.write(0, 2, 'JP_len', header)
worksheet.write(0, 3, 'English', header)
worksheet.write(0, 4, 'EN_len', header)
worksheet.write(0, 5, 'Comments', header)
worksheet.set_column('A:A', 8)
worksheet.set_column('B:B', 60)
worksheet.set_column('C:C', 5)
worksheet.set_column('D:D', 60)
worksheet.set_column('E:E', 5)
worksheet.set_column('F:F', 60)
row = 1
blocks = FILE_BLOCKS[filename]
if filename.endswith('.DAT'):
src_filepath = os.path.join(ORIGINAL_ROM_DIR, 'DANTE2', 'DAT_RPG', filename)
ASCII_MODE = 0
elif filename.startswith('CEDIT'):
src_filepath = os.path.join(ORIGINAL_ROM_DIR, filename)
ASCII_MODE = 0
else:
src_filepath = os.path.join(ORIGINAL_ROM_DIR, 'DANTE2', filename)
ASCII_MODE = 2
#if filename not in UNCOMPRESSED_FILES:
# src_filepath = 'original/decompressed/%s.decompressed' % filename
#else:
# src_filepath = 'original/%s' % filename
with open(os.path.join(src_filepath), 'rb') as f:
contents = f.read()
cursor = 0
sjis_buffer = b""
sjis_buffer_start = 0
sjis_strings = []
for c in COMPILER_MESSAGES:
print(c)
if c in contents:
#print(contents)
cursor = contents.index(c)
sjis_buffer_start = contents.index(c)
break
for (start, stop) in blocks:
print((hex(start), hex(stop)))
cursor = start
sjis_buffer_start = cursor
while cursor <= stop:
# First byte of SJIS text. Read the next one, too
if 0x80 <= contents[cursor] <= 0x9f or 0xe0 <= contents[cursor] <= 0xef:
#print(bytes(contents[cursor]))
sjis_buffer += contents[cursor].to_bytes(1, byteorder='little')
cursor += 1
sjis_buffer += contents[cursor].to_bytes(1, byteorder='little')
# Halfwidth katakana
elif 0xa1 <= contents[cursor] <= 0xdf:
sjis_buffer += contents[cursor].to_bytes(1, byteorder='little')
# ASCII text
elif 0x20 <=contents[cursor] <= 0x7e and ASCII_MODE in (1, 2):
sjis_buffer += contents[cursor].to_bytes(1, byteorder='little')
# C string formatting with %
#elif contents[cursor] == 0x25:
# #sjis_buffer += b'%'
# cursor += 1
# if contents[cursor]
# End of continuous SJIS string, so add the buffer to the strings and reset buffer
else:
sjis_strings.append((sjis_buffer_start, sjis_buffer))
sjis_buffer = b""
sjis_buffer_start = cursor+1
cursor += 1
#print(sjis_buffer)
# Catch anything left after exiting the loop
if sjis_buffer:
sjis_strings.append((sjis_buffer_start, sjis_buffer))
sjis_buffer = b''
if len(sjis_strings) == 0:
continue
for s in sjis_strings:
# Remove leading U's
while s[1].startswith(b'U'):
s = (s[0] + 1, s[1][1:])
#s[1] = s[1][1:]
#s[0] += 1
s = (s[0], s[1].rstrip(b'U'))
if s[1].startswith(b'='):
s = (s[0], s[1].replace(b'=', b'[=]'))
if len(s[1]) < THRESHOLD:
continue
loc = '0x' + hex(s[0]).lstrip('0x').zfill(5)
try:
jp = s[1].decode('shift-jis')
except UnicodeDecodeError:
print("Couldn't decode that")
continue
if len(jp.strip()) == 0:
continue
print(loc, jp)
worksheet.write(row, 0, loc)
worksheet.write(row, 1, jp)
row += 1
workbook.close()
if __name__ == '__main__':
#if len(sys.argv) < 2:
# print("Usage: python dumper.py folderwithgamefilesinit")
# sys.exit(1)
#'patched' = sys.argv[1]
workbook = xlsxwriter.Workbook("dante98-II" + '_dump.xlsx')
header = workbook.add_format({'bold': True, 'align': 'center', 'bottom': True, 'bg_color': 'gray'})
#FILES = [f for f in os.listdir('patched') if os.path.isfile(os.path.join('patched', f))]
#FILES = ['IDS.decompressed', 'IS2.decompressed']
print(FILES)
dump(FILES)
#find_blocks('patched/IDS.decompressed')