-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdkc3_texteditor.py
More file actions
642 lines (524 loc) · 24.1 KB
/
dkc3_texteditor.py
File metadata and controls
642 lines (524 loc) · 24.1 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# dkc3 text editor by koda v0.1
# CR32: 448EEC19 (HEADERLESS)
import argparse
import sys
import os
import re
import struct
def reverse_list(lst, n=None):
"""
Invierte los primeros n-1 elementos de la lista y deja el último en su lugar.
Args:
lst (list): Lista de elementos.
n (int, optional): Número de elementos a considerar para invertir.
Si no se da, se toma toda la lista.
Returns:
list: Lista con los primeros n-1 elementos invertidos y el último en su lugar.
"""
if not lst:
return []
if n is None or n > len(lst):
n = len(lst)
to_reverse = lst[:n-1]
last_elem = lst[n-1]
return to_reverse[::-1] + [last_elem]
class extraction:
def __init__(self):
pass
def read_rom(rom_file, addr, size):
"""
Reads a block of data from the ROM file.
Parameters:
addr (int): The starting address to read from.
size (int): The number of bytes to read.
Returns:
bytes: The data read from the ROM.
"""
with open(rom_file, "rb") as f:
f.seek(addr)
return f.read(size)
def read_tbl(tbl_file):
"""
Loads the TBL (table) file into a dictionary mapping byte sequences to characters.
The table format should be one mapping per line in the form:
HEX=Character(s)
Comments (lines starting with ';' or '/') and invalid lines are ignored.
Returns:
dict[bytes, str]: A mapping of byte sequences to their character representation.
"""
char_table = {}
with open(tbl_file, "r", encoding="UTF-8") as f:
for line in f:
if not line or line.startswith(";") or line.startswith("/"):
continue
if "=" in line:
hex_value, chars = line.split("=", 1)
try:
if len(hex_value) % 2 != 0:
print(f"Warning: '{hex_value}' is invalid! Skipped.")
continue
byte_key = bytes.fromhex(hex_value)
char_table[byte_key] = chars.strip("\n")
except ValueError:
print(f"Warning: '{hex_value}' is invalid! Skipped.")
return char_table
def read_ptr_table(data, base):
"""
Reads a pointer table from ROM data.
Each pointer entry is 4 bytes:
[0-1] Length (little endian)
[2-3] Position (little endian, add Base)
Args:
data (bytes): Raw ROM data containing the pointer table.
base (int): Base to CPU addrs.
Returns:
tuple: (positions, lengths)
positions (list[int]): Decoded pointer addresses.
lengths (list[int]): Corresponding lengths.
"""
positions = []
lengths = []
for i in range(0, len(data), 4):
entry = data[i:i+4]
if len(entry) < 4:
break
length = entry[0] | (entry[1] << 8)
pos = (entry[2] | (entry[3] << 8)) + base
lengths.append(length)
positions.append(pos)
return positions, lengths
def huffman_decompress(rom_file, tbl_dict, tree_start, tree_size, ptr_start, script_length):
def get_ushort(data, index):
return data[index] | (data[index + 1] << 8)
with open(rom_file, "rb") as f:
rom_data = f.read()
tree_base = tree_start + 2
results = []
for blk in range(len(ptr_start)):
pos = ptr_start[blk]
length = script_length[blk]
curr_node = get_ushort(rom_data, tree_start)
flags_addr = pos
flags_mask = 0
node_flags = 0
symbols_count = 0
output = []
current_line = ""
while symbols_count < length:
if (flags_mask >> 1) == 0:
flags_mask = 0x8000
node_flags = get_ushort(rom_data, flags_addr)
flags_addr += 2
else:
flags_mask >>= 1
right_node = (node_flags & flags_mask) == 0
node = get_ushort(rom_data, tree_base + curr_node + (2 if right_node else 0))
if node == 0:
symbol = rom_data[tree_base + curr_node - 1]
key = bytes([symbol])
val = tbl_dict.get(key)
if 1 <= symbol <= 4:
output.append(current_line)
current_line = ""
piece = val if val is not None else f"<{symbol:02X}>"
current_line += piece
else:
if val is not None:
current_line += val
else:
current_line += f"<{symbol:02X}>"
symbols_count += 1
curr_node = get_ushort(rom_data, tree_start)
flags_mask <<= 1
else:
curr_node = node
if current_line:
output.append(current_line)
if output and output[0] == "":
output.pop(0)
results.append(output)
return results
def write_out_file(file, script_text, pointers_list, lines_length):
"""
Writes data to a file, formatting each line with a semicolon and newline.
Parameters:
file (str): The path to the output file.
script_text (list): A list of strings representing the script content to write to the file.
pointers_start_address (int): The starting address of the pointer table.
pointer_table_size (int): The size of the pointer table.
address_list (list): A list of addresses corresponding to each line in the script.
lines_length (list): A list of the length of each line in the script.
"""
with open(file, "w", encoding='UTF-8') as f:
f.write(f";{{{pointers_list:08X}-{(pointers_list+lines_length-1):08X}-{lines_length:08X}}}\n")
i = 0
for line in script_text:
f.write(f"@{i+1}\n")
f.write(f";{{{line}}}\n")
f.write(f"{line}\n")
f.write("|\n")
i += 1
class insertion:
def __init__(self):
pass
def read_script(file):
"""
Reads a file containing the game's text and returns it as a single list (continuous script).
Parameters:
file (str): The path to the file to read.
Returns:
text_data (str): A single string containing all text elements merged from the script.
"""
with open(file, "r", encoding='UTF-8') as f:
lines = [
line.rstrip('\n') for line in f.readlines()
if not (line.startswith(";") or line.startswith("@") or line.startswith("|") or line.startswith("&"))
]
script = "".join(lines)
return script
def read_tbl(tbl_file):
"""
Reads a .tbl file to create a character mapping table (supports DTE/MTE, multibyte values).
Parameters:
tbl_file (str): The path to the .tbl file.
Returns:
tuple: Contains:
- char_table (dict): A dictionary where keys are bytes sequences and values are strings (characters or sequences).
- chars_lengths (set): Set array with chain char lengths.
"""
char_table = {}
chars_lengths = set()
with open(tbl_file, "r", encoding="UTF-8") as f:
for line in f:
if not line or line.startswith(";") or line.startswith("/"):
continue
if "=" in line:
hex_value, chars = line.split("=", 1)
chars = chars.strip('\n')
try:
if len(hex_value) % 2 != 0:
print(f"Warning: '{hex_value}' is invalid! Skipped.")
continue
byte_key = bytes.fromhex(hex_value)
char_table[chars] = byte_key
chars_lengths.add(len(chars))
except ValueError:
print(f"Warning: '{hex_value}' is invalid! Skipped.")
continue
chars_lengths = sorted([l for l in chars_lengths if l > 0], reverse=True)
return char_table, chars_lengths
def encode_text(blocks, char_table, max_char_len):
"""
Encodes a list of text blocks into bytearrays using a character table (supports multibyte mappings).
Recognizes <XX> sequences as raw byte values.
If an unmapped character is found, the process stops with an error.
Parameters:
blocks (list of str): List of text blocks to encode.
char_table (dict): Dictionary {str -> bytes} from the inverted .tbl.
max_char_len (int): Maximum length for multibyte sequences (from .tbl).
Returns:
list of bytearray: A list where each element is a bytearray representing an encoded block.
"""
hex_pattern = re.compile(r'<([0-9A-Fa-f]{2})>')
data_list = []
for block_index, block in enumerate(blocks, start=1):
block_data = bytearray()
idx = 0
while idx < len(block):
hex_match = hex_pattern.match(block, idx)
if hex_match:
byte_val = int(hex_match.group(1), 16)
block_data.append(byte_val)
idx += 4
continue
match = None
match_len = 0
for l in max_char_len:
if idx + l <= len(block):
chunk = block[idx:idx + l]
if chunk in char_table:
match = char_table[chunk]
match_len = l
break
if match:
block_data.extend(match)
idx += match_len
else:
problem_char = block[idx:idx + 1]
print(f"\n[ERROR] Unmapped character found at script {block_index}, position {idx}: '{problem_char}'")
print("Please check your .tbl file or input text.")
sys.exit(1)
data_list.append(block_data)
return data_list
def huffman_compress(encoded_blocks, rom_file, tree_start, tree_size, base):
def get_ushort(data, index):
return data[index] | (data[index + 1] << 8)
with open(rom_file, "rb") as f:
rom = f.read()
tree_base = tree_start + 2
root_node = get_ushort(rom, tree_start)
SymbolLUT = {}
TreeStack = []
CurrNode = root_node
Code = 0
Depth = 0
outer_break = False
while True:
TreeStack.append(CurrNode)
LeftNode = (Code & 1) != 0
if LeftNode:
CurrNode = get_ushort(rom, tree_base + CurrNode)
else:
CurrNode = get_ushort(rom, tree_base + CurrNode + 2)
if CurrNode == 0:
node_ref = TreeStack.pop()
symbol = rom[tree_base + node_ref - 1]
SymbolLUT[symbol] = (Depth, (Code >> 1) & 0xFFFF)
while True:
if len(TreeStack) == 0:
outer_break = True
break
CurrNode = TreeStack.pop()
Code >>= 1
Depth -= 1
if (Code & 1) == 0:
break
if outer_break:
break
Code |= 1
else:
Code <<= 1
Depth += 1
compressed_data = bytearray()
symbol_count = []
block_offsets = [base]
for block_idx, block in enumerate(encoded_blocks, start=1):
CurrWord = 0
CurrBits = 0
symbols_decoded = 0
block_bytes = bytearray()
for symbol in block:
if symbol not in SymbolLUT:
print(f"[ERROR] Script: {block_idx}, encounter Symbol: {symbol:02X}, not found in huffman tree.")
sys.exit(1)
Bits, CodeVal = SymbolLUT[symbol]
if CurrBits + Bits <= 16:
CurrWord = (CurrWord << Bits) & 0xFFFF
CurrWord |= (CodeVal & ((1 << Bits) - 1))
CurrBits += Bits
else:
SplitBits = 16 - CurrBits
SplitCode = (CodeVal >> (Bits - SplitBits)) & ((1 << SplitBits) - 1)
CurrWord = (CurrWord << SplitBits) & 0xFFFF
CurrWord |= SplitCode
block_bytes.append(CurrWord & 0xFF)
block_bytes.append((CurrWord >> 8) & 0xFF)
CurrBits = Bits - SplitBits
# CurrWord = Code & ((1 << CurrBits) - 1);
CurrWord = CodeVal & ((1 << CurrBits) - 1)
symbols_decoded += 1
if CurrBits != 0:
CurrWord = (CurrWord << (16 - CurrBits)) & 0xFFFF
block_bytes.append(CurrWord & 0xFF)
block_bytes.append((CurrWord >> 8) & 0xFF)
compressed_data.extend(block_bytes)
symbol_count.append(symbols_decoded)
symbols_decoded = 0
block_offsets.append(block_offsets[-1] + len(block_bytes))
# DEBBUG(IGNORE)
## if block_idx == 1:
## print(f"Block {block_idx} len={len(block_bytes)} bytes")
## print(block_bytes.hex(" ").upper())
block_offsets.pop()
return compressed_data, len(compressed_data), symbol_count, block_offsets
def create_4_bytes_pointers(script_size, script_offset):
"""
Creates a bytearray of 4-byte pointers combining size and offset values in little endian.
Parameters:
script_size (list[int]): List of block sizes (2-byte values).
script_offset (list[int]): List of block offsets (2-byte values).
Returns:
bytearray: Concatenated 4-byte pointers (size + offset), both in little endian.
"""
script_size = reverse_list(script_size)
script_offset = reverse_list(script_offset)
pointers = bytearray()
for size, offset in zip(script_size, script_offset):
size_le = size.to_bytes(2, "little")
offset_le = offset.to_bytes(2, "little")
pointers.extend(size_le + offset_le)
return pointers, len(pointers)
def write_rom(rom_file, start_offset, original_size, data, fill_free_space, fill_free_space_byte):
"""
Writes data to the ROM at the specified offset, filling any free space if requested.
Parameters:
rom_file (str): The path to the ROM file.
start_offset (int): The offset in the ROM file where data should be written.
original_size (int): The original size of the data to ensure there is enough space for the write operation.
data (bytes or bytearray): The data to write to the ROM.
fill_free_space (bool): Whether to fill the remaining space with a specific byte.
fill_free_space_byte (byte): The byte used to fill the remaining space.
Returns:
int: The amount of free space left after writing the data.
"""
free_space = int(original_size) - len(data)
if fill_free_space:
filled_data = data + bytes([fill_free_space_byte]) * free_space
else:
filled_data = data
with open(rom_file, "r+b") as f:
f.seek(start_offset)
f.write(filled_data)
return free_space
def main():
parser = argparse.ArgumentParser(
description="Donkey Kong Country 3 text editor by koda v0.1"
)
subparsers = parser.add_subparsers(dest="command", help="Commands")
# --- extract ---
extract_parser = subparsers.add_parser("extract", help="Extract text from ROM")
extract_parser.add_argument("-l", "--lang", default="en", choices=["en", "fr"],
help="Language (default: en)")
extract_parser.add_argument("-r", "--romFile", required=True,
help="ROM file path")
extract_parser.add_argument("-f", "--outFile", required=True,
help="Output text file")
extract_parser.add_argument("-t", "--tblFile", required=True,
help="Table (.tbl) file")
# --- insert ---
insert_parser = subparsers.add_parser("insert", help="Insert text into ROM")
insert_parser.add_argument("-l", "--lang", default="en", choices=["en", "fr"],
help="Language (default: en)")
insert_parser.add_argument("-r", "--romFile", required=True,
help="ROM file path")
insert_parser.add_argument("-f", "--inFile", required=True,
help="Input text file")
insert_parser.add_argument("-t", "--tblFile", required=True,
help="Table (.tbl) file")
# Version
#parser.add_argument("-v", "--version", action="version",
#version=f"%(prog)s {VERSION}")
args = parser.parse_args()
if args.command is None:
parser.print_help()
sys.exit(1)
if args.command == "extract":
rom_file = args.romFile
tbl_file = args.tblFile
out_file = args.outFile
lang = args.lang
if lang == 'en':
PTR_START_OFFSET = 0x379DF5
PTR_END_OFFSET = 0x379E44
PTR_SIZE = PTR_END_OFFSET - PTR_START_OFFSET + 1
TREE_START_OFFSET = 0x379EE5
TREE_END_OFFSET = 0x37A1E4
TREE_SIZE = TREE_END_OFFSET - TREE_START_OFFSET + 1
BASE = 0x3A0000
TEXT_START_OFFSET = 0x3A0000
TEXT_END_OFFSET = 0x3A5392
TEXT_SIZE = TEXT_END_OFFSET - TEXT_START_OFFSET + 1
elif lang == 'fr':
PTR_START_OFFSET = 0x379E45
PTR_END_OFFSET = 0x379E94
PTR_SIZE = PTR_END_OFFSET - PTR_START_OFFSET + 1
TREE_START_OFFSET = 0x37A1E5
TREE_END_OFFSET = 0x37A570
TREE_SIZE = TREE_END_OFFSET - TREE_START_OFFSET + 1
BASE = 0x3A5393
TEXT_START_OFFSET = 0x3A5393
TEXT_END_OFFSET = 0x3AA1B2
TEXT_SIZE = TEXT_END_OFFSET - TEXT_START_OFFSET + 1
else:
print("Invalid Language Exiting...")
sys.exit(1)
# Load class
extract = extraction()
# Load Tbl
tbl_dict = extraction.read_tbl(tbl_file)
# Get Pointers
ptr_table = extraction.read_rom(rom_file, PTR_START_OFFSET, PTR_SIZE)
# Split ptr and lenghts
ptr_array, length_array = extraction.read_ptr_table(ptr_table, BASE)
ptr_array = reverse_list(ptr_array)
length_array = reverse_list(length_array)
# Decomprees
decompress_blocks = extraction.huffman_decompress(rom_file, tbl_dict, TREE_START_OFFSET, TREE_SIZE, ptr_array, length_array)
# Write script
base_out_file = out_file
for i, block_text in enumerate(decompress_blocks, start=1):
out_file_i = f"{base_out_file}_{lang}_{i}.txt"
extraction.write_out_file(out_file_i, block_text, ptr_array[i-1], length_array[i-1])
print(f"Text extracted to {out_file_i}")
print(f"TEXT BLOCK SIZE: {TEXT_SIZE} / {hex(TEXT_SIZE)} bytes.")
print(f"PTR_TABLE BLOCK SIZE: {PTR_SIZE} / {hex(PTR_SIZE)} bytes.")
print("Extraction complete.\n")
elif args.command == "insert":
rom_file = args.romFile
tbl_file = args.tblFile
script_file = args.inFile
lang = args.lang
if lang == 'en':
PTR_START_OFFSET = 0x379DF5
PTR_END_OFFSET = 0x379E44
PTR_SIZE = PTR_END_OFFSET - PTR_START_OFFSET + 1
#75 different characters
TREE_START_OFFSET = 0x379EE5
TREE_END_OFFSET = 0x37A1E4
TREE_SIZE = TREE_END_OFFSET - TREE_START_OFFSET + 1
BASE = 0x0
TEXT_START_OFFSET = 0x3A0000
TEXT_END_OFFSET = 0x3A5392
TEXT_SIZE = TEXT_END_OFFSET - TEXT_START_OFFSET + 1
elif lang == 'fr':
PTR_START_OFFSET = 0x379E45
PTR_END_OFFSET = 0x379E94
PTR_SIZE = PTR_END_OFFSET - PTR_START_OFFSET + 1
#90 different characters
TREE_START_OFFSET = 0x37A1E5
TREE_END_OFFSET = 0x37A570
TREE_SIZE = TREE_END_OFFSET - TREE_START_OFFSET + 1
BASE = 0x0
TEXT_START_OFFSET = 0x3A5393
TEXT_END_OFFSET = 0x3AA1B2
TEXT_SIZE = TEXT_END_OFFSET - TEXT_START_OFFSET + 1
TEXT_SIZE = 0x6000
else:
print("Invalid Language Exiting...")
sys.exit(1)
# Load class
insert = insertion()
# Load Tbl
tbl_dict, byte_lenghts = insertion.read_tbl(tbl_file)
# Read Script
base_in_file = script_file
all_scripts = []
for i in range(1, 21):
script_path = f"{base_in_file}_{i}.txt"
script_data = insertion.read_script(script_path)
all_scripts.append(script_data)
# Encode Scripts
encoded_scripts = insertion.encode_text(all_scripts, tbl_dict, byte_lenghts)
# Compress
compress_script, compress_script_raw_size, scripts_lengths, script_offsets = insertion.huffman_compress(encoded_scripts, rom_file, TREE_START_OFFSET, TREE_SIZE, BASE)
# Create 4 byte pointer
new_ptr_table_raw_bytes, new_ptr_table_raw_bytes_size = insertion.create_4_bytes_pointers(scripts_lengths, script_offsets)
# Check raw bytes size
if compress_script_raw_size > TEXT_SIZE:
print(f"\nERROR: script size has exceeded its maximum size. Remove {compress_script_raw_size - TEXT_SIZE} bytes of excess in the block.")
sys.exit(1)
if new_ptr_table_raw_bytes_size > PTR_SIZE:
print(f"\nERROR: table pointer size has exceeded its maximum size. Remove {new_ptr_table_raw_bytes_size - PTR_SIZE} excess bytes.")
sys.exit(1)
# Write data to ROM and print summary
script_freespace = insertion.write_rom(rom_file, TEXT_START_OFFSET, TEXT_SIZE, compress_script, False, 0xFF)
print(f"Script text written to address {hex(TEXT_START_OFFSET)}, {script_freespace} bytes free.")
ptrs_freespace = insertion.write_rom(rom_file, PTR_START_OFFSET, PTR_SIZE, new_ptr_table_raw_bytes, False, 0xFF)
print(f"Pointer table written to address {hex(PTR_START_OFFSET)}, {ptrs_freespace//4} lines/pointers left.")
else:
sys.stdout.write("Usage: extract <romFile> <outFile> <tblFile>\n")
sys.stdout.write(" insert <outFile> <romFile> <tblFile>\n")
sys.stdout.write(" -v show version.\n")
sys.exit(1)
if __name__ == "__main__":
main()