Add v1.2 development quest progression and experimental battle runtime
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
"""Verified, offline progression configuration from the original 1.9.68 APK.
|
||||
|
||||
The application reads derived static files only. Stage battle dictionaries live
|
||||
in a separate archive and are read one stage at a time by a battle worker.
|
||||
Nothing in this module opens an APK, imports the research tools, or uses a
|
||||
network client. The existing commission-only Catalog is deliberately untouched.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
import gzip
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import zipfile
|
||||
|
||||
|
||||
DATA_DIR = Path(__file__).with_name("data")
|
||||
MANIFEST_NAME = "progression_manifest.json"
|
||||
DATA_NAME = "progression.json.gz"
|
||||
BATTLES_NAME = "progression_battles.zip"
|
||||
SCHEMA_VERSION = 1
|
||||
ORIGINAL_APK_SHA256 = "01678ee3a8465d818d32cbbd94baa0d8253b04b4c57b70ebc5544ed341f1015d"
|
||||
|
||||
|
||||
class ProgressionDataError(ValueError):
|
||||
"""Missing, inconsistent, or damaged derived configuration."""
|
||||
|
||||
|
||||
class ProgressionData:
|
||||
"""Small task/index data; optional stage battles are loaded lazily.
|
||||
|
||||
Public tables: tasks/task_info, stages/stage_info, scripts, setting,
|
||||
game_setting, npc_info, stage_sp, areas, mines, farming, manifest. ``data`` exposes the index
|
||||
mapping for integrations. This is shared read-only configuration; callers
|
||||
must keep character state and credentials elsewhere.
|
||||
"""
|
||||
|
||||
def __init__(self, data=None, *, directory=None):
|
||||
self.directory = Path(directory or DATA_DIR)
|
||||
self.manifest = {}
|
||||
if data is None:
|
||||
try:
|
||||
self.manifest = json.loads((self.directory / MANIFEST_NAME).read_text(encoding="utf-8"))
|
||||
if self.manifest.get("schema_version") != SCHEMA_VERSION:
|
||||
raise ProgressionDataError("Unsupported progression manifest version")
|
||||
if self.manifest.get("source", {}).get("apk_sha256") != ORIGINAL_APK_SHA256:
|
||||
raise ProgressionDataError("Progression data is not bound to the original APK")
|
||||
artifact = (self.directory / DATA_NAME).read_bytes()
|
||||
expected = self.manifest["artifacts"][DATA_NAME]["sha256"]
|
||||
if hashlib.sha256(artifact).hexdigest() != expected:
|
||||
raise ProgressionDataError("Progression artifact hash mismatch")
|
||||
decoded = gzip.decompress(artifact)
|
||||
if hashlib.sha256(decoded).hexdigest() != self.manifest["artifacts"][DATA_NAME]["decoded_sha256"]:
|
||||
raise ProgressionDataError("Progression decoded data hash mismatch")
|
||||
data = json.loads(decoded)
|
||||
except (OSError, KeyError, ValueError, EOFError) as exc:
|
||||
if isinstance(exc, ProgressionDataError):
|
||||
raise
|
||||
raise ProgressionDataError(f"Cannot load static progression data: {exc}") from exc
|
||||
if not isinstance(data, dict) or not isinstance(data.get("task_info"), dict):
|
||||
raise ProgressionDataError("Progression data must contain a full task_info mapping")
|
||||
if data.get("schema_version", SCHEMA_VERSION) != SCHEMA_VERSION:
|
||||
raise ProgressionDataError("Unsupported progression data version")
|
||||
self.data = data
|
||||
self.tasks = self.task_info = data["task_info"]
|
||||
self.stages = self.stage_info = data.get("stage_info", {})
|
||||
self.scripts = data.get("scripts", {})
|
||||
self.setting = data.get("setting", {})
|
||||
self.game_setting = data.get("game_setting", {})
|
||||
self.npc_info = data.get("npc_info", {})
|
||||
self.stage_sp = data.get("stage_sp", {})
|
||||
self.areas = (data.get("area_info") or {}).get("areaInfo", {})
|
||||
self.area_maps = (data.get("area_info") or {}).get("areaMap", {})
|
||||
self.mines = (data.get("mine_info") or {}).get("mines", {})
|
||||
self.positions = {}
|
||||
self.farming = data.get("farming") or {"kills": {}, "items": {}}
|
||||
for parent, area in self.area_maps.items():
|
||||
for entry in area.get("positions") or []:
|
||||
if entry.get("posId"):
|
||||
self.positions.setdefault(entry["posId"], []).append(entry | {"parent_area": parent})
|
||||
|
||||
def task(self, type_id):
|
||||
return self.tasks.get(str(type_id))
|
||||
|
||||
def stage(self, stage_id):
|
||||
return self.stages.get(str(stage_id))
|
||||
|
||||
def script(self, name):
|
||||
return self.scripts.get(str(name))
|
||||
|
||||
def npc_name(self, key):
|
||||
return self.npc_info.get(str(key), {}).get("name") or str(key or "")
|
||||
|
||||
def stage_battle(self, stage_id, difficulty=1):
|
||||
"""Read one original stage's battle config, never the entire archive.
|
||||
|
||||
Call this from the battle worker, not the GUI thread. The per-member
|
||||
digest is itself covered by the checked index artifact digest.
|
||||
"""
|
||||
stage_id = str(stage_id)
|
||||
stage = self.stage(stage_id) or {}
|
||||
# Injected data can carry full stage dictionaries for offline tests.
|
||||
level = (stage.get("level") or {}).get(str(difficulty), {})
|
||||
if "battle" in level:
|
||||
return level["battle"]
|
||||
record = self.data.get("battle_members", {}).get(stage_id)
|
||||
if not record:
|
||||
raise ProgressionDataError(f"Missing original battle configuration for stage {stage_id}")
|
||||
try:
|
||||
with zipfile.ZipFile(self.directory / BATTLES_NAME) as archive:
|
||||
raw = archive.read(record["member"])
|
||||
if hashlib.sha256(raw).hexdigest() != record["sha256"]:
|
||||
raise ProgressionDataError(f"Stage {stage_id} battle hash mismatch")
|
||||
return json.loads(raw).get(str(difficulty))
|
||||
except (OSError, KeyError, ValueError, zipfile.BadZipFile) as exc:
|
||||
if isinstance(exc, ProgressionDataError):
|
||||
raise
|
||||
raise ProgressionDataError(f"Cannot load stage {stage_id}: {exc}") from exc
|
||||
|
||||
def task_battle(self, type_id, step_number):
|
||||
"""Resolve source battle data; step_number is the one-based wire step."""
|
||||
task = self.task(type_id) or {}
|
||||
steps = task.get("steps") or []
|
||||
if not 1 <= int(step_number) <= len(steps):
|
||||
raise ProgressionDataError("Task battle step is outside the configured task")
|
||||
step = steps[int(step_number) - 1]
|
||||
if step.get("statusType") == "passStage":
|
||||
value = step.get("statusValue") or {}
|
||||
return self.stage_battle(value["stage"], value.get("level", 1))
|
||||
if step.get("statusType") == "battle":
|
||||
return step.get("battle")
|
||||
raise ProgressionDataError("Task step is not a battle")
|
||||
|
||||
|
||||
GameData = ProgressionData
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def load_progression_data():
|
||||
"""Load the packaged static index once; does not parse an APK."""
|
||||
return ProgressionData()
|
||||
Reference in New Issue
Block a user