feat: 实现下载时提示和下载单个文件功能
This commit is contained in:
+14
-8
@@ -76,19 +76,19 @@ theme_override_fonts/font = ExtResource("2_6wm04")
|
|||||||
theme_override_font_sizes/font_size = 24
|
theme_override_font_sizes/font_size = 24
|
||||||
|
|
||||||
[node name="SaveButton" type="Button" parent="."]
|
[node name="SaveButton" type="Button" parent="."]
|
||||||
offset_left = 89.0
|
offset_left = 93.0
|
||||||
offset_top = 902.0
|
offset_top = 913.0
|
||||||
offset_right = 253.0
|
offset_right = 257.0
|
||||||
offset_bottom = 979.0
|
offset_bottom = 990.0
|
||||||
theme_override_fonts/font = ExtResource("2_6wm04")
|
theme_override_fonts/font = ExtResource("2_6wm04")
|
||||||
theme_override_font_sizes/font_size = 30
|
theme_override_font_sizes/font_size = 30
|
||||||
text = "保存设置"
|
text = "保存设置"
|
||||||
|
|
||||||
[node name="DownloadButton" type="Button" parent="."]
|
[node name="DownloadButton" type="Button" parent="."]
|
||||||
offset_left = 495.0
|
offset_left = 493.0
|
||||||
offset_top = 912.0
|
offset_top = 905.0
|
||||||
offset_right = 671.0
|
offset_right = 669.0
|
||||||
offset_bottom = 1001.0
|
offset_bottom = 994.0
|
||||||
theme_override_fonts/font = ExtResource("2_6wm04")
|
theme_override_fonts/font = ExtResource("2_6wm04")
|
||||||
theme_override_font_sizes/font_size = 30
|
theme_override_font_sizes/font_size = 30
|
||||||
text = "下载文件"
|
text = "下载文件"
|
||||||
@@ -102,6 +102,12 @@ theme_override_fonts/font = ExtResource("2_6wm04")
|
|||||||
theme_override_font_sizes/font_size = 30
|
theme_override_font_sizes/font_size = 30
|
||||||
text = "取消"
|
text = "取消"
|
||||||
|
|
||||||
|
[node name="Tips" type="Label" parent="."]
|
||||||
|
offset_left = 711.0
|
||||||
|
offset_top = 913.0
|
||||||
|
offset_right = 1738.0
|
||||||
|
offset_bottom = 966.0
|
||||||
|
|
||||||
[connection signal="pressed" from="SaveButton" to="." method="_on_save_button_pressed"]
|
[connection signal="pressed" from="SaveButton" to="." method="_on_save_button_pressed"]
|
||||||
[connection signal="pressed" from="DownloadButton" to="." method="_on_download_button_pressed"]
|
[connection signal="pressed" from="DownloadButton" to="." method="_on_download_button_pressed"]
|
||||||
[connection signal="pressed" from="CancelButton" to="." method="_on_cancel_button_pressed"]
|
[connection signal="pressed" from="CancelButton" to="." method="_on_cancel_button_pressed"]
|
||||||
|
|||||||
@@ -1,53 +1,19 @@
|
|||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
@onready var http_request = $HTTPRequest
|
func download_file(server_path: String, local_path: String) -> void:
|
||||||
|
var http = HTTPRequest.new()
|
||||||
|
http.download_file = local_path
|
||||||
|
http.request(server_path)
|
||||||
|
|
||||||
# 下载文件
|
func download_from_origin() -> int:
|
||||||
func download_file(url: String, filename: String):
|
var origin = GameManager.data_origin
|
||||||
# 构建保存路径
|
|
||||||
var save_path = "user://" + filename
|
|
||||||
print("开始下载: ", url)
|
|
||||||
print("保存到: ", save_path)
|
|
||||||
|
|
||||||
# 发送请求
|
if origin.substr(0, 4) != "http":
|
||||||
var error = http_request.request(url)
|
return 1
|
||||||
if error != OK:
|
|
||||||
push_error("请求发送失败: " + str(error))
|
|
||||||
return false
|
|
||||||
|
|
||||||
# 连接信号(一次性)
|
|
||||||
if http_request.is_connected("request_completed", _on_request_completed):
|
|
||||||
http_request.request_completed.disconnect(_on_request_completed)
|
|
||||||
|
|
||||||
http_request.request_completed.connect(_on_request_completed.bind(save_path), CONNECT_ONE_SHOT)
|
|
||||||
|
|
||||||
return true
|
|
||||||
|
|
||||||
# 请求完成回调
|
|
||||||
func _on_request_completed(result, response_code, headers, body, save_path):
|
|
||||||
if result != HTTPRequest.RESULT_SUCCESS:
|
|
||||||
push_error("下载失败,错误代码: " + str(result))
|
|
||||||
return
|
|
||||||
|
|
||||||
if response_code != 200:
|
|
||||||
push_error("HTTP错误: " + str(response_code))
|
|
||||||
return
|
|
||||||
|
|
||||||
# 保存文件
|
|
||||||
var file = FileAccess.open(save_path, FileAccess.WRITE)
|
|
||||||
if file == null:
|
|
||||||
push_error("无法创建文件: " + save_path)
|
|
||||||
push_error("错误: " + str(FileAccess.get_open_error()))
|
|
||||||
return
|
|
||||||
|
|
||||||
file.store_buffer(body)
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
print("文件保存成功: " + save_path)
|
|
||||||
print("文件大小: " + str(body.size()) + " 字节")
|
|
||||||
|
|
||||||
func download_from_origin() -> void:
|
|
||||||
if GameManager.data_origin.substr(0, 4) != "http":
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
func load_resource():
|
||||||
|
pass
|
||||||
|
|||||||
@@ -17,4 +17,11 @@ func _on_cancel_button_pressed() -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _on_download_button_pressed() -> void:
|
func _on_download_button_pressed() -> void:
|
||||||
pass # Replace with function body.
|
$Tips.text = "提示:已开始下载,请勿关闭设置页面"
|
||||||
|
var result: int = DownloadManager.download_from_origin()
|
||||||
|
if result == 1:
|
||||||
|
$Tips.text = "提示:下载失败。数据源路径错误"
|
||||||
|
$Tips.text = "提示:正在加载资源,请勿关闭设置页面"
|
||||||
|
DownloadManager.load_resource()
|
||||||
|
$Tips.text = "提示:完成加载"
|
||||||
|
|
||||||
Reference in New Issue
Block a user