diff --git a/android/README.md b/android/README.md index 8a7cf080..02b6c3eb 100644 --- a/android/README.md +++ b/android/README.md @@ -2,7 +2,7 @@ Gradle project for the arm64-v8a APK: SDL3 `SDLActivity` + `libmain.so` (the client, with the embedded CPython 2.7). The APK holds code only. Game data (`ymir work`, maps, sound, python-root, -`python27.zip`) lives in the app's external storage directory, see `push-assets.sh`. +`python27.zip`) lives in the app's external files directory, see below. Architecture and roadmap: `docs/MOBILE-ARCHITECTURE.md`. ## Requirements @@ -28,4 +28,19 @@ Overrides: `ANDROID_HOME`, `ANDROID_NDK_HOME`, `JAVA_HOME`, `AVA2_SDK_ROOT`, `AV Android Studio can open this directory after `android/build.sh deps` has run once. +## Game data on the phone + +```bash +android/push_assets.py # after android/build.sh, with the phone attached +``` + +Stages `assets/loose` + `upstream/python-root` (scripts win) + `python27.zip` as hard links in +`build/android-data/root` and sends them with `adb push --sync` to +`/sdcard/Android/data/com.metin2.ava2/files`, where the client starts. The first push moves +about 2.9 GB; later pushes only send changed files. The three `assets/loose` symlinks into the +decoded pack_maker tree are remapped to `--pack-maker` (default `../AVA2/TEST/1. Client DEV/pack_maker`). +Uninstalling the app deletes this directory. + +Running the desktop client inside `build/android-data/root` exercises the same flattened layout. + Logs: `adb logcat -s SDL SDL/APP AndroidRuntime DEBUG`. diff --git a/android/push_assets.py b/android/push_assets.py new file mode 100755 index 00000000..2b45e821 --- /dev/null +++ b/android/push_assets.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 +"""Push the client root to the phone's app data directory (development builds). + +The APK holds code only; on Android the client runs in its external files directory +(/sdcard/Android/data/com.metin2.ava2/files) and reads the flattened client root from there: + + 1. assets/loose the client-root asset tree + 2. upstream/python-root scripts; win over same-named asset files (case-insensitive) + 3. python27.zip the embedded interpreter's stdlib + +The merged tree is staged as hard links (no copy) in build/android-data/root, then sent with +`adb push --sync`, which skips files the device already has with the same timestamp, so later +runs only transfer what changed. The first run moves about 2.8 GB. + +assets/loose keeps a few symlinks into the decoded pack_maker tree of the machine it was +committed on; they are remapped to --pack-maker (default ../AVA2/TEST/1. Client DEV/pack_maker). + +usage: android/push_assets.py [--serial SERIAL] [--pack-maker DIR] [--stdlib ZIP] [--stage-only] +""" + +import argparse +import glob +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +PACKAGE = "com.metin2.ava2" +DEVICE_DIR = f"/sdcard/Android/data/{PACKAGE}/files" +PACK_MAKER_MARK = "/pack_maker/" + + +def find_stdlib(): + candidates = glob.glob(str(ROOT / "android/app/.cxx/*/*/arm64-v8a/python27.zip")) + candidates += [str(p) for p in (ROOT / "build").glob("*/python27.zip")] + candidates = [c for c in candidates if os.path.isfile(c)] + return max(candidates, key=os.path.getmtime) if candidates else None + + +class Stage: + """Hard-link tree; a later source replaces an earlier file with the same lower-case path.""" + + def __init__(self, out): + self.out = out + self.files = {} # lower-case relative path -> staged path + self.dirs = {"": out} + + def _dir(self, rel_parent): + key = "" + for part in rel_parent.parts: + parent = self.dirs[key] + key = f"{key}/{part.lower()}" if key else part.lower() + if key not in self.dirs: + self.dirs[key] = parent / part + self.dirs[key].mkdir() + return self.dirs[key] + + def add(self, rel, source): + key = str(rel).lower() + old = self.files.pop(key, None) + if old is not None: + old.unlink() + target = self._dir(rel.parent) / rel.name + try: + os.link(source, target) + except OSError: # another volume + shutil.copy2(source, target) + self.files[key] = target + + +def walk(top, pack_maker, missing): + """Yields (relative path, real file) under top, following and remapping symlinks.""" + stack = [(Path(top), Path())] + while stack: + directory, rel = stack.pop() + for entry in sorted(os.scandir(directory), key=lambda e: e.name): + path, child = Path(entry.path), rel / entry.name + if entry.name == ".DS_Store": + continue + if entry.is_symlink() and not path.exists(): + link = os.readlink(path) + mark = link.find(PACK_MAKER_MARK) + remapped = pack_maker / link[mark + len(PACK_MAKER_MARK):] if mark >= 0 else None + if remapped is None or not remapped.exists(): + if os.path.isabs(link): # relative dead links (pack_maker's bgm/bgm -> bgm) are noise + missing.append(f"{child} -> {link}") + continue + path = remapped + if path.is_dir(): + stack.append((path, child)) + elif path.is_file(): + yield child, path + + +def adb(serial, *args, **kwargs): + command = ["adb"] + (["-s", serial] if serial else []) + list(args) + return subprocess.run(command, check=True, **kwargs) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--serial", default=os.environ.get("ADB_SERIAL")) + parser.add_argument("--pack-maker", type=Path, default=ROOT.parent / "AVA2/TEST/1. Client DEV/pack_maker") + parser.add_argument("--stdlib", help="python27.zip (default: newest Android/desktop build output)") + parser.add_argument("--stage", type=Path, default=ROOT / "build/android-data/root") + parser.add_argument("--stage-only", action="store_true", help="build the staging tree, do not push") + args = parser.parse_args() + + stdlib = args.stdlib or find_stdlib() + if not stdlib: + sys.exit("python27.zip not found; run android/build.sh first or pass --stdlib") + + if args.stage.exists(): + shutil.rmtree(args.stage) + args.stage.mkdir(parents=True) + stage, missing = Stage(args.stage), [] + for top in (ROOT / "assets/loose", ROOT / "upstream/python-root"): + for rel, path in walk(top, args.pack_maker, missing): + stage.add(rel, path) + stage.add(Path("python27.zip"), stdlib) + for line in missing: + print(f"warning: unresolved symlink {line}", file=sys.stderr) + size = sum(p.stat().st_size for p in stage.files.values()) + print(f"staged {len(stage.files)} files, {size / 2**30:.2f} GiB in {args.stage}") + if args.stage_only: + return + + adb(args.serial, "shell", "mkdir", "-p", DEVICE_DIR) + # Trailing "/." pushes the directory's contents rather than the directory itself. + adb(args.serial, "push", "--sync", f"{args.stage}/.", DEVICE_DIR) + print(f"pushed to {DEVICE_DIR}") + + +if __name__ == "__main__": + main()