From 05ebb62641497e716cf08c44903f632a87723f85 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Fri, 22 Mar 2024 21:12:11 +0100 Subject: [PATCH] Add 'initialized' var & waiting code --- Test.gd | 2 +- src/Core.gd | 40 ++++++++++++++++++++++++++++++++++++++- src/Logui.gd | 3 +++ src/Sms.gd | 3 +++ src/classes/Basemodule.gd | 4 +++- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Test.gd b/Test.gd index 37fb9a2..4857d9f 100644 --- a/Test.gd +++ b/Test.gd @@ -35,7 +35,7 @@ func _ready() -> void: # Inject CORE await get_tree().process_frame add_child(core) - await get_tree().process_frame + await core.complete_init() # Print information about CORE core.logger.info("Test.gd", await core.get_formatted_string("""Version information: Release (semantic) = %release_semantic% diff --git a/src/Core.gd b/src/Core.gd index fe99fbf..e843781 100644 --- a/src/Core.gd +++ b/src/Core.gd @@ -71,7 +71,6 @@ func _ready() -> void: inject_modules() custom_modules_node.name = "Custom Modules" add_child(custom_modules_node) - logger.infof("Core", "Initialized CORE successfully") # Initialize modules ## Initializes all modules during the first initialization phase.[br] @@ -126,6 +125,45 @@ func inject_modules() -> void: add_child(edl) add_child(storage) +# Wait for all modules to be fully initialized +## Wait for all builtin modules to be fully initialized.[br] +## [br] +## This ensures that all of CORE's builtin modules are fully initialized and ready. +## [b]Not calling this function during startup may lead to runtime issues.[/b] +func complete_init(no_success: bool = false) -> void: + var modsinit_builtin: Array[String] = ["workaround"] + var modsinit_custom: Array[String] = ["workaround"] + + while modsinit_builtin.size() != 0 and modsinit_custom.size() != 0: + # Clear arrays + modsinit_builtin = [] + modsinit_custom = [] + + # Check builtin modules + if !logger.initialized: modsinit_builtin.append("logger") + if !misc.initialized: modsinit_builtin.append("misc") + if !sms.initialized: modsinit_builtin.append("sms") + if !logui.initialized: modsinit_builtin.append("logui") + if !edl.initialized: modsinit_builtin.append("edl") + if !storage.initialized: modsinit_builtin.append("storage") + + # Check custom modules + for module_name in custom_modules: + if !custom_modules[module_name].initialized: modsinit_custom.append(module_name) + + # Print and sleep + if modsinit_builtin.size() != 0 or modsinit_custom.size() != 0: + print("Waiting for modules to finish initialization:") + if modsinit_builtin.size() != 0: + print(" Builtin: " + str(modsinit_builtin)) + if modsinit_custom.size() != 0: + print(" Custom: " + str(modsinit_custom)) + await get_tree().create_timer(1).timeout + + # Initialization complete + await get_tree().process_frame + if !no_success: logger.infof("Core", "Initialized CORE successfully") + # Registers a custom module ## Registers a new custom module. func register_custom_module(module_name: String, module_class: CoreBaseModule) -> bool: diff --git a/src/Logui.gd b/src/Logui.gd index a01f66d..685de40 100644 --- a/src/Logui.gd +++ b/src/Logui.gd @@ -55,6 +55,9 @@ func _initialize() -> void: logrtl.add_theme_font_override("bold_font", font_bold) logrtl.add_theme_font_size_override("normal_font_size", 14) logrtl.add_theme_font_size_override("bold_font_size", 14) + + # Mark as initialized + initialized = true func _pull_config() -> void: background.visible = !core.config.headless and core.config.logui_enabled diff --git a/src/Sms.gd b/src/Sms.gd index e4721ec..7e0222d 100644 --- a/src/Sms.gd +++ b/src/Sms.gd @@ -41,6 +41,9 @@ func _initialize() -> void: add_child(scenes_menu) add_child(scenes_cutscene) add_child(scenes_debug) + + # Mark as initialized + initialized = true func _pull_config() -> void: if core.config.headless: diff --git a/src/classes/Basemodule.gd b/src/classes/Basemodule.gd index 8723e2b..0117e8d 100644 --- a/src/classes/Basemodule.gd +++ b/src/classes/Basemodule.gd @@ -30,10 +30,12 @@ var core: Core ## [br] ## Will be set before [method Node._ready] @onready var logger: CoreBaseModule = core.logger +## Marks a module as fully initialized and ready. +var initialized: bool = false ## CORE's replacement for [method Object._init] and [method Node._ready] ## It's [b]strongly[/b] recommended to initialize your module here. -func _initialize() -> void: pass +func _initialize() -> void: initialized = true ## Called by [method Core.apply_configuration]. ## This should be used to update your module configuration. func _pull_config() -> void: pass