[FEATURE][EXPERIMENTAL] Новая система модулей уже в лаунчсервере

This commit is contained in:
Gravit 2019-08-26 17:24:19 +07:00
parent 114cd2f8c6
commit a980935092
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
19 changed files with 176 additions and 30 deletions

View file

@ -38,6 +38,8 @@
import pro.gravit.launcher.hasher.HashedDir;
import pro.gravit.launcher.managers.ConfigManager;
import pro.gravit.launcher.managers.GarbageManager;
import pro.gravit.launcher.modules.LauncherModulesManager;
import pro.gravit.launcher.modules.events.ClosePhase;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launchserver.auth.AuthProviderPair;
import pro.gravit.launchserver.binary.EXEL4JLauncherBinary;
@ -51,6 +53,11 @@
import pro.gravit.launchserver.manangers.*;
import pro.gravit.launchserver.manangers.hook.AuthHookManager;
import pro.gravit.launchserver.manangers.hook.BuildHookManager;
import pro.gravit.launchserver.modules.events.LaunchServerFullInitEvent;
import pro.gravit.launchserver.modules.events.LaunchServerInitPhase;
import pro.gravit.launchserver.modules.events.LaunchServerPostInitPhase;
import pro.gravit.launchserver.modules.events.NewLaunchServerInstanceEvent;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.launchserver.socket.handlers.NettyServerSocketHandler;
import pro.gravit.utils.command.*;
import pro.gravit.utils.helper.CommonHelper;
@ -226,7 +233,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
public final AuthHookManager authHookManager;
// Server
public final ModulesManager modulesManager;
public final LaunchServerModulesManager modulesManager;
public final MirrorManager mirrorManager;
@ -268,7 +275,7 @@ public void collect()
}
}
public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, LaunchServerConfig config, LaunchServerRuntimeConfig runtimeConfig, LaunchServerConfigManager launchServerConfigManager, ModulesManager modulesManager, RSAPublicKey publicKey, RSAPrivateKey privateKey, CommandHandler commandHandler) throws IOException, InvalidKeySpecException {
public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, LaunchServerConfig config, LaunchServerRuntimeConfig runtimeConfig, LaunchServerConfigManager launchServerConfigManager, LaunchServerModulesManager modulesManager, RSAPublicKey publicKey, RSAPrivateKey privateKey, CommandHandler commandHandler) throws IOException, InvalidKeySpecException {
this.dir = directories.dir;
this.env = env;
this.config = config;
@ -292,7 +299,7 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
serverCertFile = dir.resolve("server.crt");
serverKeyFile = dir.resolve("server.key");
modulesManager.initContext(this);
modulesManager.invokeEvent(new NewLaunchServerInstanceEvent(this));
// Print keypair fingerprints
CRC32 crc = new CRC32();
@ -302,11 +309,6 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
// Load class bindings.
launcherEXEBinaryClass = defaultLauncherEXEBinaryClass;
// pre init modules
//modulesManager = new ModulesManager(this);
//modulesManager.autoload(dir.resolve("modules"));
modulesManager.preInitModules();
runtime.verify();
config.verify();
if (config.components != null) {
@ -371,7 +373,7 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
pro.gravit.launchserver.command.handler.CommandHandler.registerCommands(commandHandler, this);
// init modules
modulesManager.initModules();
modulesManager.invokeEvent(new LaunchServerInitPhase(this));
if (config.components != null) {
LogHelper.debug("Init components");
config.components.forEach((k, v) -> {
@ -400,7 +402,7 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La
syncProfilesDir();
// post init modules
modulesManager.postInitModules();
modulesManager.invokeEvent(new LaunchServerPostInitPhase(this));
if (config.components != null) {
LogHelper.debug("PostInit components");
config.components.forEach((k, v) -> {
@ -457,7 +459,7 @@ public void close() throws Exception {
// Close handlers & providers
config.close(ReloadType.FULL);
modulesManager.close();
modulesManager.invokeEvent(new ClosePhase());
LogHelper.info("Save LaunchServer runtime config");
launchServerConfigManager.writeRuntimeConfig(runtime);
// Print last message before death :(
@ -504,7 +506,8 @@ public void run() {
}
if (config.netty != null)
rebindNettyServerSocket();
modulesManager.finishModules();
modulesManager.fullInitializedLaunchServer(this);
modulesManager.invokeEvent(new LaunchServerFullInitEvent(this));
}

View file

@ -3,6 +3,7 @@
import pro.gravit.launchserver.config.LaunchServerConfig;
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
import pro.gravit.launchserver.manangers.ModulesManager;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.utils.command.CommandHandler;
import java.nio.file.Path;
@ -14,7 +15,7 @@ public class LaunchServerBuilder {
private LaunchServerRuntimeConfig runtimeConfig;
private CommandHandler commandHandler;
private LaunchServer.LaunchServerEnv env;
private ModulesManager modulesManager;
private LaunchServerModulesManager modulesManager;
private LaunchServer.LaunchServerDirectories directories = new LaunchServer.LaunchServerDirectories();
private RSAPublicKey publicKey;
private RSAPrivateKey privateKey;
@ -30,7 +31,7 @@ public LaunchServerBuilder setEnv(LaunchServer.LaunchServerEnv env) {
return this;
}
public LaunchServerBuilder setModulesManager(ModulesManager modulesManager) {
public LaunchServerBuilder setModulesManager(LaunchServerModulesManager modulesManager) {
this.modulesManager = modulesManager;
return this;
}

View file

@ -2,6 +2,7 @@
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.hwid.HWIDProvider;
import pro.gravit.launcher.modules.events.PreConfigPhase;
import pro.gravit.launchserver.auth.handler.AuthHandler;
import pro.gravit.launchserver.auth.hwid.HWIDHandler;
import pro.gravit.launchserver.auth.permissions.PermissionsHandler;
@ -14,6 +15,7 @@
import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.launchserver.manangers.LaunchServerGsonManager;
import pro.gravit.launchserver.manangers.ModulesManager;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.launchserver.socket.WebSocketService;
import pro.gravit.utils.command.CommandHandler;
import pro.gravit.utils.command.JLineCommandHandler;
@ -55,10 +57,11 @@ public static void main(String[] args) throws Exception {
LaunchServerRuntimeConfig runtimeConfig;
LaunchServerConfig config;
LaunchServer.LaunchServerEnv env = LaunchServer.LaunchServerEnv.PRODUCTION;
ModulesManager modulesManager = new ModulesManager(dir.resolve("config"));
modulesManager.autoload(dir.resolve("modules"));
LaunchServerModulesManager modulesManager = new LaunchServerModulesManager(dir.resolve("modules"), dir.resolve("config"));
modulesManager.autoload();
modulesManager.initModules(null);
registerAll();
initGson();
initGson(modulesManager);
if (IOHelper.exists(dir.resolve("LaunchServer.conf"))) {
configFile = dir.resolve("LaunchServer.conf");
} else {
@ -107,6 +110,7 @@ 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)) {
@ -169,8 +173,8 @@ public void writeRuntimeConfig(LaunchServerRuntimeConfig config) throws IOExcept
server.run();
}
public static void initGson() {
Launcher.gsonManager = new LaunchServerGsonManager();
public static void initGson(LaunchServerModulesManager modulesManager) {
Launcher.gsonManager = new LaunchServerGsonManager(modulesManager);
Launcher.gsonManager.initGson();
}

View file

@ -1,6 +1,7 @@
package pro.gravit.launchserver.command.modules;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import pro.gravit.launchserver.LaunchServer;
@ -24,7 +25,7 @@ public String getUsageDescription() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
URI uri = Paths.get(args[0]).toUri();
server.modulesManager.loadModule(uri.toURL());
Path file = Paths.get(args[0]);
server.modulesManager.loadModule(file);
}
}

View file

@ -20,6 +20,6 @@ public String getUsageDescription() {
@Override
public void invoke(String... args) {
server.modulesManager.printModules();
server.modulesManager.printModulesInfo();
}
}

View file

@ -5,6 +5,7 @@
import pro.gravit.launcher.hwid.HWID;
import pro.gravit.launcher.hwid.HWIDProvider;
import pro.gravit.launcher.managers.GsonManager;
import pro.gravit.launcher.modules.events.PreGsonPhase;
import pro.gravit.launcher.request.JsonResultSerializeAdapter;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.launchserver.auth.handler.AuthHandler;
@ -15,11 +16,18 @@
import pro.gravit.launchserver.auth.texture.TextureProvider;
import pro.gravit.launchserver.components.Component;
import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
import pro.gravit.launchserver.socket.WebSocketService;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
import pro.gravit.utils.UniversalJsonAdapter;
public class LaunchServerGsonManager extends GsonManager {
private final LaunchServerModulesManager modulesManager;
public LaunchServerGsonManager(LaunchServerModulesManager modulesManager) {
this.modulesManager = modulesManager;
}
@Override
public void registerAdapters(GsonBuilder builder) {
super.registerAdapters(builder);
@ -34,6 +42,7 @@ public void registerAdapters(GsonBuilder builder) {
builder.registerTypeAdapter(HWID.class, new UniversalJsonAdapter<>(HWIDProvider.hwids));
builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers));
builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter());
modulesManager.invokeEvent(new PreGsonPhase(builder));
//ClientWebSocketService.appendTypeAdapters(builder);
}
}

View file

@ -25,7 +25,7 @@ public Type getType() {
@Override
public ModulesManager getModulesManager() {
return launchServer.modulesManager;
return null;
}
@Override

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerFullInitEvent extends LauncherModule.Event {
public final LaunchServer server;
public LaunchServerFullInitEvent(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.events.InitPhase;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerInitPhase extends InitPhase {
public final LaunchServer server;
public LaunchServerInitPhase(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.events.InitPhase;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerPostInitPhase extends InitPhase {
public final LaunchServer server;
public LaunchServerPostInitPhase(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.events;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launchserver.LaunchServer;
public class NewLaunchServerInstanceEvent extends LauncherModule.Event {
public final LaunchServer launchServer;
public NewLaunchServerInstanceEvent(LaunchServer launchServer) {
this.launchServer = launchServer;
}
}

View file

@ -0,0 +1,24 @@
package pro.gravit.launchserver.modules.impl;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.launchserver.modules.events.LaunchServerInitPhase;
import pro.gravit.utils.Version;
import pro.gravit.utils.helper.LogHelper;
public class LaunchServerCoreModule extends LauncherModule {
public LaunchServerCoreModule() {
super(new LauncherModuleInfo("LaunchServerCore", Version.getVersion()));
}
@Override
public void init(LauncherInitContext initContext) {
registerEvent(this::testEvent, LaunchServerInitPhase.class);
}
public void testEvent(LaunchServerInitPhase event)
{
LogHelper.debug("[LaunchServerCore] Event LaunchServerInitPhase passed");
}
}

View file

@ -0,0 +1,12 @@
package pro.gravit.launchserver.modules.impl;
import pro.gravit.launcher.modules.LauncherInitContext;
import pro.gravit.launchserver.LaunchServer;
public class LaunchServerInitContext implements LauncherInitContext {
public final LaunchServer server;
public LaunchServerInitContext(LaunchServer server) {
this.server = server;
}
}

View file

@ -0,0 +1,36 @@
package pro.gravit.launchserver.modules.impl;
import pro.gravit.launcher.modules.LauncherModule;
import pro.gravit.launcher.modules.LauncherModuleInfo;
import pro.gravit.launcher.modules.impl.SimpleModuleManager;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.utils.helper.LogHelper;
import java.nio.file.Path;
import java.util.Arrays;
public class LaunchServerModulesManager extends SimpleModuleManager {
public LaunchServerCoreModule coreModule;
public LaunchServerModulesManager(Path modulesDir, Path configDir) {
super(modulesDir, configDir);
coreModule = new LaunchServerCoreModule();
modules.add(coreModule);
}
public void fullInitializedLaunchServer(LaunchServer server)
{
initContext = new LaunchServerInitContext(server);
}
public void printModulesInfo()
{
for(LauncherModule module : modules)
{
LauncherModuleInfo info = module.getModuleInfo();
LogHelper.info("[MODULE] %s v: %s p: %d deps: %s", info.name, info.version.getVersionString(), info.priority, Arrays.toString(info.dependencies));
}
}
@Override
public LauncherModule getCoreModule() {
return coreModule;
}
}

View file

@ -16,6 +16,10 @@ protected LauncherModule() {
moduleInfo = new LauncherModuleInfo("UnknownModule");
}
protected LauncherModule(LauncherModuleInfo info) {
moduleInfo = info;
}
public LauncherModuleInfo getModuleInfo() {
return moduleInfo;
}
@ -29,7 +33,7 @@ public enum InitPhase
@FunctionalInterface
public interface EventHandler<T extends Event>
{
void event(T e) throws Exception;
void event(T e);
}
public static class Event
{
@ -76,7 +80,7 @@ protected <T extends Event> boolean registerEvent(EventHandler<T> handle, Class<
}
@SuppressWarnings("unchecked")
public final <T extends Event> void callEvent(T event) throws Exception
public final <T extends Event> void callEvent(T event)
{
Class<? extends Event> tClass = event.getClass();
for(Map.Entry<Class<? extends Event>, EventHandler> e : eventMap.entrySet())

View file

@ -23,5 +23,5 @@ default <T extends LauncherModule> boolean containsModule(Class<? extends T> cl
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> void invokeEvent(T event) throws Exception;
<T extends LauncherModule.Event> void invokeEvent(T event);
}

View file

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

View file

@ -5,10 +5,8 @@
public class PreGsonPhase extends LauncherModule.Event {
public GsonBuilder gsonBuilder;
public GsonBuilder gsonConfigBuilder;
public PreGsonPhase(GsonBuilder gsonBuilder, GsonBuilder gsonConfigBuilder) {
public PreGsonPhase(GsonBuilder gsonBuilder) {
this.gsonBuilder = gsonBuilder;
this.gsonConfigBuilder = gsonConfigBuilder;
}
}

View file

@ -188,7 +188,7 @@ public <T extends LauncherModule> T findModule(Class<? extends T> clazz, Predica
}
@Override
public <T extends LauncherModule.Event> void invokeEvent(T event) throws Exception {
public <T extends LauncherModule.Event> void invokeEvent(T event) {
for(LauncherModule module : modules)
{
module.callEvent(event);