download progress, gate errors

This commit is contained in:
Nordup 2023-07-02 19:53:52 +04:00
parent 017717d280
commit e0d4d4202a
8 changed files with 117 additions and 20 deletions

View file

@ -32,7 +32,9 @@ grow_horizontal = 2
grow_vertical = 2
focus_mode = 2
bbcode_enabled = true
scroll_following = true
autowrap_mode = 1
context_menu_enabled = true
threaded = true
selection_enabled = true
script = ExtResource("2_lgt6s")

View file

@ -388,8 +388,6 @@ size_flags_vertical = 3
focus_mode = 2
theme_override_font_sizes/normal_font_size = 24
bbcode_enabled = true
text = "Downloading gate info...
Please wait"
context_menu_enabled = true
selection_enabled = true
script = ExtResource("16_8emm0")

View file

@ -24,6 +24,6 @@ func get_libraries(base_url: String) -> PackedStringArray:
var unsplit_libs = GDExtension.find_extension_library("", config)
if unsplit_libs.is_empty(): return []
var libraries = unsplit_libs.split(";")
for i in range(libraries.size()): libraries[i] = Url.join(base_url, libraries[i])
return libraries
var libs = unsplit_libs.split(";")
for i in range(libs.size()): libs[i] = Url.join(base_url, libs[i])
return libs

View file

@ -1,6 +1,9 @@
extends Node
signal progress(url: String, body_size: int, downloaded_bytes: int)
var folder: String = "user://gates_data"
var timer_speed := 0.3
func _ready() -> void:
@ -10,7 +13,7 @@ func _ready() -> void:
func download(url: String) -> String:
var save_path = folder + "/" + url.md5_text() + "." + url.get_file().get_extension()
if FileAccess.file_exists(save_path):
await get_tree().process_frame # TODO: remove workaround
await get_tree().process_frame
return save_path
DirAccess.make_dir_recursive_absolute(save_path.get_base_dir())
@ -20,10 +23,27 @@ func download(url: String) -> String:
http.download_file = save_path
var err = http.request(url)
await http.request_completed
if err != OK: return ""
var timer = Timer.new()
add_child(timer)
timer.timeout.connect(print_percent.bind(url, http))
timer.start(timer_speed)
var res = await http.request_completed
var code = res[1]
print_percent(url, http)
timer.stop()
remove_child(timer)
remove_child(http)
return save_path if err == OK else ""
return save_path if code == 200 else ""
func print_percent(url: String, http: HTTPRequest) -> void:
progress.emit(url, http.get_body_size(), http.get_downloaded_bytes())
# Returns directory where file was downloaded. Keeps filename
@ -31,7 +51,7 @@ func download_shared_lib(url: String, gate_url: String) -> String:
var dir = folder + "/" + gate_url.md5_text()
var save_path = dir + "/" + url.get_file()
if FileAccess.file_exists(save_path):
await get_tree().process_frame # TODO: remove workaround
await get_tree().process_frame
return dir
DirAccess.make_dir_recursive_absolute(dir)
@ -41,10 +61,31 @@ func download_shared_lib(url: String, gate_url: String) -> String:
http.download_file = save_path
var err = http.request(url)
await http.request_completed
if err != OK: return ""
var timer = Timer.new()
add_child(timer)
timer.timeout.connect(print_percent.bind(url, http))
timer.start(timer_speed)
var res = await http.request_completed
var code = res[1]
print_percent(url, http)
timer.stop()
remove_child(timer)
remove_child(http)
return dir if err == OK else ""
if code == 200:
return save_path
else:
DirAccess.remove_absolute(save_path)
return ""
func stop_all() -> void:
pass # TODO
func _exit_tree() -> void:

View file

