2026-01-21 08:14:35 +08:00
|
|
|
extends Node
|
|
|
|
|
|
2026-02-14 12:47:52 +08:00
|
|
|
var uuid: String
|
2026-02-08 21:18:57 +08:00
|
|
|
|
2026-01-21 14:53:30 +08:00
|
|
|
func create_file(file_path: String) -> void:
|
|
|
|
|
var base_dir = file_path.get_base_dir()
|
|
|
|
|
if !DirAccess.dir_exists_absolute(base_dir):
|
|
|
|
|
DirAccess.make_dir_recursive_absolute(base_dir)
|
2026-01-21 17:03:59 +08:00
|
|
|
var file = FileAccess.open(file_path, FileAccess.WRITE)
|
|
|
|
|
file.close()
|
2026-01-21 14:53:30 +08:00
|
|
|
|
2026-01-21 11:42:57 +08:00
|
|
|
func download_file(server_path: String, local_path: String) -> void:
|
2026-01-21 14:53:30 +08:00
|
|
|
var http = HTTPRequest.new()
|
|
|
|
|
add_child(http)
|
2026-01-21 17:03:59 +08:00
|
|
|
var file_path = local_path
|
2026-01-21 14:53:30 +08:00
|
|
|
if !FileAccess.file_exists(file_path):
|
|
|
|
|
create_file(file_path)
|
2026-01-21 17:03:59 +08:00
|
|
|
http.download_file = file_path
|
2026-01-21 14:53:30 +08:00
|
|
|
http.request(server_path)
|
2026-02-13 09:46:27 +08:00
|
|
|
await http.request_completed
|
2026-01-21 08:14:35 +08:00
|
|
|
|
2026-02-08 21:18:57 +08:00
|
|
|
func get_uuid() -> void:
|
2026-02-14 12:47:52 +08:00
|
|
|
var index_file = FileAccess.open("user://download/sources/temp/index.json", FileAccess.READ)
|
2026-02-08 21:18:57 +08:00
|
|
|
var index_text = index_file.get_as_text()
|
|
|
|
|
var content = JSON.parse_string(index_text)
|
|
|
|
|
index_file.close()
|
|
|
|
|
uuid = content["uuid"]
|
|
|
|
|
|
2026-01-21 17:03:59 +08:00
|
|
|
func download_defs(type: String, origin: String) -> void:
|
2026-02-14 12:47:52 +08:00
|
|
|
var list_file = FileAccess.open("user://download/sources/%s/%ss/list.json" % [uuid, type], FileAccess.READ)
|
2026-01-21 17:03:59 +08:00
|
|
|
var list_text = list_file.get_as_text()
|
|
|
|
|
var list = JSON.parse_string(list_text)
|
2026-01-21 17:19:15 +08:00
|
|
|
list_file.close()
|
2026-01-21 17:03:59 +08:00
|
|
|
if !list:
|
|
|
|
|
return
|
|
|
|
|
for k in list:
|
|
|
|
|
var filename = list[k]
|
2026-02-14 12:47:52 +08:00
|
|
|
await download_file("%s%s/id/%s" % [origin, type, k], "user://download/sources/%s/%ss/%s.json" % [uuid, type, filename])
|
2026-01-21 17:03:59 +08:00
|
|
|
|
|
|
|
|
func download_assets(origin: String) -> void:
|
2026-02-14 12:47:52 +08:00
|
|
|
var list_file = FileAccess.open("user://download/sources/%s/assets/list.json" % [uuid], FileAccess.READ)
|
2026-01-21 17:03:59 +08:00
|
|
|
var list_text = list_file.get_as_text()
|
|
|
|
|
var list = JSON.parse_string(list_text)
|
2026-01-21 17:19:15 +08:00
|
|
|
list_file.close()
|
2026-01-21 17:03:59 +08:00
|
|
|
if !list:
|
|
|
|
|
return
|
|
|
|
|
for k in list["pics"]:
|
|
|
|
|
var filename = list["pics"][k]
|
2026-02-14 12:47:52 +08:00
|
|
|
await download_file("%sasset/pic/%s" % [origin, k], "user://download/sources/%s/assets/pics/%s" % [uuid, filename])
|
2026-01-21 17:03:59 +08:00
|
|
|
for k in list["sounds"]:
|
|
|
|
|
var filename = list["sounds"][k]
|
2026-02-14 12:47:52 +08:00
|
|
|
await download_file("%sasset/sound/%s" % [origin, k], "user://download/sources/%s/assets/sounds/%s" % [uuid, filename])
|
2026-01-21 17:03:59 +08:00
|
|
|
|
2026-01-21 11:42:57 +08:00
|
|
|
func download_from_origin() -> int:
|
2026-01-21 14:53:30 +08:00
|
|
|
var origin = GameManager.data_origin
|
|
|
|
|
var http = HTTPRequest.new()
|
|
|
|
|
add_child(http)
|
|
|
|
|
|
|
|
|
|
if origin.substr(0, 4) != "http":
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
if http.request(origin) != OK:
|
|
|
|
|
return 2
|
2026-01-21 08:14:35 +08:00
|
|
|
|
2026-01-21 14:53:30 +08:00
|
|
|
if origin[-1] != "/":
|
|
|
|
|
origin = origin + "/"
|
2026-02-08 21:18:57 +08:00
|
|
|
|
2026-02-14 12:47:52 +08:00
|
|
|
await download_file(origin + "index", "user://download/sources/temp/index.json")
|
2026-01-21 11:50:43 +08:00
|
|
|
|
2026-02-13 09:48:38 +08:00
|
|
|
get_uuid()
|
|
|
|
|
|
2026-02-14 12:47:52 +08:00
|
|
|
DirAccess.remove_absolute("user://download/sources/temp/index.json")
|
|
|
|
|
DirAccess.remove_absolute("user://download/sources/temp/")
|
2026-02-13 20:04:49 +08:00
|
|
|
|
2026-02-14 12:47:52 +08:00
|
|
|
await download_file(origin + "index", "user://download/sources/%s/index.json" % [uuid])
|
|
|
|
|
await download_file(origin + "card/list", "user://download/sources/%s/cards/list.json" % [uuid])
|
|
|
|
|
await download_file(origin + "reaction/list", "user://download/sources/%s/reactions/list.json" % [uuid])
|
|
|
|
|
await download_file(origin + "matter/list", "user://download/sources/%s/matters/list.json" % [uuid])
|
|
|
|
|
await download_file(origin + "asset/list", "user://download/sources/%s/assets/list.json" % [uuid])
|
2026-01-21 17:03:59 +08:00
|
|
|
|
2026-02-13 09:46:27 +08:00
|
|
|
await download_defs("card", origin)
|
|
|
|
|
await download_defs("reaction", origin)
|
|
|
|
|
await download_defs("matter", origin)
|
2026-01-21 17:03:59 +08:00
|
|
|
|
2026-02-13 09:46:27 +08:00
|
|
|
await download_assets(origin)
|
2026-01-21 08:14:35 +08:00
|
|
|
|
2026-02-13 20:04:49 +08:00
|
|
|
get_sources()
|
|
|
|
|
|
2026-01-21 14:53:30 +08:00
|
|
|
return 0
|
2026-01-21 11:42:57 +08:00
|
|
|
|
|
|
|
|
func load_resource():
|
2026-02-14 12:47:52 +08:00
|
|
|
var card_file = FileAccess.open("user://download/sources/%s/cards/list.json" % [uuid], FileAccess.READ)
|
2026-01-21 17:19:15 +08:00
|
|
|
GameManager.card_list = JSON.parse_string(card_file.get_as_text())
|
|
|
|
|
card_file.close()
|
|
|
|
|
|
2026-02-14 12:47:52 +08:00
|
|
|
var reaction_file = FileAccess.open("user://download/sources/%s/reactions/list.json" % [uuid], FileAccess.READ)
|
2026-01-21 17:19:15 +08:00
|
|
|
GameManager.reaction_list = JSON.parse_string(reaction_file.get_as_text())
|
|
|
|
|
reaction_file.close()
|
|
|
|
|
|
2026-02-14 12:47:52 +08:00
|
|
|
var matter_file = FileAccess.open("user://download/sources/%s/matters/list.json" % [uuid], FileAccess.READ)
|
2026-01-21 17:19:15 +08:00
|
|
|
GameManager.matter_list = JSON.parse_string(matter_file.get_as_text())
|
|
|
|
|
matter_file.close()
|
|
|
|
|
|
2026-02-14 12:47:52 +08:00
|
|
|
var asset_file = FileAccess.open("user://download/sources/%s/assets/list.json" % [uuid], FileAccess.READ)
|
2026-01-21 17:19:15 +08:00
|
|
|
var asset_list = JSON.parse_string(asset_file.get_as_text())
|
|
|
|
|
GameManager.pic_list = asset_list["pics"]
|
|
|
|
|
GameManager.sound_list = asset_list["sounds"]
|
|
|
|
|
asset_file.close()
|
2026-02-13 20:04:49 +08:00
|
|
|
|
|
|
|
|
func get_sources():
|
2026-02-14 12:47:52 +08:00
|
|
|
var dir = DirAccess.open("user://download/sources/")
|
2026-02-13 20:04:49 +08:00
|
|
|
var subdirs: PackedStringArray = dir.get_directories()
|
|
|
|
|
for subdir in subdirs:
|
|
|
|
|
if subdir == "temp":
|
|
|
|
|
continue
|
2026-02-14 12:47:52 +08:00
|
|
|
var file = FileAccess.open("user://download/sources/%s/index.json" % [subdir], FileAccess.READ)
|
2026-02-13 20:04:49 +08:00
|
|
|
var text = file.get_as_text()
|
|
|
|
|
var content = JSON.parse_string(text)
|
|
|
|
|
file.close()
|
|
|
|
|
var source_name = content["name"]
|
|
|
|
|
var source_uuid = content["uuid"]
|
|
|
|
|
GameManager.sources[source_name] = source_uuid
|