-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
124 lines (90 loc) · 3.22 KB
/
storage.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
import os, json
from typing import Optional, TypedDict, NotRequired, Any
from PySide6.QtGui import QPixmap
CONFIG_FOLDER = os.path.join(os.getenv('XDG_CONFIG_HOME', os.path.expanduser('~/.config')), 'PythonGameLauncher')
CONFIG_FILE = os.path.join(CONFIG_FOLDER, 'config.json')
GAMES_FILE = os.path.join(CONFIG_FOLDER, 'games.json')
ARTWORK_FOLDER = os.path.join(CONFIG_FOLDER, 'artwork')
class Config:
def __init__(self) -> None:
if not os.path.exists(CONFIG_FOLDER):
os.mkdir(CONFIG_FOLDER)
if not os.path.exists(ARTWORK_FOLDER):
os.mkdir(ARTWORK_FOLDER)
if not os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'w'):
pass
if not os.path.exists(GAMES_FILE):
with open(GAMES_FILE, 'w'):
pass
with open(CONFIG_FILE, 'r') as file:
config = json.load(file)
self.steamPath: str = config['steamPath']
self.tags: list[str] = config['tags']
def save(self) -> None:
config = {
'steamPath': self.steamPath,
'tags': self.tags,
}
with open(CONFIG_FILE, 'w') as file:
json.dump(config, file, indent='\t')
def updateTags(self, tags: list[str]) -> None:
self.tags = tags
class Game(TypedDict):
name: str
id: int
source: str
tags: list[str]
description: NotRequired[str]
data: dict[str, Any]
class Library:
def __init__(self) -> None:
self.games: list[Game]
if os.path.getsize(GAMES_FILE) > 0:
with open(GAMES_FILE, 'r') as file:
game_library = json.load(file)
self.games = game_library
else:
with open(GAMES_FILE, 'w') as file:
json.dump([], file, indent='\t')
self.games = []
def save(self) -> None:
self.games.sort(key = lambda x: x['name'].lower().replace('the ', ''))
with open(GAMES_FILE, 'w') as file:
json.dump(self.games, file, indent='\t')
def addNativeGame(
self,
name: str,
filepath: str,
args: Optional[list[str]] = None,
tags: Optional[list[str]] = None
) -> None:
if tags is None:
tags = []
if args is None:
args = []
id = self.getNewID()
game: Game = {
'name': name,
'id': id,
'source': 'native',
'tags': tags,
'data': {
'filepath': filepath,
'args': args
},
}
self.games.append(game)
def getNewID(self) -> int:
if len(self.games) == 0:
return 0
else:
return max([x['id'] for x in self.games]) + 1
def getLibraryImage(id: int) -> Optional[QPixmap]:
if os.path.exists(os.path.join(ARTWORK_FOLDER, f'{id}_library_image.jpg')):
path = os.path.join(ARTWORK_FOLDER, f'{id}_library_image.jpg')
elif os.path.exists(os.path.join(ARTWORK_FOLDER, f'{id}_library_image.png')):
path = os.path.join(ARTWORK_FOLDER, f'{id}_library_image.png')
else:
return None
return QPixmap(path)