fixed untouched patched file content being erased if the mode was set to "edit"

This commit is contained in:
Faraphel 2022-07-10 22:54:26 +02:00
parent e403d1dce1
commit 69aa29c630

View file

@ -1,3 +1,4 @@
from io import BytesIO
from typing import Generator, IO
from source.mkw.Patch import *
@ -45,13 +46,15 @@ class PatchFile(PatchObject):
# if the file is a special file
if self.full_path.name.startswith("#"):
print(f"special file : {self} [install to {game_subpath}]")
# print(f"special file : {self} [install to {game_subpath}]")
return
# apply operation on the file
patch_source: Path = self.get_source_path(game_subpath)
patch_name: str = game_subpath.name
patch_content: IO = open(patch_source, "rb")
patch_content: BytesIO = BytesIO(open(patch_source, "rb").read())
# the file is converted into a BytesIO because if the mode is "edit",
# the file is overwritten and if the content is untouched, the file will only be lost
for operation_name, operation in self.configuration.get("operation", {}).items():
# process every operation and get the new patch_path (if the name is changed)
@ -63,11 +66,10 @@ class PatchFile(PatchObject):
match self.configuration["mode"]:
# if the mode is copy, replace the subfile in the game by the PatchFile
case "copy" | "edit":
print(f"[copy] copying {self} to {game_subpath}")
# print(f"[{self.configuration['mode']}] copying {self} to {game_subpath}")
game_subpath.parent.mkdir(parents=True, exist_ok=True)
with open(game_subpath.parent / patch_name, "wb") as file:
patch_content.seek(0)
file.write(patch_content.read())
# if the mode is match, replace all the subfiles that match match_regex by the PatchFile
@ -77,11 +79,10 @@ class PatchFile(PatchObject):
if not game_subfile.relative_to(extracted_game.path):
raise PathOutsidePatch(game_subfile, extracted_game.path)
# patch the game with the subpatch
print(f"[match] copying {self} to {game_subfile}")
# print(f"[match] copying {self} to {game_subfile}")
game_subfile.parent.mkdir(parents=True, exist_ok=True)
with open(game_subfile, "wb") as file:
patch_content.seek(0)
file.write(patch_content.read())
# ignore if mode is "ignore", useful if the file is used as a resource for an operation