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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-05-20 22:35:05 -04:00
co-authored by Claude Happy
parent 93e3c8b0db
commit c8c3666d26
5 changed files with 64 additions and 0 deletions
@@ -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)
@@ -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:
@@ -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