This commit is contained in:
JeremyStar™ 2024-03-28 13:06:06 +01:00
parent 16cdb148ea
commit a8080668ba
3 changed files with 56 additions and 9 deletions

View file

@ -28,7 +28,7 @@ const version_release: int = 1
## The release type
const version_type: CoreTypes.VersionType = CoreTypes.VersionType.BETA
## The release type number. Resets on every new release and release type.
const version_typerelease: int = 3
const version_typerelease: int = 4
# Modules
## Use this to access CORE's logging implementation.

View file

@ -30,22 +30,30 @@ func generate_id() -> int:
logger.diagf("Edl", "Generated new download id " + str(id))
return id
func awaited_request(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Dictionary:
func awaited_request(url: String, parse_utf8: bool, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Dictionary:
logger.verbf("Edl", "Creating awaited request")
var id: int = create_download(url, method, headers, data)
start_download(id)
start_download(id, parse_utf8)
logger.diagf("Edl", "Waiting for request " + str(id) + " to finish")
while !is_download_complete(id): await get_tree().create_timer(0.1, true).timeout
var dldata: Dictionary = list_complete[id]
list_complete.erase(id)
return dldata
func batch_awaited_request(urls: PackedStringArray, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Array[Dictionary]:
func oneline_awaited_request(url: String, return_utf8: bool = true, ignore_http_code: bool = false, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Variant:
var dldata: Dictionary = await awaited_request(url, return_utf8, method, headers, data)
if dldata["result"] != Error.OK: return null
elif !ignore_http_code and dldata["http_code"] != 200: return null
else:
if return_utf8: return dldata["body_utf8"]
else: return dldata["body"]
func batch_awaited_request(urls: PackedStringArray, parse_utf8: bool, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Array[Dictionary]:
logger.verbf("Edl", "Creating " + str(urls.size()) + " awaited request(s)")
var dldata: Array[Dictionary]= []
for url in urls:
var id: int = create_download(url, method, headers, data)
start_download(id)
start_download(id, parse_utf8)
logger.diagf("Edl", "Waiting for request " + str(id) + " to finish")
while !is_download_complete(id): await get_tree().create_timer(0.1, true).timeout
dldata.append(list_complete[id])
@ -53,12 +61,12 @@ func batch_awaited_request(urls: PackedStringArray, method: HTTPClient.Method =
return dldata
func create_download(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), body: String = "") -> int:
logger.verbf("Edl", "Creating new request\n-> URL: " + url + "\n-> Method: " + str(method) + "\nHeaders:\n" + str(headers) + "\nBody:\n" + body)
logger.verbf("Edl", "Creating new request\n-> URL: " + url + "\n-> Method: " + str(method) + "\nHeaders: " + str(headers.size()) + "\nBody size: " + str(body.length()) + " Characters")
var id = generate_id()
list_queue.merge({ id: { "url": url, "method": method, "headers": headers, "body": body } })
return id
func start_download(id) -> void:
func start_download(id: int, parse_utf8: bool) -> void:
logger.verbf("Edl", "Starting request " + str(id))
list_active.merge({ id: list_queue[id] })
list_queue.erase(id)
@ -68,8 +76,8 @@ func start_download(id) -> void:
download.accept_gzip = true
download.use_threads = true
var lambda: Callable = func(result: int, http_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
logger.verbf("Edl", "Request " + str(id) + " completed\nResult: " + str(result) + "\nHTTP response code: " + str(http_code) + "\nHeaders:\n" + str(headers) + "\nBody size: " + str(body.size()) + " Bytes // " + str(core.misc.byte2mib(body.size(), true)) + " MiB")
list_complete.merge({ id: { "result": result, "http_code": http_code, "headers": headers, "body": body, "body_utf8": body.get_string_from_utf8() } })
logger.verbf("Edl", "Request " + str(id) + " completed\nResult: " + str(result) + "\nHTTP response code: " + str(http_code) + "\nHeaders: " + str(headers.size()) + "\nBody size: " + str(body.size()) + " Bytes // " + str(core.misc.byte2mib(body.size(), true)) + " MiB")
list_complete.merge({ id: { "result": result, "http_code": http_code, "headers": headers, "body": body, "body_utf8": body.get_string_from_utf8() if !parse_utf8 else "" } })
list_active.erase(id)
download.connect("request_completed", lambda)
add_child(download)

View file

@ -43,3 +43,42 @@ func mib2gib(mib: float, flatten: bool = true) -> float:
func gib2mib(gib: float, flatten: bool = true) -> float:
if flatten: return int(gib*1024)
return gib*1024
func format_stringarray(array: Array[String], item_before: String = "", item_after: String = "", separator_list: String = ", ", separator_final: String = " & ") -> String:
var output: String = ""
if array.size() == 0:
logger.warnf("Misc", "Unable to format a string with a size of 0")
return ""
elif array.size() == 1:
logger.warnf("Misc", "Unable to format a string with a size of 1")
return array[0]
for item in array:
if output == "": output = item_before + item + item_after
else: output = output.replace("If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!", separator_list) + "If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!" + item_before + item + item_after
output = output.replace("If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!", separator_final)
return output
func array_to_stringarray(array: Array) -> Array[String]:
var output: Array[String] = []
for item in array:
if typeof(item) != TYPE_STRING:
logger.error("Cannot convert Array to Array[String]: Item '" + str(item) + "' is not of type String")
return []
output.append(item)
return output
func stringarray_to_array(array: Array[String]) -> Array:
var output: Array = []
for item in array:
output.append(item)
return output
func get_center(parent_size: Vector2, child_size: Vector2) -> Vector2:
return Vector2(parent_size.x/2-child_size.x/2, parent_size.y/2-child_size.y/2)