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