1
0
Fork 0
mirror of synced 2024-06-02 18:54:41 +12:00

Lgndr: Remove LegendaryCLISingleton

Since `LegendaryCLI` isn't stateful, we can instantiate
it when needed
This commit is contained in:
loathingKernel 2022-08-01 14:31:07 +03:00
parent 5ac71e99f0
commit 38dfdc8bc2
8 changed files with 33 additions and 53 deletions

View file

@ -20,7 +20,7 @@ import rare
from rare.components.dialogs.launch_dialog import LaunchDialog
from rare.components.main_window import MainWindow
from rare.components.tray_icon import TrayIcon
from rare.shared import LegendaryCLISingleton, LegendaryCoreSingleton, GlobalSignalsSingleton, ArgumentsSingleton
from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton, ArgumentsSingleton
from rare.shared.image_manager import ImageManagerSingleton
from rare.utils import legendary_utils, config_helper
from rare.utils.paths import cache_dir, tmp_dir
@ -64,8 +64,7 @@ class App(RareApp):
# init Legendary
try:
LegendaryCLISingleton(init=True)
self.core = LegendaryCoreSingleton()
self.core = LegendaryCoreSingleton(init=True)
except configparser.MissingSectionHeaderError as e:
logger.warning(f"Config is corrupt: {e}")
if config_path := os.environ.get("XDG_CONFIG_HOME"):
@ -74,8 +73,7 @@ class App(RareApp):
path = os.path.expanduser("~/.config/legendary")
with open(os.path.join(path, "config.ini"), "w") as config_file:
config_file.write("[Legendary]")
LegendaryCLISingleton(init=True)
self.core = LegendaryCoreSingleton()
self.core = LegendaryCoreSingleton(init=True)
if "Legendary" not in self.core.lgd.config.sections():
self.core.lgd.config.add_section("Legendary")
self.core.lgd.save_config()

View file

