-
Notifications
You must be signed in to change notification settings - Fork 54
/
basic_game.py
645 lines (533 loc) · 21.3 KB
/
basic_game.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
from __future__ import annotations
import shutil
import sys
from pathlib import Path
from typing import Callable, Generic, TypeVar
import mobase
from PyQt6.QtCore import QDir, QFileInfo, QStandardPaths
from PyQt6.QtGui import QIcon
from .basic_features.basic_save_game_info import (
BasicGameSaveGame,
BasicGameSaveGameInfo,
)
def replace_variables(value: str, game: BasicGame) -> str:
"""Replace special paths in the given value."""
if value.find("%DOCUMENTS%") != -1:
value = value.replace(
"%DOCUMENTS%",
QStandardPaths.writableLocation(
QStandardPaths.StandardLocation.DocumentsLocation
),
)
if value.find("%USERPROFILE%") != -1:
value = value.replace(
"%USERPROFILE%",
QStandardPaths.writableLocation(
QStandardPaths.StandardLocation.HomeLocation
),
)
if value.find("%GAME_DOCUMENTS%") != -1:
value = value.replace(
"%GAME_DOCUMENTS%", game.documentsDirectory().absolutePath()
)
if value.find("%GAME_PATH%") != -1:
value = value.replace("%GAME_PATH%", game.gameDirectory().absolutePath())
return value
_T = TypeVar("_T")
class BasicGameMapping(Generic[_T]):
# The game:
_game: "BasicGame"
# Name of the attribute for exposure:
_exposed_name: str
# Name of the internal method:
_internal_method_name: str
# Required:
_required: bool
# Callable returning a default value (if not required):
_default: Callable[["BasicGame"], _T]
# Function to apply to the value:
_apply_fn: Callable[[_T | str], _T] | None
def __init__(
self,
game: BasicGame,
exposed_name: str,
internal_method: str,
default: Callable[[BasicGame], _T] | None = None,
apply_fn: Callable[[_T | str], _T] | None = None,
):
self._game = game
self._exposed_name = exposed_name
self._internal_method_name = internal_method
self._apply_fn = apply_fn
if hasattr(game, self._exposed_name):
value = getattr(game, self._exposed_name)
if self._apply_fn is not None:
try:
value = self._apply_fn(value)
except Exception as err:
raise ValueError(
"Basic game plugin from {} has an invalid {} property.".format(
game._fromName, # pyright: ignore[reportPrivateUsage]
self._exposed_name,
)
) from err
self._default = lambda game: value # type: ignore
elif default is not None:
self._default = default # type: ignore
elif getattr(game.__class__, self._internal_method_name) is getattr(
BasicGame, self._internal_method_name
):
raise ValueError(
"Basic game plugin from {} is missing {} property.".format(
game._fromName, # pyright: ignore[reportPrivateUsage]
self._exposed_name,
)
)
def get(self) -> _T:
"""Return the value of this mapping."""
value = self._default(self._game) # type: ignore
if isinstance(value, str):
return replace_variables(value, self._game) # type: ignore
elif isinstance(value, QDir):
return QDir(replace_variables(value.path(), self._game)) # type: ignore
# MO2 does not support Path anywhere so we always convert to str:
elif isinstance(value, Path):
return replace_variables(str(value), self._game) # type: ignore
return value
class BasicGameOptionsMapping(BasicGameMapping[list[_T]]):
"""
Represents a game mappings for which multiple options are possible. The game
plugin is responsible to choose the right option depending on the context.
"""
_index: int
def __init__(
self,
game: BasicGame,
exposed_name: str,
internal_method: str,
default: Callable[[BasicGame], _T] | None = None,
apply_fn: Callable[[list[_T] | str], list[_T]] | None = None,
):
super().__init__(game, exposed_name, internal_method, lambda g: [], apply_fn)
self._index = -1
self._current_default = default
def set_index(self, index: int):
"""
Set the index of the option to use.
Args:
index: Index of the option to use.
"""
self._index = index
def set_value(self, value: _T):
"""
Set the index corresponding of the given value. If the value is not present,
the index is set to -1.
Args:
value: The value to set the index to.
"""
try:
self._index = self.get().index(value)
except ValueError:
self._index = -1
def has_value(self) -> bool:
"""
Check if a value was set for this options mapping.
Returns:
True if a value was set, False otherwise.
"""
return self._index != -1
def current(self) -> _T:
values = self._default(self._game) # type: ignore
if not values:
return self._current_default(self._game) # type: ignore
if self._index == -1:
value = values[0]
else:
value = values[self._index]
if isinstance(value, str):
return replace_variables(value, self._game) # type: ignore
elif isinstance(value, QDir):
return QDir(replace_variables(value.path(), self._game)) # type: ignore
return value
class BasicGameMappings:
name: BasicGameMapping[str]
author: BasicGameMapping[str]
version: BasicGameMapping[mobase.VersionInfo]
description: BasicGameMapping[str]
gameName: BasicGameMapping[str]
gameShortName: BasicGameMapping[str]
gameNexusName: BasicGameMapping[str]
validShortNames: BasicGameMapping[list[str]]
nexusGameId: BasicGameMapping[int]
binaryName: BasicGameMapping[str]
launcherName: BasicGameMapping[str]
dataDirectory: BasicGameMapping[str]
documentsDirectory: BasicGameMapping[QDir]
iniFiles: BasicGameMapping[list[str]]
savesDirectory: BasicGameMapping[QDir]
savegameExtension: BasicGameMapping[str]
steamAPPId: BasicGameOptionsMapping[str]
gogAPPId: BasicGameOptionsMapping[str]
originManifestIds: BasicGameOptionsMapping[str]
originWatcherExecutables: BasicGameMapping[list[str]]
epicAPPId: BasicGameOptionsMapping[str]
eaDesktopContentId: BasicGameOptionsMapping[str]
supportURL: BasicGameMapping[str]
@staticmethod
def _default_documents_directory(game: mobase.IPluginGame):
folders = [
"{}/My Games/{}".format(
QStandardPaths.writableLocation(
QStandardPaths.StandardLocation.DocumentsLocation
),
game.gameName(),
),
"{}/{}".format(
QStandardPaths.writableLocation(
QStandardPaths.StandardLocation.DocumentsLocation
),
game.gameName(),
),
]
for folder in folders:
qdir = QDir(folder)
if qdir.exists():
return qdir
return QDir()
# Game mappings:
def __init__(self, game: BasicGame):
self._game = game
self.name = BasicGameMapping(game, "Name", "name")
self.author = BasicGameMapping(game, "Author", "author")
self.version = BasicGameMapping(
game,
"Version",
"version",
apply_fn=lambda s: mobase.VersionInfo(s) if isinstance(s, str) else s,
)
self.description = BasicGameMapping(
game,
"Description",
"description",
lambda g: "Adds basic support for game {}.".format(g.gameName()),
)
self.gameName = BasicGameMapping(game, "GameName", "gameName")
self.gameShortName = BasicGameMapping(game, "GameShortName", "gameShortName")
self.gameNexusName = BasicGameMapping(
game,
"GameNexusName",
"gameNexusName",
default=lambda g: g.gameShortName(),
)
self.validShortNames = BasicGameMapping(
game,
"GameValidShortNames",
"validShortNames",
default=lambda g: [],
apply_fn=lambda value: (
[c.strip() for c in value.split(",")] # type: ignore
if isinstance(value, str)
else value
),
)
self.nexusGameId = BasicGameMapping(
game, "GameNexusId", "nexusGameID", default=lambda g: 0, apply_fn=int
)
self.binaryName = BasicGameMapping(game, "GameBinary", "binaryName")
self.launcherName = BasicGameMapping(
game,
"GameLauncher",
"getLauncherName",
default=lambda g: "",
)
self.dataDirectory = BasicGameMapping(game, "GameDataPath", "dataDirectory")
self.documentsDirectory = BasicGameMapping(
game,
"GameDocumentsDirectory",
"documentsDirectory",
apply_fn=lambda s: QDir(s) if isinstance(s, str) else s,
default=BasicGameMappings._default_documents_directory,
)
self.iniFiles = BasicGameMapping(
game,
"GameIniFiles",
"iniFiles",
lambda g: [],
apply_fn=lambda value: (
[c.strip() for c in value.split(",")]
if isinstance(value, str)
else value
),
)
self.savesDirectory = BasicGameMapping(
game,
"GameSavesDirectory",
"savesDirectory",
apply_fn=lambda s: QDir(s) if isinstance(s, str) else s,
default=lambda g: g.documentsDirectory(),
)
self.savegameExtension = BasicGameMapping(
game, "GameSaveExtension", "savegameExtension", default=lambda g: "save"
)
# Convert Union[int, str, List[Union[int, str]]] to List[str].
def ids_apply(v: list[int] | list[str] | int | str) -> list[str]:
"""
Convert various types to a list of string. If the given value is already a
list, returns a new list with all values converted to string, otherwise
returns a list with the value convert to a string as its only element.
"""
if isinstance(v, (int, str)):
v = [str(v)]
return [str(x) for x in v]
self.steamAPPId = BasicGameOptionsMapping(
game, "GameSteamId", "steamAPPId", default=lambda g: "", apply_fn=ids_apply
)
self.gogAPPId = BasicGameOptionsMapping(
game, "GameGogId", "gogAPPId", default=lambda g: "", apply_fn=ids_apply
)
self.originManifestIds = BasicGameOptionsMapping(
game,
"GameOriginManifestIds",
"originManifestIds",
default=lambda g: "",
apply_fn=ids_apply,
)
self.originWatcherExecutables = BasicGameMapping(
game,
"GameOriginWatcherExecutables",
"originWatcherExecutables",
apply_fn=lambda s: [s] if isinstance(s, str) else s,
default=lambda g: [],
)
self.epicAPPId = BasicGameOptionsMapping(
game, "GameEpicId", "epicAPPId", default=lambda g: "", apply_fn=ids_apply
)
self.eaDesktopContentId = BasicGameOptionsMapping(
game,
"GameEaDesktopId",
"eaDesktopContentId",
default=lambda g: "",
apply_fn=ids_apply,
)
self.supportURL = BasicGameMapping(
game, "GameSupportURL", "supportURL", default=lambda g: ""
)
class BasicGame(mobase.IPluginGame):
"""This class implements some methods from mobase.IPluginGame
to make it easier to create game plugins without having to implement
all the methods of mobase.IPluginGame."""
# List of steam, GOG, origin and Epic games:
steam_games: dict[str, Path]
gog_games: dict[str, Path]
origin_games: dict[str, Path]
epic_games: dict[str, Path]
eadesktop_games: dict[str, Path]
@staticmethod
def setup():
from .eadesktop_utils import find_games as find_eadesktop_games
from .epic_utils import find_games as find_epic_games
from .gog_utils import find_games as find_gog_games
from .origin_utils import find_games as find_origin_games
from .steam_utils import find_games as find_steam_games
BasicGame.steam_games = find_steam_games()
BasicGame.gog_games = find_gog_games()
BasicGame.origin_games = find_origin_games()
BasicGame.epic_games = find_epic_games()
BasicGame.eadesktop_games = find_eadesktop_games()
# File containing the plugin:
_fromName: str
# Organizer obtained in init:
_organizer: mobase.IOrganizer
# Path to the game, as set by MO2:
_gamePath: str
def __init__(self):
super(BasicGame, self).__init__()
if not hasattr(self, "_fromName"):
self._fromName = self.__class__.__name__
self._gamePath = ""
self._mappings: BasicGameMappings = BasicGameMappings(self)
def _register_feature(self, feature: mobase.GameFeature) -> bool:
return self._organizer.gameFeatures().registerFeature(self, feature, 0, True)
# Specific to BasicGame:
def is_steam(self) -> bool:
return self._mappings.steamAPPId.has_value()
def is_gog(self) -> bool:
return self._mappings.gogAPPId.has_value()
def is_origin(self) -> bool:
return self._mappings.originManifestIds.has_value()
def is_epic(self) -> bool:
return self._mappings.epicAPPId.has_value()
def is_eadesktop(self) -> bool:
return self._mappings.eaDesktopContentId.has_value()
# IPlugin interface:
def init(self, organizer: mobase.IOrganizer) -> bool:
self._organizer = organizer
self._register_feature(BasicGameSaveGameInfo())
if self._mappings.originWatcherExecutables.get():
from .origin_utils import OriginWatcher
self.origin_watcher = OriginWatcher(
self._mappings.originWatcherExecutables.get()
)
if not self._organizer.onAboutToRun(
lambda appName: self.origin_watcher.spawn_origin_watcher()
):
print("Failed to register onAboutToRun callback!", file=sys.stderr)
return False
if not self._organizer.onFinishedRun(
lambda appName, result: self.origin_watcher.stop_origin_watcher()
):
print("Failed to register onFinishedRun callback!", file=sys.stderr)
return False
return True
def name(self) -> str:
return self._mappings.name.get()
def author(self) -> str:
return self._mappings.author.get()
def description(self) -> str:
return self._mappings.description.get()
def version(self) -> mobase.VersionInfo:
return self._mappings.version.get()
def isActive(self) -> bool:
if not self._organizer.managedGame():
return False
# Note: self is self._organizer.managedGame() does not work:
return self.name() == self._organizer.managedGame().name()
def settings(self) -> list[mobase.PluginSetting]:
return []
# IPluginGame interface:
def detectGame(self):
for steam_id in self._mappings.steamAPPId.get():
if steam_id in BasicGame.steam_games:
self.setGamePath(BasicGame.steam_games[steam_id])
return
for gog_id in self._mappings.gogAPPId.get():
if gog_id in BasicGame.gog_games:
self.setGamePath(BasicGame.gog_games[gog_id])
return
for origin_manifest_id in self._mappings.originManifestIds.get():
if origin_manifest_id in BasicGame.origin_games:
self.setGamePath(BasicGame.origin_games[origin_manifest_id])
return
for epic_id in self._mappings.epicAPPId.get():
if epic_id in BasicGame.epic_games:
self.setGamePath(BasicGame.epic_games[epic_id])
return
for eadesktop_content_id in self._mappings.eaDesktopContentId.get():
if eadesktop_content_id in BasicGame.eadesktop_games:
self.setGamePath(BasicGame.eadesktop_games[eadesktop_content_id])
return
def gameName(self) -> str:
return self._mappings.gameName.get()
def gameShortName(self) -> str:
return self._mappings.gameShortName.get()
def gameIcon(self) -> QIcon:
return mobase.getIconForExecutable(
self.gameDirectory().absoluteFilePath(self.binaryName())
)
def validShortNames(self) -> list[str]:
return self._mappings.validShortNames.get()
def gameNexusName(self) -> str:
return self._mappings.gameNexusName.get()
def nexusModOrganizerID(self) -> int:
return 0
def nexusGameID(self) -> int:
return self._mappings.nexusGameId.get()
def steamAPPId(self) -> str:
return self._mappings.steamAPPId.current()
def gogAPPId(self) -> str:
return self._mappings.gogAPPId.current()
def epicAPPId(self) -> str:
return self._mappings.epicAPPId.current()
def eaDesktopContentId(self) -> str:
return self._mappings.eaDesktopContentId.current()
def binaryName(self) -> str:
return self._mappings.binaryName.get()
def getLauncherName(self) -> str:
return self._mappings.launcherName.get()
def getSupportURL(self) -> str:
return self._mappings.supportURL.get()
def iniFiles(self) -> list[str]:
return self._mappings.iniFiles.get()
def executables(self) -> list[mobase.ExecutableInfo]:
execs: list[mobase.ExecutableInfo] = []
if self.getLauncherName():
execs.append(
mobase.ExecutableInfo(
self.gameName(),
QFileInfo(
self.gameDirectory().absoluteFilePath(self.getLauncherName())
),
)
)
execs.append(
mobase.ExecutableInfo(
self.gameName(),
QFileInfo(self.gameDirectory().absoluteFilePath(self.binaryName())),
)
)
return execs
def executableForcedLoads(self) -> list[mobase.ExecutableForcedLoadSetting]:
return []
def listSaves(self, folder: QDir) -> list[mobase.ISaveGame]:
ext = self._mappings.savegameExtension.get()
return [
BasicGameSaveGame(path)
for path in Path(folder.absolutePath()).glob(f"**/*.{ext}")
]
def initializeProfile(
self, directory: QDir, settings: mobase.ProfileSetting
) -> None:
if settings & mobase.ProfileSetting.CONFIGURATION:
for iniFile in self.iniFiles():
try:
shutil.copyfile(
self.documentsDirectory().absoluteFilePath(iniFile),
directory.absoluteFilePath(QFileInfo(iniFile).fileName()),
)
except FileNotFoundError:
Path(
directory.absoluteFilePath(QFileInfo(iniFile).fileName())
).touch()
def setGameVariant(self, variant: str) -> None:
pass
def gameVersion(self) -> str:
return mobase.getFileVersion(
self.gameDirectory().absoluteFilePath(self.binaryName())
)
def looksValid(self, directory: QDir):
return directory.exists(self.binaryName())
def isInstalled(self) -> bool:
return bool(self._gamePath)
def gameDirectory(self) -> QDir:
"""
@return directory (QDir) to the game installation.
"""
return QDir(self._gamePath)
def dataDirectory(self) -> QDir:
return QDir(
self.gameDirectory().absoluteFilePath(self._mappings.dataDirectory.get())
)
def setGamePath(self, path: Path | str) -> None:
self._gamePath = str(path)
path = Path(path)
# Check if we have a matching steam, GOG, Origin or EA Desktop id and set the
# index accordingly:
for steamid, steampath in BasicGame.steam_games.items():
if steampath == path:
self._mappings.steamAPPId.set_value(steamid)
for gogid, gogpath in BasicGame.gog_games.items():
if gogpath == path:
self._mappings.steamAPPId.set_value(gogid)
for originid, originpath in BasicGame.origin_games.items():
if originpath == path:
self._mappings.originManifestIds.set_value(originid)
for epicid, epicpath in BasicGame.epic_games.items():
if epicpath == path:
self._mappings.epicAPPId.set_value(epicid)
for eadesktopid, eadesktoppath in BasicGame.eadesktop_games.items():
if eadesktoppath == path:
self._mappings.eaDesktopContentId.set_value(eadesktopid)
def documentsDirectory(self) -> QDir:
return self._mappings.documentsDirectory.get()
def savesDirectory(self) -> QDir:
return self._mappings.savesDirectory.get()