forked from Darxoon/scriptstuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·79 lines (64 loc) · 1.83 KB
/
main.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
#!/bin/env python3
from array import array
from struct import unpack
from sys import argv
from typing import TypeVar
from functions import write_functions
from tables import write_tables
from variables import write_variables
T = TypeVar('T')
def read_ksm_container(file: bytes) -> list[bytes]:
header = list(unpack('4siiiiiiiiii', file[:0x2c]))
assert header[0] == b'KSMR'
assert header[1] == 0x10300
assert header[10] == 0
header[10] = len(file) // 4
# god python can be so beautiful
sections = [file[start * 4:end * 4] for start, end in zip(header[2:], header[3:])]
return sections
def print_raw_integers(section: bytes) -> str:
arr = array('I', section)
out = ""
for n in arr:
if n == 0:
out += f" - 0\n"
else:
out += f" - 0x{n:x}\n"
return out
def main():
if len(argv) == 1 or argv[1] == '--help' or argv[1] == '-h':
print("Sticker Star KSM Script Dumper")
print("Usage: main.py <input file.bin>")
return
with open(argv[1], 'rb') as f:
input_file = f.read()
# predefined symbol ids for expressions (incomplete)
symbol_ids = {
0x3f: 'next_function',
#0x40: exit,
0x41: '(',
0x42: ')',
0x43: '||',
0x44: '&&',
0x4a: '==',
0x4b: '!=',
0x4c: '>',
0x4d: '<',
0x4e: '>=',
0x4f: '<=',
0x53: '+',
0x54: '-',
0x55: '*',
0x56: '/',
}
sections = read_ksm_container(input_file)
write_variables(sections, symbol_ids)
write_tables(sections, symbol_ids)
write_functions(sections, symbol_ids)
# # section 0 (mysterious)
# out_str = 'section_0:\n'
# out_str += print_raw_integers(sections[0])
# with open(argv[1] + '.yaml', 'w') as f:
# f.write(out_str)
if __name__ == '__main__':
main()