From f316b0598b73500ec86a91e60e1d5e2b9b3cb7f6 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 26 Aug 2019 13:43:53 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE][EXPERIMENTAL]=20=D0=97=D0=B0=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=BD=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D0=B9=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D1=8B=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launcher/modules/LauncherModule.java | 69 +++++++++++++++++++ .../launcher/modules/LauncherModuleInfo.java | 8 +++ .../modules/LauncherModulesContext.java | 6 ++ .../modules/LauncherModulesManager.java | 23 +++++++ .../launcher/modules/events/InitEvent.java | 6 ++ .../modules/events/PostInitEvent.java | 4 ++ .../launcher/modules/events/PreInitEvent.java | 6 ++ .../launcher/modules/events/WrapperEvent.java | 6 ++ 8 files changed, 128 insertions(+) create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModule.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModuleInfo.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesContext.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/LauncherModulesManager.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/InitEvent.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PostInitEvent.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/PreInitEvent.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/modules/events/WrapperEvent.java 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 { +}