Rename modules

This commit is contained in:
JeremyStar™ 2024-03-21 15:47:39 +01:00
parent 7d905feffe
commit a46687590a
12 changed files with 92 additions and 92 deletions

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://ckhfu6nkw6c6g"]
[ext_resource type="Script" path="res://Test.gd" id="1_87mte"]
[ext_resource type="Script" path="res://Test.gd" id="1_wpahf"]
[node name="Test" type="Control"]
layout_mode = 3
@ -9,4 +9,4 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_87mte")
script = ExtResource("1_wpahf")

View file

@ -71,7 +71,7 @@ func _ready() -> void:
inject_modules()
custom_modules_node.name = "Custom Modules"
add_child(custom_modules_node)
logger.infof("core.gd", "Initialized CORE successfully")
logger.infof("Core", "Initialized CORE successfully")
# Initialize modules
## Initializes all modules during the first initialization phase.[br]
@ -93,12 +93,12 @@ func initialize_modules() -> void:
edl.name = "EasyDownLoader"
storage.name = "Storage"
# Set scripts
logger.set_script(ResourceLoader.load(basepath + "src/logger.gd"))
misc.set_script(ResourceLoader.load(basepath + "src/misc.gd"))
sms.set_script(ResourceLoader.load(basepath + "src/sms.gd"))
logui.set_script(ResourceLoader.load(basepath + "src/logui.gd"))
edl.set_script(ResourceLoader.load(basepath + "src/edl.gd"))
storage.set_script(ResourceLoader.load(basepath + "src/storage.gd"))
logger.set_script(ResourceLoader.load(basepath + "src/Logger.gd"))
misc.set_script(ResourceLoader.load(basepath + "src/Misc.gd"))
sms.set_script(ResourceLoader.load(basepath + "src/Sms.gd"))
logui.set_script(ResourceLoader.load(basepath + "src/Logui.gd"))
edl.set_script(ResourceLoader.load(basepath + "src/Edl.gd"))
storage.set_script(ResourceLoader.load(basepath + "src/Storage.gd"))
# Set reference to self
logger.core = self
misc.core = self
@ -129,30 +129,30 @@ func inject_modules() -> void:
# Registers a custom module
## Registers a new custom module.
func register_custom_module(module_name: String, module_class: CoreBaseModule) -> bool:
logger.verbf("core.gd", "Registering new custom module \"" + module_name + "\"")
logger.verbf("Core", "Registering new custom module \"" + module_name + "\"")
if !config.custom_modules:
logger.errorf("core.gd", "Registering module failed: Custom module support is disabled.")
logger.errorf("Core", "Registering module failed: Custom module support is disabled.")
return false
if custom_modules.has(module_name):
logger.errorf("core.gd", "Registering module failed: A custom module with the name \"" + module_name + "\" already exists.")
logger.errorf("Core", "Registering module failed: A custom module with the name \"" + module_name + "\" already exists.")
return false
module_class.name = module_name
logger.diagf("core.gd", "Adding module to SceneTree")
logger.diagf("Core", "Adding module to SceneTree")
custom_modules_node.add_child(module_class)
logger.diagf("core.gd", "Merging module with custom_modules")
logger.diagf("Core", "Merging module with custom_modules")
custom_modules.merge({ module_name: module_class })
logger.diagf("core.gd", "Initializing custom module")
logger.diagf("Core", "Initializing custom module")
module_class._initialize()
logger.diagf("core.gd", "Updating custom module configuration")
logger.diagf("Core", "Updating custom module configuration")
module_class._pull_config()
return true
# Unregisters a custom module
## Unregisters a custom module, making it no longer function.
func unregister_custom_module(module_name: String) -> void:
logger.verbf("core.gd", "Unregistering custom module \"" + module_name + "\"")
logger.verbf("Core", "Unregistering custom module \"" + module_name + "\"")
if !custom_modules.has(module_name):
logger.errorf("core.gd", "Unregistering module failed: A custom module with the name \"" + module_name + "\" does not exist.")
logger.errorf("Core", "Unregistering module failed: A custom module with the name \"" + module_name + "\" does not exist.")
return
custom_modules_node.remove_child(get_custom_module(module_name))
custom_modules.erase(module_name)
@ -160,9 +160,9 @@ func unregister_custom_module(module_name: String) -> void:
# Returns a custom module
## Returns a loaded custom module for access.
func get_custom_module(module_name: String) -> CoreBaseModule:
logger.diagf("core.gd", "Getting custom module \"" + module_name + "\"")
logger.diagf("Core", "Getting custom module \"" + module_name + "\"")
if !custom_modules.has(module_name):
logger.errorf("core.gd", "Getting module failed: A custom module with the name \"" + module_name + "\" does not exist.")
logger.errorf("Core", "Getting module failed: A custom module with the name \"" + module_name + "\" does not exist.")
return
return custom_modules[module_name]
@ -170,11 +170,11 @@ func get_custom_module(module_name: String) -> CoreBaseModule:
## Loads a (new) configuration file and applies it to all modules.
func reload_configuration(new_config: CoreConfiguration = CoreConfiguration.new()) -> void:
var initialized = config != null
if initialized: logger.verbf("core.gd", "Reloading CORE's configuration")
if initialized: logger.verbf("Core", "Reloading CORE's configuration")
config = new_config
if is_devmode(): # Override configuration in development mode
config.logger_level = CoreTypes.LoggerLevel.VERB
if initialized: logger.verbf("core.gd", "Overrode configuration (development mode)")
if initialized: logger.verbf("Core", "Overrode configuration (development mode)")
if initialized: apply_configuration()
# Call _pull_config() functions
@ -182,12 +182,12 @@ func reload_configuration(new_config: CoreConfiguration = CoreConfiguration.new(
## [br]
## [b]NEVER call this yourself unless you know what you are doing![/b]
func apply_configuration() -> void:
logger.verbf("core.gd", "Applying configuration")
if is_devmode(): logger.warnf("core.gd", "The CORE Framework is in development mode. Here be dragons!")
if config.headless: logger.warnf("core.gd", "CORE is in headless mode. Certain modules will not work as expected.")
logger.verbf("Core", "Applying configuration")
if is_devmode(): logger.warnf("Core", "The CORE Framework is in development mode. Here be dragons!")
if config.headless: logger.warnf("Core", "CORE is in headless mode. Certain modules will not work as expected.")
edl._pull_config()
if !config.custom_modules:
logger.verbf("core.gd", "Removing all custom modules (custom modules support is disabled)")
logger.verbf("Core", "Removing all custom modules (custom modules support is disabled)")
for module in custom_modules: unregister_custom_module(module)
logger._pull_config()
misc._pull_config()
@ -195,7 +195,7 @@ func apply_configuration() -> void:
logui._pull_config()
if config.custom_modules:
for module in custom_modules:
logger.diagf("core.gd", "Updating configuration for custom module \"" + module.name + "\"")
logger.diagf("Core", "Updating configuration for custom module \"" + module.name + "\"")
module._pull_config()
# Return development mode status
@ -231,7 +231,7 @@ func get_formatted_string(string: String) -> String:
CoreTypes.VersionType.ALPHA:
string = string.replace("%type%", "Alpha")
string = string.replace("%type_technical%", "a")
_: await logger.crashf("core.gd", "Invalid version type " + str(version_type), true)
_: await logger.crashf("Core", "Invalid version type " + str(version_type), true)
# Development mode
if is_devmode(): string = string.replace("%devmode%", "Enabled")
else: string = string.replace("%devmode%", "Disabled")

View file

@ -27,48 +27,48 @@ var list_complete: Dictionary = {}
func generate_id() -> int:
var id = randi()
if list_queue.has(id) or list_active.has(id): return generate_id()
logger.diagf("edl.gd", "Generated new download id " + str(id))
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:
logger.verbf("edl.gd", "Creating awaited request")
logger.verbf("Edl", "Creating awaited request")
var id: int = create_download(url, method, headers, data)
start_download(id)
logger.diagf("edl.gd", "Waiting for request " + str(id) + " to finish")
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]:
logger.verbf("edl.gd", "Creating " + str(urls.size()) + " awaited request(s)")
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)
logger.diagf("edl.gd", "Waiting for request " + str(id) + " to finish")
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])
list_complete.erase(id)
return dldata
func create_download(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), body: String = "") -> int:
logger.verbf("edl.gd", "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:\n" + str(headers) + "\nBody:\n" + body)
var id = generate_id()
list_queue.merge({ id: { "url": url, "method": method, "headers": headers, "body": body } })
return id
func start_download(id) -> void:
logger.verbf("edl.gd", "Starting request " + str(id))
logger.verbf("Edl", "Starting request " + str(id))
list_active.merge({ id: list_queue[id] })
list_queue.erase(id)
logger.diagf("edl.gd", "Creating new HTTPRequest \"Request #" + str(id) + "\"")
logger.diagf("Edl", "Creating new HTTPRequest \"Request #" + str(id) + "\"")
var download: HTTPRequest = HTTPRequest.new()
download.name = "Request #" + str(id)
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.gd", "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")
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() } })
list_active.erase(id)
download.connect("request_completed", lambda)
@ -78,9 +78,9 @@ func start_download(id) -> void:
func is_download_complete(id: int) -> bool: return list_complete.has(id)
func clean_queue() -> void:
logger.verbf("edl.gd", "Cleaning request queue")
logger.verbf("Edl", "Cleaning request queue")
list_queue.clear()
func clean_completed() -> void:
logger.verbf("edl.gd", "Cleaning completed requests")
logger.verbf("Edl", "Cleaning completed requests")
list_complete.clear()

