-
Notifications
You must be signed in to change notification settings - Fork 0
/
EWJ2-quiz-builder-MSX.py
147 lines (106 loc) · 4.35 KB
/
EWJ2-quiz-builder-MSX.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
import os
import struct
from itertools import cycle
from tkinter import filedialog
from math import floor
txt_file = ""
txt_file=filedialog.askopenfilename(initialdir=os.getcwd(), title="Choose TXT File" ,filetype=(('TXT file', '*.txt'),("ALL file",'*.*')))
if len(txt_file) != 0:
txt_open = open(txt_file, "r")
quiz_data= txt_open.readlines()
txt_open.close()
i=len(txt_file)- 1
short_name_file = ""
while i!= 0 and txt_file[i] != '.':
i -= 1
i -= 1
while i!= 0 and txt_file[i] != '/':
short_name_file = txt_file[i] + short_name_file
i -= 1
ROM_file = ""
ROM_file=filedialog.askopenfilename(initialdir=os.getcwd(), title="Choose ROM File" ,filetype=(('MD file', '*.md'), ('BIN file', '*.bin'),("ALL file",'*.*')))
if len(ROM_file) != 0:
ROM_open = open(ROM_file, "rb")
ROM_data = ROM_open.read()
ROM_open.close()
new_line= bytearray()
data_bin = bytearray()
footer_pointer = [1242]
footer_bin = bytearray()
button_bit = [b'\x18', b'\x19', b'\x1A']
for n in range (0, len(quiz_data)-1):
quiz_data[n] = quiz_data[n][:-1]
for n in range(0, len(quiz_data), 5):
new_line = bytearray()
new_line += struct.pack("B", 20 + int(quiz_data[n+1]))
per_count = 0
for y in range (0,5):
per_count += quiz_data[n + y].count("%")
if per_count < 8 :
new_line += b'\x07'
new_line += b'\x01'
len_list = []
ln = 0
for x in range(0, len(quiz_data[n])):
ln += 1
if quiz_data[n][x] == "%":
len_list.append(ln)
ln = 0
len_list.append(ln)
new_line += struct.pack("B", 1 + floor((30 - len_list[0]) / 2))
xl = 1
for x in range(1,len(quiz_data[n])):
if quiz_data[n][x] == "%":
new_line += b'\x07\x01'
new_line += struct.pack("B", 1 + floor((30 - len_list[xl]) / 2))
xl += 1
else:
new_line += bytes(quiz_data[n][x].upper().encode("utf-8"))
if per_count <= 1:
new_line += b'\x07\x07'
else:
new_line += b'\x07'
button_num = 1
prop_len = 0
for y in range (3,5):
for z in range(len(quiz_data[n + y])):
prop_len += 1
space_opt = 1
if prop_len > 3:
space_opt = 1
else:
space_opt = 5
while button_num < 4:
new_line += b'\x07\x07\x01' + struct.pack("B", 9 + space_opt) + button_bit[button_num - 1]
for x in range(0,len(quiz_data[n+button_num+1])):
if quiz_data[n+button_num+1][x] == "%":
new_line += b'\x07\x01\x0B'
else:
new_line += bytes(quiz_data[n+button_num+1][x].upper().encode("utf-8"))
button_num += 1
new_line += b'\x00'
footer_pointer.append(len(new_line)+footer_pointer[-1])
data_bin += new_line
footer_pointer = footer_pointer[:-1]
for n in range(len(footer_pointer)):
footer_bin += b'\x00\x27' + struct.pack(">L", footer_pointer[n])[2:]
if len(footer_bin)/4 > 73:
print("There are over 73 questions")
if len(data_bin) < 8286:
data_bin += bytes(8286 - len(data_bin))
out_file = open(short_name_file + " Bin Data.bin", "wb+")
out_file.write(data_bin + footer_bin)
out_file.close()
print("Bin Data Done")
if len(data_bin) + len(footer_bin) < 8579 :
new_ROM_data = ROM_data[:2557146] + data_bin + footer_bin + ROM_data[2565724:]
out_file = open("Earthworm Jim 2 MOD + " + short_name_file + ".md", "wb+")
out_file.write(new_ROM_data)
out_file.close()
print("New ROM File Done")
else:
print("Thus you can't add it in the ROM but have a bin file!")
else:
print("No ROM file selected")
else:
print("No file choosen")