-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumper.py
136 lines (111 loc) · 4.01 KB
/
dumper.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import textwrap
import codecs
import os
import sys
import json
class Dumper(object):
offsets = []
strings = {
'sourceLang' : 'jp',
'targetLang' : 'pt-br',
'texts' : [],
}
control_bytes = {
b'\x00\x00' : '[ENDSTRING]',
}
control_codes = {
'\u000b' : '[READ_MORE]',
'\u0000' : '[WAIT_A_SECOND]',
'\u0006\u0001' : '[つよし]',
}
def __init__(self, data, filename):
self.binary = BinaryFile(data)
self.scripts_file = open('./scripts/'+os.path.splitext(os.path.basename(filename))[0]+'.json', 'w+')
self.start()
def start(self):
self.binary.skip(4)
while True:
data = self.binary.read(4)
if data.value[0:2] != b'\x00\x00':
break
self.offsets.append(data.as_integer)
for offset in self.offsets:
print(hex(offset))
self.binary.seek(offset)
output_text = ""
while True:
data = self.binary.read(1)
n_data = self.binary.read_next(1)
test = data.value
test+=(n_data.value)
if test in self.control_bytes:
if self.control_bytes[test] == '[ENDSTRING]':
self.binary.skip(1)
for target, control_code in self.control_codes.items():
output_text = output_text.replace(target, control_code)
self.strings["texts"].append({ "source": output_text, "target": output_text })
break
else:
if int.from_bytes(test, 'big') in range(0x8140,0xEAA2):
self.binary.skip(1)
shiftjis = str(test, "shiftjis") # Converte de UTF-16 para UTF-8
output_text += codecs.encode(shiftjis, "utf-8").decode('utf-8')
else:
output_text += data.as_string
json.dump(self.strings, self.scripts_file, indent=4, ensure_ascii=False)
class BinaryFile(object):
def __init__(self, data):
self.start_offset = 0
self.current_offset = 0
self.content = data
self.end_offset = len(data)
def read(self, nbytes):
value = self.content[self.current_offset:(self.current_offset+nbytes)]
self.current_offset += nbytes
return BinaryBytes(value)
def read_next(self, nbytes):
value = self.content[self.current_offset:(self.current_offset+nbytes)]
return BinaryBytes(value)
def skip(self, nbytes):
self.current_offset += nbytes
def seek(self, nbytes):
self.current_offset = nbytes
class BinaryBytes(object):
def __init__(self, byte_value):
self.value = byte_value
self.as_integer = int.from_bytes(byte_value, 'big')
try:
self.as_string = str(byte_value.decode("ascii"))
except Exception:
self.as_string = ""
if __name__ == '__main__':
""" Program Command Line """
cmd = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
[BOMBERMAN] TEXT DUMPER
--------------------------------------------------
python dumper.py input
--------------------------------------------------
Tool to extract bomberman texts from decompressed
binary files
''')
)
cmd.add_argument(
'infile',
nargs='?',
type=argparse.FileType('rb'),
default=sys.stdin,
help='Input file.'
)
""" Program Main Routine """
args = cmd.parse_args()
if(args.infile.name != '<stdin>'):
with args.infile as input_file:
file_content = input_file.read()
Dumper(file_content, args.infile.name)
else:
cmd.print_help()