-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_android_release_apk.py
More file actions
326 lines (290 loc) · 13.5 KB
/
export_android_release_apk.py
File metadata and controls
326 lines (290 loc) · 13.5 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import argparse
import json
import sys
from pathlib import Path
import sts2_recovery as recover
LOCAL_RELEASE_SIGNING_DIR = Path(".local") / "android_release_signing"
LOCAL_RELEASE_SIGNING_CONFIG_NAME = "signing.json"
LOCAL_RELEASE_KEYSTORE_NAME = "release.keystore"
def get_local_release_signing_dir(script_dir: Path) -> Path:
return (script_dir / LOCAL_RELEASE_SIGNING_DIR).resolve()
def load_local_release_signing_defaults(script_dir: Path) -> dict[str, str | Path | None]:
signing_dir = get_local_release_signing_dir(script_dir)
config_path = signing_dir / LOCAL_RELEASE_SIGNING_CONFIG_NAME
config: dict[str, object] = {}
if config_path.exists():
try:
loaded = json.loads(config_path.read_text(encoding="utf-8", errors="replace"))
except json.JSONDecodeError as exc:
raise RuntimeError(f"invalid local release signing config: {config_path} ({exc})") from exc
if not isinstance(loaded, dict):
raise RuntimeError(f"invalid local release signing config: expected a JSON object in {config_path}")
config = loaded
keystore_file = config.get("keystore_file", LOCAL_RELEASE_KEYSTORE_NAME)
keystore_path: Path | None = None
if isinstance(keystore_file, str) and keystore_file.strip():
candidate = Path(keystore_file)
if not candidate.is_absolute():
candidate = signing_dir / candidate
keystore_path = candidate.resolve()
if not keystore_path.exists():
keystore_path = None
keystore_user = config.get("keystore_user")
keystore_pass = config.get("keystore_pass")
return {
"signing_dir": signing_dir,
"config_path": config_path,
"keystore_path": keystore_path,
"keystore_user": keystore_user if isinstance(keystore_user, str) and keystore_user.strip() else None,
"keystore_pass": keystore_pass if isinstance(keystore_pass, str) and keystore_pass.strip() else None,
}
def parse_args() -> argparse.Namespace:
script_dir = Path(__file__).resolve().parent
local_signing = load_local_release_signing_defaults(script_dir)
parser = argparse.ArgumentParser(
description="Export an Android Release 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(
"--java-home",
"-JavaHome",
type=Path,
default=None,
help="override JAVA_HOME for Gradle/Godot export",
)
parser.add_argument(
"--android-home",
"-AndroidHome",
type=Path,
default=None,
help="override ANDROID_HOME for Gradle/Godot export",
)
parser.add_argument(
"--android-sdk-root",
"-AndroidSdkRoot",
type=Path,
default=None,
help="override ANDROID_SDK_ROOT for Gradle/Godot 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",
)
parser.add_argument(
"--keystore",
"-Keystore",
type=Path,
default=local_signing["keystore_path"],
help=(
"release keystore path; defaults to .local/android_release_signing, "
"then falls back to GODOT_ANDROID_KEYSTORE_RELEASE_PATH"
),
)
parser.add_argument(
"--keystore-user",
"-KeystoreUser",
default=local_signing["keystore_user"],
help=(
"release keystore alias/user; defaults to .local/android_release_signing/signing.json, "
"then falls back to GODOT_ANDROID_KEYSTORE_RELEASE_USER"
),
)
parser.add_argument(
"--keystore-pass",
"-KeystorePass",
default=local_signing["keystore_pass"],
help=(
"release keystore password; defaults to .local/android_release_signing/signing.json, "
"then falls back to GODOT_ANDROID_KEYSTORE_RELEASE_PASSWORD"
),
)
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)
def describe_missing_recovery_requirements(output_dir: Path, report: dict[str, object]) -> str:
_ = report
if recover.recovery_has_usable_scaffold(output_dir):
return (
"已找到恢复工程,但当前目录缺少完整恢复产物。请重新运行 "
"`python .\\recover_sts2_godot.py --force` 后再导出。"
)
return (
"未发现可用的已恢复工程,请先运行 "
"`python .\\recover_sts2_godot.py --force` 完成恢复。"
)
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",
)
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
if not ensure_recovered_project(args.output_dir):
print(describe_missing_recovery_requirements(args.output_dir, report), file=sys.stderr)
return 1
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": "release",
"preset": recover.ANDROID_RELEASE_PRESET_NAME,
"apk_path": str((args.output_dir / recover.ANDROID_RELEASE_EXPORT_RELATIVE_PATH).resolve()),
"expansion_expected": recover.ANDROID_RELEASE_USES_EXPANSION,
"expansion_path": str((args.output_dir / recover.ANDROID_RELEASE_EXPANSION_RELATIVE_PATH).resolve()),
"export_preset_path": str((args.output_dir / "export_presets.cfg").resolve()),
}
android_preflight = recover.gather_android_preflight(script_dir)
report["android_export"].update(
{
"vendor_root": android_preflight["vendor_root"],
"required_artifacts": android_preflight["required_artifacts"],
"missing_artifacts": android_preflight["missing_artifacts"],
"missing_requirements": android_preflight["missing_requirements"],
"editor_settings": android_preflight["editor_settings"],
}
)
if android_preflight["missing_artifacts"] or android_preflight["missing_requirements"]:
report.update(recover.collect_report(args.output_dir, game_dir=report_game_dir))
return write_report_and_fail(report_path, report, "android_release_preflight: FAIL")
staged_artifacts = recover.stage_android_vendor_artifacts(script_dir, args.output_dir)
recover.patch_fmod_android_export_plugin(args.output_dir, staged_artifacts)
recover.patch_android_plugin_configs(args.output_dir, staged_artifacts)
export_presets_path = recover.write_export_presets(args.output_dir)
report["android_export"]["staged_artifacts"] = staged_artifacts
report["android_export"]["export_preset_path"] = str(export_presets_path)
release_bake = recover.bake_release_export_inputs(args.godot_exe, args.output_dir, target="android_release")
report["android_export"]["release_bake"] = release_bake
if not release_bake["considered_success"]:
report.update(recover.collect_report(args.output_dir, staged_artifacts, game_dir=report_game_dir))
return write_report_and_fail(report_path, report, "android_release_bake: FAIL")
android_build_result = recover.build_project_android(args.output_dir, configuration="Release")
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, staged_artifacts, 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="Release")
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, staged_artifacts, 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, "release")
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, staged_artifacts, 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="Release")
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, staged_artifacts, game_dir=report_game_dir))
return write_report_and_fail(report_path, report, "mod_build: FAIL")
android_export_info = recover.export_android_release(
args.godot_exe,
args.output_dir,
java_home=args.java_home,
android_home=args.android_home,
android_sdk_root=args.android_sdk_root,
release_keystore_path=args.keystore,
release_keystore_user=args.keystore_user,
release_keystore_pass=args.keystore_pass,
)
if android_export_info["apk_exists"]:
android_export_info["apk_inspection"] = recover.inspect_android_apk(
Path(android_export_info["apk_path"]),
recover.required_android_apk_libraries(
staged_artifacts,
build_type="release",
extra_library_names=list(android_mono_runtime.get("library_names") or []),
),
)
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
and isinstance(android_export_result.get("apk_inspection"), dict)
and not android_export_result["apk_inspection"].get("missing_required_arm64_libs")
and not android_export_result["apk_inspection"].get("contains_x86_64_libs")
and (
not android_export_result.get("expansion_expected")
or android_export_result.get("expansion_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