-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetroManagerCore.py
More file actions
124 lines (101 loc) · 5.19 KB
/
RetroManagerCore.py
File metadata and controls
124 lines (101 loc) · 5.19 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
#OS imports
import hashlib
import shutil
import os
import sys
import string
import configparser
from datetime import datetime
from RetroManagerDatabase import *
import RetroManagerDevice
import rm_util
class RetroManagerCore():
default_filepath = os.path.join(os.path.expanduser("~"), "RetroManager")
#Library (Need to make this changeable)
filepath_library = os.path.join(default_filepath, "Library")
os.makedirs(filepath_library, exist_ok=True)#Create directory if it doesnt exist
#Open/Create Database
rmdb = RetroManagerDatabase(os.path.join(default_filepath, "retromanager.db"))
def __init__(self):
super().__init__()
"""
Import passed rmGame
"""
def importGame(self, game :rmGame):
print("Trying to import rmGame object")
self.importGames([game.filePath])
"""
Imports all games passed in a list
"""
def importGames(self, gamePaths):
try:
if len(gamePaths) > 0:
for file_path in gamePaths:
# TODO Check if the game is already in Database and offer to merge?
new_path = False
gamefolder = os.path.join(self.filepath_library,os.path.splitext(os.path.basename(file_path))[0])
# If the game folder doesn't exist then create it
if not os.path.exists(gamefolder):
os.makedirs(gamefolder, exist_ok=True)# Create directory if it doesnt exist
else:
# At this point a folder with the same name already exists so we check for and
# create an incremented version
i = 0
while os.path.exists(gamefolder + f"_{i}"):
i = i+1
# when we exist we have found a folder that didnt exist
gamefolder = gamefolder + f"_{i}"
os.makedirs(gamefolder, exist_ok=True)# Create directory if it doesnt exist
# Check if file already exists. It shouldn't. but if it does then we dont want to overwrite it. TODO or do we?
gameROMpath = os.path.join(gamefolder, os.path.basename(file_path))
if not os.path.exists(gameROMpath):
new_path = shutil.copy(file_path, gameROMpath)
if(new_path):
title = (os.path.splitext(os.path.basename(new_path))[0]).split('.')[0]
# Add the game to the database
self.rmdb.addGame(title, new_path, rm_util.detectConsoleFromROM(new_path))
logging.info(f"imported file {file_path} as {title}")
else:
logging.error(f"Didn't import {file_path}. Bailed for your protection.")
# Import cover image if it exists
cover_path = os.path.splitext(file_path)[0]
for ext_img in rm_util.supported_img_ext:
imgFile = cover_path + ext_img
if os.path.exists(imgFile):
shutil.copy(imgFile, gamefolder)
# Import the save games
return True
else:
logging.warn("RetroManagerCore~importGames: empty list of gamepaths received")
except Exception as e:
logging.error(f"RetroManagerCore~importGames: {str(e)}")
return False
def importSaves(self, device: RetroManagerDevice):
# Add the saves to the library
listOfSaves = device.scanForSaves()
print(f"Found ({len(listOfSaves)}) save files.")
for save in listOfSaves:
# Build the save title
title = os.path.splitext(os.path.split(save.filePath)[1])[0] + f" - {datetime.utcfromtimestamp(os.path.getmtime(save.filePath)).strftime('%Y%m%d_%H%M%S')}"
savefolder = os.path.join(self.filepath_library,"_saves")
# If the save folder doesn't exist then create it
if not os.path.exists(savefolder):
os.makedirs(savefolder, exist_ok=True)# Create directory if it doesnt exist
#Copy the save to the saves folder. TODO replace this with matching it to a game
NewSavepath = os.path.join(savefolder, title + os.path.splitext(save.filePath)[1])
if not os.path.exists(NewSavepath):
new_path = shutil.copy(save.filePath, NewSavepath)
if(new_path):
sha256hash = compute_sha256(new_path)
self.rmdb.createSave(save.gameDBID, title, new_path, "", os.path.getmtime(save.filePath), sha256hash)
else:
logging.error(f"RetroManagerCore~importSaves: ERROR importing save ({NewSavepath})")
return False
def retrieveGamesFromLocation(self, basePath):
return False
def compute_sha256(file_name):
hash_sha256 = hashlib.sha256()
with open(file_name, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_sha256.update(chunk)
return hash_sha256.hexdigest()