Fix: 3D model loading for units/tiles + tile model server pipeline

- ModelCache: switch GLTFDocument.append_from_buffer (was append_from_file
  which returned ERR_BUSY/43); validate GLB magic bytes; explicit signal
  disconnect before queue_free to avoid freed-lambda errors
- ContentCache: add tile model fetch (content.tile_models), disk cache at
  user://content/tile_models.json, get_tile_model_url() API
- game_screen: _add_tile now loads tile CDN model via ModelCache when bound
- Server: ContentGetTileModelsHandler (content.tile_models msg), registered
  in ServiceRegistration; TileModelDto / GetTileModelsResponse added to
  ContentMessages

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-03-09 13:34:17 -04:00
co-authored by Claude Happy
parent e815dd708f
commit 6dda0edf17
7 changed files with 200 additions and 32 deletions
+72 -9
View File
@@ -5,34 +5,40 @@ extends Node
## content.sync message (cursor-based, incremental). Definitions are text
## only (stats, CDN URLs for art/models) — no binary assets are stored here.
##
## Storage layout:
## user://content/defs.json — { "version": int, "defs": { id: dto } }
## Also caches tile model bindings fetched via content.tile_models.
##
## Sync is incremental: on the first launch SinceVersion=0 fetches everything;
## on subsequent launches only cards changed since the stored cursor are fetched
## (usually 0 pages if nothing changed — instant).
## Storage layout:
## user://content/defs.json — { "version": int, "defs": { id: dto } }
## user://content/tile_models.json — { "Forest_Square": "https://...", ... }
##
## Usage:
## ContentCache.start_sync()
## ContentCache.get_definition("some-guid") → Dictionary or {}
## ContentCache.get_definition("some-guid") → Dictionary or {}
## ContentCache.get_tile_model_url("Forest", "Square") → String or ""
## ContentCache.sync_complete.connect(...)
const CACHE_DIR := "user://content"
const CACHE_PATH := "user://content/defs.json"
const PAGE_SIZE := 200
const CACHE_DIR := "user://content"
const CACHE_PATH := "user://content/defs.json"
const TILE_MODELS_PATH := "user://content/tile_models.json"
const PAGE_SIZE := 200
signal sync_progress(fetched_total: int) ## Running count of total definitions in memory.
signal sync_complete(total: int) ## Emitted once sync finishes.
signal sync_failed(error: String) ## Emitted if sync cannot complete.
signal tile_models_ready ## Emitted when tile model bindings are loaded.
var _defs: Dictionary = {} ## definitionId (String) → definition Dictionary
var _sync_version: int = 0 ## Stored DefinitionVersion cursor.
var _is_syncing: bool = false
## "Affinity_Shape" (e.g. "Forest_Square") → CDN model URL string
var _tile_models: Dictionary = {}
# ── Lifecycle ─────────────────────────────────────────────────────────────────
func _ready() -> void:
_load_from_disk()
_load_tile_models_from_disk()
# ── Public API ────────────────────────────────────────────────────────────────
@@ -68,11 +74,32 @@ func is_syncing() -> bool:
## Start (or resume) the incremental sync.
## Emits sync_progress during, sync_complete or sync_failed when done.
## Also triggers a fresh fetch of tile model bindings.
func start_sync() -> void:
if _is_syncing:
return
_is_syncing = true
_fetch_page(_sync_version)
fetch_tile_models()
## Return the CDN URL for a tile's 3D model, or "" if not assigned.
## affinity: e.g. "Forest" shape: "Square" or "Hex"
func get_tile_model_url(affinity: String, shape: String) -> String:
return _tile_models.get(affinity + "_" + shape, "")
## Fetch tile model bindings from the server and update the local cache.
## Emits tile_models_ready when done (even on error, using cached data).
func fetch_tile_models() -> void:
Network.request(
"content.tile_models",
{},
func(_type: String, p: Variant): _on_tile_models(p),
func(code: String, msg: String):
push_warning("ContentCache: tile_models fetch failed — %s %s" % [code, msg])
tile_models_ready.emit()
)
# ── Sync loop ─────────────────────────────────────────────────────────────────
@@ -113,6 +140,21 @@ func _on_error(code: String, msg: String) -> void:
push_warning("ContentCache: sync failed — %s" % detail)
sync_failed.emit(detail)
func _on_tile_models(payload: Variant) -> void:
var p := payload as Dictionary
var bindings : Array = p.get("bindings", [])
_tile_models.clear()
for item in bindings:
var d := item as Dictionary
if d == null: continue
var key := str(d.get("affinity", "")) + "_" + str(d.get("shape", ""))
var url := str(d.get("modelPublicUrl", ""))
if not key.is_empty() and not url.is_empty():
_tile_models[key] = url
_save_tile_models_to_disk()
tile_models_ready.emit()
# ── Persistence ───────────────────────────────────────────────────────────────
func _load_from_disk() -> void:
@@ -143,3 +185,24 @@ func _save_to_disk() -> void:
return
file.store_string(JSON.stringify({ "version": _sync_version, "defs": _defs }))
file.close()
func _load_tile_models_from_disk() -> void:
if not FileAccess.file_exists(TILE_MODELS_PATH):
return
var file := FileAccess.open(TILE_MODELS_PATH, FileAccess.READ)
if not file:
return
var parsed: Variant = JSON.parse_string(file.get_as_text())
file.close()
if typeof(parsed) == TYPE_DICTIONARY:
_tile_models = parsed as Dictionary
func _save_tile_models_to_disk() -> void:
DirAccess.make_dir_recursive_absolute(CACHE_DIR)
var file := FileAccess.open(TILE_MODELS_PATH, FileAccess.WRITE)
if not file:
return
file.store_string(JSON.stringify(_tile_models))
file.close()