HOI4_randomchanges/dodo.py

243 lines
8.4 KiB
Python
Raw Normal View History

2023-11-11 16:37:22 +00:00
#!/usr/bin/env python3
from pathlib import Path
2023-11-17 12:40:27 +00:00
from doit import get_var
DOIT_CONFIG = {
'default_tasks': ['build'],
'action_string_formatting': 'new'
}
2023-11-12 17:40:03 +00:00
def files(p):
for i in p:
if i.is_file():
yield i
2023-11-17 12:40:27 +00:00
class RawFiles(list):
def __init__(self, *args, **kwargs):
self.src_path = Path(kwargs.pop('src_path'))
self.image_path = Path(kwargs.pop('image_path'))
super().__init__(*args, **kwargs)
2023-11-11 16:37:22 +00:00
2023-11-17 12:40:27 +00:00
def sources(self):
for i in self:
yield self.src_path/i
def targets(self):
for i in self:
yield self.image_path/i
class BasePatchwork(dict):
2023-11-12 17:40:03 +00:00
patches_path = None
patchwork_path = None
2023-11-17 12:40:27 +00:00
vanilla_path = None
2023-11-12 17:40:03 +00:00
def __init__(self, *arkg, **kwargs):
2023-11-17 12:40:27 +00:00
self.patches_path = Path(kwargs.pop('patches_path'))
self.patchwork_path = Path(kwargs.pop('patchwork_path'))
self.vanilla_path = Path(kwargs.pop('vanilla_path'))
2023-11-12 17:40:03 +00:00
super().__init__(*arkg, **kwargs)
2023-11-11 16:37:22 +00:00
def targets(self):
for i in self.keys():
2023-11-12 17:40:03 +00:00
yield self.patchwork_path/i
2023-11-17 12:40:27 +00:00
def key_vanilla(self, orig_key):
return self.vanilla_path/orig_key
def key_target(self, orig_key):
return self.patchwork_path/orig_key
def tasks(self, actions):
def repack_action(action, **kwargs):
if callable(action):
return (action, [], kwargs)
elif isinstance(action, str):
return action.format(**kwargs)
else:
return action
2023-11-12 17:40:03 +00:00
for k, v in self.items():
2023-11-17 12:40:27 +00:00
original = self.vanilla_path/k
patch = self.patches_path/v
target = self.patchwork_path/k
yield {
'name' : k,
'file_dep' : [ original, patch ],
'targets' : [ target ],
'actions' : [ repack_action(action, original=original, patch=patch, target=target)
for action in actions ],
'clean' : True
}
class Patchwork(BasePatchwork):
def tasks_(self, actions):
for k, v in self.items():
yield {
'name' : k,
'file_dep' : [ self.patches_path/v, self.vanilla_path/k ],
'targets' : [ self.patchwork_path/k ],
'actions' : [
2023-11-12 17:40:03 +00:00
f"mkdir -p {self.patchwork_path}/{Path(k).parent}",
f"patch -u --binary -N -o '{self.patchwork_path}/{k}' "\
2023-11-17 12:40:27 +00:00
f"'{self.vanilla_path}/{k}' '{self.patches_path}/{v}'"
], # action ( mkdir, patch )
'clean' : True
}
class ScriptedPatchwork(BasePatchwork):
def __init__(self, *args, **kwargs):
from yaml import safe_load
file = kwargs.pop('source_file')
super().__init__(*args, **kwargs)
with open(file) as s:
for key, data in safe_load(s).items():
discovered_source = next((Path(self.vanilla_path)/'history'/'states').glob(f"{key}*"))
relative_path = discovered_source.relative_to(self.vanilla_path)
self[relative_path] = data
class RasterPatchwork(BasePatchwork):
pass
2023-11-15 20:26:12 +00:00
2023-11-12 17:40:03 +00:00
mod_name = 'randchgs'
2023-11-17 12:40:27 +00:00
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'))
2023-11-12 17:40:03 +00:00
dir_image = Path('build/image')
2023-11-17 12:40:27 +00:00
dir_modimage = dir_image/mod_name
2023-11-12 17:40:03 +00:00
dir_src_raw = Path('src/raw')
2023-11-17 12:40:27 +00:00
dir_src_raw_files = RawFiles(
files(Path(dir_src_raw).rglob("*")),
src_path = dir_src_raw,
image_path = dir_image
)
2023-11-12 17:40:03 +00:00
dir_patches_path = Path('src/patches')
dir_patchwork_path = Path('build/patched')
2023-11-15 20:26:12 +00:00
2023-11-17 12:40:27 +00:00
patchwork = Patchwork(
{
'map/definition.csv': 'map_definition.patch',
'history/countries/ISR - Israel.txt': 'history_countries_ISR.patch',
'history/states/454-Israel.txt': 'history_states_454.patch',
},
2023-11-15 20:26:12 +00:00
patches_path = dir_patches_path,
2023-11-17 12:40:27 +00:00
patchwork_path = dir_patchwork_path,
vanilla_path = dir_vanilla
2023-11-15 20:26:12 +00:00
)
2023-11-17 12:40:27 +00:00
patchwork_scripted = ScriptedPatchwork(
source_file = dir_patches_path/'extrapoint.yaml',
patches_path = '.',
patchwork_path = dir_patchwork_path,
vanilla_path = dir_vanilla
)
2023-11-11 16:37:22 +00:00
2023-11-17 12:40:27 +00:00
patchwork_raster = RasterPatchwork(
{
'map/terrain.bmp' : 'terrain.bmp',
},
patches_path = dir_patches_path,
patchwork_path = dir_patchwork_path,
vanilla_path = dir_vanilla
)
2023-11-11 16:37:22 +00:00
2023-11-12 17:40:03 +00:00
2023-11-17 12:40:27 +00:00
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 ]
2023-11-11 16:37:22 +00:00
2023-11-12 17:40:03 +00:00
def task_patch_history_states_scripted():
def history_state_patch(task, provinces):
source = Path(next(iter(task.file_dep)))
target = Path(task.targets[0])
target.parent.mkdir(parents=True, exist_ok=True)
seen_history = False
wrote_history = False
2023-11-17 12:40:27 +00:00
with source.open(newline='') as infile, target.open('w') as outfile:
for line_in in infile:
outfile.write(line_in)
seen_history = seen_history or ( 'history' in line_in )
if not wrote_history and seen_history and '{' in line_in:
for province_id, province_info in provinces.items():
outfile.write(
"\t\tvictory_points = {\r\n"\
f"\t\t\t{province_id} {province_info['points']}\r\n"\
"\t\t}\r\n"\
)
wrote_history = True
for state_src, state_data in patchwork_scripted.items():
2023-11-12 17:40:03 +00:00
yield {
2023-11-17 12:40:27 +00:00
'name' : state_src,
'file_dep' : [ patchwork_scripted.key_vanilla(state_src) ],
'targets' : [ patchwork_scripted.key_target(state_src) ],
2023-11-12 17:40:03 +00:00
'actions' : [ ( history_state_patch, [], {'provinces' : state_data}) ],
'clean' : True
}
2023-11-17 12:40:27 +00:00
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])
2023-11-12 17:40:03 +00:00
def task_patch():
2023-11-17 12:40:27 +00:00
def mkdir(target, original, patch):
Path(target).parent.mkdir(parents=True, exist_ok=True)
yield from patchwork.tasks([
mkdir,
"patch -u --binary -N -o '{target}' '{original}' '{patch}'"
])
2023-11-12 17:40:03 +00:00
2023-11-11 16:37:22 +00:00
def task_image():
2023-11-17 12:40:27 +00:00
filelist = dir_src_raw_files + \
list(patchwork.targets()) + \
list(patchwork_scripted.targets()) + \
list(patchwork_raster.targets())
2023-11-11 16:37:22 +00:00
return {
2023-11-17 12:40:27 +00:00
'file_dep' : filelist,
2023-11-12 17:40:03 +00:00
'targets' : dir_image_files,
'actions' : [f"mkdir -p {dir_modimage}",
f"rsync -rv {dir_src_raw}/ {patchwork.patchwork_path}/"\
f" {dir_modimage}/" ],
'clean' : True
2023-11-11 16:37:22 +00:00
}
def task_image_mod():
2023-11-12 17:40:03 +00:00
def prepare_modname_mod(task):
source = Path(next(iter(task.file_dep)))
target = Path(task.targets[0])
target.parent.mkdir(parents=True, exist_ok=True)
with source.open() as s:
with target.open('w') as o:
2023-11-11 16:37:22 +00:00
o.write(s.read())
2023-11-17 12:40:27 +00:00
o.write(f"\npath=\"{mod_install_path}\"\n")
2023-11-11 16:37:22 +00:00
return {
2023-11-17 12:40:27 +00:00
'file_dep' : [ 'src/raw/descriptor.mod' ],
2023-11-12 17:40:03 +00:00
'targets' : [f"{dir_image}/{mod_name}.mod" ],
'actions' : [ prepare_modname_mod ],
'clean' : True
2023-11-11 16:37:22 +00:00
}
def task_build():
return {
2023-11-12 17:40:03 +00:00
'file_dep' : [f"{dir_image}/{mod_name}.mod" ] + dir_image_files,
'targets' : [f"{dir_image}/{mod_name}.zip" ],
2023-11-17 12:40:27 +00:00
'actions' : [ "rm -f %(targets)s",
f"cd {dir_image} && zip -r {mod_name}.zip {mod_name}",
2023-11-12 17:40:03 +00:00
f"cd {dir_image} && zip -r {mod_name}.zip {mod_name}.mod" ],
2023-11-17 12:40:27 +00:00
'clean' : [ 'rm -rf build' ]
2023-11-11 16:37:22 +00:00
}