From 2ae5c3665e3d1f9f566c884686dc69e35d3e6167 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 29 Dec 2024 18:48:05 +0100 Subject: [PATCH] Add events --- .../de/staropensource/engine/base/Engine.kt | 34 +++-- .../engine/base/implementable/Event.kt | 136 ++++++++++++++++++ .../implementable/InternalNoArgumentsEvent.kt | 53 +++++++ .../base/implementable/NoArgumentsEvent.kt | 46 ++++++ .../event/EngineBootstrapEvent.kt | 47 ++++++ .../event/EngineInitializationEvent.kt | 47 ++++++ .../implementation/event/EngineReloadEvent.kt | 47 ++++++ .../event/EngineShutdownEvent.kt | 70 +++++++++ .../event/EngineStateChangeEvent.kt | 68 +++++++++ .../implementation/event/ProcessSpawnEvent.kt | 68 +++++++++ .../event/SubsystemRegistrationEvent.kt | 68 +++++++++ .../base/implementation/event/package-info.kt | 25 ++++ .../engine/base/utility/Process.kt | 5 + 13 files changed, 706 insertions(+), 8 deletions(-) create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementable/InternalNoArgumentsEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementable/NoArgumentsEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineBootstrapEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineInitializationEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineReloadEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineShutdownEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineStateChangeEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/ProcessSpawnEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/SubsystemRegistrationEvent.kt create mode 100644 base/src/main/kotlin/de/staropensource/engine/base/implementation/event/package-info.kt 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 ffd861b..d48601a 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/Engine.kt @@ -19,8 +19,10 @@ package de.staropensource.engine.base +import de.staropensource.engine.base.Engine.Companion.bootstrap import de.staropensource.engine.base.exception.EngineInitializationFailureException import de.staropensource.engine.base.implementable.Subsystem +import de.staropensource.engine.base.implementation.event.* import de.staropensource.engine.base.logging.Logger import de.staropensource.engine.base.utility.Environment import de.staropensource.engine.base.utility.FileAccess @@ -58,7 +60,10 @@ class Engine private constructor() { * @since v1-alpha10 */ var state: State = State.UNINITIALIZED - internal set + internal set(value: State) { + field = value + EngineStateChangeEvent.instance.emit(value) + } /** * Contains if the engine is currently @@ -155,9 +160,10 @@ class Engine private constructor() { /** * Bootstraps the engine. * - * This method initializes static variables - * and generally performs actions which - * should only be executed once. + * This method initializes static + * variables and generally performs + * actions which should only be + * performed once. * * @return `true` if bootstrapping was performed successfully, `false` if failed * @throws Throwable on initialization error @@ -193,6 +199,9 @@ class Engine private constructor() { ) } + // Emit EngineBootstrapEvent + EngineBootstrapEvent.instance.emit() + bootstrapping = false return true } catch (exception: Exception) { @@ -250,6 +259,9 @@ class Engine private constructor() { ) } + // Emit EngineInitializationEvent + EngineInitializationEvent.instance.emit() + state = State.INITIALIZED // Print initialization message @@ -307,6 +319,9 @@ class Engine private constructor() { fatal = true ) } + + // Emit EngineReloadEvent + EngineReloadEvent.instance.emit() } /** @@ -376,11 +391,11 @@ class Engine private constructor() { /** * Shuts the engine down. * - * This performs the actual shutdown, - * just without the bloat. + * This method performs + * the actual shutdown. * - * @param final whether this is the last time the engine shuts down. Doesn't actually shut the application down, just changes some messages and does other things - * @param crashed enables super careful mode to prevent further breakage + * @param final whether this is the last time the engine shuts down. Note that enabling this flag does not kill the process by itself + * @param crashed whether to enable super careful mode to prevent further breakage * @since v1-alpha10 */ @JvmStatic @@ -411,6 +426,9 @@ class Engine private constructor() { } } + // Emit EngineShutdownEvent + EngineShutdownEvent.instance.emit(final, crashed) + // Print shutdown message if (final) logger.info(""" diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt new file mode 100644 index 0000000..f65267b --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementable/Event.kt @@ -0,0 +1,136 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementable + +import de.staropensource.engine.base.Engine.Companion.logger +import de.staropensource.engine.base.utility.misc.StackTraceUtils + +/** + * An event. + * + * It's highly recommended that + * the implementing class provides + * a global instance of itself. + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +abstract class Event { + /** + * Contains all registered listeners. + * + * @since v1-alpha10 + */ + private val listeners: MutableSet.() -> Unit> = mutableSetOf() + + + // -----> Listeners + /** + * Removes a listener from this [Event]. + * + * @return registered listeners + * @since v1-alpha10 + */ + fun getListeners(): Set.() -> Unit> = listeners.toSet() + + /** + * Adds a listener to this [Event]. + * + * @param action listener to add + * @return this instance + * @since v1-alpha10 + */ + fun addListener(action: Map.() -> Unit): Event { + listeners.add(action) + return this + } + + /** + * Removes a listener from this [Event]. + * + * @param action listener to remove + * @return this instance + * @since v1-alpha10 + */ + fun removeListener(action: Map.() -> Unit): Event { + listeners.remove(action) + return this + } + + + // -----> Invocation + /** + * Emits this event with the + * specified arguments. + * + * Invoking this method will + * block the flow of execution + * until all [Event] listeners + * have been invoked. + * + * @param arguments arguments to pass to all listeners + * @return this instance + * @since v1-alpha10 + */ + @OptIn(ExperimentalStdlibApi::class) + protected fun internalEmit(arguments: Map): Event { + // Log about event emission + logger.diag(buildString { + append("Event ${this@Event::class.qualifiedName ?: ""} was emitted ") + if (arguments.isEmpty()) + append("without any arguments") + else { + append("with arguments:") + + // Iterate over arguments + var value: Any? + for (argument: String in arguments.keys) { + value = arguments[argument] + append("\n${argument} = ") + + // Pretty formatting + if (value is Enum<*>) + append("${value::class.qualifiedName ?: ""}.${value.name}") + else if (value is CharSequence) + append("\"${value}\"") + else if (value is Char) + append("'${value}'") + else if (value is Byte) + append("0x${value.toHexString(HexFormat.UpperCase)}") + else if (value is ByteArray) + append("0x[ ${value.toHexString(HexFormat.UpperCase)} ]") + else + append(value.toString()) + } + } + }) + + // Invoke all listeners + listeners.forEach { listener -> { + try { + listener.invoke(arguments) + } catch (throwable: Throwable) { + logger.error("A listener on event ${this::class.qualifiedName ?: ""} threw ${throwable::class.qualifiedName ?: ""}\n\n${StackTraceUtils.stacktraceRecursive(throwable)}") + } + } } + + return this + } +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementable/InternalNoArgumentsEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementable/InternalNoArgumentsEvent.kt new file mode 100644 index 0000000..4569e87 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementable/InternalNoArgumentsEvent.kt @@ -0,0 +1,53 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementable + +/** + * An implementation of [Event] + * without any arguments. + * + * It's highly recommended that + * the implementing class provides + * a global instance of itself. + * + * *This is an exact replica of + * [NoArgumentsEvent], just with + * the side-effect of [emit] being + * an `internal` method. This is + * used for [Event]s which must only + * be emitted by the engine itself.* + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +abstract class InternalNoArgumentsEvent internal constructor() : Event() { + /** + * Emits this event. + * + * Invoking this method will + * block the flow of execution + * until all [Event] listeners + * have been invoked. + * + * @return this instance + * @since v1-alpha10 + */ + internal fun emit(): Event = internalEmit(mapOf()) +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementable/NoArgumentsEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementable/NoArgumentsEvent.kt new file mode 100644 index 0000000..a2026c0 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementable/NoArgumentsEvent.kt @@ -0,0 +1,46 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementable + +/** + * An implementation of [Event] + * without any arguments. + * + * It's highly recommended that + * the implementing class provides + * a global instance of itself. + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +open class NoArgumentsEvent : Event() { + /** + * Calls this event. + * + * Invoking this method will + * block the flow of execution + * until all [Event] listeners + * have been invoked. + * + * @return this instance + * @since v1-alpha10 + */ + fun emit(): Event = internalEmit(mapOf()) +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineBootstrapEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineBootstrapEvent.kt new file mode 100644 index 0000000..9371c93 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineBootstrapEvent.kt @@ -0,0 +1,47 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementation.event + +import de.staropensource.engine.base.implementable.InternalNoArgumentsEvent + +/** + * Emitted after the engine + * has bootstrapped itself. + * + * This event has no arguments. + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +class EngineBootstrapEvent private constructor() : InternalNoArgumentsEvent() { + /** + * Companion object of [EngineBootstrapEvent]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Global instance of [EngineBootstrapEvent]. + * + * @since v1-alpha10 + */ + val instance: EngineBootstrapEvent = EngineBootstrapEvent() + } +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineInitializationEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineInitializationEvent.kt new file mode 100644 index 0000000..accfd67 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineInitializationEvent.kt @@ -0,0 +1,47 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementation.event + +import de.staropensource.engine.base.implementable.InternalNoArgumentsEvent + +/** + * Emitted after the engine + * has initialized itself. + * + * This event has no arguments. + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +class EngineInitializationEvent private constructor() : InternalNoArgumentsEvent() { + /** + * Companion object of [EngineInitializationEvent]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Global instance of [EngineInitializationEvent]. + * + * @since v1-alpha10 + */ + val instance: EngineInitializationEvent = EngineInitializationEvent() + } +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineReloadEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineReloadEvent.kt new file mode 100644 index 0000000..60bcf5b --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineReloadEvent.kt @@ -0,0 +1,47 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementation.event + +import de.staropensource.engine.base.implementable.InternalNoArgumentsEvent + +/** + * Emitted after the engine + * has reloaded itself. + * + * This event has no arguments. + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +class EngineReloadEvent private constructor() : InternalNoArgumentsEvent() { + /** + * Companion object of [EngineReloadEvent]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Global instance of [EngineReloadEvent]. + * + * @since v1-alpha10 + */ + val instance: EngineReloadEvent = EngineReloadEvent() + } +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineShutdownEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineShutdownEvent.kt new file mode 100644 index 0000000..f0807af --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineShutdownEvent.kt @@ -0,0 +1,70 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementation.event + +import de.staropensource.engine.base.implementable.Event + +/** + * Emitted after the state + * of the engine has changed. + * + * This event has two arguments: + * ```markdown + * | NAME | TYPE | PURPOSE/DESCRIPTION | + * | final | Boolean | whether this is the last time the engine shuts down | + * | crashed | Boolean | whether to enable super careful mode to prevent further breakage | + * ``` + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +class EngineShutdownEvent private constructor() : Event() { + /** + * Companion object of [EngineShutdownEvent]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Global instance of [EngineShutdownEvent]. + * + * @since v1-alpha10 + */ + val instance: EngineShutdownEvent = EngineShutdownEvent() + } + + /** + * Emits this event. + * + * Invoking this method will + * block the flow of execution + * until all [Event] listeners + * have been invoked. + * + * @param final whether this is the last time the engine shuts down + * @param crashed whether to enable super careful mode to prevent further breakage + * @return this instance + * @since v1-alpha10 + */ + internal fun emit(final: Boolean, crashed: Boolean): Event = internalEmit(mapOf( + Pair("final", final), + Pair("crashed", crashed) + )) +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineStateChangeEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineStateChangeEvent.kt new file mode 100644 index 0000000..567400d --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/EngineStateChangeEvent.kt @@ -0,0 +1,68 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementation.event + +import de.staropensource.engine.base.Engine +import de.staropensource.engine.base.implementable.Event + +/** + * Emitted after the state + * of the engine has changed. + * + * This event has one argument: + * ```markdown + * | NAME | TYPE | PURPOSE/DESCRIPTION | + * | state | Engine.State | the new engine state | + * ``` + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +class EngineStateChangeEvent private constructor() : Event() { + /** + * Companion object of [EngineStateChangeEvent]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Global instance of [EngineStateChangeEvent]. + * + * @since v1-alpha10 + */ + val instance: EngineStateChangeEvent = EngineStateChangeEvent() + } + + /** + * Emits this event. + * + * Invoking this method will + * block the flow of execution + * until all [Event] listeners + * have been invoked. + * + * @param state new engine [State] + * @return this instance + * @since v1-alpha10 + */ + internal fun emit(state: Engine.State): Event = internalEmit(mapOf( + Pair("state", state) + )) +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/ProcessSpawnEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/ProcessSpawnEvent.kt new file mode 100644 index 0000000..ab4a6f5 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/ProcessSpawnEvent.kt @@ -0,0 +1,68 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementation.event + +import de.staropensource.engine.base.implementable.Event +import de.staropensource.engine.base.utility.Process + +/** + * Emitted after a [Process] + * has been spawned. + * + * This event has one argument: + * ```markdown + * | NAME | TYPE | PURPOSE/DESCRIPTION | + * | process | Process | newly spawned process | + * ``` + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +class ProcessSpawnEvent private constructor() : Event() { + /** + * Companion object of [ProcessSpawnEvent]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Global instance of [ProcessSpawnEvent]. + * + * @since v1-alpha10 + */ + val instance: ProcessSpawnEvent = ProcessSpawnEvent() + } + + /** + * Emits this event. + * + * Invoking this method will + * block the flow of execution + * until all [Event] listeners + * have been invoked. + * + * @param process newly spawned [Process] + * @return this instance + * @since v1-alpha10 + */ + internal fun emit(process: Process): Event = internalEmit(mapOf( + Pair("process", process) + )) +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/SubsystemRegistrationEvent.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/SubsystemRegistrationEvent.kt new file mode 100644 index 0000000..aa90e65 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/SubsystemRegistrationEvent.kt @@ -0,0 +1,68 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.staropensource.engine.base.implementation.event + +import de.staropensource.engine.base.implementable.Event +import de.staropensource.engine.base.implementable.Subsystem + +/** + * Emitted after a subsystem + * has been registered. + * + * This event has one argument: + * ```markdown + * | NAME | TYPE | PURPOSE/DESCRIPTION | + * | subsystem | Subsystem | newly registered subsystem | + * ``` + * + * @constructor Initializes this event + * @since v1-alpha10 + */ +class SubsystemRegistrationEvent private constructor() : Event() { + /** + * Companion object of [SubsystemRegistrationEvent]. + * + * @since v1-alpha10 + */ + companion object { + /** + * Global instance of [SubsystemRegistrationEvent]. + * + * @since v1-alpha10 + */ + val instance: SubsystemRegistrationEvent = SubsystemRegistrationEvent() + } + + /** + * Emits this event. + * + * Invoking this method will + * block the flow of execution + * until all [Event] listeners + * have been invoked. + * + * @param subsystem newly registered [Subsystem] + * @return this instance + * @since v1-alpha10 + */ + internal fun emit(subsystem: Subsystem): Event = internalEmit(mapOf( + Pair("subsystem", subsystem) + )) +} diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/package-info.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/package-info.kt new file mode 100644 index 0000000..8cd67b3 --- /dev/null +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/event/package-info.kt @@ -0,0 +1,25 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/** + * Events emitted by the core engine. + * + * @since v1-alpha10 + */ +package de.staropensource.engine.base.implementation.event diff --git a/base/src/main/kotlin/de/staropensource/engine/base/utility/Process.kt b/base/src/main/kotlin/de/staropensource/engine/base/utility/Process.kt index b6520f2..d3c083d 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/utility/Process.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/utility/Process.kt @@ -25,11 +25,13 @@ import de.staropensource.engine.base.exception.io.IOAccessException import de.staropensource.engine.base.implementable.stream.ReadStream import de.staropensource.engine.base.implementable.stream.Stream import de.staropensource.engine.base.implementable.stream.WriteStream +import de.staropensource.engine.base.implementation.event.ProcessSpawnEvent import de.staropensource.engine.base.implementation.stream.JavaReadStream import de.staropensource.engine.base.implementation.stream.JavaWriteStream import java.io.IOException import java.io.InputStream import java.io.OutputStream +import kotlin.Throws /** * Provides a simplified way of @@ -199,6 +201,9 @@ class Process { standardOutput.watch() standardError.watch() } + + // Emit ProcessSpawnEvent + ProcessSpawnEvent.instance.emit(this) } catch (exception: IOException) { throw IOAccessException("Unable to spawn new process", exception) }