From c8c3666d262ed6b7cb362b72dd38ca5ff1d9f23a Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 20 May 2026 22:35:05 -0400 Subject: [PATCH] Fix 5: iOS Safari scoped asset loading and memory management - New Platform.gd autoload: is_ios_safari() detects iPhone/iPad Safari via JavaScriptBridge UA check (no Chrome/CriOS), gating all iOS memory paths - ModelCache.release_model(url, etag): evicts a model from in-memory cache on iOS Safari when a unit leaves the board; no-op on other platforms so disk cache is always preserved for fast reload - unit_mesh_builder: stores resolved model URL+ETag per unit (_unit_model_urls, _unit_model_etags); calls ModelCache.release_model in the to_remove loop so PackedScene refs are freed as soon as the unit is pooled or freed - ContentCache.get_model_url_for_definition: stub for looking up a card's Model3D CDN URL without a loaded unit dict (foundation for future eviction paths) Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Client/origin-client/autoloads/ContentCache.gd | 17 +++++++++++++++++ Client/origin-client/autoloads/ModelCache.gd | 16 ++++++++++++++++ Client/origin-client/autoloads/Platform.gd | 18 ++++++++++++++++++ Client/origin-client/project.godot | 1 + .../game/components/units/unit_mesh_builder.gd | 12 ++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 Client/origin-client/autoloads/Platform.gd diff --git a/Client/origin-client/autoloads/ContentCache.gd b/Client/origin-client/autoloads/ContentCache.gd index bd52b44..e1d1160 100644 --- a/Client/origin-client/autoloads/ContentCache.gd +++ b/Client/origin-client/autoloads/ContentCache.gd @@ -62,6 +62,23 @@ func get_definition(id: String) -> Dictionary: return _defs.get(id, {}) +## Return the CDN URL for the first Model3D asset in a card definition. +## Returns "" if the definition is not cached or has no Model3D asset. +## Intended for iOS Safari memory-management paths that need the URL without a +## loaded unit dict (e.g. pre-eviction before a unit is placed). +func get_model_url_for_definition(definition_id: String) -> String: + var def := get_definition(definition_id) + for asset: Variant in def.get("assets", []): + var d: Dictionary = asset as Dictionary + if d == null: + continue + if str(d.get("assetType", "")) == "Model3D": + var url: String = str(d.get("cdnUrl", "")).strip_edges() + if not url.is_empty() and url.to_lower() != "null": + return url + return "" + + ## True if the definition is cached. func has_definition(id: String) -> bool: return _defs.has(id) diff --git a/Client/origin-client/autoloads/ModelCache.gd b/Client/origin-client/autoloads/ModelCache.gd index 6fc2f01..54f3ea7 100644 --- a/Client/origin-client/autoloads/ModelCache.gd +++ b/Client/origin-client/autoloads/ModelCache.gd @@ -82,6 +82,22 @@ func _migrate_cache() -> void: f.close() +## Release a model from the in-memory cache without touching disk. +## On iOS Safari this immediately frees resident memory when a unit leaves the board. +## On other platforms this is a no-op — the OS manages heap pressure. +## Disk cache (.bin) is intentionally kept so the model reloads instantly if the same +## unit type is played again in the same session. +func release_model(url: String, etag: String = "") -> void: + if url.is_empty(): + return + if not Platform.is_ios_safari(): + return + var key := _cache_key(url, etag) + _mem.erase(key) + if OS.is_debug_build(): + print("ModelCache: released %s (mem_entries=%d)" % [url, _mem.size()]) + + ## Remove a cached entry (memory + disk) so the next load_model call re-downloads it. ## Use before retrying a previously-failed URL. func evict(url: String, etag: String = "") -> void: diff --git a/Client/origin-client/autoloads/Platform.gd b/Client/origin-client/autoloads/Platform.gd new file mode 100644 index 0000000..ae4b6bb --- /dev/null +++ b/Client/origin-client/autoloads/Platform.gd @@ -0,0 +1,18 @@ +extends Node +## Platform — thin detection helpers for per-platform code paths. +## +## Usage: +## if Platform.is_ios_safari(): +## ... # memory-constrained path + +## Returns true when running on iOS Safari (web export, Safari UA, iPhone/iPad, +## not Chrome/CriOS). Used to gate memory-reduction paths for the ~300 MB per-tab +## limit that iOS Safari enforces. +static func is_ios_safari() -> bool: + if not OS.has_feature("web"): + return false + var ua: String = JavaScriptBridge.eval("navigator.userAgent") + var has_safari: bool = ua.contains("Safari") + var has_chrome: bool = ua.contains("Chrome") or ua.contains("CriOS") + var is_ios_device: bool = ua.contains("iPhone") or ua.contains("iPad") + return has_safari and not has_chrome and is_ios_device diff --git a/Client/origin-client/project.godot b/Client/origin-client/project.godot index 47c87ad..ef5be35 100644 --- a/Client/origin-client/project.godot +++ b/Client/origin-client/project.godot @@ -29,6 +29,7 @@ Settings="*res://autoloads/Settings.gd" Game="*res://autoloads/Game.gd" CustomLobby="*res://autoloads/CustomLobby.gd" ModelCache="*res://autoloads/ModelCache.gd" +Platform="*res://autoloads/Platform.gd" Music="*res://autoloads/Music.gd" SFX="*res://autoloads/SFX.gd" HighlightConfig="*res://autoloads/HighlightConfig.gd" diff --git a/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd b/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd index 2f510e0..2822ba1 100644 --- a/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd +++ b/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd @@ -33,6 +33,8 @@ var turn_glow_rings: Dictionary = {} # iid → MeshInstance3D var materialize_until: Dictionary = {} # iid → Time.get_ticks_msec deadline var unit_owner_ids: Dictionary = {} # iid → accountId var _unit_load_gen: Dictionary = {} # iid → int (incremented each model-load start; stale callbacks bail out) +var _unit_model_urls: Dictionary = {} # iid → String (CDN URL used when the model was loaded) +var _unit_model_etags: Dictionary = {} # iid → String (ETag used when the model was loaded) var _active_tweens: Array[Tween] = [] # shader tweens tracked for kill-on-rebuild @@ -188,6 +190,10 @@ func rebuild_units(state: Dictionary) -> void: , CONNECT_ONE_SHOT) else: _pool_unit(body, card_type) + # Release the 3D model from memory (no-op on non-iOS; frees heap on iOS Safari). + ModelCache.release_model( + str(_unit_model_urls.get(iid, "")), + str(_unit_model_etags.get(iid, ""))) unit_bodies.erase(iid) unit_meshes.erase(iid) anim_players.erase(iid) @@ -203,6 +209,8 @@ func rebuild_units(state: Dictionary) -> void: turn_glow_rings.erase(iid) materialize_until.erase(iid) unit_owner_ids.erase(iid) + _unit_model_urls.erase(iid) + _unit_model_etags.erase(iid) # 3. Add units not yet present (newly played, or first render after connect). for iid: String in desired: @@ -357,6 +365,10 @@ func add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false, model_base_angles[iid] = base_angle needs_face_rot[iid] = needs_face_rotation + # Store the resolved URL+ETag so we can release the cached model on removal. + _unit_model_urls[iid] = model_url + _unit_model_etags[iid] = model_etag + var body_ref: Variant = weakref(body) # Stale-callback guard: bump generation so any previous in-flight load # for this iid will bail out when it fires.