-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFileInsert.py
188 lines (148 loc) · 6.2 KB
/
GFileInsert.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
import sys
import argparse
import struct
import os
from pathlib import Path
from lib.parser import readint, readintoffset, readtextoffset, count_text_bytes
import PALInstructionsSet
import operator
from hanziconv import HanziConv
from PALRecomp import dialog
import csv
import codecs
import CustomCodec
from pathlib import Path
from GFileExtract import parse
def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="%(prog)s [OPTION] [FILE]...",
description="Extract text from items table"
)
parser.add_argument(
"-v", "--version", action="version",
version = f"{parser.prog} version 0.0"
)
parser.add_argument('csv_file')
parser.add_argument('g_dat_file')
parser.add_argument('geindex_file')
return parser
return signed_value
file_ids = {
"gitem" : 7,
"gfight1" : 0,
"gfight2" : 1,
"gfight3" : 2,
"gfight4" : 3,
"ghero" : 4,
"gmon" : 5,
"glevel" : 6,
"gitem" : 7,
"gmagic" : 8,
"gmap" : 9,
}
reverse_file_ids = {v: k for k, v in file_ids.items()}
def parse_geindex(content):
current_addr = 0
count = struct.unpack('I',content[current_addr : current_addr + 4])[0]
current_addr+=4
ukn = {} #Je sais pas ce que c'est.
for i in range(0, count):
uk = struct.unpack('I',content[current_addr : current_addr + 4])[0]
current_addr+=4
id_ = struct.unpack('I',content[current_addr : current_addr + 4])[0]
current_addr+=4
ukn[id_] = uk
current_addr += 4
cnt = struct.unpack('I',content[current_addr : current_addr + 4])[0]
current_addr += 8
ptr_by_files = {
}
while (current_addr < len(content)):
if (cnt == 1):
current_int = struct.unpack('I',content[current_addr : current_addr + 4])[0]
size = 4
current_addr += (4 * size)
cnt = struct.unpack('I',content[current_addr : current_addr + 4])[0]
if (cnt == 0):
break
current_addr += 8
current_int = struct.unpack('I',content[current_addr : current_addr + 4])[0]
current_addr+=4
size = 4
current_addr += 4
addr_ptr = current_addr
ptr = struct.unpack('I',content[current_addr : current_addr + 4])[0]
current_addr += 4
file_id = struct.unpack('I',content[current_addr : current_addr + 4])[0]
if (file_id not in ptr_by_files.keys()):
ptr_by_files[file_id] = []
ptr_by_files[file_id].append(PALInstructionsSet.pointer(addr_ptr, ptr))
current_addr += (4 * (size-2))
cnt-=1
for file in ptr_by_files.keys():
ptr_by_files[file].sort(key=operator.attrgetter('to'))
return ptr_by_files
def recompile(csv_file, dat_file, geindex):
dat_key = filename = Path(dat_file).stem
[out_str, text_addresses] = parse(dat_file)
filename = Path(csv_file).stem
filesize = os.path.getsize(csv_file)
fh = open(dat_file, 'rb')
out = bytearray(fh.read())
fh2 = open(geindex, 'rb')
out_idx = bytearray(fh2.read())
ptr_by_files = parse_geindex(out_idx)
ptrs_in_file = ptr_by_files[file_ids[dat_key]]
translations = []
index_row = 0
with open(csv_file, newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
TL = dialog(row[1].replace("\n", "%N"), row[2:])
translations.append(TL)
index_row += 1
if len(text_addresses) != len(translations):
raise Exception("CSV and dat file texts don't match")
globaloffset = 0
idx_text = 0
current_idx_geindex = 0
#then we insert the new bytes
for text_addr in text_addresses:
if (current_idx_geindex < len(ptrs_in_file)):
while(ptrs_in_file[current_idx_geindex].to < text_addr):
new_ptr = ptrs_in_file[current_idx_geindex].to + globaloffset
new_ptr_bytes = struct.pack('I',new_ptr)
out_idx[ptrs_in_file[current_idx_geindex].from_:ptrs_in_file[current_idx_geindex].from_+4] = new_ptr_bytes
current_idx_geindex += 1
if (current_idx_geindex >= len(ptrs_in_file)):
break
text_addr = text_addr + globaloffset
sz = count_text_bytes(text_addr, out, b'%%')
new_text_bytes = codecs.encode(translations[idx_text].TL, "pal_custom_codec", "backslashreplace")
if (len(new_text_bytes) > 0):
out[text_addr:text_addr+sz] = []
#print("Removing " + hex(sz) + " bytes at addr " + hex(text_addr))
#print("Adding " + hex(len(new_text_bytes)) + " bytes at addr " + hex(text_addr) + "global offset " + hex(globaloffset))
for idx_param in range(0, len(translations[idx_text].params)):
param_value = translations[idx_text].params[idx_param]
param_bytes = struct.pack('H',param_value)
offset = 2 * (len(translations[idx_text].params) - idx_param)
out[text_addr - offset:text_addr - offset + 2] = param_bytes
out[text_addr:text_addr] = new_text_bytes
globaloffset = globaloffset + len(new_text_bytes) - sz
idx_text = idx_text + 1
with open("out.dat", "wb") as binary_file:
# Write bytes to file
binary_file.write(out)
with open("geindex.dat", "wb") as binary_file:
# Write bytes to file
binary_file.write(out_idx)
def main() -> None:
parser = init_argparse()
args = parser.parse_args()
if not args.csv_file or not args.g_dat_file or not args.geindex_file:
raise Exception("GFileInsert needs a csv file, a geindex file and a g dat file!")
else:
recompile(args.csv_file,args.g_dat_file,args.geindex_file)
if __name__ == "__main__":
main()