#include "platform.h" #include #include #include #include #include #include #include namespace ava2::platform { bool EnterClientDataDirectory(std::string* error) { #if defined(SDL_PLATFORM_IOS) // Like the APK, the .app holds code only. The flattened client root (ios/push_assets.py, or // copied in with Finder / the Files app — Info.plist enables file sharing) lives in Documents. const char* data = SDL_GetUserFolder(SDL_FOLDER_DOCUMENTS); std::error_code ec; if (data) std::filesystem::current_path(data, ec); if (!data || ec) { if (error) *error = std::string("Cannot enter client data directory: ") + (data ? ec.message() : SDL_GetError()); return false; } SDL_Log("Client data: %s", data); #elif defined(__APPLE__) // Keep legacy Python's relative config/chat writes outside the signed application. try { std::filesystem::path base = std::filesystem::path(SDL_GetBasePath()).lexically_normal(); if (base.filename().empty()) base = base.parent_path(); // SDL on macOS returns the bundle's Resources directory by default. if (base.filename() == "Resources" && base.parent_path().filename() == "Contents") base = base.parent_path() / "MacOS"; const auto resources = base / "../Resources"; if (base.filename() == "MacOS" && std::filesystem::is_directory(resources)) { std::filesystem::path data; if (const char* overridePath = std::getenv("AVA2_USER_DATA_DIR")) data = overridePath; else { char* preferences = SDL_GetPrefPath("AVA2", "Client"); if (!preferences) throw std::runtime_error(SDL_GetError()); data = preferences; SDL_free(preferences); } std::filesystem::create_directories(data); for (const auto& entry : std::filesystem::directory_iterator(resources)) { const auto name = entry.path().filename(); const auto extension = name.extension().string(); if (name == "lib" || extension == ".cfg" || extension == ".inf" || extension == ".chat") continue; const auto link = data / name; const auto target = std::filesystem::canonical(entry.path()); if (std::filesystem::is_symlink(link) && std::filesystem::read_symlink(link) != target) std::filesystem::remove(link); if (!std::filesystem::exists(link)) std::filesystem::create_symlink(target, link); } std::filesystem::current_path(data); SDL_Log("Client user data: %s", data.string().c_str()); } } catch (const std::exception& e) { if (error) *error = std::string("Cannot initialize client data directory: ") + e.what(); return false; } #elif defined(__ANDROID__) // The APK holds code only. The flattened client root (assets/loose + python-root + python27.zip, // android/push_assets.py) 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 ec; if (data) std::filesystem::current_path(data, ec); if (!data || ec) { if (error) *error = std::string("Cannot enter client data directory: ") + (data ? ec.message() : SDL_GetError()); return false; } SDL_Log("Client data: %s", data); #else (void)error; #endif return true; } std::string ConfigDirectory() { #ifdef __ANDROID__ // metin2.cfg / interface.cfg live in the client's working directory on Windows; the APK's // working directory is not writable, so they go to the app's internal storage instead. if (const char* internal = SDL_GetAndroidInternalStoragePath()) return internal; #endif return {}; } namespace { bool SDLCALL LifecycleWatch(void* userdata, SDL_Event* event) { static_cast(userdata)->OnEvent(event->type); return true; } } // namespace void Lifecycle::SetHints() { // Keep the loop (and the game connection) alive while paused; rendering and sound stop. SDL_SetHint(SDL_HINT_ANDROID_BLOCK_ON_PAUSE, "0"); } void Lifecycle::Install() { if (m_installed) return; m_installed = SDL_AddEventWatch(LifecycleWatch, this); } void Lifecycle::Uninstall() { if (!m_installed) return; SDL_RemoveEventWatch(LifecycleWatch, this); m_installed = false; } void Lifecycle::OnEvent(unsigned int type) { switch (type) { case SDL_EVENT_WILL_ENTER_BACKGROUND: case SDL_EVENT_DID_ENTER_BACKGROUND: if (!m_background.exchange(true)) m_enteredBackground = true; break; case SDL_EVENT_WILL_ENTER_FOREGROUND: case SDL_EVENT_DID_ENTER_FOREGROUND: if (m_background.exchange(false)) m_enteredForeground = true; break; case SDL_EVENT_LOW_MEMORY: m_lowMemory = true; break; case SDL_EVENT_TERMINATING: m_terminating = true; break; default: break; } } Lifecycle::Change Lifecycle::Poll() { if (m_enteredBackground.exchange(false)) { // A resume that already happened is reported on the next poll. return Change::EnterBackground; } if (m_enteredForeground.exchange(false)) return Change::EnterForeground; return Change::None; } SafeInsets MapSafeInsets(float windowW, float windowH, float safeX, float safeY, float safeW, float safeH, float logicalW, float logicalH) { SafeInsets out; if (windowW <= 0.0f || windowH <= 0.0f || logicalW <= 0.0f || logicalH <= 0.0f) return out; // Letterbox: uniform scale, centred. const float scale = std::min(windowW / logicalW, windowH / logicalH); const float offX = (windowW - logicalW * scale) * 0.5f; const float offY = (windowH - logicalH * scale) * 0.5f; auto clampInset = [](float v, float limit) { return std::clamp(v, 0.0f, limit); }; out.left = clampInset((safeX - offX) / scale, logicalW); out.top = clampInset((safeY - offY) / scale, logicalH); out.right = clampInset((offX + logicalW * scale - (safeX + safeW)) / scale, logicalW); out.bottom = clampInset((offY + logicalH * scale - (safeY + safeH)) / scale, logicalH); return out; } LogicalSize ChooseMobileLogicalSize(float pixelW, float pixelH, float density, int uiHeight) { if (pixelW <= 0.0f || pixelH <= 0.0f) return {1024, 768}; if (pixelW < pixelH) std::swap(pixelW, pixelH); // the activity is landscape if (density <= 0.0f) density = 1.0f; const float aspect = pixelW / pixelH; int height = uiHeight > 0 ? std::clamp(uiHeight, 600, 1080) : std::clamp(int(std::lround(pixelH / density * 1.5f)), 600, 768); int width = int(std::lround(height * aspect)) & ~1; if (width < 1024) { width = 1024; height = int(std::lround(1024.0f / aspect)) & ~1; } return {width, height}; } bool QueryDisplay(float* pixelW, float* pixelH, float* density) { const SDL_DisplayID display = SDL_GetPrimaryDisplay(); const SDL_DisplayMode* mode = display ? SDL_GetCurrentDisplayMode(display) : nullptr; if (!mode || mode->w <= 0 || mode->h <= 0) return false; const float pd = mode->pixel_density > 0.0f ? mode->pixel_density : 1.0f; *pixelW = float(mode->w) * pd; *pixelH = float(mode->h) * pd; // Android: content scale = DisplayMetrics.density (dp -> px); desktop: the UI scale. const float scale = SDL_GetDisplayContentScale(display); *density = scale > 0.0f ? scale : pd; return true; } bool ParsePhoneSpec(const std::string& spec, float* pixelW, float* pixelH, float* density) { float w = 0.0f, h = 0.0f, d = 1.0f; char extra = 0; const int n = std::sscanf(spec.c_str(), "%fx%f@%f%c", &w, &h, &d, &extra); if ((n != 2 && n != 3) || w <= 0.0f || h <= 0.0f || d <= 0.0f) return false; *pixelW = w; *pixelH = h; *density = d; return true; } SafeInsets QuerySafeInsets(SDL_Window* window, SDL_Renderer* renderer) { int ww = 0, wh = 0, lw = 0, lh = 0; SDL_Rect safe{}; SDL_RendererLogicalPresentation mode; if (!window || !renderer || !SDL_GetWindowSize(window, &ww, &wh) || !SDL_GetWindowSafeArea(window, &safe) || !SDL_GetRenderLogicalPresentation(renderer, &lw, &lh, &mode) || lw <= 0 || lh <= 0) return {}; return MapSafeInsets(float(ww), float(wh), float(safe.x), float(safe.y), float(safe.w), float(safe.h), float(lw), float(lh)); } } // namespace ava2::platform