View file

@ -187,12 +187,12 @@ STACKTRACE
await core.misc.quit_safely(69)
# Makes CORE development easier
func diagf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.DIAG, core.basepath.replace("res://", "") + "src/" + origin, message)
func verbf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.VERB, core.basepath.replace("res://", "") + "src/" + origin, message)
func infof(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.INFO, core.basepath.replace("res://", "") + "src/" + origin, message)
func warnf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.WARN, core.basepath.replace("res://", "") + "src/" + origin, message)
func errorf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.ERROR, core.basepath.replace("res://", "") + "src/" + origin, message)
func crashf(origin: String, message: String) -> void: crash(core.basepath.replace("res://", "") + "src/" + origin, message)
func diagf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.DIAG, core.basepath.replace("res://", "") + "src/" + origin + ".gd", message)
func verbf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.VERB, core.basepath.replace("res://", "") + "src/" + origin + ".gd", message)
func infof(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.INFO, core.basepath.replace("res://", "") + "src/" + origin + ".gd", message)
func warnf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.WARN, core.basepath.replace("res://", "") + "src/" + origin + ".gd", message)
func errorf(origin: String, message: String) -> void: _log(CoreTypes.LoggerLevel.ERROR, core.basepath.replace("res://", "") + "src/" + origin + ".gd", message)
func crashf(origin: String, message: String) -> void: crash(core.basepath.replace("res://", "") + "src/" + origin + ".gd", message)
# Returns a logger instance
func get_instance(origin: String) -> CoreLoggerInstance: return CoreLoggerInstance.new(self, origin)

