Skip to content

Commit 4d7a7cc

Browse files
authored
Merge pull request #127 from ZashIn/cyberpunk-fix-game-update
Cyberpunk fix game update
2 parents fba4bd8 + 6036615 commit 4d7a7cc

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

games/game_cyberpunk2077.py

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import filecmp
12
import json
23
import re
34
import shutil
@@ -266,7 +267,7 @@ class PluginDefaultSettings:
266267
plugin_name: str
267268
settings: Mapping[str, mobase.MoVariant]
268269

269-
def is_plugin_enabled(self):
270+
def is_plugin_enabled(self) -> bool:
270271
return self.organizer.isPluginEnabled(self.plugin_name)
271272

272273
def apply(self) -> bool:
@@ -280,7 +281,7 @@ def apply(self) -> bool:
280281
class Cyberpunk2077Game(BasicGame):
281282
Name = "Cyberpunk 2077 Support Plugin"
282283
Author = "6788, Zash"
283-
Version = "2.2.2"
284+
Version = "2.2.3"
284285

285286
GameName = "Cyberpunk 2077"
286287
GameShortName = "cyberpunk2077"
@@ -389,6 +390,14 @@ def settings(self) -> list[mobase.PluginSetting]:
389390
"Deploy redmod before game launch if necessary",
390391
True,
391392
),
393+
mobase.PluginSetting(
394+
"clear_cache_after_game_update",
395+
(
396+
'Clears "overwrite/r6/cache/*" if the original game files changed'
397+
" (after update)"
398+
),
399+
True,
400+
),
392401
mobase.PluginSetting(
393402
"configure_RootBuilder",
394403
"Configures RootBuilder for Cyberpunk if installed and enabled",
@@ -536,17 +545,52 @@ def _map_cache_files(self):
536545
overwritten game files.
537546
"""
538547
data_path = Path(self.dataDirectory().absolutePath())
539-
if unmapped_cache_files := self._unmapped_cache_files(data_path):
540-
qInfo('Copying "r6/cache/*" to overwrite (to catch file overwrites)')
541-
overwrite_path = Path(self._organizer.overwritePath())
542-
for file in unmapped_cache_files:
543-
dst = overwrite_path / file.relative_to(data_path)
544-
dst.parent.mkdir(parents=True, exist_ok=True)
545-
shutil.copy2(file, dst)
546-
547-
def _unmapped_cache_files(self, data_path: Path) -> list[Path]:
548-
return [
549-
path
550-
for file in self._organizer.findFiles("r6/cache", "*")
551-
if (path := Path(file).absolute()).is_relative_to(data_path)
552-
]
548+
overwrite_path = Path(self._organizer.overwritePath())
549+
cache_files = list(data_path.glob("r6/cache/*"))
550+
if self._get_setting("clear_cache_after_game_update") and any(
551+
self._is_cache_file_updated(file.relative_to(data_path), data_path)
552+
for file in cache_files
553+
):
554+
qInfo('Updated game files detected, clearing "overwrite/r6/cache/*"')
555+
shutil.rmtree(overwrite_path / "r6/cache")
556+
new_cache_files = cache_files
557+
else:
558+
new_cache_files = list(self._unmapped_cache_files(data_path))
559+
for file in new_cache_files:
560+
qInfo(f'Copying "{file}" to overwrite (to catch file overwrites)')
561+
dst = overwrite_path / file
562+
dst.parent.mkdir(parents=True, exist_ok=True)
563+
shutil.copy2(data_path / file, dst)
564+
565+
def _is_cache_file_updated(self, file: Path, data_path: Path) -> bool:
566+
"""Checks if a cache file is updated (in game dir).
567+
568+
Args:
569+
file: Relative to data dir.
570+
"""
571+
game_file = data_path.absolute() / file
572+
mapped_files = self._organizer.findFiles(file.parent, file.name)
573+
return bool(
574+
mapped_files
575+
and (mapped_file := mapped_files[0])
576+
and not (
577+
game_file.samefile(mapped_file)
578+
or filecmp.cmp(game_file, mapped_file)
579+
or ( # different backup file
580+
(
581+
backup_files := self._organizer.findFiles(
582+
file.parent, f"{file.name}.bk"
583+
)
584+
)
585+
and filecmp.cmp(game_file, backup_files[0])
586+
)
587+
)
588+
)
589+
590+
def _unmapped_cache_files(self, data_path: Path) -> Iterable[Path]:
591+
"""Yields unmapped cache files relative to `data_path`."""
592+
for file in self._organizer.findFiles("r6/cache", "*"):
593+
try:
594+
yield Path(file).absolute().relative_to(data_path)
595+
except ValueError:
596+
continue

0 commit comments

Comments
 (0)