2019-08-26 09:43:53 +03:00
|
|
|
|
package pro.gravit.launcher.modules;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public abstract class LauncherModule {
|
|
|
|
|
private LauncherModulesContext context;
|
|
|
|
|
|
|
|
|
|
private Map<Class<? extends Event>, EventHandler> eventMap = new HashMap<>();
|
|
|
|
|
protected LauncherModulesManager modulesManager;
|
2019-08-26 10:42:49 +03:00
|
|
|
|
protected final LauncherModuleInfo moduleInfo;
|
2019-08-26 09:43:53 +03:00
|
|
|
|
protected ModulesConfigManager modulesConfigManager;
|
2019-08-26 13:37:14 +03:00
|
|
|
|
protected InitStatus initStatus = InitStatus.CREATED;
|
2019-08-26 10:42:49 +03:00
|
|
|
|
|
|
|
|
|
protected LauncherModule() {
|
|
|
|
|
moduleInfo = new LauncherModuleInfo("UnknownModule");
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 13:24:19 +03:00
|
|
|
|
protected LauncherModule(LauncherModuleInfo info) {
|
|
|
|
|
moduleInfo = info;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 12:27:30 +03:00
|
|
|
|
public LauncherModuleInfo getModuleInfo() {
|
|
|
|
|
return moduleInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 12:37:59 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module initialization status at the current time
|
|
|
|
|
* CREATED - Module status immediately after loading
|
|
|
|
|
* INIT - The state of the module during the execution of the method init()
|
|
|
|
|
* FINISH - Status of the module after initialization
|
|
|
|
|
*/
|
2019-08-26 13:37:14 +03:00
|
|
|
|
public enum InitStatus
|
2019-08-26 09:43:53 +03:00
|
|
|
|
{
|
2019-08-28 17:04:44 +03:00
|
|
|
|
CREATED(false),
|
|
|
|
|
PRE_INIT_WAIT(true),
|
|
|
|
|
PRE_INIT(false),
|
|
|
|
|
INIT_WAIT(true),
|
|
|
|
|
INIT(false),
|
|
|
|
|
FINISH(true);
|
|
|
|
|
|
|
|
|
|
InitStatus(boolean b) {
|
|
|
|
|
isAvailable = b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isAvailable() {
|
|
|
|
|
return isAvailable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final boolean isAvailable;
|
2019-08-26 09:43:53 +03:00
|
|
|
|
}
|
|
|
|
|
@FunctionalInterface
|
|
|
|
|
public interface EventHandler<T extends Event>
|
|
|
|
|
{
|
2019-08-26 13:24:19 +03:00
|
|
|
|
void event(T e);
|
2019-08-26 09:43:53 +03:00
|
|
|
|
}
|
2019-08-26 10:42:49 +03:00
|
|
|
|
public static class Event
|
2019-08-26 09:43:53 +03:00
|
|
|
|
{
|
2019-08-26 10:42:49 +03:00
|
|
|
|
public boolean isCancel() {
|
|
|
|
|
return cancel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Event cancel() {
|
|
|
|
|
this.cancel = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2019-08-26 09:43:53 +03:00
|
|
|
|
|
2019-08-26 12:27:30 +03:00
|
|
|
|
protected boolean cancel = false;
|
2019-08-26 09:43:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 13:37:14 +03:00
|
|
|
|
public InitStatus getInitStatus() {
|
|
|
|
|
return initStatus;
|
2019-08-26 09:43:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 13:37:14 +03:00
|
|
|
|
public LauncherModule setInitStatus(InitStatus initStatus) {
|
|
|
|
|
this.initStatus = initStatus;
|
2019-08-26 12:27:30 +03:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 12:37:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* The internal method used by the ModuleManager
|
|
|
|
|
* DO NOT TOUCH
|
|
|
|
|
* @param context Private context
|
|
|
|
|
*/
|
2019-08-26 12:27:30 +03:00
|
|
|
|
public void setContext(LauncherModulesContext context)
|
2019-08-26 09:43:53 +03:00
|
|
|
|
{
|
|
|
|
|
if(this.context != null) throw new IllegalStateException("Module already set context");
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.modulesManager = context.getModulesManager();
|
|
|
|
|
this.modulesConfigManager = context.getModulesConfigManager();
|
2019-08-28 17:04:44 +03:00
|
|
|
|
this.setInitStatus(InitStatus.PRE_INIT_WAIT);
|
2019-08-26 12:27:30 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 12:37:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* This method is called before initializing all modules and resolving dependencies.
|
|
|
|
|
* <b>You can</b>:
|
|
|
|
|
* Use to Module Manager
|
|
|
|
|
* Add custom modules not described in the manifest
|
|
|
|
|
* Change information about your module or modules you control
|
|
|
|
|
* <b>You can not</b>:
|
|
|
|
|
* Use your dependencies
|
|
|
|
|
* Use API Launcher, LaunchServer, ServerWrapper
|
|
|
|
|
* Change the names of any modules
|
|
|
|
|
*/
|
2019-08-28 17:04:44 +03:00
|
|
|
|
public void preInitAction() {
|
2019-08-26 12:27:30 +03:00
|
|
|
|
//NOP
|
2019-08-26 09:43:53 +03:00
|
|
|
|
}
|
2019-08-28 17:04:44 +03:00
|
|
|
|
public LauncherModule preInit()
|
|
|
|
|
{
|
|
|
|
|
if(!initStatus.equals(InitStatus.PRE_INIT_WAIT)) throw new IllegalStateException("PreInit not allowed in current state");
|
|
|
|
|
initStatus = InitStatus.PRE_INIT;
|
|
|
|
|
preInitAction();
|
|
|
|
|
initStatus = InitStatus.INIT_WAIT;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2019-08-26 10:42:49 +03:00
|
|
|
|
|
2019-08-28 12:37:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Basic module initialization method
|
|
|
|
|
* <b>You can</b>:
|
|
|
|
|
* Subscribe to events
|
|
|
|
|
* Use your dependencies
|
|
|
|
|
* Use provided initContext
|
|
|
|
|
* Receive modules and access the module’s internal methods
|
|
|
|
|
* <b>You can not</b>:
|
|
|
|
|
* Modify module description, dependencies
|
|
|
|
|
* Add modules
|
|
|
|
|
* Read configuration
|
2019-08-31 18:03:33 +03:00
|
|
|
|
* @param initContext <s>null</s> on module initialization during boot or startup
|
|
|
|
|
* Not <s>null</s> during module initialization while running
|
2019-08-28 12:37:59 +03:00
|
|
|
|
*/
|
2019-08-26 12:42:04 +03:00
|
|
|
|
public abstract void init(LauncherInitContext initContext);
|
2019-08-26 09:43:53 +03:00
|
|
|
|
|
2019-08-26 12:27:30 +03:00
|
|
|
|
|
2019-08-28 12:37:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Registers an event handler for the current module
|
|
|
|
|
* @param handle your event handler
|
|
|
|
|
* @param tClass event class
|
|
|
|
|
* @param <T> event type
|
|
|
|
|
* @return true if adding a handler was successful
|
|
|
|
|
*/
|
2019-08-26 12:27:30 +03:00
|
|
|
|
protected <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)
|
2019-08-26 09:43:53 +03:00
|
|
|
|
{
|
|
|
|
|
eventMap.put(tClass, handle);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 12:37:59 +03:00
|
|
|
|
/**
|
|
|
|
|
* Call the handler of the current module
|
|
|
|
|
* @param event event handled
|
|
|
|
|
* @param <T> event type
|
|
|
|
|
*/
|
2019-08-26 10:42:49 +03:00
|
|
|
|
@SuppressWarnings("unchecked")
|
2019-08-26 13:24:19 +03:00
|
|
|
|
public final <T extends Event> void callEvent(T event)
|
2019-08-26 09:43:53 +03:00
|
|
|
|
{
|
|
|
|
|
Class<? extends Event> tClass = event.getClass();
|
|
|
|
|
for(Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(e.getKey().isAssignableFrom(tClass))
|
|
|
|
|
{
|
2019-08-26 10:42:49 +03:00
|
|
|
|
e.getValue().event(event);
|
|
|
|
|
if(event.isCancel()) return;
|
2019-08-26 09:43:53 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|