add license, move folders

This commit is contained in:
Nordup 2024-05-04 00:14:24 +04:00
parent 185cc74060
commit 271c4a46a1
132 changed files with 21 additions and 0 deletions

View file

@ -0,0 +1,46 @@
extends Node
class_name ConfigBase
var config: ConfigFile
var config_path: String
func _init(path: String) -> void:
config = ConfigFile.new()
config.load(path)
config_path = path
func get_string(section: String, key: String) -> String:
var value: String
if config.has_section_key(section, key):
value = config.get_value(section, key)
# Debug.logr(key + "=" + value)
else: Debug.logclr("don't have section " + section + ", key " + key, Color.YELLOW)
return value
func get_value(section: String, key: String):
var value
if config.has_section_key(section, key):
value = config.get_value(section, key)
# Debug.logr(key + "=" + str(value))
else: Debug.logclr("don't have section " + section + ", key " + key, Color.YELLOW)
return value
func get_sections() -> PackedStringArray:
return config.get_sections()
func get_section_keys(section: String) -> PackedStringArray:
var keys: PackedStringArray
if config.has_section(section):
keys = config.get_section_keys(section)
# Debug.logr(keys)
else: Debug.logclr("don't have section " + section, Color.YELLOW)
return keys
func set_value(section: String, key: String, value: Variant) -> void:
config.set_value(section, key, value)

View file

@ -0,0 +1,29 @@
extends ConfigBase
class_name ConfigGate
var title: String
var description: String
var image_url: String
var resource_pack_url: String
var libraries: PackedStringArray
const section = "gate"
const libs_section = "libraries"
func _init(path: String, base_url: String) -> void:
super._init(path)
title = get_string(section, "title")
description = get_string(section, "description")
image_url = Url.join(base_url, get_string(section, "image"))
resource_pack_url = Url.join(base_url, get_string(section, "resource_pack"))
libraries = get_libraries(base_url)
func get_libraries(base_url: String) -> PackedStringArray:
var unsplit_libs = GDExtension.find_extension_library("", config)
if unsplit_libs.is_empty(): return []
var libs = unsplit_libs.split(";")
for i in range(libs.size()): libs[i] = Url.join(base_url, libs[i])
return libs

View file

@ -0,0 +1,115 @@
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 download_requests: Array[DownloadRequest]
func _ready() -> void:
FileTools.remove_recursive(DOWNLOAD_FOLDER)
func is_cached(url: String) -> bool:
var save_path = DOWNLOAD_FOLDER + "/" + url.md5_text() + "." + url.get_file().get_extension()
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 FileAccess.file_exists(save_path):
await get_tree().process_frame
return save_path
DirAccess.make_dir_recursive_absolute(save_path.get_base_dir())
var result = await create_request(url, save_path, timeout)
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, timeout: float = 0) -> int:
var http = HTTPRequest.new()
http.download_file = save_path
http.use_threads = true
http.timeout = timeout
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:
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:
FileDownloader.stop_all()
FileTools.remove_recursive(DOWNLOAD_FOLDER)

View file

@ -0,0 +1,47 @@
extends Node
class_name FileTools
static func remove_recursive(path: String) -> void:
if not DirAccess.dir_exists_absolute(path) and not FileAccess.file_exists(path): return
var dir = DirAccess.open(path)
if dir:
# List directory content
var err : Error
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
remove_recursive(path + "/" + file_name)
else:
err = dir.remove(file_name)
if err != OK: Debug.logerr("Error removing: " + path + "/" + file_name)
file_name = dir.get_next()
# Remove current path
err = dir.remove(path)
if err != OK: Debug.logerr("Error removing: " + path)
else:
Debug.logerr("Error removing " + path)
static func load_external_tex(path: String) -> Texture2D:
if path.begins_with("res://"): return load(path)
if not FileAccess.file_exists(path): return null
var file = FileAccess.open(path, FileAccess.READ)
var bytes = file.get_buffer(file.get_length())
var image = Image.new()
match path.get_extension():
"png":
image.load_png_from_buffer(bytes)
["jpeg", "jpg"]:
image.load_jpg_from_buffer(bytes)
"webp":
image.load_webp_from_buffer(bytes)
"bmp":
image.load_bmp_from_buffer(bytes)
_:
return null
return ImageTexture.create_from_image(image) as Texture2D

View file

@ -0,0 +1,49 @@
extends Node
@export var gate_events: GateEvents
@export var connect_timeout: float
var c_gate: ConfigGate
func _ready() -> void:
FileDownloader.progress.connect(on_progress)
load_gate(gate_events.current_gate_url)
func load_gate(config_url: String) -> void:
Debug.logclr("======== " + config_url + " ========", Color.GREEN)
var config_path = await FileDownloader.download(config_url, connect_timeout)
if config_path.is_empty(): return error(GateEvents.GateError.NOT_FOUND)
c_gate = ConfigGate.new(config_path, config_url)
gate_events.gate_config_loaded_emit(config_url, c_gate)
var image_path = await FileDownloader.download(c_gate.image_url)
var gate = Gate.create(config_url, c_gate.title, c_gate.description, image_path, "", "")
var is_cached = FileDownloader.is_cached(c_gate.resource_pack_url)
gate_events.gate_info_loaded_emit(gate, is_cached)
gate.resource_pack = await FileDownloader.download(c_gate.resource_pack_url)
if gate.resource_pack.is_empty(): return error(GateEvents.GateError.MISSING_PACK)
Debug.logclr("Downloading GDExtension libraries: " + str(c_gate.libraries), Color.DIM_GRAY)
for lib in c_gate.libraries:
gate.shared_libs_dir = await FileDownloader.download_shared_lib(lib, config_url)
if gate.shared_libs_dir.is_empty(): return error(GateEvents.GateError.MISSING_LIBS)
gate_events.gate_loaded_emit(gate)
func error(code: GateEvents.GateError) -> void:
Debug.logclr("GateError: " + GateEvents.GateError.keys()[code], Color.MAROON)
gate_events.gate_error_emit(code)
func on_progress(url: String, body_size: int, downloaded_bytes: int) -> void:
gate_events.download_progress_emit(url, body_size, downloaded_bytes)
func _exit_tree() -> void:
FileDownloader.progress.disconnect(on_progress)
FileDownloader.stop_all()