Files
2026-01-31 10:06:08 -05:00

743 lines
23 KiB
GDScript

extends Node
## Update Manager
## Downloads and caches files from CDN, enabling hot-updates without client redeploy
## Files are stored at: {CDN_BASE_URL}/{cdnPath}
##
## For web builds: textures are kept in memory only (no disk cache)
## For desktop builds: textures are cached to disk
signal update_check_started
signal update_progress(downloaded: int, total: int, current_file: String)
signal update_completed(updated_count: int)
signal update_failed(error: String)
signal file_downloaded(file_path: String)
const LOCAL_CACHE_DIR = "user://cdn_cache/"
const MANIFEST_CACHE_FILE = "user://cdn_manifest.json"
const CLIENT_VERSION = "1.0.0"
var cdn_base_url: String = "https://assets.aeternalisgames.com/downloads/"
var _manifest: Dictionary = {} # filePath -> {fileHash, fileSize, version, assetType, cdnPath}
var _local_hashes: Dictionary = {} # filePath -> hash
var _download_queue: Array = []
var _is_updating: bool = false
var _files_downloaded: int = 0
var _files_total: int = 0
var _active_request: HTTPRequest = null
var _queued_files: Dictionary = {} # filePath -> true
var _pending_requests: Dictionary = {} # filePath -> priority (requested before manifest)
var _build_queue: Array = []
var _build_queue_active: bool = false
# Runtime loaded resources cache
var _loaded_scenes: Dictionary = {} # filePath -> PackedScene
var _loaded_textures: Dictionary = {} # filePath -> Texture2D
var _loaded_scripts: Dictionary = {} # filePath -> GDScript
# Web builds: store raw bytes in memory since disk access is unreliable
var _texture_bytes: Dictionary = {} # filePath -> PackedByteArray
var _is_web: bool = false
func _ready():
print("[UpdateManager] _ready called!")
print("[UpdateManager] OS.get_name(): %s" % OS.get_name())
print("[UpdateManager] OS.has_feature('web'): %s" % OS.has_feature("web"))
# Detect web platform
_is_web = OS.has_feature("web")
if _is_web:
print("[UpdateManager] Running on WEB - using memory-only cache")
_load_local_manifest_web()
else:
print("[UpdateManager] Running on DESKTOP - using disk cache")
# Ensure cache directory exists (desktop only)
DirAccess.make_dir_recursive_absolute(LOCAL_CACHE_DIR)
_load_local_manifest()
print("[UpdateManager] _ready complete!")
## Request manifest from server and check for updates
func check_for_updates():
print("[UpdateManager] check_for_updates() called")
if _is_updating:
push_warning("[UpdateManager] Already checking for updates")
return
update_check_started.emit()
if Network and Network.is_socket_connected():
print("[UpdateManager] Sending ASSET_MANIFEST_REQUEST")
Network.send_message(OpCodes.ASSET_MANIFEST_REQUEST, {})
else:
push_error("[UpdateManager] Network not connected, cannot check for updates")
update_failed.emit("Network not connected")
## Called by GameState when manifest response is received
func on_manifest_received(data: Dictionary):
print("[UpdateManager] on_manifest_received() called")
cdn_base_url = data.get("cdnBaseUrl", cdn_base_url)
print("[UpdateManager] CDN base URL: %s" % cdn_base_url)
var assets = data.get("assets", [])
print("[UpdateManager] Received %d assets in manifest" % assets.size())
_manifest.clear()
for asset in assets:
var file_path = asset.get("filePath", "")
if file_path.is_empty():
continue
var min_version = asset.get("minClientVersion", "")
if not min_version.is_empty() and _compare_versions(CLIENT_VERSION, min_version) < 0:
push_warning("[UpdateManager] Asset %s requires client version %s, we have %s" % [file_path, min_version, CLIENT_VERSION])
continue
_manifest[file_path] = {
"fileHash": asset.get("fileHash", ""),
"fileSize": asset.get("fileSize", 0),
"version": asset.get("version", 1),
"assetType": asset.get("assetType", 2),
"isRequired": asset.get("isRequired", false),
"cdnPath": asset.get("cdnPath", file_path)
}
print("[UpdateManager] Received manifest with %d files" % _manifest.size())
_check_for_updates()
_flush_pending_requests()
func _check_for_updates():
_download_queue.clear()
_queued_files.clear()
for file_path in _manifest:
var remote = _manifest[file_path]
var local_hash = _local_hashes.get(file_path, "")
if local_hash != remote.fileHash:
var entry = {
"filePath": file_path,
"cdnPath": remote.cdnPath,
"fileHash": remote.fileHash,
"fileSize": remote.fileSize,
"isRequired": remote.isRequired
}
_download_queue.append(entry)
_queued_files[file_path] = true
if _download_queue.is_empty():
print("[UpdateManager] All files up to date!")
update_completed.emit(0)
return
# Sort: required files first, then textures before scenes (so scenes can reference them)
_download_queue.sort_custom(func(a, b):
if a.isRequired != b.isRequired:
return a.isRequired
# Textures before scenes
var a_is_texture = a.filePath.ends_with(".png") or a.filePath.ends_with(".jpg")
var b_is_texture = b.filePath.ends_with(".png") or b.filePath.ends_with(".jpg")
return a_is_texture and not b_is_texture
)
_files_total = _download_queue.size()
_files_downloaded = 0
_is_updating = true
print("[UpdateManager] Need to download %d files" % _files_total)
_download_next()
func _download_next():
if _download_queue.is_empty():
_is_updating = false
_queued_files.clear()
if _is_web:
_save_local_manifest_web()
_save_local_manifest()
print("[UpdateManager] Update complete! Downloaded %d files" % _files_downloaded)
update_completed.emit(_files_downloaded)
return
var item = _download_queue.pop_front()
_queued_files.erase(item.filePath)
var url = cdn_base_url + item.cdnPath + "?h=" + item.fileHash
update_progress.emit(_files_downloaded, _files_total, item.filePath)
_active_request = HTTPRequest.new()
add_child(_active_request)
_active_request.request_completed.connect(_on_download_completed.bind(item))
print("[UpdateManager] Downloading: %s" % item.filePath)
var err = _active_request.request(url)
if err != OK:
push_error("[UpdateManager] Failed to start download for %s: %d" % [item.filePath, err])
_active_request.queue_free()
_active_request = null
_download_next()
func _on_download_completed(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray, item: Dictionary):
if _active_request:
_active_request.queue_free()
_active_request = null
if result != HTTPRequest.RESULT_SUCCESS or response_code != 200:
push_error("[UpdateManager] Download failed for %s: result=%d, code=%d" % [item.filePath, result, response_code])
if item.isRequired:
update_failed.emit("Failed to download required file: " + item.filePath)
_is_updating = false
return
else:
# Skip optional file and continue
_download_next()
return
# Verify hash
var actual_hash = _compute_md5(body)
if actual_hash != item.fileHash:
push_error("[UpdateManager] Hash mismatch for %s: expected %s, got %s" % [item.filePath, item.fileHash, actual_hash])
_download_next()
return
# Store in memory cache (works for both web and desktop)
_texture_bytes[item.filePath] = body
_local_hashes[item.filePath] = item.fileHash
print("[UpdateManager] Stored in _texture_bytes: %s (%d bytes)" % [item.filePath, body.size()])
# For desktop, also save to disk for persistence
if not _is_web:
var cache_path = _get_cache_path(item.filePath)
var dir_path = cache_path.get_base_dir()
DirAccess.make_dir_recursive_absolute(dir_path)
var file = FileAccess.open(cache_path, FileAccess.WRITE)
if file:
file.store_buffer(body)
file.close()
else:
push_warning("[UpdateManager] Failed to save file to disk: %s" % cache_path)
_files_downloaded += 1
print("[UpdateManager] Emitting file_downloaded signal for: %s" % item.filePath)
file_downloaded.emit(item.filePath)
print("[UpdateManager] Downloaded: %s (%d bytes)" % [item.filePath, body.size()])
_download_next()
## Request a single file on-demand (web: useful if texture wasn't ready)
func request_file(file_path: String, priority: bool = false):
if file_path.is_empty():
return
if _manifest.is_empty():
print("[UpdateManager] request_file(%s) - manifest not loaded yet" % file_path)
_pending_requests[file_path] = priority
return
if not _is_web and _local_hashes.has(file_path):
return
if _queued_files.has(file_path):
return
if not _manifest.has(file_path):
print("[UpdateManager] request_file(%s) - not in manifest" % file_path)
return
var remote = _manifest[file_path]
var entry = {
"filePath": file_path,
"cdnPath": remote.cdnPath,
"fileHash": remote.fileHash,
"fileSize": remote.fileSize,
"isRequired": remote.isRequired
}
if priority:
_download_queue.push_front(entry)
else:
_download_queue.append(entry)
_queued_files[file_path] = true
if not _is_updating:
_files_total = _download_queue.size()
_files_downloaded = 0
_is_updating = true
_download_next()
func _flush_pending_requests():
if _manifest.is_empty() or _pending_requests.is_empty():
return
var pending = _pending_requests.duplicate()
var pending_paths = pending.keys()
_pending_requests.clear()
for file_path in pending_paths:
var priority = pending.get(file_path, false)
request_file(file_path, priority)
## Queue heavy build work (e.g., sprite frame construction) to one per frame.
func queue_build(cb: Callable):
if cb == null:
return
_build_queue.append(cb)
if not _build_queue_active:
_build_queue_active = true
call_deferred("_process_build_queue")
func _process_build_queue():
if _build_queue.is_empty():
_build_queue_active = false
return
var cb = _build_queue.pop_front()
cb.call()
# Run the next build on the next frame to avoid stalling the main thread.
call_deferred("_process_build_queue")
## Get the cache path for a file (preserves directory structure)
func _get_cache_path(file_path: String) -> String:
return LOCAL_CACHE_DIR + file_path
## Load a scene from cache (for mobs, effects, etc.)
## Textures are loaded separately and injected after scene instantiation
func load_scene(file_path: String) -> PackedScene:
# Check memory cache
if _loaded_scenes.has(file_path):
return _loaded_scenes[file_path]
# Check disk cache
var cache_path = _get_cache_path(file_path)
if not FileAccess.file_exists(cache_path):
push_warning("[UpdateManager] Scene not in cache: %s" % file_path)
return null
# Load the .tscn file
var file = FileAccess.open(cache_path, FileAccess.READ)
if not file:
return null
var content = file.get_as_text()
file.close()
# Extract texture paths that the scene needs (for pre-loading)
var texture_paths = _extract_texture_paths(content)
# Pre-load all textures into memory cache
for tex_path in texture_paths:
_preload_texture_for_scene(tex_path)
# Rewrite the scene to use embedded textures or remove ext_resources
content = _rewrite_scene_for_runtime(content)
# Parse and load the scene
var scene = _parse_tscn(content, file_path)
if scene:
_loaded_scenes[file_path] = scene
return scene
## Instantiate a scene and inject textures
## Call this instead of scene.instantiate() to get textures working
func instantiate_scene(file_path: String) -> Node:
var scene = load_scene(file_path)
if not scene:
return null
var instance = scene.instantiate()
# Inject textures into the instance
_inject_textures(instance, file_path)
return instance
## Extract texture paths from a .tscn file
func _extract_texture_paths(content: String) -> Array:
var paths = []
var regex = RegEx.new()
regex.compile('\\[ext_resource[^\\]]*path="res://([^"]+)"[^\\]]*type="Texture2D"')
for match in regex.search_all(content):
var path = match.get_string(1)
if not path in paths:
paths.append(path)
return paths
## Pre-load a texture that a scene needs
func _preload_texture_for_scene(res_path: String):
# res_path is like "assets/sprites/skeleton_idle_front.png"
var filename = res_path.get_file()
# Try to find this texture in our cache
var cache_path = _find_texture_in_cache(filename)
if cache_path.is_empty():
# Try with the full path structure
cache_path = _get_cache_path(res_path)
if not FileAccess.file_exists(cache_path):
push_warning("[UpdateManager] Texture not found for scene: %s" % res_path)
return
# Load it into memory cache if not already there
if not _loaded_textures.has(res_path):
var texture = _load_texture_from_path(cache_path)
if texture:
_loaded_textures[res_path] = texture
# Also cache by filename for easier lookup
_loaded_textures[filename] = texture
print("[UpdateManager] Pre-loaded texture: %s" % res_path)
## Load a texture from a file path (handles user:// paths properly)
func _load_texture_from_path(path: String) -> Texture2D:
if not FileAccess.file_exists(path):
return null
var image = Image.new()
var err = image.load(path)
if err != OK:
push_error("[UpdateManager] Failed to load image: %s (error %d)" % [path, err])
return null
return ImageTexture.create_from_image(image)
## Rewrite scene content to remove external texture references
## We'll inject textures at runtime instead
func _rewrite_scene_for_runtime(content: String) -> String:
# Remove ext_resource lines for Texture2D - we'll load them manually
var lines = content.split("\n")
var new_lines = []
var texture_resource_ids = []
# First pass: identify texture ext_resources
var ext_res_regex = RegEx.new()
ext_res_regex.compile('\\[ext_resource[^\\]]*type="Texture2D"[^\\]]*id="([^"]+)"')
for line in lines:
var match = ext_res_regex.search(line)
if match:
texture_resource_ids.append(match.get_string(1))
# Skip this line (don't add to new_lines)
continue
new_lines.append(line)
var result = "\n".join(new_lines)
# Update load_steps count
var steps_regex = RegEx.new()
steps_regex.compile('load_steps=(\\d+)')
var steps_match = steps_regex.search(result)
if steps_match:
var old_steps = int(steps_match.get_string(1))
var new_steps = old_steps - texture_resource_ids.size()
result = result.replace("load_steps=%d" % old_steps, "load_steps=%d" % new_steps)
return result
## Inject pre-loaded textures into a scene instance
func _inject_textures(instance: Node, scene_path: String):
# Find all AnimatedSprite2D and Sprite2D nodes
_inject_textures_recursive(instance)
func _inject_textures_recursive(node: Node):
if node is AnimatedSprite2D:
_fix_animated_sprite(node)
elif node is Sprite2D:
_fix_sprite(node)
for child in node.get_children():
_inject_textures_recursive(child)
func _fix_animated_sprite(sprite: AnimatedSprite2D):
if not sprite.sprite_frames:
return
var frames = sprite.sprite_frames
# Iterate through all animations
for anim_name in frames.get_animation_names():
var frame_count = frames.get_frame_count(anim_name)
for i in range(frame_count):
var texture = frames.get_frame_texture(anim_name, i)
# If texture is an AtlasTexture, we need to fix its atlas
if texture is AtlasTexture:
var atlas_tex = texture as AtlasTexture
if atlas_tex.atlas == null:
# Try to find and assign the atlas texture
# The atlas might be referenced by a resource path we can match
pass # Atlas is already null, nothing to fix here
elif texture == null:
# Frame has no texture, might need to inject one
pass
func _fix_sprite(sprite: Sprite2D):
# If the sprite has no texture, we could try to assign one
# But we need to know which texture it should have
pass
## Search for a texture file anywhere in the cache
func _find_texture_in_cache(filename: String) -> String:
var dir = DirAccess.open(LOCAL_CACHE_DIR)
if not dir:
return ""
return _find_file_recursive(dir, LOCAL_CACHE_DIR, filename)
func _find_file_recursive(dir: DirAccess, current_path: String, filename: String) -> String:
dir.list_dir_begin()
var item = dir.get_next()
while item != "":
var full_path = current_path + item
if dir.current_is_dir():
var subdir = DirAccess.open(full_path)
if subdir:
var found = _find_file_recursive(subdir, full_path + "/", filename)
if not found.is_empty():
return found
elif item == filename:
return full_path
item = dir.get_next()
return ""
## Load a texture from cache (for items, UI, etc.)
func load_texture(file_path: String) -> Texture2D:
# Check already-loaded texture cache
if _loaded_textures.has(file_path):
#iprint("[UpdateManager] load_texture(%s) - Found in _loaded_textures cache" % file_path)
return _loaded_textures[file_path]
var buffer: PackedByteArray
# Try memory cache first (works for web and desktop)
if _texture_bytes.has(file_path):
print("[UpdateManager] load_texture(%s) - Found in _texture_bytes, creating texture..." % file_path)
buffer = _texture_bytes[file_path]
elif not _is_web:
# Desktop: try disk cache
var cache_path = _get_cache_path(file_path)
if not FileAccess.file_exists(cache_path):
print("[UpdateManager] load_texture(%s) - Not in disk cache" % file_path)
return null
var file = FileAccess.open(cache_path, FileAccess.READ)
if not file:
print("[UpdateManager] load_texture(%s) - Failed to open cached file" % file_path)
return null
buffer = file.get_buffer(file.get_length())
file.close()
# Store in memory cache for faster subsequent access
_texture_bytes[file_path] = buffer
else:
# Texture not in cache - this is normal for optional textures on web
# The calling code should handle null gracefully
print("[UpdateManager] load_texture(%s) - Not in memory cache (web build)" % file_path)
request_file(file_path)
return null
var image = Image.new()
var err = image.load_png_from_buffer(buffer)
if err != OK:
err = image.load_jpg_from_buffer(buffer)
if err != OK:
err = image.load_webp_from_buffer(buffer)
if err != OK:
push_error("[UpdateManager] Failed to load image from buffer: %s (buffer size: %d bytes)" % [file_path, buffer.size()])
return null
print("[UpdateManager] load_texture(%s) - Successfully created texture from buffer" % file_path)
var texture = ImageTexture.create_from_image(image)
_loaded_textures[file_path] = texture
if _is_web:
# Free raw bytes once the texture exists to reduce memory spikes on web.
_texture_bytes.erase(file_path)
return texture
## Check if a file is in the cache (memory or disk)
func has_file(file_path: String) -> bool:
# Check memory cache first (works for both web and desktop)
if _texture_bytes.has(file_path):
return true
# Loaded textures count as cached for web (bytes may have been freed)
if _loaded_textures.has(file_path):
return true
# For desktop, also check disk cache
if not _is_web:
var cache_path = _get_cache_path(file_path)
return FileAccess.file_exists(cache_path)
return false
## Wait until all files are available (web: checks memory/loaded cache)
func wait_for_files(paths: Array) -> void:
var remaining: Dictionary = {}
for p in paths:
var file_path := str(p)
if file_path.is_empty():
continue
if not has_file(file_path):
remaining[file_path] = true
if remaining.is_empty():
return
while remaining.size() > 0:
var downloaded_path: String = await file_downloaded
if remaining.has(downloaded_path):
remaining.erase(downloaded_path)
# Clear any that may have been loaded via other paths
for key in remaining.keys():
if has_file(key):
remaining.erase(key)
## Prefetch a set of files (web: ensures bytes are downloaded before use)
func prefetch_files(paths: Array, priority: bool = true, wait: bool = true) -> void:
if paths.is_empty():
return
var missing: Array = []
for p in paths:
var file_path := str(p)
if file_path.is_empty():
continue
if has_file(file_path):
continue
missing.append(file_path)
if missing.is_empty():
return
for file_path in missing:
request_file(file_path, priority)
# If requested, wait only for these files
if wait:
await wait_for_files(missing)
## Get the local cache path for a file
func get_cache_path(file_path: String) -> String:
return _get_cache_path(file_path)
## Clear all cached files
func clear_cache():
_loaded_scenes.clear()
_loaded_textures.clear()
_loaded_scripts.clear()
_local_hashes.clear()
# Recursively delete cache directory
_delete_directory_recursive(LOCAL_CACHE_DIR)
DirAccess.make_dir_recursive_absolute(LOCAL_CACHE_DIR)
_save_local_manifest()
func _delete_directory_recursive(path: String):
var dir = DirAccess.open(path)
if not dir:
return
dir.list_dir_begin()
var item = dir.get_next()
while item != "":
var full_path = path + "/" + item
if dir.current_is_dir():
_delete_directory_recursive(full_path)
DirAccess.remove_absolute(full_path)
else:
DirAccess.remove_absolute(full_path)
item = dir.get_next()
# ============ Internal Methods ============
func _parse_tscn(content: String, file_path: String) -> PackedScene:
# Save as a temp .tscn file and load it
# Godot doesn't have a direct way to parse tscn from string
var temp_path = "user://temp_scene_%d.tscn" % randi()
var temp_file = FileAccess.open(temp_path, FileAccess.WRITE)
if temp_file:
temp_file.store_string(content)
temp_file.close()
var scene = load(temp_path) as PackedScene
# Clean up temp file
DirAccess.remove_absolute(temp_path)
return scene
return null
func _compute_md5(data: PackedByteArray) -> String:
var ctx = HashingContext.new()
ctx.start(HashingContext.HASH_MD5)
ctx.update(data)
var hash_bytes = ctx.finish()
return hash_bytes.hex_encode()
func _compare_versions(v1: String, v2: String) -> int:
var parts1 = v1.split(".")
var parts2 = v2.split(".")
for i in range(max(parts1.size(), parts2.size())):
var p1 = int(parts1[i]) if i < parts1.size() else 0
var p2 = int(parts2[i]) if i < parts2.size() else 0
if p1 < p2:
return -1
elif p1 > p2:
return 1
return 0
func _load_local_manifest():
if not FileAccess.file_exists(MANIFEST_CACHE_FILE):
return
var file = FileAccess.open(MANIFEST_CACHE_FILE, FileAccess.READ)
if not file:
return
var json_str = file.get_as_text()
file.close()
var json = JSON.new()
if json.parse(json_str) == OK and json.data is Dictionary:
_local_hashes = json.data
print("[UpdateManager] Loaded local manifest with %d files" % _local_hashes.size())
func _save_local_manifest():
var file = FileAccess.open(MANIFEST_CACHE_FILE, FileAccess.WRITE)
if file:
file.store_string(JSON.stringify(_local_hashes))
file.close()
const WEB_HASHES_KEY: String = "cdn_manifest_hashes_v1"
func _load_local_manifest_web() -> void:
if not _is_web:
return
# JavaScriptBridge is a class in web exports; keep it simple.
# eval() returns Variant, so type it explicitly.
var raw: Variant = JavaScriptBridge.eval("localStorage.getItem('%s')" % WEB_HASHES_KEY, true)
if raw == null:
return
var json_str: String = str(raw)
if json_str.is_empty():
return
var parsed: Variant = JSON.parse_string(json_str)
if parsed is Dictionary:
_local_hashes = parsed as Dictionary
print("[UpdateManager] (WEB) Loaded local hashes: %d" % _local_hashes.size())
func _save_local_manifest_web() -> void:
if not _is_web:
return
var json_str: String = JSON.stringify(_local_hashes)
# Safer than trying to nest JSON.stringify() inside an eval string:
# we escape backslashes and quotes so the JS string literal is valid.
var escaped: String = json_str.replace("\\", "\\\\").replace("'", "\\'")
var js: String = "localStorage.setItem('%s','%s')" % [WEB_HASHES_KEY, escaped]
JavaScriptBridge.eval(js, false)