forked from Zenith-Team/Pyamoto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_spritemap.py
More file actions
65 lines (48 loc) · 2.01 KB
/
Copy pathread_spritemap.py
File metadata and controls
65 lines (48 loc) · 2.01 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
#!/usr/bin/env python3
import sys
import struct
from id_manager import SpritemapVersion
# TODO: Make this a window in the GUI
def read_spritemap(file_path):
try:
with open(file_path, 'rb') as f:
data = f.read()
except FileNotFoundError:
print(f"Error: File not found at {file_path}", file=sys.stderr)
sys.exit(1)
if not data:
print("spritemap.bin is empty.")
return
try:
if len(data) < 8:
raise ValueError("Data too short for header")
version, count = struct.unpack('>II', data[:8])
if version != SpritemapVersion:
print(f"Warning: Expected SpritemapVersion {SpritemapVersion}, but got {version}.", file=sys.stderr)
print(f"Sprite Map (Version {version}) contains {count} entries:")
if count == 0:
return
offset_table_start = 8
offsets = []
for i in range(count):
ptr = offset_table_start + (i * 4)
str_offset, = struct.unpack('>I', data[ptr:ptr+4])
offsets.append(str_offset)
current_id = 0xF000
for offset in offsets:
if offset >= len(data):
raise IndexError(f"String offset 0x{offset:X} is out of bounds")
null_terminator_index = data.find(b'\x00', offset)
if null_terminator_index == -1:
raise ValueError("Malformed spritemap entry: missing null terminator")
utf8_string_id = data[offset:null_terminator_index].decode('utf-8')
print(f" {current_id} (0x{current_id:04X}): {utf8_string_id}")
current_id += 1
except (struct.error, IndexError, ValueError, UnicodeDecodeError) as e:
print(f"Error parsing spritemap.bin: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python read_spritemap.py <path_to_spritemap.bin>", file=sys.stderr)
sys.exit(1)
read_spritemap(sys.argv[1])