-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.py
224 lines (174 loc) · 5.71 KB
/
generator.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
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
#######################################
# Darksoft Romset Generator by Aluzed #
# 2020 #
#######################################
import os
import sys
import re
import io
import hashlib
import glob
from os import walk
dir_path = os.path.dirname(os.path.realpath(__file__))
if 1 in sys.argv:
dir_path = sys.argv[1]
f = [] # List dir files
c_files = []
v_files = []
p_files = []
s_rom = m1_rom = ""
def clear_folder(folder_path):
files = glob.glob(folder_path + os.path.sep + '**', recursive=True)
for f in files:
try:
os.remove(f)
except OSError as e:
print("Error: %s : %s" % (f, e.strerror))
def concat_files(export_path, file_list):
data = ""
if len(file_list) > 0:
with open(export_path, "ab") as exported_rom:
for filename in file_list:
with open(dir_path + os.path.sep + filename, "rb") as file2:
print(filename + ' found')
exported_rom.write(file2.read())
file2.close()
exported_rom.close()
with open(export_path, "rb") as exported_rom:
data = exported_rom.read()
return data
# CROM must be concat 2 per 2
def split_per2(export_path, collection):
tmp_files = []
iter_cache = []
for item in collection:
iter_cache.append(item)
if len(iter_cache) > 0 and len(iter_cache) % 2 == 0:
tmp_path = "out" + str(len(tmp_files)) + ".bin"
join_crom0(dir_path + os.path.sep + tmp_path, iter_cache)
tmp_files.append(tmp_path)
iter_cache = []
data = concat_files(export_path, tmp_files)
clear_temp_files(tmp_files)
return data
def clear_temp_files(collection):
for item in collection:
try:
os.remove(dir_path + os.path.sep + item)
except OSError as e:
print("Error: %s : %s" % (f, e.strerror))
def copy_file(source, dest):
with open(dest, "ab") as destination_file:
with open(source, "rb") as source_file:
destination_file.write(source_file.read())
source_file.close()
destination_file.close()
def convert_roms():
global s_rom
global m1_rom
print('start rom conversion for ' + dir_path)
for (dirpath, dirnames, filenames) in walk(dir_path):
f.extend(filenames)
break
for filename in f:
# Get S Rom
if re.search(r'.s1$', filename) or re.search(r'[-_]s1.(rom|bin)', filename):
s_rom = filename
# Get M Rom
if re.search(r'.m1$', filename) or re.search(r'[-_]m1.(rom|bin)$', filename):
m1_rom = filename
# Get P Roms
if re.search(r'.p\d$', filename) or re.search(r'[-_]p\d.(rom|bin)$', filename):
p_files.append(filename)
# Get C Roms
if re.search(r'[-_]c\d.(rom|bin)$', filename) or re.search(r'.c\d$', filename):
c_files.append(filename)
# Get V Roms
if re.search(r'.v\d$', filename) or re.search(r'[-_]v\d.(rom|bin)$', filename):
v_files.append(filename)
# Second loop for SP files
for filename in f:
if re.search(r'.sp\d$', filename) or re.search(r'[-_]sp\d.(rom|bin)$', filename):
p_files.append(filename)
# Setting our export path
export_path = dir_path + os.path.sep + "export"
# Setting our SROM file path
srom_path = export_path + os.path.sep + "srom"
# Setting our M1ROM file path
m1rom_path = export_path + os.path.sep + "m1rom"
# Setting our CROM file path
crom_path = export_path + os.path.sep + "crom0"
# Setting our VROM file path
vrom_path = export_path + os.path.sep + "vroma0"
# Setting our PROM file path
prom_path = export_path + os.path.sep + "prom"
# Cascading make dir
if not os.path.exists(export_path):
os.makedirs(export_path)
else:
clear_folder(export_path)
# Handle S file
if s_rom != "":
copy_file(dir_path + os.path.sep + s_rom, srom_path)
# Handle M1 file
if m1_rom != "":
copy_file(dir_path + os.path.sep + m1_rom, m1rom_path)
# Handle C files
data = split_per2(crom_path, c_files)
if data == "":
print('something went wrong parsing c files')
else:
print('crom0 generated with SHA256 checksum : ' + hashlib.sha256(data).hexdigest())
print('crom0 generated with SHA1 checksum : ' + hashlib.sha1(data).hexdigest())
print('crom0 generated with MD5 checksum : ' + hashlib.md5(data).hexdigest())
# Handle V files
data = concat_files(vrom_path, v_files)
if data == "":
print('something went wrong parsing v files')
else:
print('vroma0 generated with SHA256 checksum : ' + hashlib.sha256(data).hexdigest())
print('vroma0 generated with SHA1 checksum : ' + hashlib.sha1(data).hexdigest())
print('vroma0 generated with MD5 checksum : ' + hashlib.md5(data).hexdigest())
# Handle P files
data = concat_files(prom_path, p_files)
if data == "":
print('something went wrong parsing v files')
else:
print('prom generated with SHA256 checksum : ' + hashlib.sha256(data).hexdigest())
print('prom generated with SHA1 checksum : ' + hashlib.sha1(data).hexdigest())
print('prom generated with MD5 checksum : ' + hashlib.md5(data).hexdigest())
def join_crom0(export_path, files):
print(files)
inputs = []
# Stop computing if array is empty
if len(files) == 0:
return None
for f in files:
fp = open(f, "rb")
inputs.append(fp)
output = open(export_path,"wb")
breakable = False
while breakable is False:
datarows = []
# Read 2 bytes from each file
for fp in inputs:
data = fp.read(2)
if not data:
breakable = True
else:
if breakable is False:
datarows.append(data)
# now inser them into the merged crom
if breakable is False:
for d in datarows:
output.write(d)
# close our new file
output.close()
# Launch /!\
try:
convert_roms()
except:
print (sys.exc_info()[0])
raise
print('job done, press a key to exit...')
input()