plugins/tools/quest_arc_artwork.py

70 lines
3.1 KiB
Python

"""Add the reviewed quest arc artwork to the retained network pack or DuneMenus artwork cache."""
import argparse
import json
from pathlib import Path
import struct
from plugin_wild_update import digest, read_archive, write_archive
NETWORK_SHA256 = "8a1bd9c2784be4231ffd2f29e9dbcb43a04eb69a5c14966077084a916769fd84"
ASSETS = "assets/dunerealms/"
CATALOG = "DuneRealmsGUI/src/main/resources/quest-categories.json"
OVERLAY = "network/resource-pack/central/overlay"
MENU_PREFIX = "menu-art/pack/"
def artwork(source):
catalog = json.loads((source / CATALOG).read_text(encoding="utf-8"))
ids = {category["id"] for category in catalog["categories"]} | {"other"}
if len(ids) != 14:
raise ValueError("Expected the thirteen story arcs and Other Work")
overlay = source / OVERLAY
paths = {}
for arc in sorted(ids):
model = f"{ASSETS}models/item/quests/{arc}.json"
definition = f"{ASSETS}items/quests/{arc}.json"
image = f"{ASSETS}textures/item/quests/{arc}.png"
definition_data = (overlay / definition).read_bytes()
model_data = (overlay / model).read_bytes()
image_data = (overlay / image).read_bytes()
if json.loads(definition_data)["model"] != {
"type": "minecraft:model",
"model": f"dunerealms:item/quests/{arc}",
}:
raise ValueError(f"Unexpected item definition for {arc}")
if json.loads(model_data)["textures"]["layer0"] != f"dunerealms:item/quests/{arc}":
raise ValueError(f"Unexpected texture reference for {arc}")
if image_data[:16] != b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR" or struct.unpack(
">II", image_data[16:24]
) != (32, 32):
raise ValueError(f"Expected a 32px PNG for {arc}")
paths.update({definition: definition_data, model: model_data, image: image_data})
return paths
def assemble(source, baseline, output, target, baseline_sha=None):
existing = baseline.read_bytes()
if digest(existing) != (NETWORK_SHA256 if target == "network" else baseline_sha):
raise ValueError("Unexpected retained archive checksum")
base = read_archive(existing)
added = artwork(source)
if target == "menus":
if "com/dunerealms/menus/MenuArt.class" not in base:
raise ValueError("Expected the retained DuneMenus artwork resolver")
added = {MENU_PREFIX + name: contents for name, contents in added.items()}
if set(added) & set(base):
raise ValueError("Quest artwork already exists in the retained archive")
base.update(added)
with output.open("xb") as candidate:
candidate.write(write_archive(base))
print(f"{target}: added {len(added)} files, SHA256 {digest(output.read_bytes())}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("source", type=Path)
parser.add_argument("baseline", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("target", choices=("network", "menus"))
parser.add_argument("--baseline-sha")
assemble(**vars(parser.parse_args()))