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:
@@ -6,13 +6,14 @@ extends Node
|
||||
##
|
||||
## The callback receives a freshly-instantiated Node3D on success, or null on
|
||||
## failure. Multiple callers requesting the same URL share a single download.
|
||||
## Cached PackedScenes are kept in memory; raw bytes are written to
|
||||
## user://remote_model_cache/<sha256>.glb
|
||||
## Parsed PackedScenes are kept in memory so re-instantiation is instant.
|
||||
## Raw GLB bytes are written to:
|
||||
## user://remote_model_cache/<sha256>.bin
|
||||
## so subsequent app launches skip the download.
|
||||
|
||||
const _CACHE_DIR := "user://remote_model_cache/"
|
||||
const _CACHE_DIR := "user://remote_model_cache"
|
||||
|
||||
# url (String) → PackedScene (null if load failed)
|
||||
# url (String) → PackedScene or null (null means "tried and failed")
|
||||
static var _mem: Dictionary = {}
|
||||
|
||||
# url (String) → Array[Callable] (callbacks waiting on an in-flight download)
|
||||
@@ -25,6 +26,8 @@ func _ready() -> void:
|
||||
|
||||
## Request a model by CDN URL.
|
||||
## callback(node: Node3D) is called on the main thread; node may be null.
|
||||
## If the same URL is already downloading, callbacks are queued and all
|
||||
## fire together when the download completes.
|
||||
func load_model(url: String, callback: Callable) -> void:
|
||||
# 1 — memory hit
|
||||
if _mem.has(url):
|
||||
@@ -32,13 +35,17 @@ func load_model(url: String, callback: Callable) -> void:
|
||||
callback.call(packed.instantiate() if packed != null else null)
|
||||
return
|
||||
|
||||
# 2 — disk hit
|
||||
# 2 — disk hit (bytes from previous session)
|
||||
var disk := _disk_path(url)
|
||||
if FileAccess.file_exists(disk):
|
||||
var packed := _load_gltf(disk)
|
||||
_mem[url] = packed
|
||||
callback.call(packed.instantiate() if packed != null else null)
|
||||
return
|
||||
var bytes := FileAccess.get_file_as_bytes(disk)
|
||||
if not bytes.is_empty():
|
||||
var packed := _parse_gltf(bytes)
|
||||
_mem[url] = packed
|
||||
callback.call(packed.instantiate() if packed != null else null)
|
||||
return
|
||||
# Empty / corrupt cache file — fall through to re-download
|
||||
DirAccess.remove_absolute(disk)
|
||||
|
||||
# 3 — already downloading → queue
|
||||
if _pending.has(url):
|
||||
@@ -56,63 +63,83 @@ func _download(url: String, disk: String) -> void:
|
||||
add_child(http)
|
||||
|
||||
http.request_completed.connect(func(_result, code, _headers, body: PackedByteArray):
|
||||
# Disconnect and free the requester before anything else.
|
||||
http.get_signal_connection_list("request_completed").clear()
|
||||
http.queue_free()
|
||||
|
||||
var callbacks: Array = _pending.get(url, [])
|
||||
_pending.erase(url)
|
||||
|
||||
if code < 200 or code >= 300:
|
||||
push_warning("ModelCache: download failed (%d) for %s" % [code, url])
|
||||
push_warning("ModelCache: download failed (HTTP %d) — %s" % [code, url])
|
||||
for cb: Callable in callbacks:
|
||||
cb.call(null)
|
||||
return
|
||||
|
||||
# Write disk cache
|
||||
if body.is_empty():
|
||||
push_warning("ModelCache: empty response body — %s" % url)
|
||||
for cb: Callable in callbacks:
|
||||
cb.call(null)
|
||||
return
|
||||
|
||||
# Write raw bytes to disk cache for future sessions.
|
||||
var f := FileAccess.open(disk, FileAccess.WRITE)
|
||||
if f:
|
||||
f.store_buffer(body)
|
||||
f.close()
|
||||
else:
|
||||
push_warning("ModelCache: could not write cache file %s" % disk)
|
||||
|
||||
var packed := _load_gltf(disk)
|
||||
# Parse directly from the downloaded bytes — avoids any file-path quirks.
|
||||
var packed := _parse_gltf(body)
|
||||
_mem[url] = packed
|
||||
|
||||
for cb: Callable in callbacks:
|
||||
cb.call(packed.instantiate() if packed != null else null)
|
||||
)
|
||||
|
||||
var err := http.request(url)
|
||||
if err != OK:
|
||||
push_warning("ModelCache: request() failed for %s (err %d)" % [url, err])
|
||||
push_warning("ModelCache: request() error %d — %s" % [err, url])
|
||||
var callbacks: Array = _pending.get(url, [])
|
||||
_pending.erase(url)
|
||||
for cb: Callable in callbacks:
|
||||
cb.call(null)
|
||||
http.get_signal_connection_list("request_completed").clear()
|
||||
http.queue_free()
|
||||
|
||||
|
||||
# Loads a .glb/.gltf from disk into a PackedScene for repeated instantiation.
|
||||
func _load_gltf(path: String) -> PackedScene:
|
||||
# Parse GLB/GLTF bytes using GLTFDocument.append_from_buffer.
|
||||
# Returns a PackedScene ready for instantiation, or null on failure.
|
||||
func _parse_gltf(data: PackedByteArray) -> PackedScene:
|
||||
# Validate GLB magic bytes ("glTF").
|
||||
if data.size() >= 4:
|
||||
var magic := data.decode_u32(0)
|
||||
if magic != 0x46546C67: # little-endian "glTF"
|
||||
push_warning("ModelCache: not a GLB file (bad magic 0x%X)" % magic)
|
||||
return null
|
||||
|
||||
var doc := GLTFDocument.new()
|
||||
var state := GLTFState.new()
|
||||
var err := doc.append_from_file(path, state)
|
||||
# Empty base_path is correct for self-contained GLBs.
|
||||
var err := doc.append_from_buffer(data, "", state)
|
||||
if err != OK:
|
||||
push_warning("ModelCache: GLTFDocument error %d loading %s" % [err, path])
|
||||
push_warning("ModelCache: GLTFDocument.append_from_buffer error %d" % err)
|
||||
return null
|
||||
|
||||
var root := doc.generate_scene(state)
|
||||
if root == null:
|
||||
push_warning("ModelCache: generate_scene returned null for %s" % path)
|
||||
push_warning("ModelCache: generate_scene returned null")
|
||||
return null
|
||||
|
||||
var packed := PackedScene.new()
|
||||
var packed := PackedScene.new()
|
||||
var pack_err := packed.pack(root)
|
||||
root.queue_free()
|
||||
|
||||
if pack_err != OK:
|
||||
push_warning("ModelCache: PackedScene.pack() error %d for %s" % [pack_err, path])
|
||||
push_warning("ModelCache: PackedScene.pack error %d" % pack_err)
|
||||
return null
|
||||
|
||||
return packed
|
||||
|
||||
|
||||
func _disk_path(url: String) -> String:
|
||||
return _CACHE_DIR + url.sha256_text() + ".glb"
|
||||
return _CACHE_DIR + "/" + url.sha256_text() + ".bin"
|
||||
|
||||
Reference in New Issue
Block a user