This commit is contained in:
dingo
2025-12-23 08:32:22 -05:00
parent 7c29268fa4
commit 6a4a64e39e
6 changed files with 195 additions and 41 deletions
Binary file not shown.
+86 -37
View File
@@ -4,7 +4,8 @@ extends Node
## Downloads and caches files from CDN, enabling hot-updates without client redeploy ## Downloads and caches files from CDN, enabling hot-updates without client redeploy
## Files are stored at: {CDN_BASE_URL}/{cdnPath} ## Files are stored at: {CDN_BASE_URL}/{cdnPath}
## ##
## For .tscn scenes: textures are also downloaded and paths are rewritten to use cache ## 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_check_started
signal update_progress(downloaded: int, total: int, current_file: String) signal update_progress(downloaded: int, total: int, current_file: String)
@@ -30,13 +31,32 @@ var _loaded_scenes: Dictionary = {} # filePath -> PackedScene
var _loaded_textures: Dictionary = {} # filePath -> Texture2D var _loaded_textures: Dictionary = {} # filePath -> Texture2D
var _loaded_scripts: Dictionary = {} # filePath -> GDScript 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(): func _ready():
# Ensure cache directory exists print("[UpdateManager] _ready called!")
DirAccess.make_dir_recursive_absolute(LOCAL_CACHE_DIR) print("[UpdateManager] OS.get_name(): %s" % OS.get_name())
_load_local_manifest() 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")
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 ## Request manifest from server and check for updates
func check_for_updates(): func check_for_updates():
print("[UpdateManager] check_for_updates() called")
if _is_updating: if _is_updating:
push_warning("[UpdateManager] Already checking for updates") push_warning("[UpdateManager] Already checking for updates")
return return
@@ -44,6 +64,7 @@ func check_for_updates():
update_check_started.emit() update_check_started.emit()
if Network and Network.is_socket_connected(): if Network and Network.is_socket_connected():
print("[UpdateManager] Sending ASSET_MANIFEST_REQUEST")
Network.send_message(OpCodes.ASSET_MANIFEST_REQUEST, {}) Network.send_message(OpCodes.ASSET_MANIFEST_REQUEST, {})
else: else:
push_error("[UpdateManager] Network not connected, cannot check for updates") push_error("[UpdateManager] Network not connected, cannot check for updates")
@@ -51,8 +72,11 @@ func check_for_updates():
## Called by GameState when manifest response is received ## Called by GameState when manifest response is received
func on_manifest_received(data: Dictionary): func on_manifest_received(data: Dictionary):
print("[UpdateManager] on_manifest_received() called")
cdn_base_url = data.get("cdnBaseUrl", cdn_base_url) cdn_base_url = data.get("cdnBaseUrl", cdn_base_url)
print("[UpdateManager] CDN base URL: %s" % cdn_base_url)
var assets = data.get("assets", []) var assets = data.get("assets", [])
print("[UpdateManager] Received %d assets in manifest" % assets.size())
_manifest.clear() _manifest.clear()
for asset in assets: for asset in assets:
@@ -156,29 +180,33 @@ func _on_download_completed(result: int, response_code: int, _headers: PackedStr
_download_next() _download_next()
return return
# Save file to cache # Verify hash
var cache_path = _get_cache_path(item.filePath) 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
# Ensure subdirectory exists # Store in memory cache (works for both web and desktop)
var dir_path = cache_path.get_base_dir() _texture_bytes[item.filePath] = body
DirAccess.make_dir_recursive_absolute(dir_path) _local_hashes[item.filePath] = item.fileHash
var file = FileAccess.open(cache_path, FileAccess.WRITE) # For desktop, also save to disk for persistence
if file: if not _is_web:
file.store_buffer(body) var cache_path = _get_cache_path(item.filePath)
file.close() var dir_path = cache_path.get_base_dir()
DirAccess.make_dir_recursive_absolute(dir_path)
# Verify hash var file = FileAccess.open(cache_path, FileAccess.WRITE)
var actual_hash = _compute_md5(body) if file:
if actual_hash == item.fileHash: file.store_buffer(body)
_local_hashes[item.filePath] = item.fileHash file.close()
_files_downloaded += 1
file_downloaded.emit(item.filePath)
print("[UpdateManager] Downloaded: %s" % item.filePath)
else: else:
push_error("[UpdateManager] Hash mismatch for %s: expected %s, got %s" % [item.filePath, item.fileHash, actual_hash]) push_warning("[UpdateManager] Failed to save file to disk: %s" % cache_path)
else:
push_error("[UpdateManager] Failed to save file: %s" % cache_path) _files_downloaded += 1
file_downloaded.emit(item.filePath)
print("[UpdateManager] Downloaded: %s (%d bytes)" % [item.filePath, body.size()])
_download_next() _download_next()
@@ -389,23 +417,36 @@ func _find_file_recursive(dir: DirAccess, current_path: String, filename: String
## Load a texture from cache (for items, UI, etc.) ## Load a texture from cache (for items, UI, etc.)
func load_texture(file_path: String) -> Texture2D: func load_texture(file_path: String) -> Texture2D:
# Check memory cache # Check already-loaded texture cache
if _loaded_textures.has(file_path): if _loaded_textures.has(file_path):
return _loaded_textures[file_path] return _loaded_textures[file_path]
# Check disk cache var buffer: PackedByteArray
var cache_path = _get_cache_path(file_path)
if not FileAccess.file_exists(cache_path):
push_warning("[UpdateManager] Texture not in cache: %s" % file_path)
return null
var file = FileAccess.open(cache_path, FileAccess.READ) # Try memory cache first (works for web and desktop)
if not file: if _texture_bytes.has(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):
push_warning("[UpdateManager] Texture not in cache: %s" % file_path)
return null
var file = FileAccess.open(cache_path, FileAccess.READ)
if not file:
push_warning("[UpdateManager] Failed to open cached file: %s" % 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:
push_warning("[UpdateManager] Texture not in memory cache (web): %s" % file_path)
return null return null
var buffer = file.get_buffer(file.get_length())
file.close()
var image = Image.new() var image = Image.new()
var err = image.load_png_from_buffer(buffer) var err = image.load_png_from_buffer(buffer)
if err != OK: if err != OK:
@@ -421,10 +462,18 @@ func load_texture(file_path: String) -> Texture2D:
_loaded_textures[file_path] = texture _loaded_textures[file_path] = texture
return texture return texture
## Check if a file is in the cache ## Check if a file is in the cache (memory or disk)
func has_file(file_path: String) -> bool: func has_file(file_path: String) -> bool:
var cache_path = _get_cache_path(file_path) # Check memory cache first (works for both web and desktop)
return FileAccess.file_exists(cache_path) if _texture_bytes.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
## Get the local cache path for a file ## Get the local cache path for a file
func get_cache_path(file_path: String) -> String: func get_cache_path(file_path: String) -> String:
+5
View File
@@ -18,6 +18,8 @@ var _current_state: String = "idle"
var _facing_direction: String = "down" # down, up, left, right, front, back var _facing_direction: String = "down" # down, up, left, right, front, back
func _ready(): func _ready():
print("[MobVisual] _ready called!")
# Try to find child nodes (they may not exist in all mob scenes) # Try to find child nodes (they may not exist in all mob scenes)
fallback_sprite = get_node_or_null("Sprite2D") fallback_sprite = get_node_or_null("Sprite2D")
name_label = get_node_or_null("NameLabel") name_label = get_node_or_null("NameLabel")
@@ -50,6 +52,9 @@ func _ready():
# Connect to UpdateManager for async texture loading # Connect to UpdateManager for async texture loading
if UpdateManager: if UpdateManager:
UpdateManager.file_downloaded.connect(_on_file_downloaded) UpdateManager.file_downloaded.connect(_on_file_downloaded)
print("[MobVisual] Connected to UpdateManager")
else:
print("[MobVisual] WARNING: UpdateManager is null!")
func update_data(data: Dictionary): func update_data(data: Dictionary):
mob_data = data mob_data = data
+46 -1
View File
@@ -179,6 +179,11 @@ func _update_hotbar():
if i >= hotbar_slots.size(): if i >= hotbar_slots.size():
break break
var slot = hotbar_slots[i] var slot = hotbar_slots[i]
# Clear existing children (sprites, labels)
for child in slot.get_children():
child.queue_free()
var hotbar_entry = GameState.hotbar[i] var hotbar_entry = GameState.hotbar[i]
if hotbar_entry and hotbar_entry is Dictionary and hotbar_entry.has("inventory_slot"): if hotbar_entry and hotbar_entry is Dictionary and hotbar_entry.has("inventory_slot"):
@@ -186,7 +191,47 @@ func _update_hotbar():
if inv_slot < GameState.inventory.size(): if inv_slot < GameState.inventory.size():
var item = GameState.inventory[inv_slot] var item = GameState.inventory[inv_slot]
if item and item.get("type") == "Consumable": if item and item.get("type") == "Consumable":
slot.text = _get_item_emoji(item) slot.text = ""
# Try to load sprite from CDN
var sprite_path = item.get("spritePath", item.get("SpritePath", ""))
var has_sprite = false
if sprite_path and not sprite_path.is_empty() and UpdateManager:
var texture = UpdateManager.load_texture(sprite_path)
if texture:
var sprite = TextureRect.new()
sprite.texture = texture
sprite.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
sprite.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
sprite.custom_minimum_size = Vector2(28, 28)
sprite.position = Vector2(6, 6) # Center in slot
# Apply tint if specified
var tint = item.get("tintColor", item.get("TintColor", ""))
if tint and not tint.is_empty():
sprite.modulate = Color.from_string(tint, Color.WHITE)
slot.add_child(sprite)
has_sprite = true
# Fall back to emoji if no sprite
if not has_sprite:
slot.text = _get_item_emoji(item)
# Add stack count label
var quantity = item.get("quantity", item.get("Quantity", 1))
if quantity > 1:
var count_label = Label.new()
count_label.text = str(quantity)
count_label.add_theme_font_size_override("font_size", 9)
count_label.add_theme_color_override("font_color", Color.WHITE)
count_label.add_theme_color_override("font_shadow_color", Color.BLACK)
count_label.add_theme_constant_override("shadow_offset_x", 1)
count_label.add_theme_constant_override("shadow_offset_y", 1)
count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
count_label.position = Vector2(slot.custom_minimum_size.x - 18, slot.custom_minimum_size.y - 14)
slot.add_child(count_label)
continue continue
slot.text = str(i + 1) slot.text = str(i + 1)
+55 -2
View File
@@ -156,14 +156,62 @@ func _refresh_inventory():
_update_slot(equipment_slots[slot_name], item, EQUIPMENT_ICONS.get(slot_name, "?")) _update_slot(equipment_slots[slot_name], item, EQUIPMENT_ICONS.get(slot_name, "?"))
func _update_slot(slot: Button, item, default_text: String = ""): func _update_slot(slot: Button, item, default_text: String = ""):
# Clear any existing children (sprites, labels)
for child in slot.get_children():
child.queue_free()
if item == null or (item is Dictionary and item.is_empty()): if item == null or (item is Dictionary and item.is_empty()):
slot.text = default_text slot.text = default_text
slot.set_meta("item", null) slot.set_meta("item", null)
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2))) slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
else: else:
slot.text = _get_item_emoji(item) slot.text = "" # Clear text, we'll use sprite instead
slot.set_meta("item", item) slot.set_meta("item", item)
# Try to load sprite from CDN
var sprite_path = item.get("spritePath", item.get("SpritePath", ""))
var has_sprite = false
if sprite_path and not sprite_path.is_empty() and UpdateManager:
var texture = UpdateManager.load_texture(sprite_path)
if texture:
var sprite = TextureRect.new()
sprite.texture = texture
sprite.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
sprite.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
sprite.custom_minimum_size = Vector2(32, 32)
sprite.anchors_preset = Control.PRESET_CENTER
sprite.position = Vector2(8, 8) # Center in 48x48 slot
# Apply tint if specified
var tint = item.get("tintColor", item.get("TintColor", ""))
if tint and not tint.is_empty():
sprite.modulate = Color.from_string(tint, Color.WHITE)
slot.add_child(sprite)
has_sprite = true
# Fall back to emoji if no sprite
if not has_sprite:
slot.text = _get_item_emoji(item)
# Add stack count label if quantity > 1
var quantity = item.get("quantity", item.get("Quantity", 1))
if quantity > 1:
var count_label = Label.new()
count_label.text = str(quantity)
count_label.add_theme_font_size_override("font_size", 10)
count_label.add_theme_color_override("font_color", Color.WHITE)
count_label.add_theme_color_override("font_shadow_color", Color.BLACK)
count_label.add_theme_constant_override("shadow_offset_x", 1)
count_label.add_theme_constant_override("shadow_offset_y", 1)
count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
count_label.vertical_alignment = VERTICAL_ALIGNMENT_BOTTOM
count_label.anchors_preset = Control.PRESET_BOTTOM_RIGHT
count_label.position = Vector2(slot.custom_minimum_size.x - 20, slot.custom_minimum_size.y - 16)
slot.add_child(count_label)
# Style based on rarity
var rarity = item.get("rarity", item.get("Rarity", "Common")) var rarity = item.get("rarity", item.get("Rarity", "Common"))
var style = _create_slot_style(Color(0.15, 0.15, 0.2)) var style = _create_slot_style(Color(0.15, 0.15, 0.2))
style.border_color = RARITY_COLORS.get(rarity, Color.GRAY) style.border_color = RARITY_COLORS.get(rarity, Color.GRAY)
@@ -261,8 +309,13 @@ func _show_tooltip(item: Dictionary, slot: Button):
var rarity = item.get("rarity", item.get("Rarity", "Common")) var rarity = item.get("rarity", item.get("Rarity", "Common"))
var item_name = item.get("name", item.get("Name", "Unknown")) var item_name = item.get("name", item.get("Name", "Unknown"))
var item_type = item.get("type", item.get("Type", "")) var item_type = item.get("type", item.get("Type", ""))
var quantity = item.get("quantity", item.get("Quantity", 1))
name_label.text = item_name # Show quantity in name if > 1
if quantity > 1:
name_label.text = "%s (x%d)" % [item_name, quantity]
else:
name_label.text = item_name
name_label.modulate = RARITY_COLORS.get(rarity, Color.WHITE) name_label.modulate = RARITY_COLORS.get(rarity, Color.WHITE)
if type_label: if type_label:
+3 -1
View File
@@ -29,9 +29,11 @@ func _show_screen(state: String):
if auth_screen: if auth_screen:
_set_screen_visible(auth_screen, state == "AUTH") _set_screen_visible(auth_screen, state == "AUTH")
print("[Main] AuthScreen visible: ", state == "AUTH") print("[Main] AuthScreen visible: ", state == "AUTH")
if not Network.is_socket_connected(): if state == "AUTH" and not Network.is_socket_connected():
print("[Main] Connecting to server...")
Network.connect_to_server() Network.connect_to_server()
await Network.connected await Network.connected
print("[Main] Connected! Calling check_for_updates...")
UpdateManager.check_for_updates() UpdateManager.check_for_updates()
if hub_screen: if hub_screen:
_set_screen_visible(hub_screen, state == "HUB") _set_screen_visible(hub_screen, state == "HUB")