@ -6,12 +6,15 @@ var c_gate: ConfigGate
func _ready() -> void:
FileDownloader.progress.connect(gate_events.download_progress_emit)
load_gate(gate_events.current_gate_url)
func load_gate(config_url: String) -> void:
Debug.logclr("======== " + config_url + " ========", Color.GREEN)
var config_path = await FileDownloader.download(config_url)
if config_path.is_empty(): return error(GateEvents.GateError.NOT_FOUND)
c_gate = ConfigGate.new(config_path, config_url)
gate_events.gate_config_loaded_emit(config_url, c_gate)
@ -20,9 +23,21 @@ func load_gate(config_url: String) -> void:
gate_events.gate_info_loaded_emit(gate)
gate.resource_pack = await FileDownloader.download(c_gate.resource_pack_url)
if gate.resource_pack.is_empty(): return error(GateEvents.GateError.MISSING_PACK)
Debug.logclr("Downloading GDExtension libraries: " + str(c_gate.libraries), Color.DIM_GRAY)
for lib in c_gate.libraries:
gate.shared_libs_dir = await FileDownloader.download_shared_lib(lib, config_url)
if gate.shared_libs_dir.is_empty(): return error(GateEvents.GateError.MISSING_LIBS)
gate_events.gate_loaded_emit(gate)
func error(code: GateEvents.GateError) -> void:
Debug.logclr("GateError: " + GateEvents.GateError.keys()[code], Color.MAROON)
gate_events.gate_error_emit(code)
func _exit_tree() -> void:
FileDownloader.progress.disconnect(gate_events.download_progress_emit)
FileDownloader.stop_all()

View file

@ -9,18 +9,21 @@ signal gate_loaded(gate: Gate)
signal gate_entered
signal exit_gate
signal download_progress(url: String, body_size: int, downloaded_bytes: int)
signal gate_error(type: GateError)
enum GateError
{
NOT_FOUND,
MISSING_PACK,
MISSING_LIBS
}
var current_search_query: String
var current_gate_url: String
var current_gate: Gate
func open_gate_emit(url: String) -> void:
current_gate_url = Url.fix_gate_url(url)
current_search_query = ""
open_gate.emit(current_gate_url)
func search_emit(query: String) -> void:
current_search_query = query
current_gate_url = ""
@ -28,6 +31,13 @@ func search_emit(query: String) -> void:
search.emit(query)
func open_gate_emit(url: String) -> void:
current_gate_url = Url.fix_gate_url(url)
current_search_query = ""
open_gate.emit(current_gate_url)
func gate_config_loaded_emit(url: String, config: ConfigGate) -> void:
gate_config_loaded.emit(url, config)
@ -52,3 +62,11 @@ func exit_gate_emit() -> void:
current_gate = null
exit_gate.emit()
func download_progress_emit(url: String, body_size: int, downloaded_bytes: int) -> void:
download_progress.emit(url, body_size, downloaded_bytes)
func gate_error_emit(type: GateError) -> void:
gate_error.emit(type)

View file

@ -11,6 +11,7 @@ var gate: Gate
func _ready() -> void:
gate_events.gate_info_loaded.connect(display_info)
gate_events.gate_error.connect(on_gate_error)
func display_info(_gate: Gate) -> void:
@ -18,3 +19,7 @@ func display_info(_gate: Gate) -> void:
title.text = "Unnamed" if gate.title.is_empty() else gate.title
description.text = "No description" if gate.description.is_empty() else gate.description
image.texture = FileTools.load_external_tex(gate.image)
func on_gate_error(_code: GateEvents.GateError) -> void:
description.set_text("")

View file

@ -6,11 +6,29 @@ extends Label
func _ready() -> void:
gate_events.gate_info_loaded.connect(func(_gate): on_gate_info_loaded())
gate_events.gate_entered.connect(on_gate_entered)
gate_events.gate_error.connect(on_gate_error)
set_text("Connecting...")
func on_gate_info_loaded() -> void:
text = "Downloading files..."
gate_events.download_progress.connect(show_progress)
func show_progress(_url: String, body_size: int, downloaded_bytes: int) -> void:
var percent = int(downloaded_bytes * 100 / body_size)
set_text("Downloading: %d%s" % [percent, "%"])
func on_gate_entered() -> void:
text = ""
gate_events.download_progress.disconnect(show_progress)
set_text("")
func on_gate_error(code: GateEvents.GateError) -> void:
match code:
GateEvents.GateError.NOT_FOUND:
set_text("Gate not found")
GateEvents.GateError.MISSING_PACK, GateEvents.GateError.MISSING_LIBS:
set_text("Cannot load gate resources")
_:
set_text("Error")