mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE] ConfigManager
This commit is contained in:
parent
e9f36fa44f
commit
a636fbe336
10 changed files with 152 additions and 30 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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<String, JsonConfigurable> 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);
|
||||
}
|
||||
}
|
|
@ -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<T> {
|
||||
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);
|
||||
}
|
Loading…
Reference in a new issue