[FEATURE] Компоненты

This commit is contained in:
Gravit 2019-03-13 16:13:21 +07:00
parent f4c2c6e3a6
commit e95557c6fd
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
8 changed files with 105 additions and 0 deletions

View file

@ -18,6 +18,7 @@
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.auth.provider.RejectAuthProvider;
import ru.gravit.launchserver.binary.*; import ru.gravit.launchserver.binary.*;
import ru.gravit.launchserver.components.Component;
import ru.gravit.utils.command.CommandHandler; import ru.gravit.utils.command.CommandHandler;
import ru.gravit.utils.command.JLineCommandHandler; import ru.gravit.utils.command.JLineCommandHandler;
import ru.gravit.utils.command.StdCommandHandler; import ru.gravit.utils.command.StdCommandHandler;
@ -532,6 +533,7 @@ public static void initGson() {
Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter());
Launcher.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter());
Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter());
Launcher.gsonBuilder.registerTypeAdapter(Component.class, new ComponentAdapter());
Launcher.gson = Launcher.gsonBuilder.create(); Launcher.gson = Launcher.gsonBuilder.create();
//Human readable //Human readable
@ -542,6 +544,7 @@ public static void initGson() {
LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter());
LaunchServer.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(PermissionsHandler.class, new PermissionsHandlerAdapter());
LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter()); LaunchServer.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter());
LaunchServer.gsonBuilder.registerTypeAdapter(Component.class, new ComponentAdapter());
LaunchServer.gson = LaunchServer.gsonBuilder.create(); LaunchServer.gson = LaunchServer.gsonBuilder.create();
} }

View file

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

View file

@ -2,6 +2,7 @@
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.launchserver.auth.handler.AuthHandler; import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -12,6 +13,11 @@ public class AuthHandlerAdapter implements JsonSerializer<AuthHandler>, JsonDese
public AuthHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public AuthHandler 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 AuthHandler> cls = AuthHandler.getHandlerClass(typename); Class<? extends AuthHandler> cls = AuthHandler.getHandlerClass(typename);
if(cls == null)
{
LogHelper.error("AuthHandler %s not found", typename);
return null;
}
return (AuthHandler) context.deserialize(json, cls); return (AuthHandler) context.deserialize(json, cls);

View file

@ -2,6 +2,7 @@
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -12,6 +13,11 @@ public class AuthProviderAdapter implements JsonSerializer<AuthProvider>, JsonDe
public AuthProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public AuthProvider 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 AuthProvider> cls = AuthProvider.getProviderClass(typename); Class<? extends AuthProvider> cls = AuthProvider.getProviderClass(typename);
if(cls == null)
{
LogHelper.error("AuthProvider %s not found", typename);
return null;
}
return (AuthProvider) context.deserialize(json, cls); return (AuthProvider) context.deserialize(json, cls);

View file

@ -0,0 +1,37 @@
package ru.gravit.launchserver.config;
import com.google.gson.*;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.components.Component;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type;
public class ComponentAdapter implements JsonSerializer<Component>, JsonDeserializer<Component> {
private static final String PROP_NAME = "component";
@Override
public Component deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<? extends Component> cls = Component.getHandlerClass(typename);
if(cls == null)
{
LogHelper.error("Component %s not found", typename);
return null;
}
return (Component) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(Component src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject();
@SuppressWarnings("unchecked")
String classPath = Component.getHandlerName((Class<Component>) src.getClass());
jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo;
}
}

View file

@ -2,6 +2,7 @@
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.launchserver.auth.hwid.HWIDHandler; import ru.gravit.launchserver.auth.hwid.HWIDHandler;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -12,6 +13,11 @@ public class HWIDHandlerAdapter implements JsonSerializer<HWIDHandler>, JsonDese
public HWIDHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public HWIDHandler 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 HWIDHandler> cls = HWIDHandler.getHandlerClass(typename); Class<? extends HWIDHandler> cls = HWIDHandler.getHandlerClass(typename);
if(cls == null)
{
LogHelper.error("HWIDHandler %s not found", typename);
return null;
}
return (HWIDHandler) context.deserialize(json, cls); return (HWIDHandler) context.deserialize(json, cls);

View file

@ -2,6 +2,7 @@
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.launchserver.auth.permissions.PermissionsHandler; import ru.gravit.launchserver.auth.permissions.PermissionsHandler;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -12,6 +13,11 @@ public class PermissionsHandlerAdapter implements JsonSerializer<PermissionsHand
public PermissionsHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public PermissionsHandler 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 PermissionsHandler> cls = PermissionsHandler.getHandlerClass(typename); Class<? extends PermissionsHandler> cls = PermissionsHandler.getHandlerClass(typename);
if(cls == null)
{
LogHelper.error("PermissionsHandler %s not found", typename);
return null;
}
return (PermissionsHandler) context.deserialize(json, cls); return (PermissionsHandler) context.deserialize(json, cls);

View file

@ -2,6 +2,7 @@
import com.google.gson.*; import com.google.gson.*;
import ru.gravit.launchserver.texture.TextureProvider; import ru.gravit.launchserver.texture.TextureProvider;
import ru.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -12,6 +13,11 @@ public class TextureProviderAdapter implements JsonSerializer<TextureProvider>,
public TextureProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public TextureProvider 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 TextureProvider> cls = TextureProvider.getProviderClass(typename); Class<? extends TextureProvider> cls = TextureProvider.getProviderClass(typename);
if(cls == null)
{
LogHelper.error("TextureProvider %s not found", typename);
return null;
}
return (TextureProvider) context.deserialize(json, cls); return (TextureProvider) context.deserialize(json, cls);