Browser seems good to go!
This commit is contained in:
@@ -25,6 +25,10 @@ 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
|
||||
@@ -101,22 +105,26 @@ func on_manifest_received(data: Dictionary):
|
||||
|
||||
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:
|
||||
_download_queue.append({
|
||||
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!")
|
||||
@@ -143,6 +151,7 @@ func _check_for_updates():
|
||||
func _download_next():
|
||||
if _download_queue.is_empty():
|
||||
_is_updating = false
|
||||
_queued_files.clear()
|
||||
if _is_web:
|
||||
_save_local_manifest_web()
|
||||
_save_local_manifest()
|
||||
@@ -151,6 +160,7 @@ func _download_next():
|
||||
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)
|
||||
@@ -215,6 +225,70 @@ func _on_download_completed(result: int, response_code: int, _headers: PackedStr
|
||||
|
||||
_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
|
||||
@@ -454,6 +528,7 @@ func load_texture(file_path: String) -> Texture2D:
|
||||
# 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()
|
||||
@@ -470,6 +545,9 @@ func load_texture(file_path: String) -> Texture2D:
|
||||
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)
|
||||
@@ -477,6 +555,9 @@ 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:
|
||||
@@ -485,6 +566,52 @@ func has_file(file_path: String) -> bool:
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user