Class MainModuleEngine
Constructing an engine is what starts the program. The constructor loads the main
configuration, registers the initial scene, runs every module's main entrypoint, and
then begins the callback loop — so by the time it returns, the whole module graph is up.
Callbacks all run on a single scheduled thread, so implementations need no synchronisation
against one another. Module initialization is the opposite: entrypoints run concurrently on an
unbounded pool, which the dependency handshake requires — see
callDependents(String, Class, Consumer, Logger).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ScheduledExecutorServiceThe single thread every callback runs on.protected ScheduledFuture<?> Tracks the repeating task that drives every registered callback, cancelled byclean().protected booleanIstrueif the Engine is initialized.protected static MainModuleEngineThe running engine, set as soon as construction begins so that entrypoints can reach it.protected final CurvedSpacetimeLoaderThe loader module entrypoints are resolved through.final LoggerThe logger the engine writes its own diagnostics to.final MainModuleConfigThe engine's settings, loaded before any module runs. -
Constructor Summary
ConstructorsConstructorDescriptionCreates the engine and brings the whole module graph up. -
Method Summary
Modifier and TypeMethodDescriptionstatic <E> voidcallDependents(String name, Class<E> moduleInitializerClass, Consumer<E> onInitialize, Logger logger) Invokes every entrypoint registered under the given name, concurrently, and waits for all of them.voidclean()Stops the callback loop, releases every callback, and then shuts the callback executor down.Returns the console handler used for the Curved Spacetime Loggers.Returns the loader this engine resolves entrypoints through.static MainModuleEngineReturns the running engine.booleanReturns whether the Engine is initialized.registerMainCallback(MainCallback mainCallback) Registers scene-independent work and initialises it on the callback thread.voidregisterScene(Scene scene) Adds a scene and gives every registered generator a callback for it.registerSceneCallbackGenerator(Function<Scene, SceneCallback> sceneCallbackGenerator) Registers a factory that produces one callback per scene.voidPrepares the loader and runs every module'smainentrypoint.voidthrowError(Throwable t, String message) Throws an error and stops the engine.
-
Field Details
-
INSTANCE
The running engine, set as soon as construction begins so that entrypoints can reach it. -
mainModuleConfig
The engine's settings, loaded before any module runs. -
logger
The logger the engine writes its own diagnostics to. -
callbackExecutor
The single thread every callback runs on. -
loader
The loader module entrypoints are resolved through. -
callbackLoopHandler
Tracks the repeating task that drives every registered callback, cancelled byclean(). -
initialized
protected boolean initializedIstrueif the Engine is initialized. Note that this doesn't mean all callbacks are registered, As a callback can be registered at any time and is registered asynchronously.
-
-
Constructor Details
-
MainModuleEngine
Creates the engine and brings the whole module graph up.Loads the main configuration, registers the initial scene, runs every
mainentrypoint, and starts the callback loop at the configured frame rate.- Parameters:
loader- the loader that resolves module entrypoints- Throws:
RuntimeException- if the main configuration cannot be loaded, or if any module's entrypoint fails
-
-
Method Details
-
registerScene
Adds a scene and gives every registered generator a callback for it.- Parameters:
scene- the scene to add
-
runEntrypoints
public void runEntrypoints()Prepares the loader and runs every module'smainentrypoint.- Throws:
RuntimeException- wrapping whatever a module's entrypoint threw
-
callDependents
public static <E> void callDependents(String name, Class<E> moduleInitializerClass, Consumer<E> onInitialize, Logger logger) throws Throwable Invokes every entrypoint registered under the given name, concurrently, and waits for all of them.The pool this uses is unbounded, and must remain so. The dependency handshake blocks a consumer in
TransferQueue.takewhile its producer is still to run, so a producer and its consumer have to be runnable at the same time; a bounded pool deadlocks as soon as its threads fill with consumers whose producers are still queued.The first entrypoint to fail aborts the whole call rather than leaving a half-initialized module graph running.
- Type Parameters:
E- the entrypoint type- Parameters:
name- the entrypoint name to invoke, such asmainmoduleInitializerClass- the type registered entrypoints must be assignable toonInitialize- the action to apply to each entrypointlogger- the logger to trace dispatch through- Throws:
Throwable- whatever the first failing entrypoint threw
-
getInstance
Returns the running engine.- Returns:
- the engine, or
nullbefore one has been constructed
-
getCurvedSpacetimeLoader
Returns the loader this engine resolves entrypoints through.- Returns:
- the loader
-
registerMainCallback
Registers scene-independent work and initialises it on the callback thread.- Parameters:
mainCallback- the callback to register- Returns:
- the completable future generated for this task
-
registerSceneCallbackGenerator
public CompletableFuture<Void> registerSceneCallbackGenerator(Function<Scene, SceneCallback> sceneCallbackGenerator) Registers a factory that produces one callback per scene.The generator is applied to every scene that exists now, and to each one registered later, so a module never has to track scenes itself.
- Parameters:
sceneCallbackGenerator- the factory producing a callback for a given scene- Returns:
- the completable future generated for this task
-
clean
public void clean()Stops the callback loop, releases every callback, and then shuts the callback executor down. -
throwError
-
getConsoleHandler
Returns the console handler used for the Curved Spacetime Loggers.- Returns:
- the console handler
-
isInitialized
public boolean isInitialized()Returns whether the Engine is initialized. Note that this doesn't mean all callbacks are registered, As a callback can be registered at any time and is registered asynchronously.- Returns:
trueif the engine is initialized
-