[FEATURE] Компоненты добавлены в конфигурацию

This commit is contained in:
Gravit 2019-03-13 16:24:28 +07:00
parent e95557c6fd
commit 3eabd1e38e
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 40 additions and 7 deletions

View file

@ -33,6 +33,7 @@
import ru.gravit.launchserver.texture.RequestTextureProvider; import ru.gravit.launchserver.texture.RequestTextureProvider;
import ru.gravit.launchserver.texture.TextureProvider; import ru.gravit.launchserver.texture.TextureProvider;
import ru.gravit.utils.helper.*; import ru.gravit.utils.helper.*;
import sun.nio.cs.ext.COMPOUND_TEXT;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -97,6 +98,8 @@ public static final class Config {
public HWIDHandler hwidHandler; public HWIDHandler hwidHandler;
public HashMap<String, Component> components;
// Misc options // Misc options
public int threadCount; public int threadCount;
@ -448,6 +451,15 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE
provider.init(); provider.init();
} }
config.authHandler.init(); config.authHandler.init();
if(config.components != null)
{
LogHelper.debug("PreInit components");
config.components.forEach((k,v) -> {
LogHelper.subDebug("PreInit component %s", k);
v.preInit(this);
});
LogHelper.debug("PreInit components successful");
}
// build hooks, anti-brutforce and other // build hooks, anti-brutforce and other
buildHookManager = new BuildHookManager(); buildHookManager = new BuildHookManager();
@ -492,6 +504,15 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE
// init modules // init modules
modulesManager.initModules(); modulesManager.initModules();
if(config.components != null)
{
LogHelper.debug("Init components");
config.components.forEach((k,v) -> {
LogHelper.subDebug("Init component %s", k);
v.init(this);
});
LogHelper.debug("Init components successful");
}
// Set launcher EXE binary // Set launcher EXE binary
launcherBinary = new JARLauncherBinary(this); launcherBinary = new JARLauncherBinary(this);
@ -517,6 +538,15 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE
// post init modules // post init modules
modulesManager.postInitModules(); modulesManager.postInitModules();
if(config.components != null)
{
LogHelper.debug("PostInit components");
config.components.forEach((k,v) -> {
LogHelper.subDebug("PostInit component %s", k);
v.postInit(this);
});
LogHelper.debug("PostInit components successful");
}
// start updater // start updater
this.updater = new Updater(this); this.updater = new Updater(this);
if(config.netty != null) if(config.netty != null)

View file

@ -1,26 +1,27 @@
package ru.gravit.launchserver.components; package ru.gravit.launchserver.components;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class Component { public abstract class Component {
private static final Map<String, Class<? extends Component>> COMPONENTS = new ConcurrentHashMap<>(4); private static final Map<String, Class<? extends Component>> COMPONENTS = new ConcurrentHashMap<>(4);
private static boolean registredComp = false; private static boolean registredComp = false;
public static void registerHandler(String name, Class<? extends Component> adapter) { public static void registerComponent(String name, Class<? extends Component> adapter) {
VerifyHelper.verifyIDName(name); VerifyHelper.verifyIDName(name);
VerifyHelper.putIfAbsent(COMPONENTS, name, Objects.requireNonNull(adapter, "adapter"), VerifyHelper.putIfAbsent(COMPONENTS, name, Objects.requireNonNull(adapter, "adapter"),
String.format("Auth handler has been already registered: '%s'", name)); String.format("Auth handler has been already registered: '%s'", name));
} }
public static Class<? extends Component> getHandlerClass(String name) { public static Class<? extends Component> getComponentClass(String name) {
return COMPONENTS.get(name); return COMPONENTS.get(name);
} }
public static String getHandlerName(Class<Component> clazz) { public static String getComponentName(Class<Component> clazz) {
for (Map.Entry<String, Class<? extends Component>> e : COMPONENTS.entrySet()) { for (Map.Entry<String, Class<? extends Component>> e : COMPONENTS.entrySet()) {
if (e.getValue().equals(clazz)) return e.getKey(); if (e.getValue().equals(clazz)) return e.getKey();
} }
@ -32,4 +33,7 @@ public static void registerComponents() {
registredComp = true; registredComp = true;
} }
} }
public abstract void preInit(LaunchServer launchServer);
public abstract void init(LaunchServer launchServer);
public abstract void postInit(LaunchServer launchServer);
} }

View file

@ -1,7 +1,6 @@
package ru.gravit.launchserver.config; package ru.gravit.launchserver.config;
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.components.Component; import ru.gravit.launchserver.components.Component;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -13,7 +12,7 @@ public class ComponentAdapter implements JsonSerializer<Component>, JsonDeserial
@Override @Override
public Component deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public Component deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<? extends Component> cls = Component.getHandlerClass(typename); Class<? extends Component> cls = Component.getComponentClass(typename);
if(cls == null) if(cls == null)
{ {
LogHelper.error("Component %s not found", typename); LogHelper.error("Component %s not found", typename);
@ -29,7 +28,7 @@ public JsonElement serialize(Component src, Type typeOfSrc, JsonSerializationCon
JsonObject jo = context.serialize(src).getAsJsonObject(); JsonObject jo = context.serialize(src).getAsJsonObject();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
String classPath = Component.getHandlerName((Class<Component>) src.getClass()); String classPath = Component.getComponentName((Class<Component>) src.getClass());
jo.add(PROP_NAME, new JsonPrimitive(classPath)); jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo; return jo;