Improve custom modules support

This commit is contained in:
JeremyStar™ 2024-02-09 15:30:20 +01:00
parent 9c9c72e4ce
commit 645a4ba981

View file

@ -38,11 +38,13 @@ var logui: CoreBaseModule
var basepath: String var basepath: String
var config: CoreConfiguration var config: CoreConfiguration
var custom_modules: Dictionary = {} var custom_modules: Dictionary = {}
var custom_modules_node: Node
# Preinitialization # Preinitialization
func _init(new_config: CoreConfiguration = CoreConfiguration.new()) -> void: func _init(new_config: CoreConfiguration = CoreConfiguration.new()) -> void:
name = "CORE" name = "CORE"
if !determine_basepath(): queue_free() if !determine_basepath(): queue_free()
custom_modules_node = Node.new()
reload_configuration(new_config) reload_configuration(new_config)
initialize_modules() initialize_modules()
apply_configuration() apply_configuration()
@ -50,6 +52,7 @@ func _init(new_config: CoreConfiguration = CoreConfiguration.new()) -> void:
# Initialization # Initialization
func _ready() -> void: func _ready() -> void:
inject_modules() inject_modules()
add_child(custom_modules_node)
logger.info("Initialized CORE successfully") logger.info("Initialized CORE successfully")
if is_devmode(): logger.warn("The CORE Framework is in development mode. Here be dragons!") if is_devmode(): logger.warn("The CORE Framework is in development mode. Here be dragons!")
if config.headless: logger.warn("CORE is in headless mode. Certain modules will not work as expected.") if config.headless: logger.warn("CORE is in headless mode. Certain modules will not work as expected.")
@ -89,8 +92,14 @@ func register_custom_module(module_name: String, module_class: CoreBaseModule) -
if custom_modules.has(module_name): if custom_modules.has(module_name):
logger.error("Registering module failed: A custom module with the name \"" + module_name + "\" already exists.") logger.error("Registering module failed: A custom module with the name \"" + module_name + "\" already exists.")
return return
module_class.name = module_name
logger.diag("Adding module to SceneTree")
custom_modules_node.add_child(module_class)
logger.diag("Merging module with custom_modules")
custom_modules.merge({ module_name: module_class }) custom_modules.merge({ module_name: module_class })
logger.diag("Initializing custom module")
module_class._initialize() module_class._initialize()
logger.diag("Updating custom module configuration")
module_class._pull_config() module_class._pull_config()
# Unregisters a custom module # Unregisters a custom module
@ -99,6 +108,7 @@ func unregister_custom_module(module_name: String) -> void:
if !custom_modules.has(module_name): if !custom_modules.has(module_name):
logger.error("Unregistering module failed: A custom module with the name \"" + module_name + "\" does not exist.") logger.error("Unregistering module failed: A custom module with the name \"" + module_name + "\" does not exist.")
return return
custom_modules_node.remove_child(get_custom_module(module_name))
custom_modules.erase(module_name) custom_modules.erase(module_name)
# Returns a custom module # Returns a custom module
@ -112,17 +122,22 @@ func get_custom_module(module_name: String) -> CoreBaseModule:
# (Re-)Load configuration # (Re-)Load configuration
func reload_configuration(new_config: CoreConfiguration = CoreConfiguration.new()) -> void: func reload_configuration(new_config: CoreConfiguration = CoreConfiguration.new()) -> void:
var initialized = config != null var initialized = config != null
if initialized: logger.info("Reloading CORE's configuration") if initialized: logger.verb("Reloading CORE's configuration")
config = new_config config = new_config
if is_devmode(): # Override configuration in development mode if is_devmode(): # Override configuration in development mode
config.logger_level = CoreTypes.LoggerLevel.VERB config.logger_level = CoreTypes.LoggerLevel.VERB
if initialized: logger.verb("Overrode configuration (development mode)")
if initialized: apply_configuration() if initialized: apply_configuration()
# Call _pull_config() functions # Call _pull_config() functions
func apply_configuration() -> void: func apply_configuration() -> void:
logger.verb("Applying configuration")
logger._pull_config() logger._pull_config()
misc._pull_config() misc._pull_config()
logui._pull_config() logui._pull_config()
for module in custom_modules:
logger.diag("Updating configuration for custom module \"" + module.name + "\"")
module._pull_config()
# Determines CORE's installation/base path # Determines CORE's installation/base path
func determine_basepath() -> bool: func determine_basepath() -> bool: