Add Engine.State, Engine.bootstrap & Engine.shutdown

This commit is contained in:
JeremyStar™ 2024-12-15 01:11:22 +01:00
parent 1e40c8b121
commit eea7f5f4af
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 169 additions and 3 deletions

View file

@ -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
fun initialize() {
Logger.instance.info("Initializing")
@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() {
// 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
}
}

View file

@ -50,7 +50,10 @@ class Main private constructor() {
Engine.initialize()
// Print hello world
logger.info("Hello World! <red><bold>Caused by <clinit> and \\<init>.")
logger.info("Hello World!")
// Shutdown engine
Engine.shutdown()
}
}
}