Update erm module

Added blocking functionality for unsecure requests and modified some function names.
This commit is contained in:
JeremyStar™ 2024-04-08 16:42:03 +02:00
parent 0ac55d4f1b
commit 330bb98dd9
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 32 additions and 11 deletions

View file

@ -46,3 +46,7 @@ Determines if [`LogUI`](/terminology#logui) should be visible or not.
The color the `LogUI` background will have. Set to `Color.TRANSPARENT` for a transparent background.
### *int* <u>logui_font_size</u> = *14*
What size the graphical log should have.
## Easy Request Maker
### *CoreTypes.BlockadeLevel* <u>erm_unsecure_requests</u> = *CoreTypes.BlockadeLevel.BLOCK*
Determines how unsecure requests should be handled.

View file

@ -13,3 +13,5 @@ Available version types, following the StarOpenSource Versioning Specification (
Available log levels, followingthe StarOpenSource Logging Specification (SOSLS) version 1.
### <u>SceneType</u>{ NONE, DEBUG, CUTSCENE, MENU, MAIN, BACKGROUND }
Available scene types.
### <u>BlockadeLevel</u>{ IGNORE, WARN, BLOCK }
To what degree *something* should be blocked.

View file

@ -43,6 +43,9 @@ class_name CoreConfiguration
@export var logui_background_color: Color
## What size the graphical log should have.
@export var logui_font_size: int
@export_category("Easy Request Maker")
## Determines how unsecure requests should be handled.
@export var erm_unsecure_requests: CoreTypes.BlockadeLevel
# Populates configuration with default values
func _init() -> void:
@ -61,3 +64,5 @@ func _init() -> void:
logui_enabled = true
logui_background_color = Color.BLACK # To disable the background, use Color.TRANSPARENT
logui_font_size = 14
# Easy Request Maker
erm_unsecure_requests = CoreTypes.BlockadeLevel.BLOCK

View file

@ -25,3 +25,5 @@ enum VersionType { RELEASE, RELEASECANDIDATE, BETA, ALPHA }
enum LoggerLevel { NONE, ERROR, WARN, INFO, VERB, DIAG }
## Available scene types.
enum SceneType { NONE, DEBUG, CUTSCENE, MENU, MAIN, BACKGROUND }
## To what degree [i]something[/i] should be blocked.
enum BlockadeLevel { IGNORE, WARN, BLOCK }

View file

@ -22,6 +22,7 @@ var list_queue: Dictionary = {}
var list_active: Dictionary = {}
var list_complete: Dictionary = {}
var config_unsecure_requests: CoreTypes.BlockadeLevel
func _cleanup() -> void:
clean_queue()
clean_completed()
@ -39,16 +40,18 @@ func generate_id() -> int:
func awaited_request(url: String, parse_utf8: bool, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Dictionary:
logger.verbf("erm", "Creating awaited request")
var id: int = create_download(url, method, headers, data)
start_download(id, parse_utf8)
if !await is_url_allowed(url): return {}
var id: int = create_request(url, method, headers, data)
start_request(id, parse_utf8)
logger.diagf("erm", "Waiting for request " + str(id) + " to finish")
while !is_download_complete(id): await get_tree().create_timer(0.1, true).timeout
while !is_request_completed(id): await get_tree().create_timer(0.1, true).timeout
var dldata: Dictionary = list_complete[id]
list_complete.erase(id)
return dldata
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 == {}: return null
if dldata["result"] != Error.OK: return null
elif !ignore_http_code and dldata["http_code"] != 200: return null
else:
@ -59,21 +62,26 @@ func batch_awaited_request(urls: PackedStringArray, parse_utf8: bool, method: HT
logger.verbf("erm", "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, parse_utf8)
logger.diagf("erm", "Waiting for request " + str(id) + " to finish")
while !is_download_complete(id): await get_tree().create_timer(0.1, true).timeout
if !await is_url_allowed(url): continue
var thread: Thread = Thread.new()
thread.start(Callable(self, "_batch_awaited_request").bind(url, parse_utf8, method, headers, data))
var id: int = thread.wait_to_finish()
dldata.append(list_complete[id])
list_complete.erase(id)
return dldata
func create_download(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), body: String = "") -> int:
func _batch_awaited_request(url: String, parse_utf8: bool, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> int:
var id: int = create_request(url, method, headers, data)
start_request(id, parse_utf8)
logger.diagf("erm", "Waiting for request " + str(id) + " to finish")
while !is_request_completed(id): await get_tree().create_timer(0.1, true).timeout
return id
logger.verbf("erm", "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: int, parse_utf8: bool) -> void:
func start_request(id: int, parse_utf8: bool) -> void:
logger.verbf("erm", "Starting request " + str(id))
list_active.merge({ id: list_queue[id] })
list_queue.erase(id)
@ -92,7 +100,7 @@ func start_download(id: int, parse_utf8: bool) -> void:
add_child(download)
download.request(list_active[id]["url"], list_active[id]["headers"], list_active[id]["method"], list_active[id]["body"])
func is_download_complete(id: int) -> bool: return list_complete.has(id)
func is_request_completed(id: int) -> bool: return list_complete.has(id)
func clean_queue() -> void:
logger.verbf("erm", "Cleaning request queue")