-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetroManagerDevice.py
More file actions
231 lines (181 loc) · 9.61 KB
/
RetroManagerDevice.py
File metadata and controls
231 lines (181 loc) · 9.61 KB
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
import os
import configparser
import logging
import shutil
from RetroManagerDatabase import rmGame, rmSave
import rm_util
class RetroManagerDevice():
_static_ConfigFileName = "RetroManager.conf"
# [General]
_static_general = "General"
_static_general_devicename = "Name"
_static_general_devicename_DEFAULT = "Unnamed Device"
_static_general_deviceType = "Type"
_static_general_deviceType_DEFAULT = "Generic"
# [ROMLocations]
_static_ROMLocations = "ROMLocations"
_static_ROMLocations_DEFAULT = "DEFAULT"
_static_ROMLocations_DEFAULT_DEFAULT = "ROMS"
_static_ROMLocations_GB = "Nintendo - Game Boy"
_static_ROMLocations_GB_DEFAULT = "GB"
_static_ROMLocations_GBC = "Nintendo - Game Boy Color"
_static_ROMLocations_GBC_DEFAULT = "GBC"
_static_ROMLocations_GBA = "Nintendo - Game Boy Advance"
_static_ROMLocations_GBA_DEFAULT = "GBA"
_static_ROMLocations_NES = "Nintendo - Nintendo Entertainment System"
_static_ROMLocations_NES_DEFAULT = "FC"
_static_ROMLocations_SNES = "Nintendo - Super Nintendo Entertainment System"
_static_ROMLocations_SNES_DEFAULT = "SFC"
_static_ROMLocations_GG = "Sega - Game Gear"
_static_ROMLocations_GG_DEFAULT = "GG"
_static_ROMLocations_MD = "Sega - Mega Drive - Genesis"
_static_ROMLocations_MD_DEFAULT = "MD"
_static_ROMLocationsDictionary = {
_static_ROMLocations_GB : _static_ROMLocations_GB_DEFAULT,
_static_ROMLocations_GBC : _static_ROMLocations_GBC_DEFAULT,
_static_ROMLocations_GBA : _static_ROMLocations_GBA_DEFAULT,
_static_ROMLocations_NES : _static_ROMLocations_NES_DEFAULT,
_static_ROMLocations_SNES : _static_ROMLocations_SNES_DEFAULT,
_static_ROMLocations_GG : _static_ROMLocations_GG_DEFAULT,
_static_ROMLocations_MD : _static_ROMLocations_MD_DEFAULT,
}
# [SaveLocations]
_static_SaveLocations = "SaveLocations"
name = ""
mountpoint = ""
def __init__(self, mountpoint):
super().__init__()
print(f"RetroManagerDevice~Init: Opening Device {mountpoint}")
self.mountpoint = mountpoint
self.configFilePath = os.path.join(mountpoint, self._static_ConfigFileName)
self.config = configparser.ConfigParser()
#Check if config file exists on the device
if not os.path.exists(self.configFilePath):
#Config file not found, create a default one
self.createDefaultConfig()
self.setDeviceName(mountpoint)
#Double check that the config file now exists
if not os.path.exists(self.configFilePath):
#TODO replace this with a proper exception
raise Exception
self.config.read(self.configFilePath)
def isRetroManagerDrive(mountpoint):
configFile= os.path.join(mountpoint, RetroManagerDevice._static_ConfigFileName)
if os.path.exists(configFile):
return True
return False
def scanForGames(self):
print(f"Tryng to scan for games")
gamesList = []
for folder_name, sub_folders, file_names in os.walk(self.mountpoint):
for filename in file_names:
# Rebuild the filepath
file_path = os.path.join(folder_name, filename)
# Filter for save files
if rm_util.check_is_game_file(file_path):
#print(f"Found game: {folder_name} ; {filename}")
# Strip the extension to create a title
title = os.path.splitext(filename)[0]
# Create the rmGame Objects
gamesList.append(rmGame(-1, title, file_path, rm_util.detectConsoleFromROM(file_path)))
return gamesList
def sendGameToDevice(self, game :rmGame):
#if len(gameslist) == 0:
# print("RetroManagerDevice~sendGamestoDevice: Cant send an empty list of games mate")
print(f"Sending ({game.title}) to ({self.name})")
destination = self.getROMLocations_DEFAULT()
if game.console.lower() == rm_util.console_gb.lower(): #Game Boy
destination = self.getROMLocations_GameBoy()
# Copy the game to the device
shutil.copy(game.filePath,os.path.join(self.mountpoint, destination))
# Copy the thumbnail
cover_path = os.path.splitext(game.filePath)[0]
for ext_img in rm_util.supported_img_ext:
imgFile = cover_path + ext_img
if os.path.exists(imgFile):
shutil.copy(imgFile, os.path.join(self.mountpoint, destination))
continue
# TODO Copy the saves
"""
"""
def scanForSaves(self):
logging.info(f"RetroManagerDevice~scanForSaves: ({self.name})@({self.mountpoint})")
# Find the saves on the device
savesList = []
for folder_name, sub_folders, file_names in os.walk(self.mountpoint):
for filename in file_names:
# Filter for save files
if rm_util.check_is_save_file(filename):
# Strip the extension to create a title
title = os.path.splitext(filename)[0]
# Rebuild the filepath
file_path = os.path.join(folder_name, filename)
# Create the rmGame Objects
# gameID, saveFilePath, saveFormat=""
savesList.append(rmSave(-1, file_path, ""))
return savesList
def sendSaveToDevice(self, save:rmSave):
logging.info(f"RetroManagerDevice~sendSaveToDevice: {save.title}")
def createDefaultConfig(self):
"""
WARNING: Be careful when calling this function as it will overwrite the
configuration file in the provided path if one exists.
"""
# Clear the running config
self.config = configparser.ConfigParser()
# We dont need to create the sections, error handling in setVariable will create those for us
self.setDeviceName(self._static_general_devicename_DEFAULT)
self.setDeviceType(self._static_general_deviceType_DEFAULT)
self.setROMLocations_DEFAULT(self._static_ROMLocations_DEFAULT_DEFAULT)
self.setROMLocations_GameBoy(self._static_ROMLocations_GB_DEFAULT)
self.setROMLocations_GameBoyColor(self._static_ROMLocations_GBC_DEFAULT)
self.setROMLocations_GameBoyAdvance(self._static_ROMLocations_GBA_DEFAULT)
self.saveConfig()
def saveConfig(self):
with open(self.configFilePath, 'w') as newConfigFile:
self.config.write(newConfigFile)
def setVariable(self, section, option, value):
try:
#Check the section exists, if it doesnt then create it
if not self.config.has_section(section):
self.config[section] = {}
self.config[section][option] = value
self.saveConfig()
except Exception as e:
logging.error("RetroManagerDevice~setVariable {e}")
def getVariable(self, section, option, default):
if (self.config.has_option(section,option)):
return self.config[section][option]
logging.warn(f"Returning default for ({section})({option})")
return default
def setDeviceName(self, newName : str):
logging.info(f"Setting DeviceName to ({newName})")
self.setVariable(self._static_general, self._static_general_devicename, newName)
def getDeviceName(self):
return self.getVariable(self._static_general, self._static_general_devicename, self._static_general_devicename_DEFAULT)
def setDeviceType(self, newType : str):
logging.info(f"Setting DeviceName to ({newType})")
self.setVariable(self._static_general, self._static_general_deviceType, newType)
def getDeviceType(self):
return self.getVariable(self._static_general, self._static_general_deviceType, self._static_general_deviceType_DEFAULT)
def setROMLocations_DEFAULT(self, newLocation : str):
logging.info(f"Setting ROM Location for DEFAULT to ({newLocation})")
self.setVariable(self._static_ROMLocations, self._static_ROMLocations_DEFAULT, newLocation)
def getROMLocations_DEFAULT(self):
return self.getVariable(self._static_ROMLocations, self._static_ROMLocations_DEFAULT, self._static_ROMLocations_DEFAULT_DEFAULT)
def setROMLocations_GameBoy(self, newLocation : str):
logging.info(f"Setting ROM Location for Game Boy to ({newLocation})")
self.setVariable(self._static_ROMLocations, self._static_ROMLocations_GB, newLocation)
def getROMLocations_GameBoy(self):
return self.getVariable(self._static_ROMLocations, self._static_ROMLocations_GB, self._static_ROMLocations_GB_DEFAULT)
def setROMLocations_GameBoyColor(self, newLocation : str):
logging.info(f"Setting ROM Location for Game Boy Color to ({newLocation})")
self.setVariable(self._static_ROMLocations, self._static_ROMLocations_GBC, newLocation)
def getROMLocations_GameBoyColor(self):
return self.getVariable(self._static_ROMLocations, self._static_ROMLocations_GBC, self._static_ROMLocations_GBC_DEFAULT)
def setROMLocations_GameBoyAdvance(self, newLocation : str):
logging.info(f"Setting ROM Location for Game Boy Advance to ({newLocation})")
self.setVariable(self._static_ROMLocations, self._static_ROMLocations_GBA, newLocation)
def getROMLocations_GameBoyAdvance(self):
return self.getVariable(self._static_ROMLocations, self._static_ROMLocations_GBA, self._static_ROMLocations_GBA_DEFAULT)
# TODO Finish adding getters and setters for ROM Locations