From f32844aa15079e88d539a5de0c3893c05e26c659 Mon Sep 17 00:00:00 2001 From: Gravit Date: Wed, 24 Apr 2019 13:08:22 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=98=D0=BC=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D1=8B=D0=B9=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=B9=D0=B4=D0=B5=D1=80=D1=8B=20=D0=B8=20HookSet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/handler/AuthHandler.java | 2 +- .../launchserver/auth/hwid/HWIDHandler.java | 2 +- .../auth/permissions/PermissionsHandler.java | 2 +- .../auth/protect/ProtectHandler.java | 2 +- .../auth/provider/AuthProvider.java | 2 +- .../auth/texture/TextureProvider.java | 2 +- .../main/java/ru/gravit/utils/HookSet.java | 29 +++++++++++++++++++ .../java/ru/gravit/utils/ProviderMap.java | 14 ++++++++- .../ru/gravit/utils/UniversalJsonAdapter.java | 5 +++- 9 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 libLauncher/src/main/java/ru/gravit/utils/HookSet.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java index 931a4cfd..a0ca3588 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java @@ -8,7 +8,7 @@ import java.util.UUID; public abstract class AuthHandler implements AutoCloseable { - public static ProviderMap providers = new ProviderMap<>(); + public static ProviderMap providers = new ProviderMap<>("AuthHandler"); private static boolean registredHandl = false; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java index 47c926c6..6aad2f97 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/HWIDHandler.java @@ -6,7 +6,7 @@ import java.util.List; public abstract class HWIDHandler implements AutoCloseable { - public static ProviderMap providers = new ProviderMap<>(); + public static ProviderMap providers = new ProviderMap<>("HWIDHandler"); private static boolean registredHandl = false; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java index f504a471..5b8c98d1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/PermissionsHandler.java @@ -4,7 +4,7 @@ import ru.gravit.utils.ProviderMap; public abstract class PermissionsHandler implements AutoCloseable { - public static ProviderMap providers = new ProviderMap<>(); + public static ProviderMap providers = new ProviderMap<>("PermissionsHandler"); private static boolean registredHandl = false; public static void registerHandlers() { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/protect/ProtectHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/protect/ProtectHandler.java index 9e1aef89..f9b8151a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/protect/ProtectHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/protect/ProtectHandler.java @@ -4,7 +4,7 @@ import ru.gravit.utils.ProviderMap; public abstract class ProtectHandler { - public static ProviderMap providers = new ProviderMap<>(); + public static ProviderMap providers = new ProviderMap<>("ProtectHandler"); private static boolean registredHandl = false; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java index 49ed9acd..6e7b7fcd 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java @@ -6,7 +6,7 @@ import java.io.IOException; public abstract class AuthProvider implements AutoCloseable { - public static ProviderMap providers = new ProviderMap<>(); + public static ProviderMap providers = new ProviderMap<>("AuthProvider"); private static boolean registredProv = false; public static AuthProviderResult authError(String message) throws AuthException { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/texture/TextureProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/texture/TextureProvider.java index 607f761e..e334ef73 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/texture/TextureProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/texture/TextureProvider.java @@ -7,7 +7,7 @@ import java.util.UUID; public abstract class TextureProvider implements AutoCloseable { - public static ProviderMap providers = new ProviderMap<>(); + public static ProviderMap providers = new ProviderMap<>("TextureProvider"); private static boolean registredProv = false; public static void registerProviders() { diff --git a/libLauncher/src/main/java/ru/gravit/utils/HookSet.java b/libLauncher/src/main/java/ru/gravit/utils/HookSet.java new file mode 100644 index 00000000..f1e1394c --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/HookSet.java @@ -0,0 +1,29 @@ +package ru.gravit.utils; + +import java.util.HashSet; +import java.util.Set; + +public class HookSet { + public Set> list = new HashSet<>(); + @FunctionalInterface + public interface Hook + { + boolean hook(R context); + } + public void registerHook(Hook hook) + { + list.add(hook); + } + public boolean unregisterHook(Hook hook) + { + return list.remove(hook); + } + public boolean hook(R context) + { + for(Hook hook : list) + { + if(hook.hook(context)) return true; + } + return false; + } +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/ProviderMap.java b/libLauncher/src/main/java/ru/gravit/utils/ProviderMap.java index ce32eef1..f2c2aa1e 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/ProviderMap.java +++ b/libLauncher/src/main/java/ru/gravit/utils/ProviderMap.java @@ -8,13 +8,25 @@ public class ProviderMap { protected final Map> PROVIDERS = new ConcurrentHashMap<>(4); + protected final String name; protected boolean registredProviders = false; + public ProviderMap(String name) { + this.name = name; + } + + public ProviderMap() { + this.name = "Unnamed"; + } + public String getName() + { + return name; + } public void registerProvider(String name, Class adapter) { VerifyHelper.verifyIDName(name); VerifyHelper.putIfAbsent(PROVIDERS, name, Objects.requireNonNull(adapter, "adapter"), - String.format("Protect handler has been already registered: '%s'", name)); + String.format("%s has been already registered: '%s'", this.name, name)); } public Class getProviderClass(String name) { diff --git a/libLauncher/src/main/java/ru/gravit/utils/UniversalJsonAdapter.java b/libLauncher/src/main/java/ru/gravit/utils/UniversalJsonAdapter.java index 00b864ab..f3a1003d 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/UniversalJsonAdapter.java +++ b/libLauncher/src/main/java/ru/gravit/utils/UniversalJsonAdapter.java @@ -7,15 +7,18 @@ public class UniversalJsonAdapter implements JsonSerializer, JsonDeserializer { public final ProviderMap providerMap; + public final String name; public final String PROP_NAME; public UniversalJsonAdapter(ProviderMap providerMap) { this.providerMap = providerMap; + this.name = providerMap.getName(); this.PROP_NAME = "type"; } public UniversalJsonAdapter(ProviderMap providerMap, String PROP_NAME) { this.providerMap = providerMap; + this.name = providerMap.getName(); this.PROP_NAME = PROP_NAME; } @@ -23,7 +26,7 @@ public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); Class cls = providerMap.getProviderClass(typename); if (cls == null) { - LogHelper.error("Provider %s not found", typename); + LogHelper.error("%s %s not found", name, typename); return null; } return context.deserialize(json, cls);