[FEATURE][EXPERIMENTAL] Зависимости модулей

This commit is contained in:
Gravit 2019-08-26 14:42:49 +07:00
parent f316b0598b
commit efa1bf0dd7
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 56 additions and 16 deletions

View file

@ -8,27 +8,37 @@ public abstract class LauncherModule {
private Map<Class<? extends Event>, EventHandler> eventMap = new HashMap<>(); private Map<Class<? extends Event>, EventHandler> eventMap = new HashMap<>();
protected LauncherModulesManager modulesManager; protected LauncherModulesManager modulesManager;
protected final LauncherModuleInfo moduleInfo;
protected ModulesConfigManager modulesConfigManager; protected ModulesConfigManager modulesConfigManager;
InitPhase initPhase = InitPhase.CREATED; InitPhase initPhase = InitPhase.CREATED;
protected LauncherModule() {
moduleInfo = new LauncherModuleInfo("UnknownModule");
}
public enum InitPhase public enum InitPhase
{ {
CREATED, CREATED,
INIT, INIT,
FINISH FINISH
} }
public enum EventAction
{
CONTINUE,
INTERRUPT
}
@FunctionalInterface @FunctionalInterface
public interface EventHandler<T extends Event> public interface EventHandler<T extends Event>
{ {
EventAction event(T e) throws Exception; void event(T e) throws Exception;
} }
public interface Event public static class Event
{ {
public boolean isCancel() {
return cancel;
}
public Event cancel() {
this.cancel = true;
return this;
}
protected boolean cancel;
} }
public InitPhase getInitPhase() { public InitPhase getInitPhase() {
@ -43,7 +53,8 @@ Map<Class<? extends Event>, EventHandler> setContext(LauncherModulesContext cont
this.modulesConfigManager = context.getModulesConfigManager(); this.modulesConfigManager = context.getModulesConfigManager();
return eventMap; return eventMap;
} }
public abstract LauncherModuleInfo init();
public abstract void init();
<T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass) <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)
{ {
@ -51,7 +62,8 @@ <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)
return true; return true;
} }
<T extends Event> EventAction callEvent(T event) throws Exception @SuppressWarnings("unchecked")
<T extends Event> void callEvent(T event) throws Exception
{ {
Class<? extends Event> tClass = event.getClass(); Class<? extends Event> tClass = event.getClass();
for(Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet()) for(Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet())
@ -59,11 +71,9 @@ <T extends Event> EventAction callEvent(T event) throws Exception
if(e.getKey().isAssignableFrom(tClass)) if(e.getKey().isAssignableFrom(tClass))
{ {
@SuppressWarnings("unchecked") e.getValue().event(event);
EventAction action = e.getValue().event(event); if(event.isCancel()) return;
if(action.equals(EventAction.INTERRUPT)) return action;
} }
} }
return EventAction.CONTINUE;
} }
} }

View file

@ -3,6 +3,36 @@
import pro.gravit.utils.Version; import pro.gravit.utils.Version;
public class LauncherModuleInfo { public class LauncherModuleInfo {
public String name; public final String name;
public Version version; public final Version version;
public final int priority;
public final String[] dependencies;
public LauncherModuleInfo(String name, Version version) {
this.name = name;
this.version = version;
this.priority = 0;
this.dependencies = new String[]{};
}
public LauncherModuleInfo(String name) {
this.name = name;
this.version = new Version(1,0,0);
this.priority = 0;
this.dependencies = new String[]{};
}
public LauncherModuleInfo(String name, Version version, String[] dependencies) {
this.name = name;
this.version = version;
this.priority = 0;
this.dependencies = dependencies;
}
public LauncherModuleInfo(String name, Version version, int priority, String[] dependencies) {
this.name = name;
this.version = version;
this.priority = priority;
this.dependencies = dependencies;
}
} }

View file

@ -19,5 +19,5 @@ default <T extends LauncherModule> boolean containsModule(Class<? extends T> cl
ClassLoader getModuleClassLoader(); ClassLoader getModuleClassLoader();
<T extends LauncherModule> T getModule(Class<? extends T> clazz); <T extends LauncherModule> T getModule(Class<? extends T> clazz);
<T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate); <T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate);
<T extends LauncherModule.Event> LauncherModule.EventAction invokeEvent(T event) throws Exception; <T extends LauncherModule.Event> void invokeEvent(T event) throws Exception;
} }