"""Build scoped cooker and rail overlays for retained Wild Update installations.""" import argparse import json from pathlib import Path from plugin_wild_update import digest, read_archive, write_archive DEVICES = ("spice_furnace", "spice_smoker", "spice_blast_furnace", "spice_campfire", "spice_soul_campfire", "spice_coal") ITEMS = DEVICES + ("spice_tender",) ASSETS = {f"assets/dunerealms/{path}/crafting/{item}.{extension}" for item in ITEMS for path, extension in (("items", "json"), ("models/item", "json"), ("textures/item", "png"))} PREFIX = "com/dunerealms/jarvis/" CLASSES = {PREFIX + name + ".class" for name in ("QuestObservations", "CookSpeed", "SpiceFurnaces", "SpiceFurnaces$1")} META = "item-templates.json" CATALOG = "spice-crafting.json" GUI = "com/dunerealms/gui/" MENUS = "com/dunerealms/menus/" FRONTENDS = { "gui": (("DuneGUI", "OrganisationMenus", "CookbookCatalog"), GUI, CATALOG), "menus": (("HubMenu", "NativeServiceMenu"), MENUS, None), } def frontend_entries(kind, entries): families, prefix, resource = FRONTENDS[kind] roots = [prefix + family for family in families] selected = {name: data for name, data in entries.items() if name == resource or any( name == root + ".class" or name.startswith(root + "$") and name.endswith(".class") for root in roots)} expected = {root + ".class" for root in roots} | ({resource} if resource else set()) if not expected <= selected.keys(): raise ValueError(f"{kind} has incomplete Cookbook entry points") return selected def catalog_grades(catalog, policy): if len(catalog) != len(ITEMS) or {row["id"] for row in catalog} != set(ITEMS): raise ValueError("Unexpected spice Cookbook entries") for row in catalog: grade = policy[row["id"]] mark = row["mark"] if (mark != row["baseMark"] + 1 or grade["equipmentMark"] != mark or grade["material"] != row["material"] or grade["quality"] != row["quality"] or row["quality"] != ("common", "uncommon", "rare", "very_rare", "legendary")[mark // 3]): raise ValueError("Cookbook grade differs from the issued item: " + row["id"]) def encoded(value): return json.dumps(value, ensure_ascii=False, indent=2).encode("utf-8") + b"\n" def staged(source, compiled, gui, menus, output): jars = read_archive(compiled.read_bytes()) policy = json.loads(jars[META])["templates"] overlay = {name: jars[name] for name in CLASSES} overlay[META] = encoded({item: policy[item] for item in ITEMS}) gui_archive = read_archive(gui.read_bytes()) catalog_grades(json.loads(gui_archive[CATALOG]), policy) (output / "cookbook-gui.overlay.zip").write_bytes(write_archive(frontend_entries("gui", gui_archive))) (output / "cookbook-menus.overlay.zip").write_bytes( write_archive(frontend_entries("menus", read_archive(menus.read_bytes())))) (output / "spice-cookers.overlay.zip").write_bytes(write_archive(overlay)) (output / "spice-cookers.art.zip").write_bytes(write_archive({ name: (source / name).read_bytes() for name in ASSETS})) def assemble(kind, baseline, overlay): if kind in FRONTENDS: if frontend_entries(kind, overlay).keys() != overlay.keys(): raise ValueError("Unexpected entry in Cookbook frontend overlay") return baseline | overlay if kind == "jarvis": if set(overlay) != CLASSES | {META} or PREFIX + "ItemOriginRemainders.class" not in baseline: raise ValueError("Unexpected cooker classes or retained provenance layer") additions = json.loads(overlay[META]) policy = json.loads(baseline[META]) if set(additions) != set(ITEMS) or set(additions) & policy["templates"].keys(): raise ValueError("Expected new cooker item templates only") policy["templates"].update(additions) return baseline | overlay | {META: encoded(policy)} if set(overlay) != ASSETS or set(overlay) & baseline.keys(): raise ValueError("Expected only the new recipe models and sprites") return baseline | overlay def main(): parser = argparse.ArgumentParser(description=__doc__) commands = parser.add_subparsers(dest="action", required=True) pack = commands.add_parser("extract") for field in ("source", "compiled", "gui", "menus", "output"): pack.add_argument(field, type=Path) install = commands.add_parser("assemble") install.add_argument("kind", choices=("jarvis", "pack", *FRONTENDS)) for field in ("baseline", "overlay", "output"): install.add_argument(field, type=Path) install.add_argument("baseline_sha") install.add_argument("overlay_sha") args = parser.parse_args() if args.action == "extract": staged(args.source, args.compiled, args.gui, args.menus, args.output) return baseline, overlay = args.baseline.read_bytes(), args.overlay.read_bytes() if digest(baseline) != args.baseline_sha or digest(overlay) != args.overlay_sha: raise ValueError("Unexpected cooker baseline or payload") result = write_archive(assemble(args.kind, read_archive(baseline), read_archive(overlay))) with args.output.open("xb") as target: target.write(result) print(digest(result)) if __name__ == "__main__": main()