Files
sor/Client/origin-client/autoloads/ModelCache.gd
T
e815dd708f Feat: load 3D models from card definition assets on game board
Client:
- Added ModelCache autoload (autoloads/ModelCache.gd)
  - Downloads .glb files from CDN via HTTPRequest
  - Disk cache: user://remote_model_cache/<sha256>.glb
  - Memory cache: PackedScene per URL for instant re-instantiation
  - Concurrent requests for same URL queue and share one download
  - Loads via GLTFDocument.append_from_file + generate_scene
- game_screen.gd: _add_unit_mesh now looks up cardDefinitionId
  → ContentCache.get_definition() → finds Model3D asset URL
  → calls ModelCache.load_model() to async replace placeholder geometry
  - Placeholder (sphere/cylinder/box) shown immediately
  - Real model swaps in on download completion
  - Guards against body being freed before model arrives
  - Added _unit_bodies dict for O(1) body lookup
- project.godot: registered ModelCache autoload

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>
2026-03-09 12:37:25 -04:00

119 lines
3.3 KiB
GDScript

extends Node
## ModelCache — downloads and caches .glb / .gltf 3D models from CDN.
##
## Usage:
## ModelCache.load_model(url, func(node: Node3D): ...)
##
## 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
## so subsequent app launches skip the download.
const _CACHE_DIR := "user://remote_model_cache/"
# url (String) → PackedScene (null if load failed)
static var _mem: Dictionary = {}
# url (String) → Array[Callable] (callbacks waiting on an in-flight download)
static var _pending: Dictionary = {}
func _ready() -> void:
DirAccess.make_dir_recursive_absolute(_CACHE_DIR)
## Request a model by CDN URL.
## callback(node: Node3D) is called on the main thread; node may be null.
func load_model(url: String, callback: Callable) -> void:
# 1 — memory hit
if _mem.has(url):
var packed: PackedScene = _mem[url]
callback.call(packed.instantiate() if packed != null else null)
return
# 2 — disk hit
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
# 3 — already downloading → queue
if _pending.has(url):
_pending[url].append(callback)
return
# 4 — start download
_pending[url] = [callback]
_download(url, disk)
func _download(url: String, disk: String) -> void:
var http := HTTPRequest.new()
http.use_threads = true
add_child(http)
http.request_completed.connect(func(_result, code, _headers, body: PackedByteArray):
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])
for cb: Callable in callbacks:
cb.call(null)
return
# Write disk cache
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)
_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])
var callbacks: Array = _pending.get(url, [])
_pending.erase(url)
for cb: Callable in callbacks:
cb.call(null)
# Loads a .glb/.gltf from disk into a PackedScene for repeated instantiation.
func _load_gltf(path: String) -> PackedScene:
var doc := GLTFDocument.new()
var state := GLTFState.new()
var err := doc.append_from_file(path, state)
if err != OK:
push_warning("ModelCache: GLTFDocument error %d loading %s" % [err, path])
return null
var root := doc.generate_scene(state)
if root == null:
push_warning("ModelCache: generate_scene returned null for %s" % path)
return null
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])
return null
return packed
func _disk_path(url: String) -> String:
return _CACHE_DIR + url.sha256_text() + ".glb"