Fix compacting
This commit is contained in:
+17
-13
@@ -45,10 +45,13 @@ def compact_chains(data: object) -> list[list[object]]:
|
||||
|
||||
for chain in chains:
|
||||
if isinstance(chain, list):
|
||||
if len(chain) != 3:
|
||||
raise ValueError("Invalid compact chain entry")
|
||||
compact.append(chain)
|
||||
continue
|
||||
if len(chain) == 2 and isinstance(chain[1], list):
|
||||
compact.append(chain)
|
||||
continue
|
||||
if len(chain) == 3 and isinstance(chain[1], int) and isinstance(chain[2], list):
|
||||
compact.append([chain[0], chain[2]])
|
||||
continue
|
||||
raise ValueError("Invalid compact chain entry")
|
||||
|
||||
if not isinstance(chain, dict):
|
||||
raise ValueError("Expected verbose or compact chain entries as input")
|
||||
@@ -58,16 +61,15 @@ def compact_chains(data: object) -> list[list[object]]:
|
||||
continue
|
||||
|
||||
base_id = chain["base_id"]
|
||||
base_icon = crop_icon(items[0].get("icon_nfp_16x16"))
|
||||
compact_items = []
|
||||
compact_items: list[list[object]] = []
|
||||
|
||||
for item in items[1:]:
|
||||
for item in items:
|
||||
compact_items.append([
|
||||
item["item_id"],
|
||||
crop_icon(item.get("icon_nfp_16x16")),
|
||||
])
|
||||
|
||||
compact.append([base_id, base_icon, compact_items])
|
||||
compact.append([base_id, compact_items])
|
||||
|
||||
return compact
|
||||
|
||||
@@ -76,17 +78,19 @@ def main() -> int:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Rewrite atc_chains.json into a compact format used by compcount.lua."
|
||||
)
|
||||
parser.add_argument("path", nargs="?", default="atc_chains.json")
|
||||
parser.add_argument("source", nargs="?", default="atc_chains_uncompressed.json")
|
||||
parser.add_argument("destination", nargs="?", default="atc_chains.json")
|
||||
args = parser.parse_args()
|
||||
|
||||
path = Path(args.path)
|
||||
original_text = path.read_text(encoding="utf-8")
|
||||
source_path = Path(args.source)
|
||||
destination_path = Path(args.destination)
|
||||
original_text = source_path.read_text(encoding="utf-8")
|
||||
data = json.loads(original_text)
|
||||
compact = {"chains": compact_chains(data)}
|
||||
compact_text = json.dumps(compact, separators=(",", ":"))
|
||||
path.write_text(compact_text, encoding="utf-8")
|
||||
destination_path.write_text(compact_text, encoding="utf-8")
|
||||
|
||||
print(f"Rewrote {path}")
|
||||
print(f"Compacted {source_path} -> {destination_path}")
|
||||
print(f"Old size: {len(original_text.encode('utf-8'))} bytes")
|
||||
print(f"New size: {len(compact_text.encode('utf-8'))} bytes")
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user