mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
[FEATURE][EXPERIMENTAL] Заготовки новой системы модулей
This commit is contained in:
parent
867ae334fa
commit
f316b0598b
8 changed files with 128 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package pro.gravit.launcher.modules;
|
||||
|
||||
import pro.gravit.utils.Version;
|
||||
|
||||
public class LauncherModuleInfo {
|
||||
public String name;
|
||||
public Version version;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package pro.gravit.launcher.modules;
|
||||
|
||||
public interface LauncherModulesContext {
|
||||
LauncherModulesManager getModulesManager();
|
||||
ModulesConfigManager getModulesConfigManager();
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package pro.gravit.launcher.modules.events;
|
||||
|
||||
import pro.gravit.launcher.modules.LauncherModule;
|
||||
|
||||
public interface InitEvent extends LauncherModule.Event {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package pro.gravit.launcher.modules.events;
|
||||
|
||||
public interface PostInitEvent {
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package pro.gravit.launcher.modules.events;
|
||||
|
||||
import pro.gravit.launcher.modules.LauncherModule;
|
||||
|
||||
public interface PreInitEvent extends LauncherModule.Event {
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package pro.gravit.launcher.modules.events;
|
||||
|
||||
import pro.gravit.launcher.modules.LauncherModule;
|
||||
|
||||
public interface WrapperEvent extends LauncherModule.Event {
|
||||
}
|
Loading…
Reference in a new issue