-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneko_tool.py
154 lines (137 loc) · 5.31 KB
/
neko_tool.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
import sys
from nekosdk_advscript2 import NekosdkAdvscript2
import os
import json
class Decompiler:
def __init__(self, filename):
with open(filename, 'rb') as f:
self.scr = NekosdkAdvscript2.from_io(f)
self.id_to_idx = {}
for idx, node in enumerate(self.scr.nodes):
self.id_to_idx[node.id] = idx
def dump(self, node):
# if True:
# print(f"{node.id}. type1={node.type1} opcode={node.opcode} some_ofs={node.some_ofs} next_id={node.next_id}")
# for i, s in enumerate(node.strs):
# ss = s.value.strip().strip(chr(0))
# if ss:
# print(f" s{i}: {repr(ss)}")
if node.opcode == 5:
s1 = node.strs[1].value.strip().strip(chr(0)).replace('\r\n', '\n')
s2 = node.strs[2].value.strip().strip(chr(0)).replace('\r\n', '\n')
if s1:
names.add(s1)
self.jsondata.append({
'id': node.id,
'name': s1,
'message' : s2,
})
else:
self.jsondata.append({
'id': node.id,
'message' : s2,
})
def dump_to_json(self, json_filename):
self.jsondata = []
i = 0
while True:
try:
n = self.scr.nodes[i]
except IndexError:
break
self.dump(n)
i = self.id_to_idx.get(n.next_id)
if i is None:
break
json.dump(self.jsondata, open(json_filename, 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
def test(self):
i = 0
while True:
try:
node = self.scr.nodes[i]
except IndexError:
break
if node.opcode == 5:
s1 = node.strs[1].value.strip().strip(chr(0)).replace('\r\n', '\n')
s2 = node.strs[2].value.strip().strip(chr(0)).replace('\r\n', '\n')
new_s1 = "名字测试" + '\x00'
new_s2 = "汉化测试" + '\x00'
node.strs[0].value = node.strs[0].value.replace(s1, new_s1)
node.strs[0].value = node.strs[0].value.replace(s2, new_s2)
node.strs[1].value = new_s1
node.strs[2].value = new_s2
i = self.id_to_idx.get(node.next_id)
if i is None:
break
def replace_with_json(self, json_filename):
self.jsondata = json.load(open(json_filename, 'r', encoding='utf-8'))
for item in self.jsondata:
node = self.scr.nodes[self.id_to_idx[item['id']]]
old_s0 = node.strs[0].value
old_s1 = node.strs[1].value.strip().strip(chr(0)).replace('\r\n', '\n')
old_s2 = node.strs[2].value.strip().strip(chr(0)).replace('\r\n', '\n')
s1 = item.get('name')
s2 = item.get('message')
# s1 = "名字测试"
# s2 = "汉化测试"
if s1:
s1 = s1 + '\x00'
# node.strs[0].value = node.strs[0].value.replace(old_s1, s1)
node.strs[1].value = s1
s2 = s2 + '\x00'
# node.strs[0].value = node.strs[0].value.replace(old_s2, s2)
node.strs[2].value = s2
def write(self, filename):
with open(filename, 'wb') as f:
self.scr.write(f)
def extract_json():
os.makedirs(json_path, exist_ok=True)
filenames = os.listdir(script_path)
for filename in filenames:
input_filename = os.path.join(script_path, filename)
json_filename = os.path.join(json_path, filename + ".json")
decompiler = Decompiler(input_filename)
decompiler.dump_to_json(json_filename)
with open('names.txt', 'w', encoding='utf-8') as f:
for name in names:
f.write(name + '\n')
def recompile_bin():
os.makedirs(output_path, exist_ok=True)
filenames = os.listdir(script_path)
for filename in filenames[:]:
input_filename = os.path.join(script_path, filename)
output_filename = os.path.join(output_path, filename)
json_filename = os.path.join(translated_json_path, filename + ".json")
print("recompile", filename)
decompiler = Decompiler(input_filename)
decompiler.replace_with_json(json_filename)
decompiler.write(output_filename)
def test():
os.makedirs(test_path, exist_ok=True)
filenames = os.listdir(script_path)
for filename in filenames[:1]:
input_filename = os.path.join(script_path, filename)
test_filename = os.path.join(test_path, filename)
decompiler = Decompiler(input_filename)
decompiler.test()
decompiler.write(test_filename)
script_path = "script"
output_path = "script_zh"
json_path = "gt_input"
translated_json_path = "gt_output"
test_path = "test"
names = set()
command = sys.argv[1]
if __name__ == "__main__":
match command:
case "extract":
extract_json()
case "recompile":
recompile_bin()
case "test":
test()
case _:
print("invalid command")
# extract_json()
# recompile_bin()
# test()