Feature: ETag-based CDN model cache invalidation
When admin replaces a model/art file via the CDN browser, the S3 ETag is now captured and stored on CardAsset.ContentETag. The ETag is included in the content sync DTO (CardAssetDto) so clients receive it on every sync. Client-side ModelCache now keys its disk cache on sha256(url + "#" + etag) instead of sha256(url), so updating a file at the same CDN path busts the disk cache automatically. A _CACHE_VER="v2" sentinel purges old-scheme .bin files on first launch after the client update. Changes: - CdnUploadResult: add string? ETag field - S3CdnClient: capture ETag from PutObjectAsync response - CardAsset: add string? ContentETag property - Migration 20260318000001_AddAssetContentETag: add content_etag text column - ContentMessages: add string? ContentETag to CardAssetDto - ContentDtoMapper: map ContentETag in CardAsset → CardAssetDto - CdnBrowser.razor: UploadFileToKeyAsync returns Task<string?> (ETag); ReplaceAsync calls PropagateAssetETagAsync which updates CardAsset rows and touches CardDefinition.UpdatedAt to trigger a version bump - ModelCache.gd: versioned disk key + cache migration + etag parameter on load_model() and evict() - game_screen.gd: pass model_asset.contentETag to load_model / retry chain 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:
@@ -2,61 +2,105 @@ extends Node
|
||||
## ModelCache — downloads and caches .glb / .gltf 3D models from CDN.
|
||||
##
|
||||
## Usage:
|
||||
## ModelCache.load_model(url, func(node: Node3D): ...)
|
||||
## ModelCache.load_model(url, func(node: Node3D): ..., etag)
|
||||
##
|
||||
## The callback receives a freshly-instantiated Node3D on success, or null on
|
||||
## failure. Multiple callers requesting the same URL share a single download.
|
||||
## Parsed PackedScenes are kept in memory so re-instantiation is instant.
|
||||
## Raw GLB bytes are written to:
|
||||
## user://remote_model_cache/<sha256>.bin
|
||||
## failure. Multiple callers requesting the same URL+ETag share a single
|
||||
## download. Parsed PackedScenes are kept in memory so re-instantiation is
|
||||
## instant. Raw GLB bytes are written to:
|
||||
## user://remote_model_cache/<sha256(url#etag)>.bin
|
||||
## so subsequent app launches skip the download.
|
||||
##
|
||||
## Cache versioning:
|
||||
## A sentinel file "user://remote_model_cache/_cache_ver" stores the current
|
||||
## cache scheme version. When the scheme changes (bump _CACHE_VER), all
|
||||
## stale .bin files from the old scheme are purged on first launch.
|
||||
|
||||
const _CACHE_DIR := "user://remote_model_cache"
|
||||
const _CACHE_VER := "v2" # bump this when the disk-key scheme changes
|
||||
|
||||
# On web exports, direct CDN fetches are blocked by CORS (Ceph RadosGW doesn't send
|
||||
# Access-Control-Allow-Origin). Route through the game server proxy instead.
|
||||
const _CDN_BASE := "https://cdn.aeternalisgames.com"
|
||||
const _PROXY_BASE := "https://hub.aeternalisgames.com/cdn"
|
||||
|
||||
# url (String) → PackedScene or null (null means "tried and failed")
|
||||
# cache-key (String) → PackedScene or null (null means "tried and failed")
|
||||
static var _mem: Dictionary = {}
|
||||
|
||||
# url (String) → Array[Callable] (callbacks waiting on an in-flight download)
|
||||
# cache-key (String) → Array[Callable] (callbacks waiting on an in-flight download)
|
||||
static var _pending: Dictionary = {}
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
DirAccess.make_dir_recursive_absolute(_CACHE_DIR)
|
||||
_migrate_cache()
|
||||
|
||||
|
||||
## Purge all .bin files if the cache version sentinel doesn't match _CACHE_VER.
|
||||
## This evicts entries written under an old disk-key scheme so stale models
|
||||
## are re-downloaded with the new scheme.
|
||||
func _migrate_cache() -> void:
|
||||
var ver_path := _CACHE_DIR + "/_cache_ver"
|
||||
var stored := ""
|
||||
if FileAccess.file_exists(ver_path):
|
||||
var f := FileAccess.open(ver_path, FileAccess.READ)
|
||||
if f:
|
||||
stored = f.get_as_text().strip_edges()
|
||||
f.close()
|
||||
|
||||
if stored == _CACHE_VER:
|
||||
return
|
||||
|
||||
# Version mismatch — delete every .bin file in the cache directory.
|
||||
var dir := DirAccess.open(_CACHE_DIR)
|
||||
if dir:
|
||||
dir.list_dir_begin()
|
||||
var name := dir.get_next()
|
||||
while name != "":
|
||||
if name.ends_with(".bin"):
|
||||
DirAccess.remove_absolute(_CACHE_DIR + "/" + name)
|
||||
name = dir.get_next()
|
||||
dir.list_dir_end()
|
||||
|
||||
# Write new sentinel.
|
||||
var f := FileAccess.open(ver_path, FileAccess.WRITE)
|
||||
if f:
|
||||
f.store_string(_CACHE_VER)
|
||||
f.close()
|
||||
|
||||
|
||||
## Remove a cached entry (memory + disk) so the next load_model call re-downloads it.
|
||||
## Use before retrying a previously-failed URL.
|
||||
func evict(url: String) -> void:
|
||||
_mem.erase(url)
|
||||
var disk := _disk_path(url)
|
||||
func evict(url: String, etag: String = "") -> void:
|
||||
var key := _cache_key(url, etag)
|
||||
_mem.erase(key)
|
||||
var disk := _disk_path(url, etag)
|
||||
if FileAccess.file_exists(disk):
|
||||
DirAccess.remove_absolute(disk)
|
||||
|
||||
|
||||
## 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
|
||||
## If the same URL+ETag is already downloading, callbacks are queued and all
|
||||
## fire together when the download completes.
|
||||
func load_model(url: String, callback: Callable) -> void:
|
||||
## etag: the ContentETag from the server DTO (empty string = no etag, use URL only).
|
||||
func load_model(url: String, callback: Callable, etag: String = "") -> void:
|
||||
var key := _cache_key(url, etag)
|
||||
var disk := _disk_path(url, etag)
|
||||
|
||||
# 1 — memory hit
|
||||
if _mem.has(url):
|
||||
var packed: PackedScene = _mem[url]
|
||||
if _mem.has(key):
|
||||
var packed: PackedScene = _mem[key]
|
||||
callback.call(packed.instantiate() if packed != null else null)
|
||||
return
|
||||
|
||||
# 2 — disk hit (bytes from previous session)
|
||||
var disk := _disk_path(url)
|
||||
if FileAccess.file_exists(disk):
|
||||
var bytes := FileAccess.get_file_as_bytes(disk)
|
||||
if not bytes.is_empty():
|
||||
var packed := _parse_gltf(bytes)
|
||||
if packed != null:
|
||||
_mem[url] = packed
|
||||
_mem[key] = packed
|
||||
callback.call(packed.instantiate())
|
||||
return
|
||||
# Corrupted cache file — delete and re-download.
|
||||
@@ -64,13 +108,13 @@ func load_model(url: String, callback: Callable) -> void:
|
||||
DirAccess.remove_absolute(disk)
|
||||
|
||||
# 3 — already downloading → queue
|
||||
if _pending.has(url):
|
||||
_pending[url].append(callback)
|
||||
if _pending.has(key):
|
||||
_pending[key].append(callback)
|
||||
return
|
||||
|
||||
# 4 — start download
|
||||
_pending[url] = [callback]
|
||||
_download(url, disk)
|
||||
_pending[key] = [callback]
|
||||
_download(url, key, disk)
|
||||
|
||||
|
||||
## Returns the URL to actually fetch — on web, rewrites CDN URLs to go through
|
||||
@@ -81,7 +125,7 @@ func _fetch_url(url: String) -> String:
|
||||
return url
|
||||
|
||||
|
||||
func _download(url: String, disk: String) -> void:
|
||||
func _download(url: String, key: String, disk: String) -> void:
|
||||
var fetch := _fetch_url(url)
|
||||
print("ModelCache: downloading — %s" % fetch)
|
||||
var http := HTTPRequest.new()
|
||||
@@ -94,8 +138,8 @@ func _download(url: String, disk: String) -> void:
|
||||
http.get_signal_connection_list("request_completed").clear()
|
||||
http.queue_free()
|
||||
|
||||
var callbacks: Array = _pending.get(url, [])
|
||||
_pending.erase(url)
|
||||
var callbacks: Array = _pending.get(key, [])
|
||||
_pending.erase(key)
|
||||
|
||||
# Log details to help diagnose content-type / truncation issues.
|
||||
var ct := ""
|
||||
@@ -130,7 +174,7 @@ func _download(url: String, disk: String) -> void:
|
||||
|
||||
# Parse directly from the downloaded bytes — avoids any file-path quirks.
|
||||
var packed := _parse_gltf(body)
|
||||
_mem[url] = packed
|
||||
_mem[key] = packed
|
||||
|
||||
if packed != null:
|
||||
# Only write a good file to the disk cache.
|
||||
@@ -146,8 +190,8 @@ func _download(url: String, disk: String) -> void:
|
||||
var err := http.request(fetch)
|
||||
if err != OK:
|
||||
push_warning("ModelCache: request() error %d — %s" % [err, fetch])
|
||||
var callbacks: Array = _pending.get(url, [])
|
||||
_pending.erase(url)
|
||||
var callbacks: Array = _pending.get(key, [])
|
||||
_pending.erase(key)
|
||||
for cb: Callable in callbacks:
|
||||
cb.call(null)
|
||||
http.get_signal_connection_list("request_completed").clear()
|
||||
@@ -196,5 +240,12 @@ func _parse_gltf(data: PackedByteArray) -> PackedScene:
|
||||
return packed
|
||||
|
||||
|
||||
func _disk_path(url: String) -> String:
|
||||
return _CACHE_DIR + "/" + url.sha256_text() + ".bin"
|
||||
## Disk key is sha256(url + "#" + etag) when etag is non-empty, or sha256(url) otherwise.
|
||||
func _cache_key(url: String, etag: String) -> String:
|
||||
if etag.is_empty():
|
||||
return url
|
||||
return url + "#" + etag
|
||||
|
||||
|
||||
func _disk_path(url: String, etag: String = "") -> String:
|
||||
return _CACHE_DIR + "/" + _cache_key(url, etag).sha256_text() + ".bin"
|
||||
|
||||
Reference in New Issue
Block a user