-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_android_apk.py
More file actions
189 lines (164 loc) · 7.79 KB
/
export_android_apk.py
File metadata and controls
189 lines (164 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import argparse
import json
import sys
from pathlib import Path
import sts2_recovery as recover
def parse_args() -> argparse.Namespace:
script_dir = Path(__file__).resolve().parent
parser = argparse.ArgumentParser(description="Export an Android Debug APK from an already recovered Slay the Spire 2 Godot project.")
parser.add_argument(
"--output-dir",
"-OutputDir",
type=Path,
default=script_dir / "build" / "recovered_project",
help="existing recovered project directory",
)
parser.add_argument(
"--godot-exe",
"-GodotExe",
type=Path,
default=recover.find_default_godot(script_dir),
help="path to Godot console executable for Android export",
)
parser.add_argument(
"--skip-mod-build",
"-SkipModBuild",
action="store_true",
help="skip rebuilding the repo-owned development mod before export",
)
parser.add_argument(
"--allow-missing-mono-runtime",
action="store_true",
help="continue export even if the Android .NET runtime staging step cannot find native libs",
)
return parser.parse_args()
def load_existing_report(report_path: Path) -> dict[str, object]:
if not report_path.exists():
return {}
try:
data = json.loads(report_path.read_text(encoding="utf-8", errors="replace"))
except json.JSONDecodeError:
return {}
return data if isinstance(data, dict) else {}
def write_report_and_fail(report_path: Path, report: dict[str, object], message: str) -> int:
recover.write_partial_report(report_path, report)
recover.log(message)
recover.log(f"report: {report_path}")
return 1
def ensure_recovered_project(output_dir: Path) -> bool:
return recover.recovery_has_usable_scaffold(output_dir) and (output_dir / "export_presets.cfg").exists()
def load_staged_artifacts(report: dict[str, object]) -> dict[str, dict[str, str]] | None:
android_export = report.get("android_export")
if not isinstance(android_export, dict):
return None
staged_artifacts = android_export.get("staged_artifacts")
return staged_artifacts if isinstance(staged_artifacts, dict) else None
def main() -> int:
args = parse_args()
script_dir = Path(__file__).resolve().parent
args.output_dir = args.output_dir.resolve()
report_path = args.output_dir / "recovery_report.json"
args.godot_exe = recover.ensure_bundled_tool(
requested_exe=args.godot_exe,
default_exe=recover.find_default_godot(script_dir),
archive_path=script_dir / "tools" / "Godot_v4.5.1-stable_mono_win64.zip",
extract_root=script_dir / "tools" / "godot451",
label="godot",
)
if not ensure_recovered_project(args.output_dir):
print(
"未发现可用的已恢复工程,请先运行 "
"`python .\\recover_sts2_godot.py --force` 完成恢复。",
file=sys.stderr,
)
return 1
report = load_existing_report(report_path)
game_dir_value = report.get("game_dir")
report_game_dir = Path(game_dir_value).resolve() if isinstance(game_dir_value, str) and Path(game_dir_value).exists() else None
report["output_dir"] = str(args.output_dir)
report["godot_exe"] = str(args.godot_exe)
report["android_export"] = {
**(report.get("android_export") if isinstance(report.get("android_export"), dict) else {}),
"enabled": True,
"build_type": "debug",
"preset": recover.ANDROID_DEBUG_PRESET_NAME,
"apk_path": str((args.output_dir / recover.ANDROID_DEBUG_EXPORT_RELATIVE_PATH).resolve()),
"export_preset_path": str((args.output_dir / "export_presets.cfg").resolve()),
}
android_build_result = recover.build_project_android(args.output_dir, configuration="Debug")
report["android_export"]["dotnet_build"] = {
"exit_code": android_build_result.returncode,
}
if android_build_result.returncode != 0:
report.update(recover.collect_report(args.output_dir, game_dir=report_game_dir))
return write_report_and_fail(report_path, report, "android_dotnet_build: FAIL")
android_publish_result = recover.publish_project_android(args.output_dir, configuration="Debug")
report["android_export"]["dotnet_publish"] = {
"exit_code": android_publish_result.returncode,
}
if android_publish_result.returncode != 0:
report.update(recover.collect_report(args.output_dir, game_dir=report_game_dir))
return write_report_and_fail(report_path, report, "android_dotnet_publish: FAIL")
android_mono_runtime = recover.stage_android_mono_runtime(args.output_dir, "debug")
report["android_export"]["mono_runtime"] = android_mono_runtime
if (
not android_mono_runtime.get("present")
and not android_mono_runtime.get("placeholder_aar")
and not args.allow_missing_mono_runtime
):
report.update(recover.collect_report(args.output_dir, game_dir=report_game_dir))
return write_report_and_fail(report_path, report, "android_mono_runtime: FAIL")
if not android_mono_runtime.get("present") and args.allow_missing_mono_runtime:
recover.log("warning: android_mono_runtime missing; continuing because --allow-missing-mono-runtime was set.")
if android_mono_runtime.get("placeholder_aar"):
recover.log("warning: android_mono_runtime missing; created placeholder mono AAR for Gradle.")
mod_csproj = args.output_dir / "_generated_mod_projects" / recover.MOD_ID / "AhahahaMenuMod.csproj"
if not mod_csproj.exists():
mod_csproj = recover.write_generated_mod_project(script_dir, args.output_dir)
recover.add_mod_project_to_solution(args.output_dir, mod_csproj)
if not args.skip_mod_build:
mod_build_result = recover.build_mod_project(mod_csproj, configuration="Debug")
report["mod_build"] = {
"project": str(mod_csproj),
"exit_code": mod_build_result.returncode,
}
if mod_build_result.returncode != 0:
report.update(recover.collect_report(args.output_dir, game_dir=report_game_dir))
return write_report_and_fail(report_path, report, "mod_build: FAIL")
android_export_info = recover.export_android_debug(args.godot_exe, args.output_dir)
staged_artifacts = load_staged_artifacts(report)
if android_export_info["apk_exists"]:
if staged_artifacts is not None:
android_export_info["apk_inspection"] = recover.inspect_android_apk(
Path(android_export_info["apk_path"]),
recover.required_android_apk_libraries(
staged_artifacts,
build_type="debug",
extra_library_names=list(android_mono_runtime.get("library_names") or []),
),
)
else:
android_export_info["apk_inspection"] = recover.inspect_android_apk(Path(android_export_info["apk_path"]))
report["android_export"]["godot_export"] = android_export_info
report.update(recover.collect_report(args.output_dir, staged_artifacts, game_dir=report_game_dir))
recover.write_partial_report(report_path, report)
android_export_result = report["android_export"].get("godot_export")
android_export_success = (
isinstance(android_export_result, dict)
and android_build_result.returncode == 0
and android_export_result.get("exit_code") == 0
and android_export_result.get("apk_exists") is True
)
if not android_export_success:
recover.log("android_export: FAIL")
recover.log(f"report: {report_path}")
return 1
recover.log("android_export: PASS")
recover.log(f"report: {report_path}")
return 0
if __name__ == "__main__":
try:
raise SystemExit(main())
except Exception as exc:
print(f"ERROR: {exc}", file=sys.stderr)
raise