fix: restore full Godot auth in progressive web build
This commit is contained in:
@@ -47,6 +47,7 @@ var _active_pack_scene_name: String = ""
|
||||
var _pack_overlay: CanvasLayer
|
||||
var _pack_status_label: Label
|
||||
var _pack_progress_bar: ProgressBar
|
||||
var _web_pack_progress_last_emit_msec: int = 0
|
||||
|
||||
const PACK_MANIFEST_PATH: String = "/packs/manifest.json"
|
||||
const PACK_CACHE_DIR: String = "user://scene-packs"
|
||||
@@ -160,8 +161,21 @@ func _process(_delta: float) -> void:
|
||||
_pack_progress_bar.value = downloaded
|
||||
if is_instance_valid(_pack_status_label):
|
||||
_pack_status_label.text = "正在加载场景 %s" % _format_download_progress(downloaded, total)
|
||||
_notify_web_auth_pack_progress(downloaded, total)
|
||||
scene_pack_progress.emit(_active_pack_scene_name, downloaded, total)
|
||||
|
||||
func _notify_web_auth_pack_progress(downloaded: int, total: int) -> void:
|
||||
if OS.get_name() != "Web" or _active_pack_scene_name != "auth":
|
||||
return
|
||||
var now := Time.get_ticks_msec()
|
||||
if now - _web_pack_progress_last_emit_msec < 250:
|
||||
return
|
||||
_web_pack_progress_last_emit_msec = now
|
||||
if not Engine.has_singleton("JavaScriptBridge"):
|
||||
return
|
||||
var bridge: Object = Engine.get_singleton("JavaScriptBridge")
|
||||
bridge.eval("window.whaletownPackProgress && window.whaletownPackProgress(%d, %d)" % [downloaded, total], true)
|
||||
|
||||
func _ensure_scene_pack(scene_name: String) -> bool:
|
||||
var packKey := str(SCENE_PACK_KEYS.get(scene_name, ""))
|
||||
if packKey.is_empty() or bool(_loaded_scene_packs.get(packKey, false)):
|
||||
@@ -181,10 +195,11 @@ func _ensure_scene_pack(scene_name: String) -> bool:
|
||||
if fileName.is_empty() or not fileName.ends_with(".pck"):
|
||||
_show_pack_error(scene_name, "场景资源清单格式错误")
|
||||
return false
|
||||
var expectedSize := int(entry.get("size", 0))
|
||||
var cacheDirAbsolute := ProjectSettings.globalize_path(PACK_CACHE_DIR)
|
||||
DirAccess.make_dir_recursive_absolute(cacheDirAbsolute)
|
||||
var localPath := "%s/%s" % [PACK_CACHE_DIR, fileName]
|
||||
if FileAccess.file_exists(localPath) and ProjectSettings.load_resource_pack(localPath, true):
|
||||
if _is_scene_pack_file_valid(localPath, fileName, expectedSize) and ProjectSettings.load_resource_pack(localPath, true):
|
||||
_loaded_scene_packs[packKey] = true
|
||||
_hide_pack_overlay()
|
||||
return true
|
||||
@@ -194,6 +209,10 @@ func _ensure_scene_pack(scene_name: String) -> bool:
|
||||
if packUrl.is_empty() or not await _download_pack(scene_name, packUrl, localPath):
|
||||
_show_pack_error(scene_name, "场景下载失败,请检查网络后重试")
|
||||
return false
|
||||
if not _is_scene_pack_file_valid(localPath, fileName, expectedSize):
|
||||
DirAccess.remove_absolute(ProjectSettings.globalize_path(localPath))
|
||||
_show_pack_error(scene_name, "场景资源下载不完整,请重新进入")
|
||||
return false
|
||||
if not ProjectSettings.load_resource_pack(localPath, true):
|
||||
DirAccess.remove_absolute(ProjectSettings.globalize_path(localPath))
|
||||
_show_pack_error(scene_name, "场景资源损坏,请重新进入")
|
||||
@@ -202,6 +221,26 @@ func _ensure_scene_pack(scene_name: String) -> bool:
|
||||
_hide_pack_overlay()
|
||||
return true
|
||||
|
||||
func _is_scene_pack_file_valid(local_path: String, file_name: String, expected_size: int) -> bool:
|
||||
if not FileAccess.file_exists(local_path):
|
||||
return false
|
||||
var file := FileAccess.open(local_path, FileAccess.READ)
|
||||
if file == null:
|
||||
return false
|
||||
var actualSize := file.get_length()
|
||||
file.close()
|
||||
if expected_size > 0 and actualSize != expected_size:
|
||||
return false
|
||||
var stem := file_name.trim_suffix(".pck")
|
||||
var separator := stem.rfind("-")
|
||||
if separator < 0:
|
||||
return true
|
||||
var expectedHashPrefix := stem.substr(separator + 1).to_lower()
|
||||
if expectedHashPrefix.length() != 12:
|
||||
return true
|
||||
var actualHash := FileAccess.get_sha256(local_path).to_lower()
|
||||
return not actualHash.is_empty() and actualHash.begins_with(expectedHashPrefix)
|
||||
|
||||
func _download_pack_manifest() -> Dictionary:
|
||||
var request := HTTPRequest.new()
|
||||
request.timeout = 12.0
|
||||
|
||||
Reference in New Issue
Block a user