View file

@ -22,8 +22,8 @@
extends CoreBaseModule
func quit_safely(exitcode: int = 0) -> void:
logger.infof("misc.gd", "Shutting down (code " + str(exitcode) + ")")
logger.diagf("misc.gd", "Waiting for log messages to be flushed")
logger.infof("Misc", "Shutting down (code " + str(exitcode) + ")")
logger.diagf("Misc", "Waiting for log messages to be flushed")
await get_tree().create_timer(0.25).timeout
get_tree().quit(exitcode)

View file

@ -45,15 +45,15 @@ func _initialize() -> void:
func _pull_config() -> void:
if core.config.headless:
# Remove all scenes
logger.verbf("sms.gd", "Removing all scenes (triggered by headless mode)")
logger.verbf("Sms", "Removing all scenes (triggered by headless mode)")
for scene in scenes: remove_scene(scene, true)
# Add a scene to some scene collection
func add_scene(sname: String, type: CoreTypes.SceneType, sclass: Node) -> bool:
if core.config.headless: return false
logger.verbf("sms.gd", "Adding scene \"" + sname + "\" of type " + str(type))
logger.verbf("Sms", "Adding scene \"" + sname + "\" of type " + str(type))
if exists(sname) != CoreTypes.SceneType.NONE:
logger.errorf("sms.gd", "Scene with name \"" + sname + "\" already exists")
logger.errorf("Sms", "Scene with name \"" + sname + "\" already exists")
return true
sclass.name = sname
match(type):
@ -63,17 +63,17 @@ func add_scene(sname: String, type: CoreTypes.SceneType, sclass: Node) -> bool:
CoreTypes.SceneType.MAIN: scenes_main.add_child(sclass)
CoreTypes.SceneType.BACKGROUND: scenes_background.add_child(sclass)
CoreTypes.SceneType.NONE:
logger.errorf("sms.gd", "CoreTypes.SceneType.NONE is not a valid scene type")
logger.errorf("Sms", "CoreTypes.SceneType.NONE is not a valid scene type")
return false
_: await logger.crashf("sms.gd", "Invalid SceneType " + str(type), true)
_: await logger.crashf("Sms", "Invalid SceneType " + str(type), true)
scenes.merge({ sname: { "type": type, "class": sclass } })
return true
# Remove a scene from some scene collection
func remove_scene(sname: String, force_remove: bool = false) -> bool:
if core.config.headless and !force_remove: return false
if force_remove: await logger.crashf("sms.gd", "force_remove = true is not allowed", true)
logger.verbf("sms.gd", "Removing scene \"" + sname + "\"")
if force_remove: await logger.crashf("Sms", "force_remove = true is not allowed", true)
logger.verbf("Sms", "Removing scene \"" + sname + "\"")
match(exists(sname)):
CoreTypes.SceneType.DEBUG: scenes_debug.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.CUTSCENE: scenes_cutscene.remove_child(scenes[sname]["class"])
@ -81,9 +81,9 @@ func remove_scene(sname: String, force_remove: bool = false) -> bool:
CoreTypes.SceneType.MAIN: scenes_main.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.BACKGROUND: scenes_background.remove_child(scenes[sname]["class"])
CoreTypes.SceneType.NONE:
logger.errorf("sms.gd", "Scene \"" + sname + "\" does not exist")
logger.errorf("Sms", "Scene \"" + sname + "\" does not exist")
return false
_: await logger.crashf("sms.gd", "Invalid SceneType " + str(exists(sname)), true)
_: await logger.crashf("Sms", "Invalid SceneType " + str(exists(sname)), true)
scenes.erase(sname)
return true
@ -96,8 +96,8 @@ func get_scene(sname: String) -> Node:
CoreTypes.SceneType.MENU: return scenes[sname]["class"]
CoreTypes.SceneType.MAIN: return scenes[sname]["class"]
CoreTypes.SceneType.BACKGROUND: return scenes[sname]["class"]
CoreTypes.SceneType.NONE: logger.errorf("sms.gd", "Scene \"" + sname + "\" does not exist")
_: await logger.crashf("sms.gd", "Invalid SceneType " + str(exists(sname)), true)
CoreTypes.SceneType.NONE: logger.errorf("Sms", "Scene \"" + sname + "\" does not exist")
_: await logger.crashf("Sms", "Invalid SceneType " + str(exists(sname)), true)
return null
# Return a scene collection for scene manipulation
@ -109,8 +109,8 @@ func get_scene_collection(type: CoreTypes.SceneType) -> Node:
CoreTypes.SceneType.MENU: return scenes_menu
CoreTypes.SceneType.MAIN: return scenes_main
CoreTypes.SceneType.BACKGROUND: return scenes_background
CoreTypes.SceneType.NONE: logger.errorf("sms.gd", "No scene collection was found for CoreTypes.SceneType.NONE")
_: await logger.crashf("sms.gd", "Invalid SceneType " + str(type), true)
CoreTypes.SceneType.NONE: logger.errorf("Sms", "No scene collection was found for CoreTypes.SceneType.NONE")
_: await logger.crashf("Sms", "Invalid SceneType " + str(type), true)
return null
# Return scenes in some scene collection

