From 538ba01aec31274e80968f7764caac08f8e0742f Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 5 Feb 2024 18:03:16 +0100 Subject: [PATCH] Add support for custom modules https://yourdick.zip --- Test.gd | 2 ++ docs/docs/about.md | 2 +- docs/docs/reference/api/core.md | 10 +++++++--- src/core.gd | 29 ++++++++++++++++++++++++++++- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Test.gd b/Test.gd index 9e15cf1..e12fd0e 100644 --- a/Test.gd +++ b/Test.gd @@ -43,6 +43,8 @@ Type (technical) = %type_technical% Typerelease = %release_type% Development mode = %devmode% Headless mode = %headless%""")) + # Print hi + core.logger.info("Hi there!") # Update CORE configuration func configure_core() -> void: diff --git a/docs/docs/about.md b/docs/docs/about.md index 825f73f..70e3d1b 100644 --- a/docs/docs/about.md +++ b/docs/docs/about.md @@ -16,4 +16,4 @@ If you want to use the CORE Framework in a new project, then your answer is **ye - [x] Logger implementation - [ ] HTTP Request helper - [ ] Mod Loader -- [ ] Support for custom modules \ No newline at end of file +- [x] Support for custom modules diff --git a/docs/docs/reference/api/core.md b/docs/docs/reference/api/core.md index 7ba0f37..670f779 100644 --- a/docs/docs/reference/api/core.md +++ b/docs/docs/reference/api/core.md @@ -26,10 +26,14 @@ Stores the path to CORE's installation directory. ## Functions ### *void* _init(*CoreConfiguration* new_config) -:::note -This function will be called when calling `CORE.new()`. -::: Determines the base path, loads the configuration file and initializes all modules. +### *void* register_custom_module(*String* module_name, *CoreBaseModule* module_class) +Registers a custom module. +### *void* unregister_custom_module(*String* module_name) +Unregisters a custom module. +### *CoreBaseModule* get_custom_module(*String* module_name) +Returns a registered custom module. \ +Please note that you can't get CORE's builtin modules with this function. ### *void* reload_configuration(*CoreConfiguration* new_config) Loads a new CoreConfiguration class and applies it's settings. ### *bool* is_devmode() diff --git a/src/core.gd b/src/core.gd index e66d7f0..1eae33d 100644 --- a/src/core.gd +++ b/src/core.gd @@ -30,13 +30,14 @@ const version_type: CoreTypes.VersionType = CoreTypes.VersionType.ALPHA const version_typerelease: int = 0 # Modules -var config: CoreConfiguration var logger: CoreBaseModule var misc: CoreBaseModule var logui: CoreBaseModule # Variables var basepath: String +var config: CoreConfiguration +var custom_modules: Dictionary = {} # Preinitialization func _init(new_config: CoreConfiguration = CoreConfiguration.new()) -> void: @@ -82,6 +83,32 @@ func inject_modules() -> void: add_child(misc) add_child(logui) +# Registers a custom module +func register_custom_module(module_name: String, module_class: CoreBaseModule) -> void: + logger.verb("Registering new custom module \"" + module_name + "\"") + if custom_modules.has(module_name): + logger.error("Registering module failed: A custom module with the name \"" + module_name + "\" already exists.") + return + custom_modules.merge({ module_name: module_class }) + module_class._initialize() + module_class._pull_config() + +# Unregisters a custom module +func unregister_custom_module(module_name: String) -> void: + logger.verb("Unregistering custom module \"" + module_name + "\"") + if !custom_modules.has(module_name): + logger.error("Unregistering module failed: A custom module with the name \"" + module_name + "\" does not exist.") + return + custom_modules.erase(module_name) + +# Returns a custom module +func get_custom_module(module_name: String) -> CoreBaseModule: + logger.diag("Getting custom module \"" + module_name + "\"") + if !custom_modules.has(module_name): + logger.error("Getting module failed: A custom module with the name \"" + module_name + "\" does not exist.") + return + return custom_modules[module_name] + # (Re-)Load configuration func reload_configuration(new_config: CoreConfiguration = CoreConfiguration.new()) -> void: var initialized = config != null