-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm_util.py
107 lines (96 loc) · 2.87 KB
/
rm_util.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
import os
import zipfile
# Console Titles
console_gb = "Game Boy"
console_megadrive = "Sega Mega Drive"
console_nes = "NES"
console_snes = "SNES"
# Dictionary that includes supported ROM extensions.
# If an extension is used by multiple devices it is set to a blank string ""
supported_rom_ext = {
".bin" : "",
".bkp" : "",
".dx2" : "",
".fds" : console_nes,
".fig" : console_snes,
".gb" : console_gb,
".gba" : "GBA",
".gbc" : "GBC",
".gcm" : "GameCube",
".gd3" : console_snes,
".gd7" : console_snes,
".gen" : console_snes,
".md" : console_megadrive,
".nes" : console_nes,
".nez" : console_nes,
".sfc" : console_snes,
".smc" : console_snes,
".smd" : console_megadrive,
".sms" : "Sega Master System",
".zfb" : "Arcade (Final Burn)",
".zfc" : console_nes,
".zgb" : "", # Used by SF2000 for Game Boy, Game Boy Color, and Game Boy Advance
".zip" : "",
".zmd" : console_megadrive,
".zsf" : console_snes
}
supported_img_ext = [
".gif",
".jpg",
".jpeg",
".png"
]
supported_sav_ext = [
".sav", # Generic save ext used almost everywhere
".sa0",
".sa1",
".sa2",
".sa3",
".srm"
]
def detectConsoleFromROM(romPath):
#If its a zip file peek inside
if(romPath.lower().endswith(".zip")):
romPath = getBiggestFileFromZip(romPath)
if romPath == "":
return "";
# Check for single use filenames first
ext = os.path.splitext(romPath)[1].lower()
console = supported_rom_ext.get(ext,"")
if console != "":
return console
# TODO for zgb check the file size?
# TODO if the extension is a zip or zip subtype then peek inside
return ""
def convertToHumanReadableFilesize(filesize : int) -> str:
humanReadableFileSize = "ERROR"
if filesize is None:
humanReadableFileSize = ""
elif filesize > 1024*1024: #More than 1 Megabyte
humanReadableFileSize = f"{round(filesize/(1024*1024),2)} MB"
elif filesize > 1024: #More than 1 Kilobyte
humanReadableFileSize = f"{round(filesize/1024,2)} KB"
else: #Less than 1 Kilobyte
humanReadableFileSize = f"{filesize} Bytes"
def check_is_game_file(filePath):
#If its a zip file peek inside
if(filePath.lower().endswith(".zip")):
filePath = getBiggestFileFromZip(filePath)
for ext in supported_rom_ext:
if filePath.lower().endswith(ext):
return True
return False
def check_is_save_file(filePath):
for ext in supported_sav_ext:
if filePath.lower().endswith(ext):
return True
return False
def getBiggestFileFromZip(zipPath):
zipFileList = zipfile.ZipFile(zipPath).infolist()
biggest = ""
biggestSize = 0
for f in zipFileList:
if f.file_size > biggestSize:
biggest = f.filename
biggestSize = f.file_size
return biggest