8686# --
8787# -- - 15/11/2023 Lyaaaaa
8888# -- - Updated _downloads_done to call _reset before emitting "downloads_finished"
89+ # --
90+ # -- - 18/12/2023 Lyaaaaa
91+ # -- - Replaced the prints by push_error when needed.
92+ # -- - Replaced the _downloading var by _is_busy.
93+ # -- - Added is_busy method.
94+ # -- - is_downloading is now depreciated and calls is_busy.
95+ # -- - Updated _download_next_file to set busy to true.
96+ # --
97+ # -- - 05/02/2024 Lyaaaaa
98+ # -- - Added use_threads = true in _init to always have it on. It is important
99+ # -- for download speed
89100# ------------------------------------------------------------------------------
90101class_name FileDownloader
91102extends HTTPRequest
@@ -112,11 +123,13 @@ var _downloaded_size : float = 0
112123
113124var _last_method : int
114125
115- var _downloading : bool = false :
116- get = is_downloading
126+ var _busy : bool = false :
127+ get = is_busy ,
128+ set = set_busy
117129
118130
119131func _init () -> void :
132+ use_threads = true
120133 set_process (false )
121134 connect ("request_completed" , Callable (self , "_on_request_completed" ))
122135
@@ -134,7 +147,8 @@ func start_download(p_urls : PackedStringArray = file_urls,
134147 file_urls = p_urls
135148 save_path = p_save_path
136149 _create_directory ()
137- _download_next_file ()
150+ if not is_busy ():
151+ _download_next_file ()
138152
139153
140154func get_stats () -> Dictionary :
@@ -147,15 +161,23 @@ func get_stats() -> Dictionary:
147161
148162
149163func is_downloading () -> bool :
150- return _downloading
164+ # Depreciated. Will be removed in next release
165+ return is_busy ()
166+
167+
168+ func is_busy () -> bool :
169+ return _busy
170+
171+ func set_busy (p_busy : bool ) -> void :
172+ _busy = p_busy
151173
152174
153175func _reset () -> void :
154176 _current_url = ""
155177 _current_url_index = 0
156178 _downloaded_percent = 0
157179 _downloaded_size = 0
158- _downloading = false
180+ set_busy ( false )
159181
160182
161183func _downloads_done () -> void :
@@ -178,14 +200,13 @@ func _send_get_request() -> void:
178200 var error = request (_current_url , _headers , HTTPClient .METHOD_GET )
179201 if error == OK :
180202 emit_signal ("downloads_started" )
181- _downloading = true
182203 _last_method = HTTPClient .METHOD_GET
183204 set_process (true )
184205
185206 elif error == ERR_INVALID_PARAMETER :
186- print ("Given string isn't a valid url " )
207+ push_error ("Given string isn't a valid url: " , _current_url )
187208 elif error == ERR_CANT_CONNECT :
188- print ("Can't connect to host" )
209+ push_error ("Can't connect to host" )
189210
190211
191212func _update_stats () -> void :
@@ -214,6 +235,7 @@ func _create_directory() -> void:
214235
215236func _download_next_file () -> void :
216237 if _current_url_index < file_urls .size ():
238+ set_busy (true )
217239 _current_url = file_urls [_current_url_index ]
218240 _file_name = _current_url .get_file ()
219241 download_file = save_path .path_join (_file_name )
@@ -253,7 +275,7 @@ func _on_request_completed(p_result,
253275 emit_signal ("file_downloaded" , _file_name )
254276 _download_next_file ()
255277 else :
256- print ("HTTP Request error: " , p_result )
278+ push_error ("HTTP Request error: " , p_result )
257279
258280
259281func _on_file_downloaded () -> void :
@@ -266,3 +288,4 @@ func _on_file_downloaded() -> void:
266288
267289 else :
268290 _downloads_done ()
291+
0 commit comments