forked from EricGoldsteinNz/tadpole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzfbmagic.py
321 lines (228 loc) · 10.9 KB
/
zfbmagic.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
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
import sys
import os
import glob
import requests
import struct
import frogtool
import shutil
from PIL import Image
from PyQt5.QtWidgets import QMessageBox
def find_matching_ext(ifold , core,file_name,isRom):
fn = os.path.splitext(os.path.basename(file_name))[0]
input_path = os.path.join(ifold, fn)
#fname_noext = os.path.splitext(os.path.basename(input_folder))[0]
file_ext = os.path.splitext(file_name)[1][1:]
print("coming filename is " + file_name)
files = glob.glob(input_path + ".*" )
print("path to file is " + input_path)
print(files)
for x in glob.glob(input_path + ".*" ):
fname = os.path.splitext(os.path.basename(x))[0]
fext = os.path.splitext(x)[1][1:]
if fext != file_ext:
if isRom and fext not in ["bmp" , "png" , "gif" , "jpeg" , "jpg" ,"txt" , "pdf" , "sav" , "srm"]:
print("file except png found , extension is " + fext)
return fext
else:
if fext in ["bmp" , "png" , "gif" , "jpeg" , "jpg"]:
print("file for img found , extension is " + fext)
return fext
if not isRom:
print("Image file not found")
else:
print("Rom File not found")
return False
def zfb_from_null(core , file_name, output_folder , apptxt , pretxt , addext , existadd):
file_path = os.path.join(output_folder , file_name)
fname_noext = os.path.splitext(os.path.basename(file_path))[0]
file_ext = os.path.splitext(file_name)[1][1:]
print(f"File path : {file_path} \n File Name : {file_name}")
print (f"F no ext before : {fname_noext}")
fname = fname_noext + "." + file_ext
fname_noext = os.path.splitext(os.path.basename(file_name))[0]
print (f"F no ext after : {fname_noext}")
print(f"extenion is {file_ext}")
placeholder_data = b'\x00' * 0xEA00 + b'\x00\x00\x00\x00' + f"{core};{fname}.gba".encode('utf-8') + b'\x00\x00'
zfb_filename = os.path.join(output_folder , pretxt + fname_noext + apptxt + addext + existadd + '.zfb')
with open(zfb_filename, 'wb') as zfb:
zfb.write(placeholder_data)
return True
def zfb_from_image(img , input_folder , core , file_name, output_folder , apptxt , pretxt , addext , existadd):
thumb_size = (144, 208)
img = img.resize(thumb_size)
img = img.convert("RGB")
file_path = os.path.join(output_folder , file_name)
fname_noext = os.path.splitext(os.path.basename(file_path))[0]
file_ext = os.path.splitext(file_name)[1][1:]
raw_data = []
# Convert image to RGB565
for y in range(thumb_size[1]):
for x in range(thumb_size[0]):
r, g, b = img.getpixel((x, y))
rgb = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)
raw_data.append(struct.pack('H', rgb))
raw_data_bytes = b''.join(raw_data)
zfb_filename = os.path.join(output_folder , pretxt + fname_noext + apptxt + addext + existadd + '.zfb')
fname = fname_noext + "." + file_ext
# Write the image data to the .zfb file
try:
with open(zfb_filename, 'wb') as zfb:
# Fill with 00 bytes until offset 0xEA00
zfb.write(raw_data_bytes)
zfb.write(b'\x00\x00\x00\x00') # Write four 00 bytes
#zfb.write(f"{core};{os.path.splitext(file_name)[0]}.{extension}.gba".encode('utf-8')) # Write the modified filename
zfb.write(f"{core};{fname}.gba".encode('utf-8')) # Write the modified filename
print(f"Shortcut is : {core};{fname}.gba")
zfb.write(b'\x00\x00') # Write two 00 bytes
except Exception as e:
QMessageBox.information(self,"Error" , f"Error is {str(e)}")
return True
# progress , progresstype , ifexist
def create_zfb_files(wind ,wdir ,sdir , pdir , core, apptxt , pretxt , doRef , doExt , files , doMove , ifexist, prg , prgT ,justzfb = False):
if len(files) > 0:
input_fold = os.path.dirname(files[0])
input_folder = input_fold
else:
input_fold = wdir.strip()
input_folder = os.path.join(input_fold, "ROMS", core)
if justzfb:
input_folder = input_fold
output_folder = os.path.join(input_fold, "zfbs")
else:
#input_folder = os.path.join(input_fold , "ROMS\\" + core)
output_folder = os.path.join(wdir.strip(), sdir)
addext = ""
if doExt:
addext = "." + file_ext
core = core.strip()
pdir = pdir.strip()
rom_ext = False;
img_ext = False;
try:
if not os.path.isdir(os.path.join(output_folder)):
if not justzfb:
tdq = QMessageBox.question(wind,'Stock Directory', 'Selected Stock Folder doesnt Exist , Do you want to Create it?' , QMessageBox.Yes|QMessageBox.No)
if tdq == QMessageBox.Yes:
os.makedirs(output_folder)
QMessageBox.information(wind,"Directory" , "Directory Created")
else:
return
else:
os.makedirs(output_folder)
# Check if folders are selected
if not input_folder or not output_folder or not core: #or not extension:
if not justzfb:
QMessageBox.warning(wind,'Warning', 'Please fill in all the fields and select multicore rom dir and Stock dir.')
else:
QMessageBox.warning(wind,'Warning', 'Please fill in all fields , Select Input Folder and Enter the CORE name')
return
thumb_size = (144, 208)
rom_list = [];
if not files:
files = os.listdir(input_folder)
# Iterate over all files in the input folder
total_files = len(files)
print(f"Total files are {total_files}")
proc_files = 0
if not prgT:
prg.progress.reset()
prg.setText("Creating Rom Zfbs..")
progress = 0
prg.showProgress(progress, True)
else:
prg.show()
prg.setValue(0)
for file_name in files:
if os.path.isdir(os.path.join(input_folder , file_name)):
proc_files += 1
continue
file_path = os.path.join(input_folder, file_name)
fname_noext = os.path.splitext(os.path.basename(file_path))[0]
file_ext = os.path.splitext(file_name)[1][1:]
# Skip if file name has been processed
if fname_noext in rom_list:
print("skipping as name already in rom list")
proc_files += 1
continue
# if File is an Image , try to find a matching ROM extension
if file_ext in ["png" , "bmp" , "jpg" , "jpeg" , "gif"]:
rom_ext = find_matching_ext(input_folder , core , file_name , True)
if rom_ext:
#file_name = os.path.join(input_folder, fname_noext + "." +rom_ext)
file_name = fname_noext + "." +rom_ext
img_ext = file_ext
else:
rom_list.append(fname_noext)
proc_files += 1
continue
elif file_ext in ["txt" , "pdf" , "sav" , "srm" , "exe"]:
rom_list.append(fname_noext)
proc_files += 1
continue
else:
img_ext = find_matching_ext(input_folder , core ,file_name , False)
if img_ext:
file_path = os.path.join(input_folder, fname_noext + "." +img_ext)
rom_ext = file_ext
crn = fname_noext + "." + rom_ext
cri = os.path.join(input_folder, crn)
cro = os.path.join(wdir, "ROMS", core, crn)
if len(files) > 0 and not justzfb and doMove:
print("copying file from")
print(cri)
print("copying file to")
print(cro)
shutil.copyfile(cri , cro)
efname = pretxt + fname_noext + apptxt + addext + '.zfb'
existadd = ""
if os.path.exists(os.path.join(output_folder , efname)):
if ifexist == "Skip":
rom_list.append(fname_noext)
proc_files += 1
continue
elif ifexist == "Rename":
existadd = "_1"
if img_ext:
with Image.open(file_path) as img:
#zfb_from_image(img , input_folder , core , file_name , output_folder )
zfb_from_image(img , input_folder , core , file_name, output_folder , apptxt , pretxt , addext , existadd)
rom_list.append(fname_noext)
proc_files += 1
else:
tfp = os.path.join(os.getcwd(), "placeholders", pdir)
print("the placeholder dir n file is " + tfp)
try:
with Image.open(tfp) as img:
print("using the placeholder " + pdir)
zfb_from_image(img , input_folder , core , file_name, output_folder , apptxt , pretxt , addext , existadd )
print("png created with placeholder")
rom_list.append(fname_noext)
proc_files += 1
except:
zfb_from_null(core , file_name, output_folder , apptxt , pretxt , addext , existadd)
rom_list.append(fname_noext)
proc_files += 1
rom_ext = False
img_ext = False
if not prgT:
progress = (int(proc_files/total_files * 100))
prg.showProgress(progress, True)
prg.show()
else:
prg.setValue(int(proc_files/total_files * 100))
QMessageBox.information(wind,'Success', 'ZFB files created successfully.')
if doRef and not justzfb:
rebuildAll(wdir)
QMessageBox.information(wind,'Roms Refresh', 'All Rom lists have been refreshed.')
except Exception as e:
QMessageBox.critical(wind,'Error', f'An error occurred while creating the ZFB files: {str(e)}')
if not prgT:
prg.close()
else:
prg.hide()
def rebuildAll(wdir):
#frogtool.run(wdir, "ALL", "-sc")
#frogtool.process_sys(wdir, "ALL", False)
for console in frogtool.systems.keys():
result = frogtool.process_sys(wdir, console, False)
return