From c2daf2a98d37421a4ab59bd2125cf5a92f700e28 Mon Sep 17 00:00:00 2001 From: Nordup Date: Sun, 2 Jul 2023 21:54:07 +0400 Subject: [PATCH] stop all download requests --- project/scripts/loading/file_downloader.gd | 150 ++++++++++++--------- 1 file changed, 84 insertions(+), 66 deletions(-) diff --git a/project/scripts/loading/file_downloader.gd b/project/scripts/loading/file_downloader.gd index eeb8557..f7c2499 100644 --- a/project/scripts/loading/file_downloader.gd +++ b/project/scripts/loading/file_downloader.gd @@ -1,92 +1,110 @@ extends Node +class DownloadRequest: + var save_path: String + var http: HTTPRequest + var timer: Timer + + func _init(_save_path: String, _http: HTTPRequest, _timer: Timer) -> void: + save_path = _save_path + http = _http + timer = _timer + +const DOWNLOAD_FOLDER := "user://gates_data" +const PROGRESS_DELAY := 0.3 + signal progress(url: String, body_size: int, downloaded_bytes: int) -var folder: String = "user://gates_data" -var timer_speed := 0.3 +var download_requests: Array[DownloadRequest] func _ready() -> void: - FileTools.remove_recursive(folder) + FileTools.remove_recursive(DOWNLOAD_FOLDER) func download(url: String) -> String: - var save_path = folder + "/" + url.md5_text() + "." + url.get_file().get_extension() + var save_path = DOWNLOAD_FOLDER + "/" + url.md5_text() + "." + url.get_file().get_extension() + if FileAccess.file_exists(save_path): await get_tree().process_frame return save_path DirAccess.make_dir_recursive_absolute(save_path.get_base_dir()) - var http = HTTPRequest.new() - http.use_threads = true - add_child(http) + var result = await create_request(url, save_path) - http.download_file = save_path - var err = http.request(url) - 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 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 -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 - return dir - DirAccess.make_dir_recursive_absolute(dir) - - var http = HTTPRequest.new() - http.use_threads = true - add_child(http) - - http.download_file = save_path - var err = http.request(url) - 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) - - if code == 200: + if result == 200: return save_path else: DirAccess.remove_absolute(save_path) return "" +# Returns directory where file was downloaded. Keeps filename +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 FileAccess.file_exists(save_path): + await get_tree().process_frame + return dir + DirAccess.make_dir_recursive_absolute(dir) + + var result = await create_request(url, save_path) + + if result == 200: + return dir + else: + DirAccess.remove_absolute(save_path) + return "" + + +func create_request(url: String, save_path: String) -> int: + var http = HTTPRequest.new() + http.download_file = save_path + http.use_threads = true + add_child(http) + + var timer = create_progress_emitter(url, http) + var download_request = DownloadRequest.new(save_path, http, timer) + download_requests.append(download_request) + + var err = http.request(url) + if err != OK: return err + var code = (await http.request_completed)[1] + + progress.emit(url, http.get_body_size(), http.get_downloaded_bytes()) + timer.stop() + remove_child(timer) + remove_child(http) + download_requests.erase(download_request) + + return code + + +func create_progress_emitter(url: String, http: HTTPRequest) -> Timer: + var timer = Timer.new() + add_child(timer) + + var progress_emit = func(): + progress.emit(url, http.get_body_size(), http.get_downloaded_bytes()) + timer.timeout.connect(progress_emit) + timer.start(PROGRESS_DELAY) + + return timer + + func stop_all() -> void: - pass # TODO + for request in download_requests: + request.http.cancel_request() + remove_child(request.http) + + request.timer.stop() + remove_child(request.timer) + + DirAccess.remove_absolute(request.save_path) + + download_requests.clear() func _exit_tree() -> void: - FileTools.remove_recursive(folder) + FileTools.remove_recursive(DOWNLOAD_FOLDER)