From d65d858bc9c00ac18d3a7a20263b684f0bd29484 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 26 Aug 2019 18:22:24 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE][EXPERIMENTAL]=20=D0=9D=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=D0=B9=20=D0=B2=20ServerWra?= =?UTF-8?q?pper=20=D0=B8=20ClientLauncher/LauncherEngine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launchserver/LaunchServerStarter.java | 12 +++---- .../pro/gravit/launcher/LauncherEngine.java | 27 ++++++++++---- .../launcher/client/ClientLauncher.java | 19 ++++++---- .../launcher/client/ClientModuleContext.java | 30 ---------------- .../launcher/client/ClientModuleManager.java | 27 +++++++------- .../client/events/ClientEngineInitPhase.java | 12 +++++++ .../client/events/ClientGuiPhase.java | 12 +++++++ .../events/ClientLauncherInitPhase.java | 6 ++++ .../client/events/ClientPreGuiPhase.java | 12 +++++++ .../launcher/gui/JSRuntimeProvider.java | 12 +++---- .../launcher/managers/ClientGsonManager.java | 9 +++++ .../java/pro/gravit/launcher/Launcher.java | 2 -- .../managers/SimpleModuleManager.java | 2 +- .../pro/gravit/launcher/modules/Module.java | 2 +- .../launcher/modules/ModuleContext.java | 2 +- .../launcher/modules/ModulesManager.java | 2 +- .../launcher/server/ModulesManager.java | 20 ----------- .../launcher/server/ServerModuleContext.java | 35 ------------------- .../gravit/launcher/server/ServerWrapper.java | 23 ++++++------ .../server/ServerWrapperGsonManager.java | 8 +++++ .../server/ServerWrapperInitContext.java | 11 ++++++ .../server/ServerWrapperInitPhase.java | 11 ++++++ .../server/ServerWrapperModulesManager.java | 15 ++++++++ .../setup/ServerWrapperPostSetupEvent.java | 11 ++++++ .../setup/ServerWrapperPreSetupEvent.java | 11 ++++++ .../server/setup/ServerWrapperSetup.java | 4 ++- .../server/setup/ServerWrapperSetupEvent.java | 11 ++++++ 27 files changed, 207 insertions(+), 141 deletions(-) delete mode 100644 Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleContext.java create mode 100644 Launcher/src/main/java/pro/gravit/launcher/client/events/ClientEngineInitPhase.java create mode 100644 Launcher/src/main/java/pro/gravit/launcher/client/events/ClientGuiPhase.java create mode 100644 Launcher/src/main/java/pro/gravit/launcher/client/events/ClientLauncherInitPhase.java create mode 100644 Launcher/src/main/java/pro/gravit/launcher/client/events/ClientPreGuiPhase.java delete mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/ModulesManager.java delete mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerModuleContext.java create mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitContext.java create mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitPhase.java create mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperModulesManager.java create mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPostSetupEvent.java create mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPreSetupEvent.java create mode 100644 ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetupEvent.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java index ab1e48a1..5e0f9898 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java @@ -100,6 +100,12 @@ public static void main(String[] args) throws Exception { IOHelper.write(publicKeyFile, publicKey.getEncoded()); IOHelper.write(privateKeyFile, privateKey.getEncoded()); } + modulesManager.invokeEvent(new PreConfigPhase()); + generateConfigIfNotExists(configFile, localCommandHandler, env); + LogHelper.info("Reading LaunchServer config file"); + try (BufferedReader reader = IOHelper.newReader(configFile)) { + config = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class); + } if (!Files.exists(runtimeConfigFile)) { LogHelper.info("Reset LaunchServer runtime config file"); runtimeConfig = new LaunchServerRuntimeConfig(); @@ -110,12 +116,6 @@ public static void main(String[] args) throws Exception { runtimeConfig = Launcher.gsonManager.gson.fromJson(reader, LaunchServerRuntimeConfig.class); } } - modulesManager.invokeEvent(new PreConfigPhase()); - generateConfigIfNotExists(configFile, localCommandHandler, env); - LogHelper.info("Reading LaunchServer config file"); - try (BufferedReader reader = IOHelper.newReader(configFile)) { - config = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class); - } LaunchServer.LaunchServerConfigManager launchServerConfigManager = new LaunchServer.LaunchServerConfigManager() { @Override diff --git a/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java index df2edcb3..c48bf6dd 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java @@ -7,6 +7,8 @@ import pro.gravit.launcher.client.DirBridge; import pro.gravit.launcher.client.FunctionalBridge; import pro.gravit.launcher.client.LauncherUpdateController; +import pro.gravit.launcher.client.events.ClientEngineInitPhase; +import pro.gravit.launcher.client.events.ClientPreGuiPhase; import pro.gravit.launcher.guard.LauncherGuardManager; import pro.gravit.launcher.gui.JSRuntimeProvider; import pro.gravit.launcher.gui.RuntimeProvider; @@ -14,6 +16,9 @@ import pro.gravit.launcher.managers.ClientGsonManager; import pro.gravit.launcher.managers.ClientHookManager; import pro.gravit.launcher.managers.ConsoleManager; +import pro.gravit.launcher.modules.LauncherModulesManager; +import pro.gravit.launcher.modules.events.PreConfigPhase; +import pro.gravit.launcher.modules.impl.SimpleModuleManager; import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.RequestException; import pro.gravit.launcher.request.auth.RestoreSessionRequest; @@ -33,10 +38,15 @@ public static void main(String... args) throws Throwable { //if(!LauncherAgent.isStarted()) throw new SecurityException("JavaAgent not set"); LogHelper.printVersion("Launcher"); LogHelper.printLicense("Launcher"); + + LauncherEngine.modulesManager = new ClientModuleManager(); + LauncherConfig.getAutogenConfig().initModules(); + LauncherEngine.modulesManager.initModules(null); // Start Launcher - initGson(); + initGson(LauncherEngine.modulesManager); ConsoleManager.initConsole(); HWIDProvider.registerHWIDs(); + LauncherEngine.modulesManager.invokeEvent(new PreConfigPhase()); LauncherConfig config = Launcher.getConfig(); if (config.environment.equals(LauncherConfig.LauncherEnvironment.PROD)) { if (!LauncherAgent.isStarted()) throw new SecurityException("LauncherAgent must started"); @@ -55,8 +65,8 @@ public static void main(String... args) throws Throwable { System.exit(0); } - public static void initGson() { - Launcher.gsonManager = new ClientGsonManager(); + public static void initGson(ClientModuleManager modulesManager) { + Launcher.gsonManager = new ClientGsonManager(modulesManager); Launcher.gsonManager.initGson(); } @@ -64,6 +74,8 @@ public static void initGson() { private final AtomicBoolean started = new AtomicBoolean(false); public RuntimeProvider runtimeProvider; + public static ClientModuleManager modulesManager; + private LauncherEngine() { } @@ -71,9 +83,10 @@ private LauncherEngine() { @LauncherAPI public void start(String... args) throws Throwable { - Launcher.modulesManager = new ClientModuleManager(this); - LauncherConfig.getAutogenConfig().initModules(); - Launcher.modulesManager.preInitModules(); + //Launcher.modulesManager = new ClientModuleManager(this); + ClientPreGuiPhase event = new ClientPreGuiPhase(null); + LauncherEngine.modulesManager.invokeEvent(event); + runtimeProvider = event.runtimeProvider; if (runtimeProvider == null) runtimeProvider = new JSRuntimeProvider(); ClientHookManager.initGuiHook.hook(runtimeProvider); runtimeProvider.init(false); @@ -105,7 +118,7 @@ public void start(String... args) throws Throwable { Objects.requireNonNull(args, "args"); if (started.getAndSet(true)) throw new IllegalStateException("Launcher has been already started"); - Launcher.modulesManager.initModules(); + LauncherEngine.modulesManager.invokeEvent(new ClientEngineInitPhase(this)); runtimeProvider.preLoad(); FunctionalBridge.getHWID = CommonHelper.newThread("GetHWID Thread", true, FunctionalBridge::getHWID); FunctionalBridge.getHWID.start(); diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java index b0be3c91..6005399f 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java +++ b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncher.java @@ -32,6 +32,7 @@ import pro.gravit.launcher.LauncherAgent; import pro.gravit.launcher.LauncherConfig; import pro.gravit.launcher.LauncherEngine; +import pro.gravit.launcher.client.events.ClientLauncherInitPhase; import pro.gravit.launcher.guard.LauncherGuardManager; import pro.gravit.launcher.gui.JSRuntimeProvider; import pro.gravit.launcher.hasher.FileNameMatcher; @@ -39,6 +40,8 @@ import pro.gravit.launcher.hwid.HWIDProvider; import pro.gravit.launcher.managers.ClientGsonManager; import pro.gravit.launcher.managers.ClientHookManager; +import pro.gravit.launcher.modules.events.PostInitPhase; +import pro.gravit.launcher.modules.events.PreConfigPhase; import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.PlayerProfile; import pro.gravit.launcher.request.Request; @@ -435,10 +438,12 @@ public static Process launch( @LauncherAPI public static void main(String... args) throws Throwable { LauncherEngine engine = LauncherEngine.clientInstance(); - Launcher.modulesManager = new ClientModuleManager(engine); + //Launcher.modulesManager = new ClientModuleManager(engine); + LauncherEngine.modulesManager = new ClientModuleManager(); LauncherConfig.getAutogenConfig().initModules(); //INIT - initGson(); - Launcher.modulesManager.preInitModules(); + initGson(LauncherEngine.modulesManager); + //Launcher.modulesManager.preInitModules(); + LauncherEngine.modulesManager.invokeEvent(new PreConfigPhase()); JVMHelper.verifySystemProperties(ClientLauncher.class, true); EnvHelper.checkDangerousParams(); JVMHelper.checkStackTrace(ClientLauncher.class); @@ -472,7 +477,7 @@ public static void main(String... args) throws Throwable { playerProfile = params.pp; Request.setSession(params.session); checkJVMBitsAndVersion(); - Launcher.modulesManager.initModules(); + LauncherEngine.modulesManager.invokeEvent(new ClientLauncherInitPhase()); // Verify ClientLauncher sign and classpath LogHelper.debug("Verifying ClientLauncher sign and classpath"); LinkedList classPath = resolveClassPathList(params.clientDir, profile.getClassPath()); @@ -524,7 +529,7 @@ public static void main(String... args) throws Throwable { // else hdir.removeR(s.file); //} Launcher.profile.pushOptionalFile(clientHDir, false); - Launcher.modulesManager.postInitModules(); + LauncherEngine.modulesManager.invokeEvent(new PostInitPhase()); // Start WatchService, and only then client CommonHelper.newThread("Asset Directory Watcher", true, assetWatcher).start(); CommonHelper.newThread("Client Directory Watcher", true, clientWatcher).start(); @@ -555,8 +560,8 @@ private static Stream resolveClassPathStream(Path clientDir, String... cla return builder.build(); } - private static void initGson() { - Launcher.gsonManager = new ClientGsonManager(); + private static void initGson(ClientModuleManager moduleManager) { + Launcher.gsonManager = new ClientGsonManager(moduleManager); Launcher.gsonManager.initGson(); } diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleContext.java b/Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleContext.java deleted file mode 100644 index b1d92836..00000000 --- a/Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleContext.java +++ /dev/null @@ -1,30 +0,0 @@ -package pro.gravit.launcher.client; - -import pro.gravit.launcher.Launcher; -import pro.gravit.launcher.LauncherEngine; -import pro.gravit.launcher.modules.ModuleContext; -import pro.gravit.launcher.modules.ModulesConfigManager; -import pro.gravit.launcher.modules.ModulesManager; - -public class ClientModuleContext implements ModuleContext { - public final LauncherEngine engine; - - ClientModuleContext(LauncherEngine engine) { - this.engine = engine; - } - - @Override - public Type getType() { - return Type.CLIENT; - } - - @Override - public ModulesManager getModulesManager() { - return Launcher.modulesManager; - } - - @Override - public ModulesConfigManager getModulesConfigManager() { - return null; // ClientModuleContext не поддерживает modulesConfigManager - } -} diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleManager.java b/Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleManager.java index 68f8f60b..ee48f182 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleManager.java +++ b/Launcher/src/main/java/pro/gravit/launcher/client/ClientModuleManager.java @@ -1,25 +1,28 @@ package pro.gravit.launcher.client; -import java.net.URL; -import java.util.ArrayList; +import pro.gravit.launcher.modules.LauncherModule; +import pro.gravit.launcher.modules.impl.SimpleModuleManager; -import pro.gravit.launcher.LauncherEngine; -import pro.gravit.launcher.managers.SimpleModuleManager; +import java.io.IOException; +import java.nio.file.Path; public class ClientModuleManager extends SimpleModuleManager { - - public ClientModuleManager(LauncherEngine engine) { - context = new ClientModuleContext(engine); - modules = new ArrayList<>(); + public ClientModuleManager() { + super(null, null); } @Override - public void loadModule(URL jarpath, String classname) { - throw new SecurityException("Custom JAR's load not allowed here"); + public void autoload() throws IOException { + throw new UnsupportedOperationException(); } @Override - public void loadModuleFull(URL jarpath) { - throw new SecurityException("Custom JAR's load not allowed here"); + public void autoload(Path dir) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public LauncherModule loadModule(Path file) throws IOException { + throw new UnsupportedOperationException(); } } diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientEngineInitPhase.java b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientEngineInitPhase.java new file mode 100644 index 00000000..b877971f --- /dev/null +++ b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientEngineInitPhase.java @@ -0,0 +1,12 @@ +package pro.gravit.launcher.client.events; + +import pro.gravit.launcher.LauncherEngine; +import pro.gravit.launcher.modules.events.InitPhase; + +public class ClientEngineInitPhase extends InitPhase { + public final LauncherEngine engine; + + public ClientEngineInitPhase(LauncherEngine engine) { + this.engine = engine; + } +} diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientGuiPhase.java b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientGuiPhase.java new file mode 100644 index 00000000..2e70f857 --- /dev/null +++ b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientGuiPhase.java @@ -0,0 +1,12 @@ +package pro.gravit.launcher.client.events; + +import pro.gravit.launcher.gui.RuntimeProvider; +import pro.gravit.launcher.modules.LauncherModule; + +public class ClientGuiPhase extends LauncherModule.Event { + public final RuntimeProvider runtimeProvider; + + public ClientGuiPhase(RuntimeProvider runtimeProvider) { + this.runtimeProvider = runtimeProvider; + } +} diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientLauncherInitPhase.java b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientLauncherInitPhase.java new file mode 100644 index 00000000..4f10e732 --- /dev/null +++ b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientLauncherInitPhase.java @@ -0,0 +1,6 @@ +package pro.gravit.launcher.client.events; + +import pro.gravit.launcher.modules.events.InitPhase; + +public class ClientLauncherInitPhase extends InitPhase { +} diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientPreGuiPhase.java b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientPreGuiPhase.java new file mode 100644 index 00000000..ebbad380 --- /dev/null +++ b/Launcher/src/main/java/pro/gravit/launcher/client/events/ClientPreGuiPhase.java @@ -0,0 +1,12 @@ +package pro.gravit.launcher.client.events; + +import pro.gravit.launcher.gui.RuntimeProvider; +import pro.gravit.launcher.modules.LauncherModule; + +public class ClientPreGuiPhase extends LauncherModule.Event { + public RuntimeProvider runtimeProvider; + + public ClientPreGuiPhase(RuntimeProvider runtimeProvider) { + this.runtimeProvider = runtimeProvider; + } +} diff --git a/Launcher/src/main/java/pro/gravit/launcher/gui/JSRuntimeProvider.java b/Launcher/src/main/java/pro/gravit/launcher/gui/JSRuntimeProvider.java index 95e65a6b..1248415c 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/gui/JSRuntimeProvider.java +++ b/Launcher/src/main/java/pro/gravit/launcher/gui/JSRuntimeProvider.java @@ -11,16 +11,13 @@ import javax.script.ScriptEngine; import javax.script.ScriptException; -import pro.gravit.launcher.JSApplication; -import pro.gravit.launcher.Launcher; -import pro.gravit.launcher.LauncherAPI; -import pro.gravit.launcher.LauncherConfig; -import pro.gravit.launcher.NewLauncherSettings; +import pro.gravit.launcher.*; import pro.gravit.launcher.client.ClientLauncher; import pro.gravit.launcher.client.DirBridge; import pro.gravit.launcher.client.FunctionalBridge; import pro.gravit.launcher.client.ServerPinger; import pro.gravit.launcher.client.UserSettings; +import pro.gravit.launcher.client.events.ClientGuiPhase; import pro.gravit.launcher.hasher.FileNameMatcher; import pro.gravit.launcher.hasher.HashedDir; import pro.gravit.launcher.hasher.HashedEntry; @@ -28,6 +25,7 @@ import pro.gravit.launcher.hwid.NoHWID; import pro.gravit.launcher.hwid.OshiHWID; import pro.gravit.launcher.managers.SettingsManager; +import pro.gravit.launcher.modules.events.ClosePhase; import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.PlayerProfile; import pro.gravit.launcher.profiles.Texture; @@ -163,9 +161,9 @@ public void run(String[] args) throws ScriptException, NoSuchMethodException, IO preLoad(); loadScript(Launcher.INIT_SCRIPT_FILE); LogHelper.info("Invoking start() function"); - Launcher.modulesManager.postInitModules(); + LauncherEngine.modulesManager.invokeEvent(new ClientGuiPhase(this)); ((Invocable) engine).invokeFunction("start", (Object) args); - Launcher.modulesManager.finishModules(); + LauncherEngine.modulesManager.invokeEvent(new ClosePhase()); } @Override diff --git a/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java b/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java index 7e788093..2a16f9c3 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java +++ b/Launcher/src/main/java/pro/gravit/launcher/managers/ClientGsonManager.java @@ -2,18 +2,27 @@ import com.google.gson.GsonBuilder; +import pro.gravit.launcher.client.ClientModuleManager; import pro.gravit.launcher.client.UserSettings; import pro.gravit.launcher.hwid.HWID; import pro.gravit.launcher.hwid.HWIDProvider; +import pro.gravit.launcher.modules.events.PreGsonPhase; import pro.gravit.launcher.request.websockets.ClientWebSocketService; import pro.gravit.utils.UniversalJsonAdapter; public class ClientGsonManager extends GsonManager { + private final ClientModuleManager moduleManager; + + public ClientGsonManager(ClientModuleManager moduleManager) { + this.moduleManager = moduleManager; + } + @Override public void registerAdapters(GsonBuilder builder) { super.registerAdapters(builder); builder.registerTypeAdapter(UserSettings.class, new UniversalJsonAdapter<>(UserSettings.providers)); builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids)); ClientWebSocketService.appendTypeAdapters(builder); + moduleManager.invokeEvent(new PreGsonPhase(builder)); } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/Launcher.java b/LauncherAPI/src/main/java/pro/gravit/launcher/Launcher.java index 50593619..a3737e66 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/Launcher.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/Launcher.java @@ -34,8 +34,6 @@ public final class Launcher { private static final AtomicReference CONFIG = new AtomicReference<>(); @LauncherAPI - public static ModulesManager modulesManager = null; - @LauncherAPI public static final int PROTOCOL_MAGIC_LEGACY = 0x724724_00 + 24; @LauncherAPI public static final int PROTOCOL_MAGIC = 0xA205B064; // e = 2.718281828 diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java index 70673128..e66d88ee 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/managers/SimpleModuleManager.java @@ -18,7 +18,7 @@ import pro.gravit.utils.PublicURLClassLoader; import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.LogHelper; - +@Deprecated public class SimpleModuleManager implements ModulesManager { protected final class ModulesVisitor extends SimpleFileVisitor { private ModulesVisitor() { diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/Module.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/Module.java index 3f327ef5..5dd082f1 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/Module.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/Module.java @@ -1,7 +1,7 @@ package pro.gravit.launcher.modules; import pro.gravit.utils.Version; - +@Deprecated public interface Module extends AutoCloseable { String getName(); diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModuleContext.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModuleContext.java index 6b0c0613..c435c99b 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModuleContext.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModuleContext.java @@ -1,5 +1,5 @@ package pro.gravit.launcher.modules; - +@Deprecated public interface ModuleContext { enum Type { SERVER, CLIENT, LAUNCHSERVER diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesManager.java b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesManager.java index 54f8150a..5321dee6 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesManager.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/modules/ModulesManager.java @@ -1,7 +1,7 @@ package pro.gravit.launcher.modules; import java.net.URL; - +@Deprecated public interface ModulesManager extends AutoCloseable { void initModules(); diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ModulesManager.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ModulesManager.java deleted file mode 100644 index b0a2ca96..00000000 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ModulesManager.java +++ /dev/null @@ -1,20 +0,0 @@ -package pro.gravit.launcher.server; - -import java.net.URL; -import java.nio.file.Paths; -import java.util.ArrayList; - -import pro.gravit.launcher.managers.SimpleModuleManager; -import pro.gravit.launcher.managers.SimpleModulesConfigManager; -import pro.gravit.utils.PublicURLClassLoader; - -public class ModulesManager extends SimpleModuleManager { - public SimpleModulesConfigManager modulesConfigManager; - - public ModulesManager(ServerWrapper wrapper) { - modules = new ArrayList<>(); - modulesConfigManager = new SimpleModulesConfigManager(Paths.get("modules-config")); - classloader = new PublicURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader()); - context = new ServerModuleContext(wrapper, classloader, modulesConfigManager); - } -} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerModuleContext.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerModuleContext.java deleted file mode 100644 index c8065589..00000000 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerModuleContext.java +++ /dev/null @@ -1,35 +0,0 @@ -package pro.gravit.launcher.server; - -import pro.gravit.launcher.Launcher; -import pro.gravit.launcher.managers.SimpleModulesConfigManager; -import pro.gravit.launcher.modules.ModuleContext; -import pro.gravit.launcher.modules.ModulesConfigManager; -import pro.gravit.launcher.modules.ModulesManager; -import pro.gravit.utils.PublicURLClassLoader; - -public class ServerModuleContext implements ModuleContext { - public final PublicURLClassLoader classLoader; - public final ServerWrapper wrapper; - public final SimpleModulesConfigManager modulesConfigManager; - - public ServerModuleContext(ServerWrapper wrapper, PublicURLClassLoader classLoader, SimpleModulesConfigManager modulesConfigManager) { - this.classLoader = classLoader; - this.wrapper = wrapper; - this.modulesConfigManager = modulesConfigManager; - } - - @Override - public Type getType() { - return Type.SERVER; - } - - @Override - public ModulesManager getModulesManager() { - return Launcher.modulesManager; - } - - @Override - public ModulesConfigManager getModulesConfigManager() { - return modulesConfigManager; - } -} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java index 8c7bba1c..3949e2ad 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java @@ -17,6 +17,8 @@ import pro.gravit.launcher.LauncherConfig; import pro.gravit.launcher.config.JsonConfigurable; import pro.gravit.launcher.events.request.ProfilesRequestEvent; +import pro.gravit.launcher.modules.events.PostInitPhase; +import pro.gravit.launcher.modules.events.PreConfigPhase; import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.RequestException; @@ -30,7 +32,7 @@ import pro.gravit.utils.helper.SecurityHelper; public class ServerWrapper extends JsonConfigurable { - public ModulesManager modulesManager; + public static ServerWrapperModulesManager modulesManager; public Config config; public PublicURLClassLoader ucp; public ClassLoader loader; @@ -38,6 +40,7 @@ public class ServerWrapper extends JsonConfigurable { public static ServerWrapper wrapper; public static Path modulesDir = Paths.get(System.getProperty("serverwrapper.modulesDir", "modules")); + public static Path modulesConfigDir = Paths.get(System.getProperty("serverwrapper.modulesConfigDir", "modules-config")); public static Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json")); public static Path publicKeyFile = Paths.get(System.getProperty("serverwrapper.publicKeyFile", "public.key")); public static boolean disableSetup = Boolean.valueOf(System.getProperty("serverwrapper.disableSetup", "false")); @@ -93,13 +96,13 @@ public boolean loopAuth(int count, int sleeptime) { return false; } - public static void initGson() { - Launcher.gsonManager = new ServerWrapperGsonManager(); + public static void initGson(ServerWrapperModulesManager modulesManager) { + Launcher.gsonManager = new ServerWrapperGsonManager(modulesManager); Launcher.gsonManager.initGson(); } public void run(String... args) throws Throwable { - initGson(); + initGson(modulesManager); if (args.length > 0 && args[0].equals("setup") && !disableSetup) { LogHelper.debug("Read ServerWrapperConfig.json"); loadConfig(); @@ -107,10 +110,7 @@ public void run(String... args) throws Throwable { setup.run(); System.exit(0); } - modulesManager = new ModulesManager(wrapper); - modulesManager.autoload(modulesDir); - Launcher.modulesManager = modulesManager; - modulesManager.preInitModules(); + modulesManager.invokeEvent(new PreConfigPhase()); LogHelper.debug("Read ServerWrapperConfig.json"); loadConfig(); updateLauncherConfig(); @@ -120,7 +120,7 @@ public void run(String... args) throws Throwable { if (config.syncAuth) auth(); else CommonHelper.newThread("Server Auth Thread", true, () -> loopAuth(config.reconnectCount, config.reconnectSleep)); - modulesManager.initModules(); + modulesManager.invokeEvent(new ServerWrapperInitPhase(this)); String classname = (config.mainclass == null || config.mainclass.isEmpty()) ? args[0] : config.mainclass; if (classname.length() == 0) { LogHelper.error("MainClass not found. Please set MainClass for ServerWrapper.cfg or first commandline argument"); @@ -156,7 +156,7 @@ public void run(String... args) throws Throwable { if (loader != null) mainClass = Class.forName(classname, true, loader); else mainClass = Class.forName(classname); MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class)); - modulesManager.postInitModules(); + modulesManager.invokeEvent(new PostInitPhase()); Request.service.reconnectCallback = () -> { LogHelper.debug("WebSocket connect closed. Try reconnect"); @@ -202,6 +202,9 @@ public void updateLauncherConfig() { public static void main(String... args) throws Throwable { LogHelper.printVersion("ServerWrapper"); LogHelper.printLicense("ServerWrapper"); + modulesManager = new ServerWrapperModulesManager(modulesDir, modulesConfigDir); + modulesManager.autoload(); + modulesManager.initModules(null); ServerWrapper.wrapper = new ServerWrapper(ServerWrapper.Config.class, configFile); ServerWrapper.wrapper.run(args); } diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java index b3ee9806..a40c0aa1 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperGsonManager.java @@ -3,12 +3,20 @@ import com.google.gson.GsonBuilder; import pro.gravit.launcher.managers.GsonManager; +import pro.gravit.launcher.modules.events.PreGsonPhase; import pro.gravit.launcher.request.websockets.ClientWebSocketService; public class ServerWrapperGsonManager extends GsonManager { + private final ServerWrapperModulesManager modulesManager; + + public ServerWrapperGsonManager(ServerWrapperModulesManager modulesManager) { + this.modulesManager = modulesManager; + } + @Override public void registerAdapters(GsonBuilder builder) { super.registerAdapters(builder); ClientWebSocketService.appendTypeAdapters(builder); + modulesManager.invokeEvent(new PreGsonPhase(builder)); } } diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitContext.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitContext.java new file mode 100644 index 00000000..7f6fa83b --- /dev/null +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitContext.java @@ -0,0 +1,11 @@ +package pro.gravit.launcher.server; + +import pro.gravit.launcher.modules.LauncherInitContext; + +public class ServerWrapperInitContext implements LauncherInitContext { + public final ServerWrapper serverWrapper; + + public ServerWrapperInitContext(ServerWrapper serverWrapper) { + this.serverWrapper = serverWrapper; + } +} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitPhase.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitPhase.java new file mode 100644 index 00000000..8366a08d --- /dev/null +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperInitPhase.java @@ -0,0 +1,11 @@ +package pro.gravit.launcher.server; + +import pro.gravit.launcher.modules.LauncherModule; + +public class ServerWrapperInitPhase extends LauncherModule.Event { + public final ServerWrapper serverWrapper; + + public ServerWrapperInitPhase(ServerWrapper serverWrapper) { + this.serverWrapper = serverWrapper; + } +} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperModulesManager.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperModulesManager.java new file mode 100644 index 00000000..8eeff4c5 --- /dev/null +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapperModulesManager.java @@ -0,0 +1,15 @@ +package pro.gravit.launcher.server; + +import pro.gravit.launcher.modules.impl.SimpleModuleManager; + +import java.nio.file.Path; + +public class ServerWrapperModulesManager extends SimpleModuleManager { + public ServerWrapperModulesManager(Path modulesDir, Path configDir) { + super(modulesDir, configDir); + } + public void fullInitializeServerWrapper(ServerWrapper serverWrapper) + { + initContext = new ServerWrapperInitContext(serverWrapper); + } +} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPostSetupEvent.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPostSetupEvent.java new file mode 100644 index 00000000..e37d6a85 --- /dev/null +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPostSetupEvent.java @@ -0,0 +1,11 @@ +package pro.gravit.launcher.server.setup; + +import pro.gravit.launcher.modules.LauncherModule; + +public class ServerWrapperPostSetupEvent extends LauncherModule.Event { + public final ServerWrapperSetup setup; + + public ServerWrapperPostSetupEvent(ServerWrapperSetup setup) { + this.setup = setup; + } +} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPreSetupEvent.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPreSetupEvent.java new file mode 100644 index 00000000..a8d0506e --- /dev/null +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperPreSetupEvent.java @@ -0,0 +1,11 @@ +package pro.gravit.launcher.server.setup; + +import pro.gravit.launcher.modules.LauncherModule; + +public class ServerWrapperPreSetupEvent extends LauncherModule.Event { + public final ServerWrapperSetup setup; + + public ServerWrapperPreSetupEvent(ServerWrapperSetup setup) { + this.setup = setup; + } +} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java index 6a63ffae..9d79ce68 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java @@ -20,6 +20,7 @@ public class ServerWrapperSetup { public void run() throws IOException { ServerWrapper wrapper = ServerWrapper.wrapper; + ServerWrapper.modulesManager.invokeEvent(new ServerWrapperPreSetupEvent(this)); System.out.println("Print jar filename:"); String jarName = commands.commandHandler.readLine(); Path jarPath = Paths.get(jarName); @@ -86,6 +87,7 @@ public void run() throws IOException { Path startScriptBak = Paths.get("start.bak"); IOHelper.move(startScript, startScriptBak); } + ServerWrapper.modulesManager.invokeEvent(new ServerWrapperSetupEvent(this)); try (Writer writer = IOHelper.newWriter(startScript)) { if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX) { writer.append("#!/bin/sh\n\n"); @@ -108,7 +110,7 @@ public void run() throws IOException { writer.append(ServerWrapper.class.getName()); writer.append("\n"); } - + ServerWrapper.modulesManager.invokeEvent(new ServerWrapperPostSetupEvent(this)); } public ServerWrapperSetup() throws IOException { diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetupEvent.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetupEvent.java new file mode 100644 index 00000000..0c1d95dc --- /dev/null +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetupEvent.java @@ -0,0 +1,11 @@ +package pro.gravit.launcher.server.setup; + +import pro.gravit.launcher.modules.LauncherModule; + +public class ServerWrapperSetupEvent extends LauncherModule.Event { + public final ServerWrapperSetup setup; + + public ServerWrapperSetupEvent(ServerWrapperSetup setup) { + this.setup = setup; + } +}