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

This commit is contained in:
Gravit 2019-08-26 13:43:53 +07:00
parent 867ae334fa
commit f316b0598b
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
8 changed files with 128 additions and 0 deletions

View file

@ -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<Class<? extends Event>, 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<T extends Event>
{
EventAction event(T e) throws Exception;
}
public interface Event
{
}
public InitPhase getInitPhase() {
return initPhase;
}
Map<Class<? extends Event>, 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();
<T extends Event> boolean registerEvent(EventHandler<T> handle, Class<T> tClass)
{
eventMap.put(tClass, handle);
return true;
}
<T extends Event> EventAction callEvent(T event) throws Exception
{
Class<? extends Event> tClass = event.getClass();
for(Map.Entry<Class<? extends Event>, 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;
}
}

View file

@ -0,0 +1,8 @@
package pro.gravit.launcher.modules;
import pro.gravit.utils.Version;
public class LauncherModuleInfo {
public String name;
public Version version;
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules;
public interface LauncherModulesContext {
LauncherModulesManager getModulesManager();
ModulesConfigManager getModulesConfigManager();
}

View file

@ -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 <T extends LauncherModule> boolean containsModule(Class<? extends T> clazz)
{
return getModule(clazz) != null;
}
ClassLoader getModuleClassLoader();
<T extends LauncherModule> T getModule(Class<? extends T> clazz);
<T extends LauncherModule> T findModule(Class<? extends T> clazz, Predicate<Version> versionPredicate);
<T extends LauncherModule.Event> LauncherModule.EventAction invokeEvent(T event) throws Exception;
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
public interface InitEvent extends LauncherModule.Event {
}

View file

@ -0,0 +1,4 @@
package pro.gravit.launcher.modules.events;
public interface PostInitEvent {
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
public interface PreInitEvent extends LauncherModule.Event {
}

View file

@ -0,0 +1,6 @@
package pro.gravit.launcher.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
public interface WrapperEvent extends LauncherModule.Event {
}