mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE] Именнованный провайдеры и HookSet
This commit is contained in:
parent
dbdf3f3c89
commit
f32844aa15
9 changed files with 52 additions and 8 deletions
|
@ -8,7 +8,7 @@
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public abstract class AuthHandler implements AutoCloseable {
|
public abstract class AuthHandler implements AutoCloseable {
|
||||||
public static ProviderMap<AuthHandler> providers = new ProviderMap<>();
|
public static ProviderMap<AuthHandler> providers = new ProviderMap<>("AuthHandler");
|
||||||
private static boolean registredHandl = false;
|
private static boolean registredHandl = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class HWIDHandler implements AutoCloseable {
|
public abstract class HWIDHandler implements AutoCloseable {
|
||||||
public static ProviderMap<HWIDHandler> providers = new ProviderMap<>();
|
public static ProviderMap<HWIDHandler> providers = new ProviderMap<>("HWIDHandler");
|
||||||
private static boolean registredHandl = false;
|
private static boolean registredHandl = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
import ru.gravit.utils.ProviderMap;
|
import ru.gravit.utils.ProviderMap;
|
||||||
|
|
||||||
public abstract class PermissionsHandler implements AutoCloseable {
|
public abstract class PermissionsHandler implements AutoCloseable {
|
||||||
public static ProviderMap<PermissionsHandler> providers = new ProviderMap<>();
|
public static ProviderMap<PermissionsHandler> providers = new ProviderMap<>("PermissionsHandler");
|
||||||
private static boolean registredHandl = false;
|
private static boolean registredHandl = false;
|
||||||
|
|
||||||
public static void registerHandlers() {
|
public static void registerHandlers() {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
import ru.gravit.utils.ProviderMap;
|
import ru.gravit.utils.ProviderMap;
|
||||||
|
|
||||||
public abstract class ProtectHandler {
|
public abstract class ProtectHandler {
|
||||||
public static ProviderMap<ProtectHandler> providers = new ProviderMap<>();
|
public static ProviderMap<ProtectHandler> providers = new ProviderMap<>("ProtectHandler");
|
||||||
private static boolean registredHandl = false;
|
private static boolean registredHandl = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public abstract class AuthProvider implements AutoCloseable {
|
public abstract class AuthProvider implements AutoCloseable {
|
||||||
public static ProviderMap<AuthProvider> providers = new ProviderMap<>();
|
public static ProviderMap<AuthProvider> providers = new ProviderMap<>("AuthProvider");
|
||||||
private static boolean registredProv = false;
|
private static boolean registredProv = false;
|
||||||
|
|
||||||
public static AuthProviderResult authError(String message) throws AuthException {
|
public static AuthProviderResult authError(String message) throws AuthException {
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public abstract class TextureProvider implements AutoCloseable {
|
public abstract class TextureProvider implements AutoCloseable {
|
||||||
public static ProviderMap<TextureProvider> providers = new ProviderMap<>();
|
public static ProviderMap<TextureProvider> providers = new ProviderMap<>("TextureProvider");
|
||||||
private static boolean registredProv = false;
|
private static boolean registredProv = false;
|
||||||
|
|
||||||
public static void registerProviders() {
|
public static void registerProviders() {
|
||||||
|
|
29
libLauncher/src/main/java/ru/gravit/utils/HookSet.java
Normal file
29
libLauncher/src/main/java/ru/gravit/utils/HookSet.java
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package ru.gravit.utils;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class HookSet<R> {
|
||||||
|
public Set<Hook<R>> list = new HashSet<>();
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Hook<R>
|
||||||
|
{
|
||||||
|
boolean hook(R context);
|
||||||
|
}
|
||||||
|
public void registerHook(Hook<R> hook)
|
||||||
|
{
|
||||||
|
list.add(hook);
|
||||||
|
}
|
||||||
|
public boolean unregisterHook(Hook<R> hook)
|
||||||
|
{
|
||||||
|
return list.remove(hook);
|
||||||
|
}
|
||||||
|
public boolean hook(R context)
|
||||||
|
{
|
||||||
|
for(Hook<R> hook : list)
|
||||||
|
{
|
||||||
|
if(hook.hook(context)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,13 +8,25 @@
|
||||||
|
|
||||||
public class ProviderMap<R> {
|
public class ProviderMap<R> {
|
||||||
protected final Map<String, Class<? extends R>> PROVIDERS = new ConcurrentHashMap<>(4);
|
protected final Map<String, Class<? extends R>> PROVIDERS = new ConcurrentHashMap<>(4);
|
||||||
|
protected final String name;
|
||||||
protected boolean registredProviders = false;
|
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<? extends R> adapter) {
|
public void registerProvider(String name, Class<? extends R> adapter) {
|
||||||
VerifyHelper.verifyIDName(name);
|
VerifyHelper.verifyIDName(name);
|
||||||
VerifyHelper.putIfAbsent(PROVIDERS, name, Objects.requireNonNull(adapter, "adapter"),
|
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<? extends R> getProviderClass(String name) {
|
public Class<? extends R> getProviderClass(String name) {
|
||||||
|
|
|
@ -7,15 +7,18 @@
|
||||||
|
|
||||||
public class UniversalJsonAdapter<R> implements JsonSerializer<R>, JsonDeserializer<R> {
|
public class UniversalJsonAdapter<R> implements JsonSerializer<R>, JsonDeserializer<R> {
|
||||||
public final ProviderMap<R> providerMap;
|
public final ProviderMap<R> providerMap;
|
||||||
|
public final String name;
|
||||||
public final String PROP_NAME;
|
public final String PROP_NAME;
|
||||||
|
|
||||||
public UniversalJsonAdapter(ProviderMap<R> providerMap) {
|
public UniversalJsonAdapter(ProviderMap<R> providerMap) {
|
||||||
this.providerMap = providerMap;
|
this.providerMap = providerMap;
|
||||||
|
this.name = providerMap.getName();
|
||||||
this.PROP_NAME = "type";
|
this.PROP_NAME = "type";
|
||||||
}
|
}
|
||||||
|
|
||||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, String PROP_NAME) {
|
public UniversalJsonAdapter(ProviderMap<R> providerMap, String PROP_NAME) {
|
||||||
this.providerMap = providerMap;
|
this.providerMap = providerMap;
|
||||||
|
this.name = providerMap.getName();
|
||||||
this.PROP_NAME = PROP_NAME;
|
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();
|
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
|
||||||
Class<? extends R> cls = providerMap.getProviderClass(typename);
|
Class<? extends R> cls = providerMap.getProviderClass(typename);
|
||||||
if (cls == null) {
|
if (cls == null) {
|
||||||
LogHelper.error("Provider %s not found", typename);
|
LogHelper.error("%s %s not found", name, typename);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return context.deserialize(json, cls);
|
return context.deserialize(json, cls);
|
||||||
|
|
Loading…
Reference in a new issue