diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java new file mode 100644 index 00000000..fb50946e --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java @@ -0,0 +1,69 @@ +package pro.gravit.launcher.modules; + +import java.util.HashMap; +import java.util.Map; + +public abstract class LauncherModule { + private LauncherModulesContext context; + + private Map, EventHandler> eventMap = new HashMap<>(); + protected LauncherModulesManager modulesManager; + protected ModulesConfigManager modulesConfigManager; + InitPhase initPhase = InitPhase.CREATED; + public enum InitPhase + { + CREATED, + INIT, + FINISH + } + public enum EventAction + { + CONTINUE, + INTERRUPT + } + @FunctionalInterface + public interface EventHandler + { + EventAction event(T e) throws Exception; + } + public interface Event + { + + } + + public InitPhase getInitPhase() { + return initPhase; + } + + Map, EventHandler> setContext(LauncherModulesContext context) + { + if(this.context != null) throw new IllegalStateException("Module already set context"); + this.context = context; + this.modulesManager = context.getModulesManager(); + this.modulesConfigManager = context.getModulesConfigManager(); + return eventMap; + } + public abstract LauncherModuleInfo init(); + + boolean registerEvent(EventHandler handle, Class tClass) + { + eventMap.put(tClass, handle); + return true; + } + + EventAction callEvent(T event) throws Exception + { + Class tClass = event.getClass(); + for(Map.Entry, EventHandler> e : eventMap.entrySet()) + { + + if(e.getKey().isAssignableFrom(tClass)) + { + @SuppressWarnings("unchecked") + EventAction action = e.getValue().event(event); + if(action.equals(EventAction.INTERRUPT)) return action; + } + } + return EventAction.CONTINUE; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java new file mode 100644 index 00000000..bdec10b5 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java @@ -0,0 +1,8 @@ +package pro.gravit.launcher.modules; + +import pro.gravit.utils.Version; + +public class LauncherModuleInfo { + public String name; + public Version version; +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesContext.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesContext.java new file mode 100644 index 00000000..77de956d --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesContext.java @@ -0,0 +1,6 @@ +package pro.gravit.launcher.modules; + +public interface LauncherModulesContext { + LauncherModulesManager getModulesManager(); + ModulesConfigManager getModulesConfigManager(); +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java new file mode 100644 index 00000000..2cd8a73d --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java @@ -0,0 +1,23 @@ +package pro.gravit.launcher.modules; + +import pro.gravit.utils.Version; + +import java.util.function.Predicate; + +public interface LauncherModulesManager { + LauncherModule loadModule(LauncherModule module); + LauncherModule getModule(String name); + default boolean containsModule(String name) + { + return getModule(name) != null; + } + + default boolean containsModule(Class clazz) + { + return getModule(clazz) != null; + } + ClassLoader getModuleClassLoader(); + T getModule(Class clazz); + T findModule(Class clazz, Predicate versionPredicate); + LauncherModule.EventAction invokeEvent(T event) throws Exception; +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/InitEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/InitEvent.java new file mode 100644 index 00000000..ec67f096 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/InitEvent.java @@ -0,0 +1,6 @@ +package pro.gravit.launcher.modules.events; + +import pro.gravit.launcher.modules.LauncherModule; + +public interface InitEvent extends LauncherModule.Event { +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PostInitEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PostInitEvent.java new file mode 100644 index 00000000..f92ab1d0 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PostInitEvent.java @@ -0,0 +1,4 @@ +package pro.gravit.launcher.modules.events; + +public interface PostInitEvent { +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PreInitEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PreInitEvent.java new file mode 100644 index 00000000..2c2b2cbf --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PreInitEvent.java @@ -0,0 +1,6 @@ +package pro.gravit.launcher.modules.events; + +import pro.gravit.launcher.modules.LauncherModule; + +public interface PreInitEvent extends LauncherModule.Event { +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/WrapperEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/WrapperEvent.java new file mode 100644 index 00000000..919e7a88 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/WrapperEvent.java @@ -0,0 +1,6 @@ +package pro.gravit.launcher.modules.events; + +import pro.gravit.launcher.modules.LauncherModule; + +public interface WrapperEvent extends LauncherModule.Event { +}