View file

@ -22,40 +22,40 @@ var storage_location: String = ""
func open_storage(location: String, create_new: bool = true, sanity_check: bool = true, fail_on_sanity_check: bool = false) -> bool:
if is_open:
logger.errorf("storage.gd", "Failed to open storage: A storage file is already open")
logger.errorf("Storage", "Failed to open storage: A storage file is already open")
return false
logger.verbf("storage.gd", "Opening storage file at \"" + location + "\"")
logger.verbf("Storage", "Opening storage file at \"" + location + "\"")
var file: FileAccess
if !FileAccess.file_exists(location):
if create_new:
file = FileAccess.open(location, FileAccess.WRITE)
if file == null:
await logger.crashf("storage.gd", "Could not open storage file at \"" + location + "\": Failed with code " + str(FileAccess.get_open_error()))
await logger.crashf("Storage", "Could not open storage file at \"" + location + "\": Failed with code " + str(FileAccess.get_open_error()))
return false
file.store_string("{}")
file.close()
else:
logger.errorf("storage.gd", "Failed to open storage: create_new is set to false")
logger.errorf("Storage", "Failed to open storage: create_new is set to false")
return false
file = FileAccess.open(location, FileAccess.READ)
var storage_temp: Variant = file.get_as_text()
file.close()
storage_temp = JSON.parse_string(storage_temp)
if typeof(storage_temp) != TYPE_DICTIONARY:
logger.errorf("storage.gd", "Failed to open storage: Parsed storage file is of type " + str(typeof(storage_temp)))
logger.errorf("Storage", "Failed to open storage: Parsed storage file is of type " + str(typeof(storage_temp)))
return false
if sanity_check:
var check_result: Array[String] = perform_sanity_check(storage_temp)
if check_result.size() != 0:
if fail_on_sanity_check:
logger.errorf("storage.gd", "Sanity check failed (stopping):")
logger.errorf("Storage", "Sanity check failed (stopping):")
for error in check_result:
logger.errorf("storage.gd", "-> " + error)
logger.errorf("Storage", "-> " + error)
return false
else:
logger.warnf("storage.gd", "Sanity check failed (continuing anyway):")
logger.warnf("Storage", "Sanity check failed (continuing anyway):")
for error in check_result:
logger.warnf("storage.gd", "-> " + error)
logger.warnf("Storage", "-> " + error)
storage = storage_temp
storage_location = location
is_open = true
@ -63,90 +63,90 @@ func open_storage(location: String, create_new: bool = true, sanity_check: bool
func close_storage() -> bool:
if !is_open:
logger.errorf("storage.gd", "Failed to close storage: No storage file is open")
logger.errorf("Storage", "Failed to close storage: No storage file is open")
return false
logger.verbf("storage.gd", "Closing storage file")
logger.verbf("Storage", "Closing storage file")
storage = {}
is_open = false
return true
func save_storage() -> bool:
if !is_open:
logger.errorf("storage.gd", "Failed to save storage: No storage file is open")
logger.errorf("Storage", "Failed to save storage: No storage file is open")
return false
var file: FileAccess = FileAccess.open(storage_location, FileAccess.WRITE)
if file == null:
await logger.crashf("storage.gd", "Could not open storage file at \"" + storage_location + "\": Failed with code " + str(FileAccess.get_open_error()))
await logger.crashf("Storage", "Could not open storage file at \"" + storage_location + "\": Failed with code " + str(FileAccess.get_open_error()))
return false
logger.diagf("storage.gd", "Writing storage file to disk")
logger.diagf("Storage", "Writing storage file to disk")
file.store_string(JSON.stringify(storage))
file.close()
return true
func nuke_storage(autosave: bool = true) -> bool:
if !is_open:
logger.errorf("storage.gd", "Failed to nuke storage: No storage file is open")
logger.errorf("Storage", "Failed to nuke storage: No storage file is open")
return false
logger.warnf("storage.gd", "Nuking storage")
logger.warnf("Storage", "Nuking storage")
storage = {}
if autosave: save_storage()
return true
func get_key(key: String, default: Variant) -> Variant:
if !is_open:
logger.errorf("storage.gd", "Failed to get key: No storage file is open")
logger.errorf("Storage", "Failed to get key: No storage file is open")
return NAN
logger.diagf("storage.gd", "Returning storage key \"" + key + "\" (default='" + default + "')")
logger.diagf("Storage", "Returning storage key \"" + key + "\" (default='" + default + "')")
return storage.get(key, default)
func set_key(key: String, value: Variant, overwrite: bool = true, autosave: bool = true) -> bool:
if !is_open:
logger.errorf("storage.gd", "Failed to set key: No storage file is open")
logger.errorf("Storage", "Failed to set key: No storage file is open")
return false
logger.diagf("storage.gd", "Updating storage key \"" + key + "\" with value '" + str(value) + "' (overwrite='" + str(overwrite) + "' autosave='" + str(autosave) + "'")
logger.diagf("Storage", "Updating storage key \"" + key + "\" with value '" + str(value) + "' (overwrite='" + str(overwrite) + "' autosave='" + str(autosave) + "'")
storage.merge({key: value}, overwrite)
if autosave: save_storage()
return true
func del_key(key: String, autosave: bool = true) -> bool:
if !is_open:
logger.errof("storage.gd", "Failed to delete key: No storage file is open")
logger.errof("Storage", "Failed to delete key: No storage file is open")
return false
logger.diagf("storage.gd", "Deleting storage key \"" + key + "\" (autosave='" + str(autosave) + "')")
logger.diagf("Storage", "Deleting storage key \"" + key + "\" (autosave='" + str(autosave) + "')")
storage.erase(key)
if autosave: save_storage()
return true
func get_dict() -> Dictionary:
if !is_open:
logger.errorf("storage.gd", "Failed to get dictionary: No storage file is open")
logger.errorf("Storage", "Failed to get dictionary: No storage file is open")
return {}
logger.verbf("storage.gd", "Returning storage dictionary")
logger.verbf("Storage", "Returning storage dictionary")
return storage
func save_dict(dict: Dictionary, sanity_check: bool = true, fail_on_sanity_check: bool = false, autosave: bool = true) -> bool:
if !is_open:
logger.errorf("storage.gd", "Failed to save dictionary: No storage file is open")
logger.errorf("Storage", "Failed to save dictionary: No storage file is open")
return false
logger.verbf("storage.gd", "Saving custom dictionary as storage")
logger.verbf("Storage", "Saving custom dictionary as storage")
if sanity_check:
var check_result: Array[String] = perform_sanity_check(dict)
if check_result.size() != 0:
if fail_on_sanity_check:
logger.errorf("storage.gd", "Sanity check failed (stopping):")
logger.errorf("Storage", "Sanity check failed (stopping):")
for error in check_result:
logger.errorf("storage.gd", "-> " + error)
logger.errorf("Storage", "-> " + error)
return false
else:
logger.warnf("storage.gd", "Sanity check failed (continuing anyway):")
logger.warnf("Storage", "Sanity check failed (continuing anyway):")
for error in check_result:
logger.warnf("storage.gd", "-> " + error)
logger.warnf("Storage", "-> " + error)
storage = dict
if autosave: save_storage()
return true
func perform_sanity_check(storage_check: Dictionary) -> Array[String]:
logger.verbf("storage.gd", "Performing a sanity check on some storage dictionary")
logger.verbf("Storage", "Performing a sanity check on some storage dictionary")
var errors: Array[String] = []
for key in storage_check:
if typeof(key) != TYPE_STRING:
@ -155,5 +155,5 @@ func perform_sanity_check(storage_check: Dictionary) -> Array[String]:
if typeof(storage_check[key]) != TYPE_NIL and typeof(storage_check[key]) != TYPE_STRING and typeof(storage_check[key]) != TYPE_INT and typeof(storage_check[key]) != TYPE_FLOAT and typeof(storage_check[key]) != TYPE_BOOL and typeof(storage_check[key]) != TYPE_ARRAY and typeof(storage_check[key]) != TYPE_DICTIONARY:
errors.append("The value of \"" + key + "\" is not null, a string, an integer, a float, boolean, array or dictionary (type '" + type_string(typeof(key)) + "')")
logger.verbf("storage.gd", "Completed sanity check with " + str(errors.size()) + " errors")
logger.verbf("Storage", "Completed sanity check with " + str(errors.size()) + " errors")
return errors