From 69aa29c630e437007d734aa7b0d8fb6da92a43d8 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sun, 10 Jul 2022 22:54:26 +0200 Subject: [PATCH] fixed untouched patched file content being erased if the mode was set to "edit" --- source/mkw/Patch/PatchFile.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/source/mkw/Patch/PatchFile.py b/source/mkw/Patch/PatchFile.py index 4b56ac2..33a45bd 100644 --- a/source/mkw/Patch/PatchFile.py +++ b/source/mkw/Patch/PatchFile.py @@ -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