-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
179 lines (164 loc) · 9.75 KB
/
data.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
import os
from FF8GameData.FF8HexReader.mngrp import Mngrp
from FF8GameData.FF8HexReader.mngrphd import Mngrphd
from FF8GameData.gamedata import GameData
from FF8GameData.m00x.dataclass import m000bin, m001bin, m002bin, m003bin, m004bin, TypeId
class BinManager:
CHAR_SEP = '>'
def __init__(self, game_data: GameData):
self.m000bin = m000bin()
self.m001bin = m001bin()
self.m002bin = m002bin()
self.m003bin = m003bin()
self.m004bin = m004bin()
self.bin_list = (self.m000bin, self.m001bin, self.m002bin, self.m003bin, self.m004bin)
self.mngrp = None
self.mngrphd = None
self.game_data = game_data
def read_mngrp_file(self, file_mngrp, file_mngrphd):
file_mngrp_data = bytearray()
with open(file_mngrp, "rb") as file:
file_mngrp_data.extend(file.read())
file_mngrphd_data = bytearray()
with open(file_mngrphd, "rb") as file:
file_mngrphd_data.extend(file.read())
self.mngrphd = Mngrphd(game_data=self.game_data, data_hex=file_mngrphd_data)
# MNGRP updated
self.mngrp = Mngrp(game_data=self.game_data, data_hex=file_mngrp_data, header_entry_list=self.mngrphd.get_valid_entry_list())
for bin_data in self.bin_list:
if bin_data.input_id == TypeId.CARD:
input_table = self.game_data.card_data_json["card_info"]
elif bin_data.input_id == TypeId.SPELL:
input_table = self.game_data.magic_data_json["magic"]
elif bin_data.input_id == TypeId.ITEM:
input_table = self.game_data.item_data_json['items']
else:
print("Error reading input table")
exit(0)
if bin_data.output_id == TypeId.CARD:
output_table = self.game_data.card_data_json["card_info"]
elif bin_data.output_id == TypeId.SPELL:
output_table = self.game_data.magic_data_json["magic"]
elif bin_data.output_id == TypeId.ITEM:
output_table = self.game_data.item_data_json['items']
else:
print("Error reading output table")
exit(0)
file_bin_data = self.mngrp.get_section_by_id(bin_data.mngrp_bin_id).get_data_hex()
for data in bin_data.list_data:
index = data.offset
for entry in data.entries:
entry.text_offset = int.from_bytes(bytearray(file_bin_data[index:index + 2]), byteorder='little')
entry.amount_received = int(file_bin_data[index + 2])
entry.unk = int.from_bytes(bytearray(file_bin_data[index + 3:index + 5]), byteorder='little')
entry.element_in_id = str(int(file_bin_data[index + 5])) + ':' + input_table[int(file_bin_data[index + 5])]['name']
entry.amount_required = int(file_bin_data[index + 6])
entry.element_out_id = str(int(file_bin_data[index + 7])) + ':' + output_table[int(file_bin_data[index + 7])]['name']
index += entry.ENTRY_SIZE
for index_bin_data, bin_data in enumerate(self.bin_list):
file_msg_data = self.mngrp.get_section_by_id(bin_data.mngrp_msg_id).get_data_hex()
for index_data, data in enumerate(bin_data.list_data):
print(f"index_data: {index_data}")
print(f"data: {data}")
for index_data_entry, data_entry in enumerate(data.entries):
print(f"index_data_entry: {index_data_entry}")
print(f"data_entry: {data_entry}")
if index_data_entry == len(data.entries) - 1 and index_data == len(bin_data.list_data) - 1:
end_offset = len(file_msg_data)
print("2")
elif index_data_entry == len(data.entries) - 1:
end_offset = self.bin_list[index_bin_data].list_data[index_data + 1].entries[0].text_offset
print("3")
else:
end_offset = data.entries[index_data_entry + 1].text_offset
print(f"data_entry.text_offset: {data_entry.text_offset}")
print(f"end_offset: {end_offset}")
raw_data_text = file_msg_data[data_entry.text_offset:end_offset]
print(f"raw_data_text: {raw_data_text}")
self.bin_list[index_bin_data].list_data[index_data].entries[index_data_entry].text = self.game_data.translate_hex_to_str(raw_data_text)
def write_mngrp_file(self, file_mngrp, file_mngrphd):
file_mngrp_data = bytearray()
with open(file_mngrp, "rb") as file:
file_mngrp_data.extend(file.read())
file_mngrphd_data = bytearray()
with open(file_mngrphd, "rb") as file:
file_mngrphd_data.extend(file.read())
self.mngrphd = Mngrphd(game_data=self.game_data, data_hex=file_mngrphd_data)
self.mngrp = Mngrp(game_data=self.game_data, data_hex=file_mngrp_data, header_entry_list=self.mngrphd.get_entry_list())
i = 0
for bin_data in self.bin_list:
file_bin_data = bytearray()
file_msg_data = bytearray()
for data in bin_data.list_data:
for entry in data.entries:
file_bin_data.extend(entry.text_offset)
file_bin_data.extend([entry.amount_received])
file_bin_data.extend(entry.unk)
file_bin_data.extend([entry.element_in_id])
file_bin_data.extend([entry.amount_required])
file_bin_data.extend([entry.element_out_id])
file_msg_data.extend(entry.text)
i+=1
self.mngrp.set_section_by_id_and_bytearray(bin_data.mngrp_bin_id, file_bin_data)
self.mngrp.set_section_by_id_and_bytearray(bin_data.mngrp_msg_id, file_msg_data)
self.mngrp.update_data_hex()
self.mngrphd.update_data_hex()
with open(file_mngrp, "wb") as file:
file.write(self.mngrp.get_data_hex())
with open(file_mngrphd, "wb") as file:
file.write(self.mngrphd.get_data_hex())
def read_pandemona_file(self, path_input):
for mbin in self.bin_list:
current_line = 0
text_offset = 0
print(f"mbin: {mbin}")
with open(os.path.join(path_input, mbin.name + '.pandemona'), "r", encoding="utf8") as file:
str_read = file.readlines()
print(f"str_read: {str_read}")
for index_data, data in enumerate(mbin.list_data):
print(f"Data: {data}")
current_line += 1 # Line of data description ignored
for index_entry, entry in enumerate(data.entries):
print("Looping !")
print(entry)
print(index_entry)
print(data.entries[index_entry])
current_line += 1 # Ignoring the first line that just specify the entry index
print(str_read[current_line])
# Using [:-1] to remove the \n that we manually added
text_read = str_read[current_line].split(f'{self.CHAR_SEP}')[1][:-1]
entry.text = self.game_data.translate_str_to_hex(text_read)
print(data.entries[index_entry])
print(entry.text)
entry.text.extend([0x00]) # Adding the 0x00 that have been removed to note the end of the string
entry.text_offset = text_offset.to_bytes(2, byteorder='little')
entry.element_in_id = int(str_read[current_line + 1].split(f'{self.CHAR_SEP}')[1][:-1].split(':')[0])
entry.amount_required = int(str_read[current_line + 2].split(f'{self.CHAR_SEP}')[1][:-1])
entry.element_out_id = int(str_read[current_line + 3].split(f'{self.CHAR_SEP}')[1][:-1].split(':')[0])
entry.amount_received = int(str_read[current_line + 4].split(f'{self.CHAR_SEP}')[1][:-1])
entry.unk = int(str_read[current_line + 5].split(f'{self.CHAR_SEP}')[1][:-1]).to_bytes(2,
byteorder='little')
text_offset += len(entry.text) # +1 for the 0x00 that have been added.
current_line += 6
data.entries[index_entry] = entry
print(data.entries[index_entry])
current_line += 1 # The ------\n alone added
def write_pandemona_file(self, path_output):
str_output = ""
for mbin in self.bin_list:
for index_data, data in enumerate(mbin.list_data):
str_output += f"Data n°{index_data}, name:{data.name}, data description:{data.description}\n"
for nb_entry, entry in enumerate(data.entries):
str_entry = ""
str_entry += f"Entry n°{nb_entry}\n"
str_entry += f"Text{self.CHAR_SEP}{entry.text}\n"
str_entry += f"Input ID{self.CHAR_SEP}{entry.element_in_id}\n"
str_entry += f"Amount required{self.CHAR_SEP}{entry.amount_required}\n"
str_entry += f"Output ID{self.CHAR_SEP}{entry.element_out_id}\n"
str_entry += f"Amount received{self.CHAR_SEP}{entry.amount_received}\n"
str_entry += f"unk{self.CHAR_SEP}{entry.unk}\n"
str_output += str_entry
str_output += '-----------------------------------------------------------------\n'
with open(os.path.join(path_output, mbin.name + '.pandemona'), "w", encoding="utf8") as file:
file.write(str_output)
str_output = ""