2021-02-10 23:48:25 +13:00
|
|
|
import os
|
2021-06-21 07:55:31 +12:00
|
|
|
import platform
|
2021-03-10 04:52:51 +13:00
|
|
|
import shutil
|
2021-03-09 08:36:42 +13:00
|
|
|
from logging import getLogger
|
2021-02-10 23:48:25 +13:00
|
|
|
|
2021-11-02 10:53:04 +13:00
|
|
|
from PyQt5.QtCore import pyqtSignal, QCoreApplication, QObject, QRunnable
|
2021-03-19 00:45:59 +13:00
|
|
|
|
2021-09-07 07:10:18 +12:00
|
|
|
from legendary.core import LegendaryCore
|
2021-11-02 10:53:04 +13:00
|
|
|
from legendary.models.game import VerifyResult
|
2021-09-07 07:10:18 +12:00
|
|
|
from legendary.utils.lfs import validate_files
|
2022-02-26 06:43:27 +13:00
|
|
|
from rare.shared import LegendaryCoreSingleton
|
2022-02-06 02:48:50 +13:00
|
|
|
from rare.utils import config_helper
|
2021-02-10 23:48:25 +13:00
|
|
|
|
2021-03-09 08:36:42 +13:00
|
|
|
logger = getLogger("Legendary Utils")
|
|
|
|
|
2021-02-10 23:48:25 +13:00
|
|
|
|
2021-05-01 21:17:18 +12:00
|
|
|
def uninstall(app_name: str, core: LegendaryCore, options=None):
|
2021-04-06 21:00:13 +12:00
|
|
|
if not options:
|
|
|
|
options = {"keep_files": False}
|
2021-03-09 08:36:42 +13:00
|
|
|
igame = core.get_installed_game(app_name)
|
2021-05-01 21:17:18 +12:00
|
|
|
|
|
|
|
# remove shortcuts link
|
2021-06-21 07:55:31 +12:00
|
|
|
if platform.system() == "Linux":
|
2021-05-01 21:17:18 +12:00
|
|
|
if os.path.exists(os.path.expanduser(f"~/Desktop/{igame.title}.desktop")):
|
|
|
|
os.remove(os.path.expanduser(f"~/Desktop/{igame.title}.desktop"))
|
2021-12-24 22:09:50 +13:00
|
|
|
if os.path.exists(
|
|
|
|
os.path.expanduser(f"~/.local/share/applications/{igame.title}.desktop")
|
|
|
|
):
|
|
|
|
os.remove(
|
|
|
|
os.path.expanduser(f"~/.local/share/applications/{igame.title}.desktop")
|
|
|
|
)
|
2021-05-01 21:17:18 +12:00
|
|
|
|
2021-06-21 07:55:31 +12:00
|
|
|
elif platform.system() == "Windows":
|
2021-12-24 22:09:50 +13:00
|
|
|
if os.path.exists(
|
|
|
|
os.path.expanduser(f"~/Desktop/{igame.title.split(':')[0]}.lnk")
|
|
|
|
):
|
2021-05-12 21:45:10 +12:00
|
|
|
os.remove(os.path.expanduser(f"~/Desktop/{igame.title.split(':')[0]}.lnk"))
|
2021-05-21 23:40:10 +12:00
|
|
|
elif os.path.exists(
|
2021-12-24 22:09:50 +13:00
|
|
|
os.path.expandvars(
|
|
|
|
f"%appdata%/Microsoft/Windows/Start Menu/{igame.title.split(':')[0]}.lnk"
|
|
|
|
)
|
|
|
|
):
|
|
|
|
os.remove(
|
|
|
|
os.path.expandvars(
|
|
|
|
f"%appdata%/Microsoft/Windows/Start Menu/{igame.title.split(':')[0]}.lnk"
|
|
|
|
)
|
|
|
|
)
|
2021-05-12 21:45:10 +12:00
|
|
|
|
2021-03-09 08:36:42 +13:00
|
|
|
try:
|
|
|
|
# Remove DLC first so directory is empty when game uninstall runs
|
|
|
|
dlcs = core.get_dlc_for_game(app_name)
|
|
|
|
for dlc in dlcs:
|
|
|
|
if (idlc := core.get_installed_game(dlc.app_name)) is not None:
|
|
|
|
logger.info(f'Uninstalling DLC "{dlc.app_name}"...')
|
2021-04-06 21:00:13 +12:00
|
|
|
core.uninstall_game(idlc, delete_files=not options["keep_files"])
|
2021-03-09 08:36:42 +13:00
|
|
|
|
|
|
|
logger.info(f'Removing "{igame.title}" from "{igame.install_path}"...')
|
2021-12-24 22:09:50 +13:00
|
|
|
core.uninstall_game(
|
|
|
|
igame, delete_files=not options["keep_files"], delete_root_directory=True
|
|
|
|
)
|
|
|
|
logger.info("Game has been uninstalled.")
|
2021-04-06 21:00:13 +12:00
|
|
|
if not options["keep_files"]:
|
|
|
|
shutil.rmtree(igame.install_path)
|
2021-03-09 08:36:42 +13:00
|
|
|
|
|
|
|
except Exception as e:
|
2021-12-24 22:09:50 +13:00
|
|
|
logger.warning(
|
|
|
|
f"Removing game failed: {e!r}, please remove {igame.install_path} manually."
|
|
|
|
)
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2021-08-31 01:14:40 +12:00
|
|
|
logger.info("Removing sections in config file")
|
2022-02-06 02:48:50 +13:00
|
|
|
config_helper.remove_section(app_name)
|
|
|
|
config_helper.remove_section(f"{app_name}.env")
|
|
|
|
|
|
|
|
config_helper.save_config()
|
2021-08-31 01:14:40 +12:00
|
|
|
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2021-11-10 09:31:25 +13:00
|
|
|
def update_manifest(app_name: str, core: LegendaryCore):
|
|
|
|
game = core.get_game(app_name)
|
2022-02-02 10:29:34 +13:00
|
|
|
logger.info(f"Reloading game manifest of {game.app_title}")
|
2021-11-10 09:31:25 +13:00
|
|
|
new_manifest_data, base_urls = core.get_cdn_manifest(game)
|
|
|
|
# overwrite base urls in metadata with current ones to avoid using old/dead CDNs
|
|
|
|
game.base_urls = base_urls
|
|
|
|
# save base urls to game metadata
|
|
|
|
core.lgd.set_game_meta(game.app_name, game)
|
|
|
|
|
|
|
|
new_manifest = core.load_manifest(new_manifest_data)
|
2021-12-24 22:09:50 +13:00
|
|
|
logger.debug(f"Base urls: {base_urls}")
|
2021-11-10 09:31:25 +13:00
|
|
|
# save manifest with version name as well for testing/downgrading/etc.
|
2021-12-24 22:09:50 +13:00
|
|
|
core.lgd.save_manifest(
|
|
|
|
game.app_name, new_manifest_data, version=new_manifest.meta.build_version
|
|
|
|
)
|
2021-11-10 09:31:25 +13:00
|
|
|
|
|
|
|
|
2021-11-11 10:07:28 +13:00
|
|
|
class VerifySignals(QObject):
|
2022-01-16 04:40:31 +13:00
|
|
|
status = pyqtSignal(int, int, str)
|
2021-10-15 10:05:00 +13:00
|
|
|
summary = pyqtSignal(int, int, str)
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2021-11-11 10:07:28 +13:00
|
|
|
|
|
|
|
class VerifyWorker(QRunnable):
|
2021-11-23 08:09:05 +13:00
|
|
|
num: int = 0
|
|
|
|
total: int = 1 # set default to 1 to avoid DivisionByZero before it is initialized
|
|
|
|
|
2022-01-16 04:40:31 +13:00
|
|
|
def __init__(self, app_name):
|
2021-11-11 10:07:28 +13:00
|
|
|
super(VerifyWorker, self).__init__()
|
|
|
|
self.signals = VerifySignals()
|
|
|
|
self.setAutoDelete(True)
|
2022-02-26 06:43:27 +13:00
|
|
|
self.core = LegendaryCoreSingleton()
|
|
|
|
self.app_name = app_name
|
2021-03-10 04:52:51 +13:00
|
|
|
|
|
|
|
def run(self):
|
2022-02-26 06:43:27 +13:00
|
|
|
if not self.core.is_installed(self.app_name):
|
2021-03-10 04:52:51 +13:00
|
|
|
logger.error(f'Game "{self.app_name}" is not installed')
|
|
|
|
return
|
2021-11-10 09:31:25 +13:00
|
|
|
|
2022-01-16 04:40:31 +13:00
|
|
|
logger.info(f'Loading installed manifest for "{self.app_name}"')
|
2022-02-26 06:43:27 +13:00
|
|
|
igame = self.core.get_installed_game(self.app_name)
|
|
|
|
manifest_data, _ = self.core.get_installed_manifest(self.app_name)
|
|
|
|
manifest = self.core.load_manifest(manifest_data)
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2022-01-16 04:40:31 +13:00
|
|
|
files = sorted(manifest.file_manifest_list.elements,
|
|
|
|
key=lambda a: a.filename.lower())
|
2021-03-10 04:52:51 +13:00
|
|
|
|
|
|
|
# build list of hashes
|
|
|
|
file_list = [(f.filename, f.sha_hash.hex()) for f in files]
|
2022-01-16 04:40:31 +13:00
|
|
|
total = len(file_list)
|
|
|
|
num = 0
|
2021-03-10 04:52:51 +13:00
|
|
|
failed = []
|
|
|
|
missing = []
|
|
|
|
|
2022-01-16 04:40:31 +13:00
|
|
|
logger.info(f'Verifying "{igame.title}" version "{manifest.meta.build_version}"')
|
2021-03-10 04:52:51 +13:00
|
|
|
repair_file = []
|
2022-01-16 04:40:31 +13:00
|
|
|
for result, path, result_hash, _ in validate_files(igame.install_path, file_list):
|
|
|
|
num += 1
|
|
|
|
self.signals.status.emit(num, total, self.app_name)
|
|
|
|
|
|
|
|
if result == VerifyResult.HASH_MATCH:
|
|
|
|
repair_file.append(f"{result_hash}:{path}")
|
|
|
|
continue
|
|
|
|
elif result == VerifyResult.HASH_MISMATCH:
|
|
|
|
logger.error(f'File does not match hash: "{path}"')
|
|
|
|
repair_file.append(f"{result_hash}:{path}")
|
|
|
|
failed.append(path)
|
|
|
|
elif result == VerifyResult.FILE_MISSING:
|
|
|
|
logger.error(f'File is missing: "{path}"')
|
|
|
|
missing.append(path)
|
|
|
|
else:
|
|
|
|
logger.error(f'Other failure (see log), treating file as missing: "{path}"')
|
|
|
|
missing.append(path)
|
2021-03-10 04:52:51 +13:00
|
|
|
|
|
|
|
# always write repair file, even if all match
|
|
|
|
if repair_file:
|
2022-02-26 06:43:27 +13:00
|
|
|
repair_filename = os.path.join(self.core.lgd.get_tmp_path(), f'{self.app_name}.repair')
|
2022-01-16 04:40:31 +13:00
|
|
|
with open(repair_filename, 'w') as f:
|
|
|
|
f.write('\n'.join(repair_file))
|
2021-03-10 04:52:51 +13:00
|
|
|
logger.debug(f'Written repair file to "{repair_filename}"')
|
|
|
|
|
|
|
|
if not missing and not failed:
|
2022-01-16 04:40:31 +13:00
|
|
|
logger.info('Verification finished successfully.')
|
2021-03-10 04:52:51 +13:00
|
|
|
else:
|
2022-01-16 04:40:31 +13:00
|
|
|
logger.warning(
|
|
|
|
f'Verification failed, {len(failed)} file(s) corrupted, {len(missing)} file(s) are missing.')
|
|
|
|
self.signals.summary.emit(len(failed), len(missing), self.app_name)
|
2021-03-10 04:52:51 +13:00
|
|
|
|
|
|
|
|
2021-12-05 12:56:51 +13:00
|
|
|
def import_game(core: LegendaryCore, app_name: str, path: str) -> str:
|
|
|
|
_tr = QCoreApplication.translate
|
2022-02-02 10:29:34 +13:00
|
|
|
logger.info(f"Import {app_name}")
|
2021-12-04 13:11:20 +13:00
|
|
|
game = core.get_game(app_name, update_meta=False)
|
2021-12-05 12:56:51 +13:00
|
|
|
if not game:
|
|
|
|
return _tr("LgdUtils", "Could not get game for {}").format(app_name)
|
|
|
|
|
|
|
|
if core.is_installed(app_name):
|
|
|
|
logger.error(f"{game.app_title} is already installed")
|
|
|
|
return _tr("LgdUtils", "{} is already installed").format(game.app_title)
|
|
|
|
|
|
|
|
if not os.path.exists(path):
|
|
|
|
logger.error("Path does not exist")
|
|
|
|
return _tr("LgdUtils", "Path does not exist")
|
|
|
|
|
2021-03-10 04:52:51 +13:00
|
|
|
manifest, igame = core.import_game(game, path)
|
2021-12-24 22:09:50 +13:00
|
|
|
exe_path = os.path.join(path, manifest.meta.launch_exe.lstrip("/"))
|
2021-12-05 12:56:51 +13:00
|
|
|
|
|
|
|
if not os.path.exists(exe_path):
|
|
|
|
logger.error(f"Launch Executable of {game.app_title} does not exist")
|
2021-12-24 22:09:50 +13:00
|
|
|
return _tr("LgdUtils", "Launch executable of {} does not exist").format(
|
|
|
|
game.app_title
|
|
|
|
)
|
2021-12-05 12:56:51 +13:00
|
|
|
|
|
|
|
if game.is_dlc:
|
2021-12-24 22:09:50 +13:00
|
|
|
release_info = game.metadata.get("mainGameItem", {}).get("releaseInfo")
|
2021-12-05 12:56:51 +13:00
|
|
|
if release_info:
|
2021-12-24 22:09:50 +13:00
|
|
|
main_game_appname = release_info[0]["appId"]
|
|
|
|
main_game_title = game.metadata["mainGameItem"]["title"]
|
2021-12-05 12:56:51 +13:00
|
|
|
if not core.is_installed(main_game_appname):
|
2021-12-24 22:09:50 +13:00
|
|
|
return _tr("LgdUtils", "Game is a DLC, but {} is not installed").format(
|
|
|
|
main_game_title
|
|
|
|
)
|
2021-12-05 12:56:51 +13:00
|
|
|
else:
|
|
|
|
return _tr("LgdUtils", "Unable to get base game information for DLC")
|
|
|
|
|
2021-03-10 04:52:51 +13:00
|
|
|
total = len(manifest.file_manifest_list.elements)
|
2021-12-24 22:09:50 +13:00
|
|
|
found = sum(
|
|
|
|
os.path.exists(os.path.join(path, f.filename))
|
|
|
|
for f in manifest.file_manifest_list.elements
|
|
|
|
)
|
2021-03-10 04:52:51 +13:00
|
|
|
ratio = found / total
|
2021-12-04 13:11:20 +13:00
|
|
|
|
2021-12-05 12:56:51 +13:00
|
|
|
if ratio < 0.9:
|
2021-12-04 13:11:20 +13:00
|
|
|
logger.warning(
|
2021-12-24 22:09:50 +13:00
|
|
|
"Game files are missing. It may be not the latest version or it is corrupt"
|
|
|
|
)
|
2021-12-04 13:11:20 +13:00
|
|
|
# return False
|
2021-03-10 04:52:51 +13:00
|
|
|
core.install_game(igame)
|
|
|
|
if igame.needs_verification:
|
2021-12-05 12:56:51 +13:00
|
|
|
logger.info(f"{igame.title} needs verification")
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2022-02-02 10:29:34 +13:00
|
|
|
logger.info(f"Successfully imported Game: {game.app_title}")
|
2021-12-05 12:56:51 +13:00
|
|
|
return ""
|