Skip to content

Commit

Permalink
Merge pull request #127 from ZashIn/cyberpunk-fix-game-update
Browse files Browse the repository at this point in the history
Cyberpunk fix game update
  • Loading branch information
Silarn authored Nov 17, 2023
2 parents fba4bd8 + 6036615 commit 4d7a7cc
Showing 1 changed file with 60 additions and 16 deletions.
76 changes: 60 additions & 16 deletions games/game_cyberpunk2077.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import filecmp
import json
import re
import shutil
Expand Down Expand Up @@ -266,7 +267,7 @@ class PluginDefaultSettings:
plugin_name: str
settings: Mapping[str, mobase.MoVariant]

def is_plugin_enabled(self):
def is_plugin_enabled(self) -> bool:
return self.organizer.isPluginEnabled(self.plugin_name)

def apply(self) -> bool:
Expand All @@ -280,7 +281,7 @@ def apply(self) -> bool:
class Cyberpunk2077Game(BasicGame):
Name = "Cyberpunk 2077 Support Plugin"
Author = "6788, Zash"
Version = "2.2.2"
Version = "2.2.3"

GameName = "Cyberpunk 2077"
GameShortName = "cyberpunk2077"
Expand Down Expand Up @@ -389,6 +390,14 @@ def settings(self) -> list[mobase.PluginSetting]:
"Deploy redmod before game launch if necessary",
True,
),
mobase.PluginSetting(
"clear_cache_after_game_update",
(
'Clears "overwrite/r6/cache/*" if the original game files changed'
" (after update)"
),
True,
),
mobase.PluginSetting(
"configure_RootBuilder",
"Configures RootBuilder for Cyberpunk if installed and enabled",
Expand Down Expand Up @@ -536,17 +545,52 @@ def _map_cache_files(self):
overwritten game files.
"""
data_path = Path(self.dataDirectory().absolutePath())
if unmapped_cache_files := self._unmapped_cache_files(data_path):
qInfo('Copying "r6/cache/*" to overwrite (to catch file overwrites)')
overwrite_path = Path(self._organizer.overwritePath())
for file in unmapped_cache_files:
dst = overwrite_path / file.relative_to(data_path)
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(file, dst)

def _unmapped_cache_files(self, data_path: Path) -> list[Path]:
return [
path
for file in self._organizer.findFiles("r6/cache", "*")
if (path := Path(file).absolute()).is_relative_to(data_path)
]
overwrite_path = Path(self._organizer.overwritePath())
cache_files = list(data_path.glob("r6/cache/*"))
if self._get_setting("clear_cache_after_game_update") and any(
self._is_cache_file_updated(file.relative_to(data_path), data_path)
for file in cache_files
):
qInfo('Updated game files detected, clearing "overwrite/r6/cache/*"')
shutil.rmtree(overwrite_path / "r6/cache")
new_cache_files = cache_files
else:
new_cache_files = list(self._unmapped_cache_files(data_path))
for file in new_cache_files:
qInfo(f'Copying "{file}" to overwrite (to catch file overwrites)')
dst = overwrite_path / file
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(data_path / file, dst)

def _is_cache_file_updated(self, file: Path, data_path: Path) -> bool:
"""Checks if a cache file is updated (in game dir).
Args:
file: Relative to data dir.
"""
game_file = data_path.absolute() / file
mapped_files = self._organizer.findFiles(file.parent, file.name)
return bool(
mapped_files
and (mapped_file := mapped_files[0])
and not (
game_file.samefile(mapped_file)
or filecmp.cmp(game_file, mapped_file)
or ( # different backup file
(
backup_files := self._organizer.findFiles(
file.parent, f"{file.name}.bk"
)
)
and filecmp.cmp(game_file, backup_files[0])
)
)
)

def _unmapped_cache_files(self, data_path: Path) -> Iterable[Path]:
"""Yields unmapped cache files relative to `data_path`."""
for file in self._organizer.findFiles("r6/cache", "*"):
try:
yield Path(file).absolute().relative_to(data_path)
except ValueError:
continue

0 comments on commit 4d7a7cc

Please sign in to comment.