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)
)