-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathguitool.py
More file actions
264 lines (204 loc) · 9.56 KB
/
guitool.py
File metadata and controls
264 lines (204 loc) · 9.56 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import sys
import pathlib
import os
import json
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QFileDialog, QMessageBox, QDesktopWidget)
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QDesktopServices
from file_formats import scb
import streamutility
import io
from file_formats import scb
from file_formats import msg
from file_formats import scb0
export_directory = "./output"
def filter_null_chars(obj):
if isinstance(obj, str):
return obj.replace('\u0000', '')
elif isinstance(obj, list):
return [filter_null_chars(item) for item in obj]
elif isinstance(obj, dict):
return {key: filter_null_chars(value) for key, value in obj.items()}
else:
return obj
def exportJSON(script: scb.Scb, file: pathlib.Path):
json_file = {"filename": file.name, "strings": []}
for ds in script.msg_block.dialogue_strings_block.dialogue_strings:
json_file["strings"].append(ds.body)
global export_directory
if not os.path.exists(export_directory):
os.makedirs(export_directory)
json_file = filter_null_chars(json_file)
json_output_path = pathlib.Path(export_directory) / f"{file.stem}.json"
with open(json_output_path, "w", encoding="utf-16") as f:
json.dump(json_file, f, ensure_ascii=False, indent=1)
return json_output_path
def importJSON(file: pathlib.Path) -> json:
json_file_path = file.parent / f'{file.name}'
with open(json_file_path, 'r', encoding='utf-16') as f:
json_file = json.load(f)
f.close()
return json_file
def extractSCB(filename, old_script: scb.Scb):
global export_directory
scb0_file_path = export_directory + '/'+ f'{filename}.scb0'
new_scb0 = open(scb0_file_path, "+wb")
new_scb0.write(old_script.header_cache.files[0].file)
new_scb0.flush()
return new_scb0
def injectTranslation(new_SCB0, translated_dialogue_json):
global export_directory
newMSGBlock = msg.constructMSGBlock(new_SCB0, translated_dialogue_json)
new_SCB0_file_path = f'{new_SCB0.name}.translated'
new_script = open(new_SCB0_file_path, "+wb")
file_SCB0 = scb0.Scb0.from_file(new_SCB0.name)
new_script.write(file_SCB0.header)
# Write header of sections for new SCB file
new_section_offset = 0
for section in file_SCB0.sections:
label = section.label
len_section = section.len_section
if new_section_offset == 0:
new_section_offset = section.ofs_section
if section.label[0:3] == 'MSG':
len_section = len(newMSGBlock.getvalue()) # gets length of new MSG block
streamutility.writeStrToLong(new_script, label)
streamutility.writePadding(new_script, 4, streamutility.Padding.post_MSG_padding)
streamutility.writeHexToLong(new_script, len_section)
streamutility.writeHexToLong(new_script, new_section_offset)
streamutility.writePadding(new_script, 16, streamutility.Padding.post_MSG_padding)
new_section_offset += len_section
# Write section data
for section in file_SCB0.sections:
block = section.block
if section.label[0:3] == 'MSG':
block = newMSGBlock.getvalue()
new_script.write(block)
new_script.flush()
return new_script
def writeSCB(filename, old_script: scb.Scb, new_SCB0):
global export_directory
new_script_path = export_directory + '/'+ f'{filename}.translated.scb'
new_script = open(new_script_path, "+wb")
# writeIV(new_script)
writePAC(old_script, new_script, new_SCB0)
def writeIV(new_script):
# initialization_vector = old_script.initialization_vector
# new_script.write(initialization_vector)
blank_iv = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
new_script.write(blank_iv)
def writePAC(old_script: scb.Scb, new_script: io.BufferedRandom, new_SCB0: io.BufferedRandom):
# PAC Header
new_script.write(old_script.header_cache.header)
streamutility.writeHexToLong(new_script, old_script.header_cache.num_files)
new_script.write(old_script.header_cache.header_cache_padding)
streamutility.writeHexToLong(new_script, old_script.header_cache.ofs_entry)
streamutility.writeHexToLong(new_script, old_script.header_cache.ofs_msg)
streamutility.writeHexToLong(new_script, old_script.header_cache.ofs_file)
new_script.write(old_script.header_cache.header_cache_padding_2)
new_file_offset = 0
for i in range(0, len(old_script.header_cache.files)):
file: scb.Scb.PacFile
file = old_script.header_cache.files[i]
file_length = file.len_file
new_script.write(file.filesize_padding)
if i == 0: # SCB file length, from new SCB0
file_length = new_SCB0.tell()
streamutility.writeHexToLong(new_script, file_length)
streamutility.writeHexToLong(new_script, new_file_offset)
streamutility.writeHexToLong(new_script, file.fn_index)
streamutility.writeHexToLong(new_script, file.fp_index)
new_script.write(file.file_meta_padding)
new_file_offset += file_length
# PAC files
new_SCB0.seek(0)
new_script.write(new_SCB0.read())
new_script.write(old_script.scb_padding)
def createSCB(jsonfile: pathlib.Path):
global export_directory
if not os.path.exists(export_directory):
os.makedirs(export_directory)
translated_dialogue_json = importJSON(jsonfile)
old_script = scb.Scb.from_file(jsonfile.parent / f'{jsonfile.stem}.scb')
new_SCB0 = extractSCB(jsonfile.stem, old_script)
newSCB0translated = injectTranslation(new_SCB0, translated_dialogue_json)
writeSCB(jsonfile.stem, old_script, newSCB0translated)
class SCBExporter(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('IM@S-2 SCB Converter')
self.setFixedSize(600, 400)
self.layout = QVBoxLayout()
self.scb_file_label = QLabel('No SCB file selected')
self.scb_file_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.scb_file_label)
self.json_file_label = QLabel('No JSON file selected')
self.json_file_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.json_file_label)
self.select_scb_button = QPushButton('Select SCB File')
self.select_scb_button.clicked.connect(self.select_scb_file)
self.layout.addWidget(self.select_scb_button)
self.select_json_button = QPushButton('Select JSON File')
self.select_json_button.clicked.connect(self.select_json_file)
self.layout.addWidget(self.select_json_button)
self.export_button = QPushButton('Export SCB to JSON')
self.export_button.clicked.connect(self.process_scb_file)
self.export_button.setEnabled(False)
self.layout.addWidget(self.export_button)
self.create_scb_button = QPushButton('Create SCB from JSON')
self.create_scb_button.clicked.connect(self.process_json_file)
self.create_scb_button.setEnabled(False)
self.layout.addWidget(self.create_scb_button)
self.open_output_button = QPushButton('Open Output Directory')
self.open_output_button.clicked.connect(self.open_output_directory)
self.layout.addWidget(self.open_output_button)
self.setLayout(self.layout)
self.center_window()
def center_window(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def select_scb_file(self):
scb_filename, _ = QFileDialog.getOpenFileName(self, "Select SCB File", "", "SCB Files (*.scb);;All Files (*)")
if scb_filename:
self.scb_file = pathlib.Path(scb_filename)
self.scb_file_label.setText(f"Selected SCB File: {self.scb_file.name}")
self.export_button.setEnabled(True)
def select_json_file(self):
json_filename, _ = QFileDialog.getOpenFileName(self, "Select JSON File", "", "JSON Files (*.json);;All Files (*)")
if json_filename:
self.json_file = pathlib.Path(json_filename)
self.json_file_label.setText(f"Selected JSON File: {self.json_file.name}")
self.create_scb_button.setEnabled(True)
def process_scb_file(self):
if hasattr(self, 'scb_file'):
try:
script = scb.Scb.from_file(self.scb_file)
output_path = ".\\"+str(exportJSON(script, self.scb_file))
QMessageBox.information(self, "Success", f"Exported to {output_path}")
except Exception as e:
QMessageBox.critical(self, "Error", f"An error occurred: {str(e)}")
def process_json_file(self):
if hasattr(self, 'json_file'):
try:
createSCB(self.json_file)
QMessageBox.information(self, "Success", f"SCB file created from {self.json_file.name}")
except Exception as e:
QMessageBox.critical(self, "Error", f"An error occurred: {str(e)}")
def open_output_directory(self):
global export_directory
if not os.path.exists(export_directory):
os.makedirs(export_directory)
output_path = pathlib.Path(export_directory)
if not output_path.exists():
QMessageBox.warning(self, "Warning", f"Output directory does not exist: {output_path}")
else:
QDesktopServices.openUrl(QUrl.fromLocalFile(str(output_path)))
def main():
app = QApplication(sys.argv)
window = SCBExporter()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()