|
|
|
@ -7,6 +7,8 @@ import java.util.List; |
|
|
|
|
import mightypork.gamecore.audio.AudioModule; |
|
|
|
|
import mightypork.gamecore.core.config.Config; |
|
|
|
|
import mightypork.gamecore.core.events.ShutdownEvent; |
|
|
|
|
import mightypork.gamecore.core.init.InitTask; |
|
|
|
|
import mightypork.gamecore.core.plugins.AppPlugin; |
|
|
|
|
import mightypork.gamecore.graphics.GraphicsModule; |
|
|
|
|
import mightypork.gamecore.input.InputModule; |
|
|
|
|
import mightypork.utils.annotations.Stub; |
|
|
|
@ -23,19 +25,19 @@ import mightypork.utils.logging.Log; |
|
|
|
|
* @author MightyPork |
|
|
|
|
*/ |
|
|
|
|
public class App extends BusNode { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static App instance; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final AppBackend backend; |
|
|
|
|
private final EventBus eventBus = new EventBus(); |
|
|
|
|
private boolean started = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** List of installed App plugins */ |
|
|
|
|
protected final DelegatingList plugins = new DelegatingList(); |
|
|
|
|
/** List of initializers */ |
|
|
|
|
protected final List<InitTask> initializers = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Create an app with given backend. |
|
|
|
|
* |
|
|
|
@ -46,24 +48,24 @@ public class App extends BusNode { |
|
|
|
|
if (App.instance != null) { |
|
|
|
|
throw new IllegalStateException("App already initialized"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// store current instance in static field
|
|
|
|
|
App.instance = this; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// join the bus
|
|
|
|
|
this.eventBus.subscribe(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create plugin registry attached to bus
|
|
|
|
|
this.eventBus.subscribe(this.plugins); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// initialize and use backend
|
|
|
|
|
this.backend = backend; |
|
|
|
|
this.backend.bind(this); |
|
|
|
|
this.eventBus.subscribe(backend); |
|
|
|
|
this.backend.initialize(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Add a plugin to the app. Plugins can eg. listen to bus events and react |
|
|
|
|
* to them. |
|
|
|
@ -75,14 +77,14 @@ public class App extends BusNode { |
|
|
|
|
if (started) { |
|
|
|
|
throw new IllegalStateException("App already started, cannot add plugins."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// attach to event bus
|
|
|
|
|
plugins.add(plugin); |
|
|
|
|
plugin.bind(this); |
|
|
|
|
plugin.initialize(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Add an initializer to the app. |
|
|
|
|
* |
|
|
|
@ -93,11 +95,11 @@ public class App extends BusNode { |
|
|
|
|
if (started) { |
|
|
|
|
throw new IllegalStateException("App already started, cannot add initializers."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initializers.add(initializer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get current backend |
|
|
|
|
* |
|
|
|
@ -107,8 +109,8 @@ public class App extends BusNode { |
|
|
|
|
{ |
|
|
|
|
return backend; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Initialize the App and start operating.<br> |
|
|
|
|
* This method should be called after adding all required initializers and |
|
|
|
@ -120,41 +122,41 @@ public class App extends BusNode { |
|
|
|
|
throw new IllegalStateException("Already started."); |
|
|
|
|
} |
|
|
|
|
started = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pre-init hook, just in case anyone wanted to have one.
|
|
|
|
|
Log.f2("Calling pre-init hook..."); |
|
|
|
|
preInit(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.i("=== Starting initialization sequence ==="); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sort initializers by order.
|
|
|
|
|
final List<InitTask> orderedInitializers = InitTask.inOrder(initializers); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (final InitTask initTask : orderedInitializers) { |
|
|
|
|
Log.f1("Running init task \"" + initTask.getName() + "\"..."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initTask.bind(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set the task options
|
|
|
|
|
initTask.init(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initTask.before(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// main task action
|
|
|
|
|
initTask.run(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// after hook for extra actions immeditaely after the task completes
|
|
|
|
|
initTask.after(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.i("=== Initialization sequence completed ==="); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// user can now start the main loop etc.
|
|
|
|
|
Log.f2("Calling post-init hook..."); |
|
|
|
|
postInit(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Hook called before the initialization sequence starts. |
|
|
|
|
*/ |
|
|
|
@ -162,8 +164,8 @@ public class App extends BusNode { |
|
|
|
|
protected void preInit() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Hook called after the initialization sequence is finished. |
|
|
|
|
*/ |
|
|
|
@ -171,8 +173,8 @@ public class App extends BusNode { |
|
|
|
|
protected void postInit() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Shut down the running instance.<br> |
|
|
|
|
* Deinitialize backend modules and terminate the JVM. |
|
|
|
@ -181,9 +183,9 @@ public class App extends BusNode { |
|
|
|
|
{ |
|
|
|
|
if (instance != null) { |
|
|
|
|
Log.i("Dispatching Shutdown event..."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bus().send(new ShutdownEvent(new Runnable() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() |
|
|
|
|
{ |
|
|
|
@ -196,19 +198,19 @@ public class App extends BusNode { |
|
|
|
|
} catch (final Throwable e) { |
|
|
|
|
Log.e(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.i("Shutdown completed."); |
|
|
|
|
System.exit(0); |
|
|
|
|
} |
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
Log.w("App is not running."); |
|
|
|
|
System.exit(0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the currently running App instance. |
|
|
|
|
* |
|
|
|
@ -218,8 +220,8 @@ public class App extends BusNode { |
|
|
|
|
{ |
|
|
|
|
return instance; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get graphics module from the running app's backend |
|
|
|
|
* |
|
|
|
@ -229,19 +231,19 @@ public class App extends BusNode { |
|
|
|
|
{ |
|
|
|
|
return instance.backend.getGraphics(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get audio module from the running app's backend |
|
|
|
|
* |
|
|
|
|
* @return audio module |
|
|
|
|
*/ |
|
|
|
|
public static AudioModule audio() |
|
|
|
|
public static AudioModule sound() |
|
|
|
|
{ |
|
|
|
|
return instance.backend.getAudio(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get input module from the running app's backend |
|
|
|
|
* |
|
|
|
@ -251,8 +253,8 @@ public class App extends BusNode { |
|
|
|
|
{ |
|
|
|
|
return instance.backend.getInput(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get event bus instance. |
|
|
|
|
* |
|
|
|
@ -262,8 +264,8 @@ public class App extends BusNode { |
|
|
|
|
{ |
|
|
|
|
return instance.eventBus; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the main config, if initialized. |
|
|
|
|
* |
|
|
|
@ -274,8 +276,8 @@ public class App extends BusNode { |
|
|
|
|
{ |
|
|
|
|
return cfg("main"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get a config by alias. |
|
|
|
|
* |
|
|
|
|