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;
|
|
|
|
|
|
2019-08-31 15:44:43 +03:00
|
|
|
|
@SuppressWarnings("rawtypes")
|
2019-10-19 19:46:04 +03:00
|
|
|
|
private final Map<Class<? extends Event>, EventHandler> eventMap = new HashMap<>();
|
2019-08-26 09:43:53 +03:00
|
|
|
|
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
|
|
|
|
|
*/
|
2019-10-19 19:46:04 +03:00
|
|
|
|
public enum InitStatus {
|
2019-09-05 15:09:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* When creating an object
|
|
|
|
|
*/
|
2019-08-28 17:04:44 +03:00
|
|
|
|
CREATED(false),
|
2019-09-05 15:09:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* After setting the context
|
|
|
|
|
*/
|
2019-08-28 17:04:44 +03:00
|
|
|
|
PRE_INIT_WAIT(true),
|
2019-09-05 15:09:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* During the pre-initialization phase
|
|
|
|
|
*/
|
2019-08-28 17:04:44 +03:00
|
|
|
|
PRE_INIT(false),
|
2019-09-05 15:09:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* Awaiting initialization phase
|
|
|
|
|
*/
|
2019-08-28 17:04:44 +03:00
|
|
|
|
INIT_WAIT(true),
|
2019-09-05 15:09:28 +03:00
|
|
|
|
/**
|
|
|
|
|
* During the initialization phase
|
|
|
|
|
*/
|
2019-08-28 17:04:44 +03:00
|
|
|
|
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
|
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
|
|
2019-08-26 09:43:53 +03:00
|
|
|
|
@FunctionalInterface
|
2019-10-19 19:46:04 +03:00
|
|
|
|
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-10-19 19:46:04 +03:00
|
|
|
|
|
|
|
|
|
public static class Event {
|
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
|
2019-10-19 19:46:04 +03:00
|
|
|
|
*
|
2019-08-28 12:37:59 +03:00
|
|
|
|
* @param context Private context
|
|
|
|
|
*/
|
2019-10-19 19:46:04 +03:00
|
|
|
|
public void setContext(LauncherModulesContext context) {
|
|
|
|
|
if (this.context != null) throw new IllegalStateException("Module already set context");
|
2019-08-26 09:43:53 +03:00
|
|
|
|
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>:
|
2019-09-05 15:09:28 +03:00
|
|
|
|
* - Use to Module Manager
|
|
|
|
|
* - Add custom modules not described in the manifest
|
|
|
|
|
* - Change information about your module or modules you control
|
2019-08-28 12:37:59 +03:00
|
|
|
|
* <b>You can not</b>:
|
2019-09-05 15:09:28 +03:00
|
|
|
|
* - Use your dependencies
|
|
|
|
|
* - Use Launcher, LaunchServer, ServerWrapper API
|
|
|
|
|
* - Change the names of any modules
|
2019-08-28 12:37:59 +03:00
|
|
|
|
*/
|
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-10-19 19:46:04 +03:00
|
|
|
|
|
|
|
|
|
public LauncherModule preInit() {
|
|
|
|
|
if (!initStatus.equals(InitStatus.PRE_INIT_WAIT))
|
|
|
|
|
throw new IllegalStateException("PreInit not allowed in current state");
|
2019-08-28 17:04:44 +03:00
|
|
|
|
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>:
|
2019-09-05 15:09:28 +03:00
|
|
|
|
* - Subscribe to events
|
|
|
|
|
* - Use your dependencies
|
|
|
|
|
* - Use provided initContext
|
|
|
|
|
* - Receive modules and access the module’s internal methods
|
2019-08-28 12:37:59 +03:00
|
|
|
|
* <b>You can not</b>:
|
2019-09-05 15:09:28 +03:00
|
|
|
|
* - Modify module description, dependencies
|
|
|
|
|
* - Add modules
|
|
|
|
|
* - Read configuration
|
2019-10-19 19:46:04 +03:00
|
|
|
|
*
|
2019-08-31 18:15:42 +03:00
|
|
|
|
* @param initContext <b>null</b> on module initialization during boot or startup
|
2019-10-19 19:46:04 +03:00
|
|
|
|
* Not <b>null</b> 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
|
2019-10-19 19:46:04 +03:00
|
|
|
|
*
|
2019-08-28 12:37:59 +03:00
|
|
|
|
* @param handle your event handler
|
|
|
|
|
* @param tClass event class
|
2019-10-19 19:46:04 +03:00
|
|
|
|
* @param <T> event type
|
2019-08-28 12:37:59 +03:00
|
|
|
|
* @return true if adding a handler was successful
|
|
|
|
|
*/
|
2019-10-19 19:46:04 +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
|
2019-10-19 19:46:04 +03:00
|
|
|
|
*
|
2019-08-28 12:37:59 +03:00
|
|
|
|
* @param event event handled
|
2019-10-19 19:46:04 +03:00
|
|
|
|
* @param <T> event type
|
2019-08-28 12:37:59 +03:00
|
|
|
|
*/
|
2019-08-26 10:42:49 +03:00
|
|
|
|
@SuppressWarnings("unchecked")
|
2019-10-19 19:46:04 +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();
|
2019-10-19 19:46:04 +03:00
|
|
|
|
for (@SuppressWarnings("rawtypes") Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet()) {
|
2019-08-26 09:43:53 +03:00
|
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
|
if (e.getKey().isAssignableFrom(tClass)) {
|
2019-08-26 10:42:49 +03:00
|
|
|
|
e.getValue().event(event);
|
2019-10-19 19:46:04 +03:00
|
|
|
|
if (event.isCancel()) return;
|
2019-08-26 09:43:53 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|