Debug: log ModelCache HTTP result/headers/bytes; fix weakref captures

- ModelCache._download: log result enum, HTTP code, body size,
  Content-Type header, and first 16 bytes for every download — enough
  to diagnose why Godot receives non-GLB data despite CDN serving valid GLB
- Moved disk-cache write to AFTER a successful parse so corrupt bytes
  are never written; disk cache read now deletes + re-downloads on bad parse
- game_screen: replace raw `body` lambda captures with weakref to
  eliminate "Lambda capture freed" noise (behavior unchanged)

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 14:06:23 -04:00
co-authored by Claude Happy
parent 6dda0edf17
commit 03b553feab
2 changed files with 49 additions and 21 deletions
+38 -14
View File
@@ -41,10 +41,12 @@ func load_model(url: String, callback: Callable) -> void:
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
if packed != null:
_mem[url] = packed
callback.call(packed.instantiate())
return
# Corrupted cache file — delete and re-download.
push_warning("ModelCache: deleting corrupt disk cache for %s" % url)
DirAccess.remove_absolute(disk)
# 3 — already downloading → queue
@@ -58,11 +60,13 @@ func load_model(url: String, callback: Callable) -> void:
func _download(url: String, disk: String) -> void:
print("ModelCache: downloading — %s" % url)
var http := HTTPRequest.new()
http.use_threads = true
http.body_size_limit = -1 # unlimited
add_child(http)
http.request_completed.connect(func(_result, code, _headers, body: PackedByteArray):
http.request_completed.connect(
func(res: int, code: int, headers: PackedStringArray, body: PackedByteArray):
# Disconnect and free the requester before anything else.
http.get_signal_connection_list("request_completed").clear()
http.queue_free()
@@ -70,28 +74,48 @@ func _download(url: String, disk: String) -> void:
var callbacks: Array = _pending.get(url, [])
_pending.erase(url)
# Log details to help diagnose content-type / truncation issues.
var ct := ""
for h: String in headers:
if h.to_lower().begins_with("content-type"):
ct = h
break
var first_hex := ""
for i in mini(16, body.size()):
first_hex += "%02X " % body[i]
print("ModelCache: result=%d http=%d size=%d [%s] first=[%s]" \
% [res, code, body.size(), ct, first_hex.strip_edges()])
# res is the HTTPRequest.Result enum; anything != 0 means a network error.
if res != HTTPRequest.RESULT_SUCCESS:
push_warning("ModelCache: network error result=%d http=%d — %s" % [res, code, url])
for cb: Callable in callbacks:
cb.call(null)
return
if code < 200 or code >= 300:
push_warning("ModelCache: download failed (HTTP %d) — %s" % [code, url])
push_warning("ModelCache: HTTP %d — %s" % [code, url])
for cb: Callable in callbacks:
cb.call(null)
return
if body.is_empty():
push_warning("ModelCache: empty response body — %s" % url)
push_warning("ModelCache: empty response — %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()
# Parse directly from the downloaded bytes — avoids any file-path quirks.
var packed := _parse_gltf(body)
_mem[url] = packed
if packed != null:
# Only write a good file to the disk cache.
var f := FileAccess.open(disk, FileAccess.WRITE)
if f:
f.store_buffer(body)
f.close()
for cb: Callable in callbacks:
cb.call(packed.instantiate() if packed != null else null)
)
@@ -427,8 +427,10 @@ func _add_tile(x: int, y: int, affinity: String) -> void:
if model_url.is_empty():
return
var body_ref := weakref(body)
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
if not is_instance_valid(body):
var b := body_ref.get_ref() as StaticBody3D
if not is_instance_valid(b):
if model_node != null:
model_node.queue_free()
return
@@ -436,13 +438,13 @@ func _add_tile(x: int, y: int, affinity: String) -> void:
return
# Remove the placeholder MeshInstance3D.
for child in body.get_children():
for child in b.get_children():
if child is MeshInstance3D:
child.queue_free()
# Tile body sits at wp + (0, 0.25, 0); model should author its
# centre at y = 0 so it fills the same space as the box placeholder.
body.add_child(model_node)
b.add_child(model_node)
)
@@ -540,9 +542,11 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
if model_url.is_empty():
return
var body_ref := weakref(body)
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
# Guard: body may have been freed by a state rebuild before load finished.
if not is_instance_valid(body):
var b := body_ref.get_ref() as StaticBody3D
if not is_instance_valid(b):
if model_node != null:
model_node.queue_free()
return
@@ -550,15 +554,15 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
return
# Remove the placeholder MeshInstance3D.
for child in body.get_children():
for child in b.get_children():
if child is MeshInstance3D:
child.queue_free()
# Position model so its local origin sits at the body's position.
# Models should be authored with their base (foot) at y = 0.
# Adjust the body back down so the model base lands on the tile surface.
body.position = wp + Vector3(0, 0.5, 0)
body.add_child(model_node)
b.position = wp + Vector3(0, 0.5, 0)
b.add_child(model_node)
_unit_meshes[iid] = model_node
)