"""Build deterministic progression artifacts from the original APK, offline. Usage: python tools/build_progression_data.py [--apk PATH] [--output DIRECTORY] The manifest binds every recovered table, script summary, retained AS3 source, and output artifact to SHA-256. No account data or network calls are used. """ from __future__ import annotations import argparse from collections import Counter import gzip import hashlib import io import json from pathlib import Path import sys import zipfile import re import zlib ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) from tools.research_battle_v12 import recover_table from ranch_assistant.game_data import ( BATTLES_NAME, DATA_NAME, MANIFEST_NAME, ORIGINAL_APK_SHA256, SCHEMA_VERSION, ) TABLES = ("task_info", "stage_info", "stage_sp", "setting", "game_setting", "npc_info", "area_info", "mine_info") SOURCES = ( "center/TaskCenter.as", "center/PosCenter.as", "center/GuideCenter.as", "center/MonsterCenter.as", "center/BagCenter.as", "center/StageCenter.as", "element/task/TaskStepVo.as", "element/task/TaskVo.as", "element/task/TaskType.as", "element/task/TaskStatusType.as", "element/npc/ConditionVo.as", "element/stage/StageVo.as", "element/city/CityVo.as", "net/NetTask.as", "net/NetBattle.as", "panel/taskstartpanel/TaskStartPanel.as", "panel/story/StoryPanel.as", "panel/stage/StageStartPanel.as", "panel/story/GAoNanoEventVo.as", "center/LibCenter.as", "panel/wild/AreaVo.as", "panel/wild/AreaIconUI.as", "panel/wild/AreaPositionIconUI.as", "panel/wild/WildPanel.as", "panel/wild/StageMap.as", "panel/loopStagePanel/LoopStagePanel.as", "battle/BattleGround.as", "panel/battle/BattlePanel.as", "element/reward/RewardPieceVo.as", "panel/stage/StagePanel.as", "panel/task/TaskViewPanel.as", ) # These commands affect presentation only. Control-flow and arbitrary events # are rejected, even though the original player supports additional commands. PRESENTATION_COMMANDS = frozenset({ "tag", "text", "wait", "top", "setLayer", "moveLayer", "playSound", "stopSound", "shake", "flash", "localBgm", "end", }) def digest(value): return hashlib.sha256(value).hexdigest() def encode(value): return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False).encode("utf-8") def repair_text(value): """Same reversible legacy-text repair as the existing catalog builder.""" if isinstance(value, dict): return {str(key): repair_text(item) for key, item in value.items()} if isinstance(value, list): return [repair_text(item) for item in value] if isinstance(value, str): try: return value.encode("latin1").decode("gbk") except (UnicodeEncodeError, UnicodeDecodeError): pass return value def summarize_script(raw, member): script = json.loads(raw) expressions = script.get("exps") blocked, events, commands, lines = [], [], Counter(), 0 if not isinstance(expressions, list): expressions = [] blocked.append("missing expression list") ended = False for expression in expressions: if ended: break key, para = expression.get("key"), expression.get("para") or {} commands[str(key)] += 1 if key == "event": events.append(para) if para.get("event") != "GAoEvent" or str(para.get("eventData", "")).strip() not in ("true", "false"): blocked.append("special event") elif key not in PRESENTATION_COMMANDS: blocked.append(f"unsupported command: {key}") if key == "text": lines += 1 # Inline GAo commands can contain their own branching/event code. tags = re.findall(r"\[([^\]]*)\]", para.get("text", "")) supported = {"show", "hide", "l", "r", "cm", "color", "colorEnd", "wait", "br", "b", "bEnd", "i", "iEnd", "size", "sizeEnd", "align", "alignEnd"} for tag in tags: if re.fullmatch(r"\{gn\.[A-Za-z_][A-Za-z0-9_]*\}", tag): continue # Escaped dialogue placeholders, not script commands. if tag.split("=", 1)[0] not in supported: blocked.append(f"unsupported inline command: {tag.split('=', 1)[0]}") if key == "end": if para.get("end") is True: ended = True else: blocked.append("unverified end command") answer = None if events: answer = str(events[-1].get("eventData", "")).startswith("true") return {"member": member, "sha256": digest(raw), "commands": dict(commands), "dialogue_lines": lines, "events": events, "answer": answer, "supported": not blocked, "block_reasons": sorted(set(blocked))} def support_report(tasks, scripts): uses, mainline = {}, set() special_acceptance, conditions, guides = [], [], [] for key, task in sorted(tasks.items()): for number, step in enumerate([task.get("startStep") or {}] + (task.get("steps") or [])): for phase in ("story", "afterStory"): name = step.get(phase) if name: uses.setdefault(name, []).append({"task_type_id": key, "step_number": number, "phase": phase}) if task.get("type") == 1: mainline.add(name) if step.get("passGuide"): guides.append({"task_type_id": key, "step_number": number, "record": step["passGuide"]}) condition = step.get("condition") or {} if condition.get("passStage") or condition.get("lang") not in (None, "", "all"): conditions.append({"task_type_id": key, "step_number": number, "condition": condition, "reason": "Retained ConditionVo loader anomaly; declared conditions checked conservatively"}) start = task.get("startStep") or {} if (task.get("type") in (1, 2) and start.get("position") and start.get("npc") and (not start.get("story") or scripts[start["story"]].get("answer") is not True)): special_acceptance.append(key) return { "source_apk_sha256": ORIGINAL_APK_SHA256, "scope": "Static planning support only; no game server calls or combat execution.", "script_classification_basis": "Original GAo expression keys and literal text formatting; StoryPanel/GAoNanoEventVo event handlers. Bundled GAo renderer implementation is not retained.", "counts": {"referenced_scripts": len(scripts), "linear_scripts": sum(item["supported"] for item in scripts.values()), "mainline_referenced_scripts": len(mainline), "mainline_linear_scripts": sum(scripts[name]["supported"] for name in mainline)}, "unsupported_scripts": {name: {"reasons": item["block_reasons"], "uses": uses[name]} for name, item in sorted(scripts.items()) if not item["supported"]}, "special_acceptance_task_types": special_acceptance, "guide_gates": guides, "condition_loader_anomalies": conditions, "unknown_task_types": {key: task.get("type") for key, task in sorted(tasks.items()) if task.get("type") not in (1, 2, 3, 4, 5)}, "special_local_hooks": [{"task_type_id": "1001006", "step_number": 4, "action": "GachaPanel.popInGacha('task1')", "supported": False}], "delegated_categories": {"3": "event", "4": "commission_plan driven by existing QuestPolicy", "5": "bond"}, "supported_nonbattle_statuses": ["none", "getItem", "getMonster", "getMoney", "killMonster", "passLevel", "unlockMonster", "sysLock"], "battle_routing": {"battle": "battleStart(task) -> real battle -> taskCompleteStep", "passStage": "same task route; stage configuration; never a sweep"}, } def farming_index(stages): """Potential sources, never fabricated rewards/kills or drop guarantees.""" index = {"kills": {}, "items": {}} def number(value, default=0): try: return float(value) except (ValueError, TypeError): return default for stage_id, stage in sorted(stages.items()): for difficulty, level in (stage.get("level") or {}).items(): battle = level.get("battle") or {} if not isinstance(battle, dict) or not battle: continue counts = Counter() for team in battle.get("teams") or []: for monster in team.get("mons") or []: # BattleGround.deadHandler: team != 1, !isObject, mode==2. key = str(monster.get("type", "")) if ("team" in monster and number(monster["team"]) != 1 and not monster.get("isObject") and number(monster.get("mode")) == 2 and key.isdigit()): counts[key] += 1 record = {"stage_id": str(stage_id), "difficulty": int(difficulty)} for key, count in sorted(counts.items()): index["kills"].setdefault(key, []).append(record | {"configured_enemies": count}) items = set() for group in (level.get("reward") or {}).get("rewards") or []: if number(group.get("item_num")) > 0 and number(group.get("item_chance"), 1) > 0: for item in group.get("items") or []: parts = str(item.get("name", "")).split(",") maximum = number(parts[2] if len(parts) > 2 else 1) if parts[0].isdigit() and maximum > 0 and number(item.get("value"), 1) > 0: items.add(parts[0]) if number(group.get("monMode")) == 0 and number(group.get("num")) > 0 and number(group.get("chance"), 1) > 0: for monster in group.get("monsters") or []: key = str(monster.get("name", "")) if key.isdigit() and number(monster.get("value"), 1) > 0: items.add(key) for key in sorted(items): index["items"].setdefault(key, []).append(record) return index def build(apk, output): apk, output = Path(apk), Path(output) with apk.open("rb") as stream: apk_sha256 = hashlib.file_digest(stream, "sha256").hexdigest() if apk_sha256 != ORIGINAL_APK_SHA256: raise ValueError("Only the verified original 1.9.68 APK is accepted") tables, table_manifest = {}, {} with zipfile.ZipFile(apk) as archive: main_bytes = archive.read("assets/MYRMain.app") for name in TABLES: original, metadata = recover_table(archive, name) value = original.get("datas", original) if isinstance(original, dict) else original metadata["record_count"] = len(value) metadata["recovered_sha256"] = digest(encode(original)) tables[name] = repair_text(value) table_manifest[name] = metadata tasks = tables["task_info"] references = {str(step[key]) for task in tasks.values() for step in [task.get("startStep") or {}] + (task.get("steps") or []) for key in ("story", "afterStory") if step.get(key)} scripts = {} for name in sorted(references): member = f"assets/GAoNano/script/{name}.json" scripts[name] = summarize_script(archive.read(member), member) stages = {str(row["id"]): row for row in tables["stage_info"]} farm_sources = farming_index(stages) tables["stage_info"] = {} members = {} output.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(output / BATTLES_NAME, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as archive: for key, stage in sorted(stages.items()): battles = {str(level): value.get("battle") for level, value in (stage.get("level") or {}).items()} raw = encode(battles) member = f"stages/{key}.json" info = zipfile.ZipInfo(member, date_time=(1980, 1, 1, 0, 0, 0)) info.create_system = 3 info.external_attr = 0o100644 << 16 info.compress_type = zipfile.ZIP_DEFLATED archive.writestr(info, raw, compresslevel=9) members[key] = {"member": member, "sha256": digest(raw)} tables["stage_info"][key] = {field: value for field, value in stage.items() if field != "level"} tables["stage_info"][key]["level"] = { str(level): {field: item for field, item in value.items() if field != "battle"} | {"battle_available": bool(value.get("battle"))} for level, value in (stage.get("level") or {}).items()} data = {"schema_version": SCHEMA_VERSION, **tables, "scripts": scripts, "battle_members": members, "farming": farm_sources} raw = encode(data) compressed_stream = io.BytesIO() with gzip.GzipFile(filename="", fileobj=compressed_stream, mode="wb", compresslevel=9, mtime=0) as stream: stream.write(raw) compressed = compressed_stream.getvalue() (output / DATA_NAME).write_bytes(compressed) support_name = "progression_support.json" support = support_report(tasks, scripts) support["farming"] = {"kill_species": len(farm_sources["kills"]), "item_or_egg_types": len(farm_sources["items"]), "kill_rule": "team != 1 and !isObject and mode == 2; declared battle teams only", "sweep_kill_credit": False, "loot": "Configured potential rewards; actual inventory and kills must be refreshed after settlement", "unindexed": "Dynamic controller/skill spawns and external reward sources"} support_bytes = (json.dumps(support, ensure_ascii=False, indent=2, sort_keys=True) + "\n").encode("utf-8") (output / support_name).write_bytes(support_bytes) source_root = ROOT / "analysis/as3/scripts/net/pixvi/myr" manifest = { "schema_version": SCHEMA_VERSION, "builder": {"sha256": digest(Path(__file__).read_bytes()), "python": sys.version.split()[0], "zlib": zlib.ZLIB_VERSION}, "source": {"apk_version": "1.9.68", "apk_sha256": apk_sha256, "main_swf_sha256": digest(main_bytes), "amf_reader_sha256": digest((ROOT / "ranch_assistant/amf.py").read_bytes())}, "tables": table_manifest, "as3_sources": {str((source_root / name).relative_to(ROOT)).replace("\\", "/"): digest((source_root / name).read_bytes()) for name in SOURCES}, "artifacts": {DATA_NAME: {"sha256": digest(compressed), "bytes": len(compressed), "decoded_sha256": digest(raw), "decoded_bytes": len(raw)}, BATTLES_NAME: {"sha256": digest((output / BATTLES_NAME).read_bytes()), "bytes": (output / BATTLES_NAME).stat().st_size}, support_name: {"sha256": digest(support_bytes), "bytes": len(support_bytes)}}, "counts": {"tasks": len(tasks), "steps": sum(len(task.get("steps") or []) for task in tasks.values()), "task_types": dict(Counter(str(task.get("type")) for task in tasks.values())), "stages": len(stages), "scripts": len(scripts), "areas": len(tables["area_info"].get("areaInfo", {})), "mines": len(tables["mine_info"].get("mines", {})), "supported_scripts": sum(script["supported"] for script in scripts.values()), **support["counts"]}, "task_update_mode": "legacy" if tables["setting"].get("useOldNewTaskFunction") is True else "new", "limitations": ["Static original APK baseline; live resource version not verified.", "Script support means linear dialogue/presentation and boolean GAoEvent only.", "ConditionVo passStage loading and lang assignment anomalies require conservative checks.", "No account data, network operations, combat execution, or battle results."], } (output / MANIFEST_NAME).write_text(json.dumps(manifest, ensure_ascii=False, indent=2, sort_keys=True) + "\n", encoding="utf-8") return manifest def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--apk", type=Path) parser.add_argument("--output", type=Path, default=ROOT / "ranch_assistant/data") args = parser.parse_args() if args.apk is None: candidates = list(ROOT.glob("*_1.9.68.apk")) if len(candidates) != 1: parser.error("Pass --apk with the original 1.9.68 APK") args.apk = candidates[0] manifest = build(args.apk, args.output) print(json.dumps({"counts": manifest["counts"], "artifacts": manifest["artifacts"]}, ensure_ascii=True)) if __name__ == "__main__": main()