From a572a142ca85e56200ca34a7eb354aef1115e7bc Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sun, 19 Nov 2023 22:06:49 +0400 Subject: [PATCH 1/2] basic object for patchset grouping --- dodo.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/dodo.py b/dodo.py index 6c3f963..cbcf918 100644 --- a/dodo.py +++ b/dodo.py @@ -94,6 +94,17 @@ class ScriptedPatchwork(BasePatchwork): class RasterPatchwork(BasePatchwork): pass + +class Patchset(list): + def __init__(self, *args, **kwargs): + self.modimage_path = kwargs.pop('modimage_path') + super().__init__(*args, **kwargs) + + def image_files(self): + for patches in self: + if isinstance(patches, BasePatchwork): + pass + mod_name = 'randchgs' mod_install_path = get_var('descriptor_mod_path', 'C:/Users/User/Documents/Paradox Interactive/Hearts of Iron IV/mod/randchgs') dir_vanilla = Path(get_var('vanilla_path', '../vanilla/current')) -- 2.45.2 From 40ed0ba9b46b3f8301a4be96b5af728ffd5cd2da Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sun, 19 Nov 2023 22:59:07 +0400 Subject: [PATCH 2/2] some reorganisation of actions --- dodo.py | 81 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/dodo.py b/dodo.py index cbcf918..fc53544 100644 --- a/dodo.py +++ b/dodo.py @@ -92,18 +92,46 @@ class ScriptedPatchwork(BasePatchwork): self[relative_path] = data class RasterPatchwork(BasePatchwork): - pass + @staticmethod + def patch_action(target, original, patch): + from PIL import Image + with Image.open(original) as base, Image.open(patch) as patch: + def mask(): + for bg, p in zip(base.tobytes(), patch.tobytes()): + if p == 35: + yield bg + else: + yield p + + output = Image.frombytes(base.mode, base.size, bytes(mask())) + output.putpalette(base.palette) + output.save(target) + output.close() class Patchset(list): def __init__(self, *args, **kwargs): - self.modimage_path = kwargs.pop('modimage_path') + self.modimage_path = Path(kwargs.pop('modimage_path')) super().__init__(*args, **kwargs) def image_files(self): for patches in self: if isinstance(patches, BasePatchwork): - pass + for target in patches: + yield self.modimage_path/target + elif isinstance(patches, RawFiles): + for target in patches: + yield self.modimage_path/target.relative_to(patches.src_path) + + def image_sources(self): + for patches in self: + if isinstance(patches, BasePatchwork): + for target in patches: + yield patches.patchwork_path/target + elif isinstance(patches, RawFiles): + for target in patches: + yield target + mod_name = 'randchgs' mod_install_path = get_var('descriptor_mod_path', 'C:/Users/User/Documents/Paradox Interactive/Hearts of Iron IV/mod/randchgs') @@ -146,17 +174,15 @@ patchwork_raster = RasterPatchwork( vanilla_path = dir_vanilla ) - -def collect_image_files(patchworks, dir_modimage): - for pw in patchworks: - for target in pw: - yield dir_modimage/target -dir_image_files = list(collect_image_files( - [patchwork, patchwork_scripted, patchwork_raster], - dir_modimage=dir_modimage -)) -dir_image_files += [ dir_modimage/f.relative_to(dir_src_raw) for f in dir_src_raw_files ] - +patch_set = Patchset( + ( + dir_src_raw_files, + patchwork, + patchwork_scripted, + patchwork_raster, + ), + modimage_path = dir_modimage +) def task_patch_history_states_scripted(): def history_state_patch(task, provinces): @@ -187,22 +213,7 @@ def task_patch_history_states_scripted(): } def task_patch_raster(): - def raster_patch(target, original, patch): - from PIL import Image - with Image.open(original) as base, Image.open(patch) as patch: - def mask(): - for bg, p in zip(base.tobytes(), patch.tobytes()): - if p == 35: - yield bg - else: - yield p - - output = Image.frombytes(base.mode, base.size, bytes(mask())) - output.putpalette(base.palette) - output.save(target) - output.close() - - yield from patchwork_raster.tasks([raster_patch]) + yield from patchwork_raster.tasks([patchwork_raster.patch_action]) def task_patch(): def mkdir(target, original, patch): @@ -213,13 +224,9 @@ def task_patch(): ]) def task_image(): - filelist = dir_src_raw_files + \ - list(patchwork.targets()) + \ - list(patchwork_scripted.targets()) + \ - list(patchwork_raster.targets()) return { - 'file_dep' : filelist, - 'targets' : dir_image_files, + 'file_dep' : list(patch_set.image_sources()), + 'targets' : list(patch_set.image_files()), 'actions' : [f"mkdir -p {dir_modimage}", f"rsync -rv {dir_src_raw}/ {patchwork.patchwork_path}/"\ f" {dir_modimage}/" ], @@ -244,7 +251,7 @@ def task_image_mod(): def task_build(): return { - 'file_dep' : [f"{dir_image}/{mod_name}.mod" ] + dir_image_files, + 'file_dep' : [f"{dir_image}/{mod_name}.mod" ] + list (patch_set.image_files()), 'targets' : [f"{dir_image}/{mod_name}.zip" ], 'actions' : [ "rm -f %(targets)s", f"cd {dir_image} && zip -r {mod_name}.zip {mod_name}", -- 2.45.2