Перевод конфигов на Json часть 6

Адаптеры
Комплируется, пока не работает
This commit is contained in:
Gravit 2018-12-24 00:19:43 +07:00
parent 2c06cdec24
commit 137b83f428
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
9 changed files with 184 additions and 0 deletions

View file

@ -10,8 +10,11 @@
import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.launchserver.auth.AuthLimiter;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.auth.handler.MemoryAuthHandler;
import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler;
import ru.gravit.launchserver.auth.hwid.HWIDHandler;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.RejectAuthProvider;
import ru.gravit.launchserver.binary.EXEL4JLauncherBinary;
import ru.gravit.launchserver.binary.EXELauncherBinary;
import ru.gravit.launchserver.binary.JARLauncherBinary;
@ -19,6 +22,10 @@
import ru.gravit.launchserver.command.handler.CommandHandler;
import ru.gravit.launchserver.command.handler.JLineCommandHandler;
import ru.gravit.launchserver.command.handler.StdCommandHandler;
import ru.gravit.launchserver.config.AuthHandlerAdapter;
import ru.gravit.launchserver.config.AuthProviderAdapter;
import ru.gravit.launchserver.config.HWIDHandlerAdapter;
import ru.gravit.launchserver.config.TextureProviderAdapter;
import ru.gravit.launchserver.manangers.BuildHookManager;
import ru.gravit.launchserver.manangers.MirrorManager;
import ru.gravit.launchserver.manangers.ModulesManager;
@ -361,6 +368,10 @@ public static void initGson()
if(Launcher.gson != null) return;
Launcher.gsonBuilder = new GsonBuilder();
Launcher.gsonBuilder.setPrettyPrinting();
Launcher.gsonBuilder.registerTypeAdapter(AuthProvider.class, new AuthProviderAdapter());
Launcher.gsonBuilder.registerTypeAdapter(TextureProvider.class, new TextureProviderAdapter());
Launcher.gsonBuilder.registerTypeAdapter(AuthHandler.class, new AuthHandlerAdapter());
Launcher.gsonBuilder.registerTypeAdapter(HWIDHandler.class, new HWIDHandlerAdapter());
Launcher.gson = Launcher.gsonBuilder.create();
}
@ -412,6 +423,11 @@ private void generateConfigIfNotExists() throws IOException {
newConfig.launch4j = new ExeConf();
newConfig.buildPostTransform = new PostBuildTransformConf();
newConfig.env = LauncherConfig.LauncherEnvironment.STD;
newConfig.authHandler = new AuthHandler[]{new MemoryAuthHandler()};
newConfig.hwidHandler = new AcceptHWIDHandler();
newConfig.authProvider = new AuthProvider[]{new RejectAuthProvider()};
newConfig.port = 7420;
newConfig.bindAddress = "0.0.0.0";
//try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launchserver/defaults/config.cfg"))) {
// newConfig = Launcher.gson.fromJson(reader,Config.class);
//}

View file

@ -25,6 +25,18 @@ public static void registerHandler(String name, Class adapter) {
VerifyHelper.putIfAbsent(AUTH_HANDLERS, name, Objects.requireNonNull(adapter, "adapter"),
String.format("Auth handler has been already registered: '%s'", name));
}
public static Class getHandlerClass(String name)
{
return AUTH_HANDLERS.get(name);
}
public static String getHandlerName(Class clazz)
{
for(Map.Entry<String,Class> e: AUTH_HANDLERS.entrySet())
{
if(e.getValue().equals(clazz)) return e.getKey();
}
return null;
}
public static void registerHandlers() {
if (!registredHandl) {

View file

@ -43,4 +43,17 @@ public void check(HWID hwid, String username) throws HWIDException {
public abstract List<HWID> getHwid(String username) throws HWIDException;
public abstract void unban(List<HWID> hwid) throws HWIDException;
public static Class getHandlerClass(String name)
{
return HW_HANDLERS.get(name);
}
public static String getHandlerName(Class clazz)
{
for(Map.Entry<String,Class> e: HW_HANDLERS.entrySet())
{
if(e.getValue().equals(clazz)) return e.getKey();
}
return null;
}
}

View file

@ -50,4 +50,17 @@ public AuthHandler getAccociateHandler(int this_position) {
@Override
public abstract void close() throws IOException;
public static Class getProviderClass(String name)
{
return AUTH_PROVIDERS.get(name);
}
public static String getProviderName(Class clazz)
{
for(Map.Entry<String,Class> e: AUTH_PROVIDERS.entrySet())
{
if(e.getValue().equals(clazz)) return e.getKey();
}
return null;
}
}

View file

@ -0,0 +1,29 @@
package ru.gravit.launchserver.config;
import com.google.gson.*;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import java.lang.reflect.Type;
public class AuthHandlerAdapter implements JsonSerializer<AuthHandler>, JsonDeserializer<AuthHandler> {
private static final String PROP_NAME = "type";
@Override
public AuthHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<AuthHandler> cls = AuthHandler.getHandlerClass(typename);
return (AuthHandler) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(AuthHandler src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject();
String classPath = AuthHandler.getHandlerName(src.getClass());
jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo;
}
}

View file

@ -0,0 +1,30 @@
package ru.gravit.launchserver.config;
import com.google.gson.*;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import java.lang.reflect.Type;
public class AuthProviderAdapter implements JsonSerializer<AuthProvider>, JsonDeserializer<AuthProvider> {
private static final String PROP_NAME = "type";
@Override
public AuthProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<AuthProvider> cls = AuthProvider.getProviderClass(typename);
return (AuthProvider) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(AuthProvider src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject();
String classPath = AuthProvider.getProviderName(src.getClass());
jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo;
}
}

View file

@ -0,0 +1,29 @@
package ru.gravit.launchserver.config;
import com.google.gson.*;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.auth.hwid.HWIDHandler;
import java.lang.reflect.Type;
public class HWIDHandlerAdapter implements JsonSerializer<HWIDHandler>, JsonDeserializer<HWIDHandler> {
private static final String PROP_NAME = "type";
@Override
public HWIDHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<HWIDHandler> cls = HWIDHandler.getHandlerClass(typename);
return (HWIDHandler) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(HWIDHandler src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject();
String classPath = HWIDHandler.getHandlerName(src.getClass());
jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo;
}
}

View file

@ -0,0 +1,29 @@
package ru.gravit.launchserver.config;
import com.google.gson.*;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.texture.TextureProvider;
import java.lang.reflect.Type;
public class TextureProviderAdapter implements JsonSerializer<TextureProvider>, JsonDeserializer<TextureProvider> {
private static final String PROP_NAME = "type";
@Override
public TextureProvider deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<AuthProvider> cls = TextureProvider.getProviderClass(typename);
return (TextureProvider) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(TextureProvider src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject();
String classPath = TextureProvider.getProviderName(src.getClass());
jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo;
}
}

View file

@ -38,4 +38,17 @@ public static void registerProviders() {
public abstract Texture getSkinTexture(UUID uuid, String username, String client) throws IOException;
public static Class getProviderClass(String name)
{
return TEXTURE_PROVIDERS.get(name);
}
public static String getProviderName(Class clazz)
{
for(Map.Entry<String,Class> e: TEXTURE_PROVIDERS.entrySet())
{
if(e.getValue().equals(clazz)) return e.getKey();
}
return null;
}
}