basic object for patchset grouping #3
							
								
								
									
										88
									
								
								dodo.py
									
									
									
									
									
								
							
							
						
						
									
										88
									
								
								dodo.py
									
									
									
									
									
								
							| @ -92,7 +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 = Path(kwargs.pop('modimage_path')) | ||||
|         super().__init__(*args, **kwargs) | ||||
| 
 | ||||
|     def image_files(self): | ||||
|         for patches in self: | ||||
|             if isinstance(patches, BasePatchwork): | ||||
|                 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') | ||||
| @ -135,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): | ||||
| @ -176,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): | ||||
| @ -202,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}/" ], | ||||
| @ -233,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}", | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user