basic object for patchset grouping #3

Merged
tea merged 2 commits from patchset into main 2023-11-19 19:00:27 +00:00
Showing only changes of commit 40ed0ba9b4 - Show all commits

81
dodo.py
View File

@ -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}",