From 8efcf24dd09fd1f377b2f58a33360c53ee611d5d Mon Sep 17 00:00:00 2001 From: Nordup Date: Tue, 8 Oct 2024 04:54:03 +0400 Subject: [PATCH] await previous request --- app/scripts/loading/file_downloader.gd | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/scripts/loading/file_downloader.gd b/app/scripts/loading/file_downloader.gd index b54e8c8..15e63e7 100644 --- a/app/scripts/loading/file_downloader.gd +++ b/app/scripts/loading/file_downloader.gd @@ -24,12 +24,19 @@ func _ready() -> void: func is_cached(url: String) -> bool: var save_path = DOWNLOAD_FOLDER + "/" + url.md5_text() + "." + url.get_file().get_extension() + for request in download_requests: + if request.save_path == save_path: + return false + return FileAccess.file_exists(save_path) func download(url: String, timeout: float = 0) -> String: var save_path = DOWNLOAD_FOLDER + "/" + url.md5_text() + "." + url.get_file().get_extension() + if has_request(save_path): + await request_completed(save_path) + if FileAccess.file_exists(save_path): await get_tree().process_frame return save_path @@ -48,6 +55,9 @@ func download_shared_lib(url: String, gate_url: String) -> String: var dir = DOWNLOAD_FOLDER + "/" + gate_url.md5_text() var save_path = dir + "/" + url.get_file() + if has_request(save_path): + await request_completed(save_path) + if FileAccess.file_exists(save_path): await get_tree().process_frame return dir @@ -61,6 +71,16 @@ func download_shared_lib(url: String, gate_url: String) -> String: return "" +func has_request(save_path: String) -> bool: + return download_requests.any(func(request: DownloadRequest): return request.save_path == save_path) + + +func request_completed(save_path: String) -> void: + for request in download_requests: + if request.save_path == save_path: + await request.http.request_completed + + func create_request(url: String, save_path: String, timeout: float = 0) -> int: var http = HTTPRequest.new() http.download_file = save_path