@ -10,11 +10,12 @@ from legendary.models.downloading import ConditionCheckResult
from legendary.models.game import Game
from legendary.utils.selective_dl import get_sdl_appname
from rare.lgndr.cli import LegendaryCLI
from rare.lgndr.api_arguments import LgndrInstallGameArgs
from rare.lgndr.api_exception import LgndrException
from rare.lgndr.api_monkeys import LgndrIndirectStatus
from rare.lgndr.core import LegendaryCore
from rare.shared import LegendaryCLISingleton, LegendaryCoreSingleton, ApiResultsSingleton, ArgumentsSingleton
from rare.shared import LegendaryCoreSingleton, ApiResultsSingleton, ArgumentsSingleton
from rare.ui.components.dialogs.install_dialog import Ui_InstallDialog
from rare.utils.extra_widgets import PathEdit
from rare.models.install import InstallDownloadModel, InstallQueueItemModel
@ -337,7 +338,7 @@ class InstallInfoWorker(QRunnable):
def run(self):
try:
if not self.dl_item.options.overlay:
cli = LegendaryCLISingleton()
cli = LegendaryCLI(self.core)
status = LgndrIndirectStatus()
result = cli.install_game(
LgndrInstallGameArgs(**self.dl_item.options.as_install_kwargs(), indirect_status=status)

View file

@ -11,7 +11,8 @@ from PyQt5.QtCore import QThread, pyqtSignal, QProcess
from legendary.core import LegendaryCore
from legendary.models.downloading import WriterTask
from rare.shared import GlobalSignalsSingleton, LegendaryCLISingleton
from rare.lgndr.cli import LegendaryCLI
from rare.shared import GlobalSignalsSingleton
from rare.models.install import InstallQueueItemModel
from rare.utils.misc import create_desktop_link
from rare.lgndr.downloading import UIUpdate
@ -170,7 +171,7 @@ class DownloadThread(QThread):
f'To download saves for this game run "legendary sync-saves {self.item.download.game.app_name}"'
)
LegendaryCLISingleton().clean_post_install(
LegendaryCLI(self.core).clean_post_install(
self.item.download.game, self.item.download.igame,
self.item.download.repair, self.item.download.repair_file
)

View file

@ -10,10 +10,10 @@ from PyQt5.QtCore import Qt, QModelIndex, pyqtSignal, QRunnable, QObject, QThrea
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtWidgets import QFileDialog, QGroupBox, QCompleter, QTreeView, QHeaderView, qApp, QMessageBox
from rare.lgndr.cli import LegendaryCLI
from rare.lgndr.api_arguments import LgndrImportGameArgs
from rare.lgndr.api_exception import LgndrException
from rare.lgndr.api_monkeys import LgndrIndirectStatus
from rare.shared import LegendaryCLISingleton, LegendaryCoreSingleton, GlobalSignalsSingleton, ApiResultsSingleton
from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton, ApiResultsSingleton
from rare.ui.components.tabs.games.import_sync.import_group import Ui_ImportGroup
from rare.utils.extra_widgets import IndicatorLineEdit, PathEdit
from rare.widgets.elide_label import ElideLabel
@ -28,7 +28,7 @@ def find_app_name(path: str, core) -> Optional[str]:
with open(os.path.join(path, ".egstore", i)) as file:
app_name = json.load(file).get("AppName")
return app_name
elif app_name := LegendaryCLISingleton().resolve_aliases(os.path.basename(os.path.normpath(path))):
elif app_name := LegendaryCLI(core).resolve_aliases(os.path.basename(os.path.normpath(path))):
# return None if game does not exist (Workaround for overlay)
if not core.get_game(app_name):
return None
@ -99,7 +99,7 @@ class ImportWorker(QRunnable):
return result
def __import_game(self, path: Path, app_name: str, app_title: str):
cli = LegendaryCLISingleton()
cli = LegendaryCLI(self.core)
status = LgndrIndirectStatus()
args = LgndrImportGameArgs(
app_path=str(path),

View file

@ -14,7 +14,7 @@ from PyQt5.QtNetwork import QLocalServer, QLocalSocket
from .console import Console
from .lgd_helper import get_launch_args, InitArgs, get_configured_process, LaunchArgs, GameArgsError
from .message_models import ErrorModel, Actions, FinishedModel, BaseModel, StateChangedModel
from ..shared import LegendaryCLISingleton, LegendaryCoreSingleton
from ..shared import LegendaryCoreSingleton
from ..widgets.rare_app import RareApp
@ -69,8 +69,7 @@ class GameProcessApp(RareApp):
self.game_process = QProcess()
self.app_name = app_name
self.logger = getLogger(self.app_name)
LegendaryCLISingleton(init=True)
self.core = LegendaryCoreSingleton()
self.core = LegendaryCoreSingleton(init=True)
lang = self.settings.value("language", self.core.language_code, type=str)
self.load_translator(lang)

View file

@ -17,8 +17,9 @@ from .manager import DLManager
class LegendaryCLI(LegendaryCLIReal):
def __init__(self, override_config=None, api_timeout=None):
self.core = LegendaryCore(override_config, timeout=api_timeout)
# noinspection PyMissingConstructor
def __init__(self, core: LegendaryCore):
self.core = core
self.logger = logging.getLogger('cli')
self.logging_queue = None
@ -96,19 +97,10 @@ class LegendaryCLI(LegendaryCLIReal):
if not os.path.exists(repair_file):
logger.info('Game has not been verified yet.')
# Rare: Dodge the path below for now
# Rare: we do not want to verify while preparing the download in the InstallDialog
# Rare: we handle it differently through the GameInfo tab
logger.error('Game has not been verified yet.')
return
if not args.yes:
if not get_boolean_choice(f'Verify "{game.app_name}" now ("no" will abort repair)?'):
return
try:
self.verify_game(args, print_command=False, repair_mode=True, repair_online=args.repair_and_update)
except ValueError:
logger.error('To repair a game with a missing manifest you must run the command with '
'"--repair-and-update". However this will redownload any file that does '
'not match the current hash in its entirety.')
return
else:
logger.info(f'Using existing repair file: {repair_file}')

View file

@ -8,35 +8,24 @@ and only ONCE!
from argparse import Namespace
from typing import Optional
from rare.lgndr.cli import LegendaryCLI
from rare.lgndr.core import LegendaryCore
from rare.models.apiresults import ApiResults
from rare.models.signals import GlobalSignals
_legendary_cli_signleton: Optional[LegendaryCLI] = None
_legendary_core_singleton: Optional[LegendaryCore] = None
_global_signals_singleton: Optional[GlobalSignals] = None
_arguments_singleton: Optional[Namespace] = None
_api_results_singleton: Optional[ApiResults] = None
def LegendaryCLISingleton(init: bool = False) -> LegendaryCLI:
global _legendary_cli_signleton
if _legendary_cli_signleton is None and not init:
raise RuntimeError("Uninitialized use of LegendaryCLISingleton")
if _legendary_cli_signleton is None:
_legendary_cli_signleton = LegendaryCLI(override_config=None, api_timeout=10)
return _legendary_cli_signleton
def LegendaryCoreSingleton(init: bool = False) -> LegendaryCore:
global _legendary_cli_signleton
if _legendary_cli_signleton is None:
raise RuntimeError("LegendaryCLI is not initialized yet")
# if _legendary_cli_signleton is None:
# _legendary_cli_signleton = LegendaryCLISingleton(init)
return _legendary_cli_signleton.core
global _legendary_core_singleton
if _legendary_core_singleton is None and not init:
raise RuntimeError("Uninitialized use of LegendaryCoreSingleton")
if _legendary_core_singleton is None:
_legendary_core_singleton = LegendaryCore()
return _legendary_core_singleton
def GlobalSignalsSingleton(init: bool = False) -> GlobalSignals:

View file

@ -5,10 +5,10 @@ from logging import getLogger
from PyQt5.QtCore import pyqtSignal, QObject, QRunnable, QStandardPaths
from legendary.core import LegendaryCore
from rare.lgndr.cli import LegendaryCLI
from rare.lgndr.api_monkeys import LgndrIndirectStatus
from rare.lgndr.api_arguments import LgndrVerifyGameArgs, LgndrUninstallGameArgs
from rare.lgndr.api_exception import LgndrException
from rare.shared import LegendaryCLISingleton, LegendaryCoreSingleton, ArgumentsSingleton
from rare.shared import LegendaryCoreSingleton, ArgumentsSingleton
from rare.utils import config_helper
logger = getLogger("Legendary Utils")
@ -40,7 +40,7 @@ def uninstall_game(core: LegendaryCore, app_name: str, keep_files=False, keep_co
os.remove(start_menu_shortcut)
status = LgndrIndirectStatus()
LegendaryCLISingleton().uninstall_game(
LegendaryCLI(core).uninstall_game(
LgndrUninstallGameArgs(
app_name=app_name,
keep_files=keep_files,
@ -88,7 +88,6 @@ class VerifyWorker(QRunnable):
super(VerifyWorker, self).__init__()
self.signals = VerifyWorker.Signals()
self.setAutoDelete(True)
self.cli = LegendaryCLISingleton()
self.core = LegendaryCoreSingleton()
self.args = ArgumentsSingleton()
self.app_name = app_name
@ -97,6 +96,7 @@ class VerifyWorker(QRunnable):
self.signals.status.emit(self.app_name, num, total, percentage, speed)
def run(self):
cli = LegendaryCLI(self.core)
status = LgndrIndirectStatus()
args = LgndrVerifyGameArgs(app_name=self.app_name,
indirect_status=status,
@ -104,7 +104,7 @@ class VerifyWorker(QRunnable):
# lk: first pass, verify with the current manifest
repair_mode = False
result = self.cli.verify_game(
result = cli.verify_game(
args, print_command=False, repair_mode=repair_mode, repair_online=not self.args.offline
)
if result is None:
@ -115,7 +115,7 @@ class VerifyWorker(QRunnable):
# lk: this try-except block handles the exception caused by a missing manifest
# lk: and is raised only in the case we are offline
repair_mode = True
result = self.cli.verify_game(
result = cli.verify_game(
args, print_command=False, repair_mode=repair_mode, repair_online=not self.args.offline
)
if result is None:
@ -131,7 +131,7 @@ class VerifyWorker(QRunnable):
igame = self.core.get_installed_game(self.app_name)
game = self.core.get_game(self.app_name, platform=igame.platform)
repair_file = os.path.join(self.core.lgd.get_tmp_path(), f'{self.app_name}.repair')
self.cli.clean_post_install(game=game, igame=igame, repair=True, repair_file=repair_file)
cli.clean_post_install(game=game, igame=igame, repair=True, repair_file=repair_file)
self.signals.result.emit(self.app_name, success, *result)