From eea7f5f4af613b8084ec03511c19e6f5fb3b89cf Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 15 Dec 2024 01:11:22 +0100 Subject: [PATCH] Add Engine.State, Engine.bootstrap & Engine.shutdown --- .../de/staropensource/engine/base/Engine.kt | 167 +++++++++++++++++- .../de/staropensource/engine/testapp/Main.kt | 5 +- 2 files changed, 169 insertions(+), 3 deletions(-) diff --git a/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt b/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt index 8a4bed3..bca4e69 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt @@ -38,13 +38,176 @@ class Engine private constructor() { */ companion object { /** - * Initializes the engine. + * Contains the [Logger] instance of the engine. * * @since v1-alpha10 */ + internal val logger: Logger = Logger("engine-core") + + /** + * Contains the state the engine is in. + * + * @since v1-alpha10 + */ + var state: State = State.UNINITIALIZED + private set + + /** + * Contains if the engine is currently + * bootstrapping or has been bootstrapped. + * + * `null` indicates that the engine hasn't + * been bootstrapped yet, `true` indicates + * that the engine has been bootstrapped + * and `false` indicates that the engine + * is currently bootstrapping itself. + * + * @see bootstrap + * @since v1-alpha10 + */ + var bootstrapping: Boolean? = null + + + // -----> Initialization + /** + * Bootstraps the engine. + * + * This method initializes static variables + * and generally performs actions which + * should only be executed once. + * + * @throws Throwable on initialization error + * @since v1-alpha10 + */ @JvmStatic + @Throws(Throwable::class) + private fun bootstrap() { + if (state != State.UNINITIALIZED || bootstrapping != null) + return + + bootstrapping = true + logger.info("Bootstrapping") + + // Run bootstrapping code + + bootstrapping = false + } + + /** + * Initializes the engine. + * + * @throws Throwable on initialization error + * @since v1-alpha10 + */ + @JvmStatic + @Throws(Throwable::class) fun initialize() { - Logger.instance.info("Initializing") + // Abort if initializing, shutting down + // or the engine has crashed + if ( + bootstrapping == true + || state == State.INITIALIZING + || state == State.SHUTTING_DOWN + || state == State.CRASHED + ) return + + // Bootstrap if not already done so + bootstrap() + + state = State.INITIALIZING + logger.info("Initializing") + + // Run initialization code + + state = State.INITIALIZED + } + + /** + * Shuts the engine down. + * + * @throws Throwable on shutdown error + * @since v1-alpha10 + */ + @JvmStatic + @Throws(Throwable::class) + fun shutdown() { + // Abort if not initialized + if (state != State.INITIALIZED) + return + + state = State.SHUTTING_DOWN + logger.info("Shutting down") + + // Run shutdown code + + state = State.SHUT_DOWN } } + + /** + * Represents the states the engine can be in. + * + * @since v1-alpha10 + */ + enum class State { + /** + * Specifies that the engine has + * not been initialized at least + * once yet. + * + * @since v1-alpha10 + */ + UNINITIALIZED, + + /** + * Indicates that the engine is + * in the initialization process. + * + * Many methods are considered + * unsafe and should not be called. + * + * @since v1-alpha10 + */ + INITIALIZING, + + /** + * Specifies that the engine has fully + * initialized and can be safely used. + * + * @since v1-alpha10 + */ + INITIALIZED, + + /** + * Indicates that the engine is in + * the process of shutting down. + * + * Many methods are considered + * unsafe and should not be called. + */ + SHUTTING_DOWN, + + /** + * Indicates that the engine has been shut down. + * + * This may not be a final state. + * + * @since v1-alpha10 + */ + SHUT_DOWN, + + /** + * Indicates that the engine has crashed and + * is in a state where further usage is + * HIGHLY discouraged. Any further calls may + * cause the process or virtual machine to + * exit with a fatal error. + * + * This is a final state. The engine cannot + * be initialized again after having crashed. + * + * @since v1-alpha10 + */ + CRASHED + } } diff --git a/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt b/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt index bc499bb..a9827e2 100644 --- a/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt +++ b/testapp/src/main/kotlin/de/staropensource/engine/testapp/Main.kt @@ -50,7 +50,10 @@ class Main private constructor() { Engine.initialize() // Print hello world - logger.info("Hello World! Caused by and \\.") + logger.info("Hello World!") + + // Shutdown engine + Engine.shutdown() } } }