android: run from the app's external files directory

The APK holds code only: on Android the client enters
SDL_GetAndroidExternalStoragePath() at startup, where the flattened client
root (assets/loose + python-root + python27.zip) is pushed. Script-root
discovery now also accepts a flattened root (game.py beside uiscript/) in
the working directory. Missing data shows a message box, and fatal errors
go to logcat because stderr is not captured on Android.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 18:55:50 +09:00
co-authored by Claude Opus 5.5
parent 3ce5fc1a6c
commit b0044c9097
+30 -4
View File
@@ -211,6 +211,21 @@ int main(int argc, char** argv) {
return 1;
}
#elif defined(__ANDROID__)
// The APK holds code only. The flattened client root (assets/loose + python-root + python27.zip,
// android/push-assets.sh) lives in the app's external files directory; the legacy code reads
// paths relative to the working directory.
{
const char* data = SDL_GetAndroidExternalStoragePath();
std::error_code error;
if (data) std::filesystem::current_path(data, error);
if (!data || error) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Cannot enter client data directory: %s",
data ? error.message().c_str() : SDL_GetError());
return 1;
}
SDL_Log("Client data: %s", data);
}
#endif
try {
int frame_limit = 0, auto_slot = -1, fps_cap = -1, game_frames = 1, rendered_game_frames = 0;
@@ -282,11 +297,19 @@ int main(int argc, char** argv) {
if (script_root.empty()) {
if (std::filesystem::exists("upstream/python-root")) script_root = "upstream/python-root";
else if (std::filesystem::exists("root")) script_root = "root";
else {
auto res = std::filesystem::path(SDL_GetBasePath()) / "../Resources/root";
if (std::filesystem::exists(res)) script_root = res.string();
}
else if (auto res = std::filesystem::path(SDL_GetBasePath()) / "../Resources/root"; std::filesystem::exists(res))
script_root = res.string();
// A flattened client root (scripts beside the assets), e.g. the Android data directory.
else if (std::filesystem::exists("game.py") && std::filesystem::exists("uiscript")) script_root = ".";
}
#ifdef __ANDROID__
if (stdlib.empty() || script_root.empty()) {
const std::string message = "Game data not found in " + std::filesystem::current_path().string()
+ ".\nPush it with android/push-assets.sh.";
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "AVA2", message.c_str(), nullptr);
throw std::runtime_error(message);
}
#endif
bool original_phase_mode = view_script.empty();
#ifdef AVA2_HAS_PYTHON
@@ -2413,6 +2436,9 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
return 0;
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
#ifdef __ANDROID__
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", e.what()); // stderr is not captured on Android
#endif
return 1;
}
}