diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index f3299a08..66646a1e 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -4,7 +4,9 @@ import com.google.gson.GsonBuilder; import ru.gravit.launcher.Launcher; import ru.gravit.launcher.LauncherConfig; +import ru.gravit.launcher.NeedGarbageCollection; import ru.gravit.launcher.hasher.HashedDir; +import ru.gravit.launcher.managers.ConfigManager; import ru.gravit.launcher.managers.GarbageManager; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.serialize.signed.SignedObjectHolder; @@ -22,10 +24,11 @@ import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.binary.*; import ru.gravit.launchserver.components.Component; +import ru.gravit.utils.config.JsonConfigurable; +import ru.gravit.launchserver.config.adapter.*; import ru.gravit.utils.command.CommandHandler; import ru.gravit.utils.command.JLineCommandHandler; import ru.gravit.utils.command.StdCommandHandler; -import ru.gravit.launchserver.config.*; import ru.gravit.launchserver.manangers.*; import ru.gravit.launchserver.manangers.hook.AuthHookManager; import ru.gravit.launchserver.manangers.hook.BuildHookManager; @@ -360,6 +363,8 @@ public static void main(String... args) throws Throwable { public final ReconfigurableManager reconfigurableManager; + public final ConfigManager configManager; + public final BuildHookManager buildHookManager; @@ -487,32 +492,15 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE reconfigurableManager = new ReconfigurableManager(); socketHookManager = new SocketHookManager(); authHookManager = new AuthHookManager(); + configManager = new ConfigManager(); GarbageManager.registerNeedGC(sessionManager); reloadManager.registerReloadable("launchServer", this); - if (config.permissionsHandler instanceof Reloadable) - reloadManager.registerReloadable("permissionsHandler", (Reloadable) config.permissionsHandler); + registerObject("permissionsHandler", config.permissionsHandler); for (int i = 0; i < config.auth.length; ++i) { AuthProviderPair pair = config.auth[i]; - if (pair.provider instanceof Reloadable) - reloadManager.registerReloadable("auth.".concat(pair.name).concat(".provider"), (Reloadable) pair.provider); - if (pair.handler instanceof Reloadable) - reloadManager.registerReloadable("auth.".concat(pair.name).concat(".handler"), (Reloadable) pair.handler); - if (pair.textureProvider instanceof Reloadable) - reloadManager.registerReloadable("auth.".concat(pair.name).concat(".texture"), (Reloadable) pair.textureProvider); - } - - Arrays.stream(config.mirrors).forEach(mirrorManager::addMirror); - - if (config.permissionsHandler instanceof Reconfigurable) - reconfigurableManager.registerReconfigurable("permissionsHandler", (Reconfigurable) config.permissionsHandler); - for (int i = 0; i < config.auth.length; ++i) { - AuthProviderPair pair = config.auth[i]; - if (pair.provider instanceof Reconfigurable) - reconfigurableManager.registerReconfigurable("auth.".concat(pair.name).concat(".provider"), (Reconfigurable) pair.provider); - if (pair.handler instanceof Reconfigurable) - reconfigurableManager.registerReconfigurable("auth.".concat(pair.name).concat(".handler"), (Reconfigurable) pair.handler); - if (pair.textureProvider instanceof Reconfigurable) - reconfigurableManager.registerReconfigurable("auth.".concat(pair.name).concat(".texture"), (Reconfigurable) pair.textureProvider); + registerObject("auth.".concat(pair.name).concat(".provider"), pair.provider); + registerObject("auth.".concat(pair.name).concat(".handler"), pair.handler); + registerObject("auth.".concat(pair.name).concat(".texture"), pair.textureProvider); } Arrays.stream(config.mirrors).forEach(mirrorManager::addMirror); @@ -524,6 +512,7 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE LogHelper.debug("Init components"); config.components.forEach((k,v) -> { LogHelper.subDebug("Init component %s", k); + registerObject("component.".concat(k),v); v.init(this); }); LogHelper.debug("Init components successful"); @@ -800,6 +789,26 @@ public void restart() { } } + public void registerObject(String name, Object object) + { + if(object instanceof Reloadable) + { + reloadManager.registerReloadable(name, (Reloadable) object); + } + if(object instanceof Reconfigurable) + { + reconfigurableManager.registerReconfigurable(name, (Reconfigurable) object); + } + if(object instanceof NeedGarbageCollection) + { + GarbageManager.registerNeedGC((NeedGarbageCollection) object); + } + if(object instanceof JsonConfigurable) + { + + } + } + public void fullyRestart() { restart(); JVMHelper.RUNTIME.exit(0); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/AuthHandlerAdapter.java similarity index 96% rename from LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/AuthHandlerAdapter.java index f796e921..d636f196 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthHandlerAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/AuthHandlerAdapter.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.config; +package ru.gravit.launchserver.config.adapter; import com.google.gson.*; import ru.gravit.launchserver.auth.handler.AuthHandler; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/AuthProviderAdapter.java similarity index 96% rename from LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/AuthProviderAdapter.java index f4e74c95..017208c9 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/AuthProviderAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/AuthProviderAdapter.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.config; +package ru.gravit.launchserver.config.adapter; import com.google.gson.*; import ru.gravit.launchserver.auth.provider.AuthProvider; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/ComponentAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/ComponentAdapter.java similarity index 96% rename from LaunchServer/src/main/java/ru/gravit/launchserver/config/ComponentAdapter.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/ComponentAdapter.java index 65c8f7da..1342a1d5 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/ComponentAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/ComponentAdapter.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.config; +package ru.gravit.launchserver.config.adapter; import com.google.gson.*; import ru.gravit.launchserver.components.Component; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/HWIDHandlerAdapter.java similarity index 96% rename from LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/HWIDHandlerAdapter.java index 3a2d994c..0c97d3dc 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/HWIDHandlerAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/HWIDHandlerAdapter.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.config; +package ru.gravit.launchserver.config.adapter; import com.google.gson.*; import ru.gravit.launchserver.auth.hwid.HWIDHandler; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/PermissionsHandlerAdapter.java similarity index 96% rename from LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/PermissionsHandlerAdapter.java index bc2e21ae..aef7f2fc 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/PermissionsHandlerAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/PermissionsHandlerAdapter.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.config; +package ru.gravit.launchserver.config.adapter; import com.google.gson.*; import ru.gravit.launchserver.auth.permissions.PermissionsHandler; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/ProtectHandlerAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/ProtectHandlerAdapter.java similarity index 96% rename from LaunchServer/src/main/java/ru/gravit/launchserver/config/ProtectHandlerAdapter.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/ProtectHandlerAdapter.java index b342a14f..a1120de5 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/ProtectHandlerAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/ProtectHandlerAdapter.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.config; +package ru.gravit.launchserver.config.adapter; import com.google.gson.*; import ru.gravit.launchserver.auth.protect.ProtectHandler; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/TextureProviderAdapter.java similarity index 96% rename from LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/TextureProviderAdapter.java index 75aba651..908099a9 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/config/adapter/TextureProviderAdapter.java @@ -1,4 +1,4 @@ -package ru.gravit.launchserver.config; +package ru.gravit.launchserver.config.adapter; import com.google.gson.*; import ru.gravit.launchserver.texture.TextureProvider; diff --git a/libLauncher/src/main/java/ru/gravit/launcher/managers/ConfigManager.java b/libLauncher/src/main/java/ru/gravit/launcher/managers/ConfigManager.java new file mode 100644 index 00000000..465790c5 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/launcher/managers/ConfigManager.java @@ -0,0 +1,55 @@ +package ru.gravit.launcher.managers; + +import ru.gravit.utils.config.JsonConfigurable; +import ru.gravit.utils.helper.LogHelper; +import ru.gravit.utils.helper.VerifyHelper; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Objects; + +public class ConfigManager { + private final HashMap CONFIGURABLE = new HashMap<>(); + + public void registerConfigurable(String name, JsonConfigurable reconfigurable) { + VerifyHelper.putIfAbsent(CONFIGURABLE, name.toLowerCase(), Objects.requireNonNull(reconfigurable, "adapter"), + String.format("Reloadable has been already registered: '%s'", name)); + } + + public void printConfigurables() { + LogHelper.info("Print configurables"); + CONFIGURABLE.forEach((k, v) -> LogHelper.subInfo(k)); + LogHelper.info("Found %d configurables", CONFIGURABLE.size()); + } + + public void save(String name) throws IOException + { + CONFIGURABLE.get(name).saveConfig(); + } + + public void load(String name) throws IOException + { + CONFIGURABLE.get(name).loadConfig(); + } + + public void save(String name, Path path) throws IOException + { + CONFIGURABLE.get(name).saveConfig(path); + } + + public void reset(String name) throws IOException + { + CONFIGURABLE.get(name).resetConfig(); + } + + public void load(String name, Path path) throws IOException + { + CONFIGURABLE.get(name).loadConfig(path); + } + + public void reset(String name, Path path) throws IOException + { + CONFIGURABLE.get(name).resetConfig(path); + } +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java b/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java new file mode 100644 index 00000000..ca8cb6b2 --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java @@ -0,0 +1,58 @@ +package ru.gravit.utils.config; + +import ru.gravit.launcher.Launcher; +import ru.gravit.utils.helper.IOHelper; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.lang.reflect.Type; +import java.nio.file.Path; + +public abstract class JsonConfigurable { + private Type type; + protected Path configPath; + public void saveConfig() throws IOException + { + saveConfig(configPath); + } + public void loadConfig() throws IOException + { + loadConfig(configPath); + } + + public JsonConfigurable(Type type, Path configPath) { + this.type = type; + this.configPath = configPath; + } + + public void saveConfig(Path configPath) throws IOException + { + try(BufferedWriter writer = IOHelper.newWriter(configPath)) + { + Launcher.gson.toJson(getConfig(), type, writer); + } + } + public void loadConfig(Path configPath) throws IOException + { + try (BufferedReader reader = IOHelper.newReader(configPath)) { + setConfig(Launcher.gson.fromJson(reader, type)); + } + } + + public void resetConfig() throws IOException + { + setConfig(getDefaultConfig()); + saveConfig(); + } + + public void resetConfig(Path newPath) throws IOException + { + setConfig(getDefaultConfig()); + saveConfig(newPath); + } + + public abstract T getConfig(); + public abstract T getDefaultConfig(); + public abstract void setConfig(T config); +}