|
| 1 | +package zone.moddev.mc.orespawn.api; |
| 2 | + |
| 3 | +import java.lang.reflect.InvocationHandler; |
| 4 | +import java.lang.reflect.InvocationTargetException; |
| 5 | +import java.lang.reflect.Method; |
| 6 | +import java.lang.reflect.Modifier; |
| 7 | +import java.lang.reflect.Proxy; |
| 8 | +import java.util.IdentityHashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.function.Function; |
| 12 | +import java.util.function.Supplier; |
| 13 | + |
| 14 | +import com.google.gson.JsonElement; |
| 15 | +import com.mojang.serialization.Codec; |
| 16 | + |
| 17 | +/** |
| 18 | + * Keeps the legacy public Codec descriptor while avoiding class-versus-interface |
| 19 | + * invocation bytecode in OreSpawn's own startup and profile decoding paths. |
| 20 | + */ |
| 21 | +final class LegacyCodecBridge { |
| 22 | + private static final Map<Object, Function<JsonElement, ?>> DECODERS = new IdentityHashMap<>(); |
| 23 | + |
| 24 | + private LegacyCodecBridge() { |
| 25 | + } |
| 26 | + |
| 27 | + @SuppressWarnings("unchecked") |
| 28 | + static <A> Codec<A> create(Function<JsonElement, A> decoder) { |
| 29 | + if (decoder == null) throw new NullPointerException("decoder"); |
| 30 | + Class<?> codecType = Codec.class; |
| 31 | + Object codec; |
| 32 | + try { |
| 33 | + if (codecType.isInterface()) { |
| 34 | + codec = Proxy.newProxyInstance(codecType.getClassLoader(), new Class<?>[] { codecType }, |
| 35 | + new CodecInvocationHandler<>(decoder)); |
| 36 | + } else { |
| 37 | + Method factory = codecType.getMethod("of", Function.class); |
| 38 | + codec = factory.invoke(null, decoder); |
| 39 | + } |
| 40 | + } catch (ReflectiveOperationException exception) { |
| 41 | + throw linkageFailure("create the legacy ore-pattern codec", codecType, exception); |
| 42 | + } |
| 43 | + synchronized (DECODERS) { |
| 44 | + DECODERS.put(codec, decoder); |
| 45 | + } |
| 46 | + return (Codec<A>) codec; |
| 47 | + } |
| 48 | + |
| 49 | + static Object decode(Object codec, JsonElement input) { |
| 50 | + Function<JsonElement, ?> decoder; |
| 51 | + synchronized (DECODERS) { |
| 52 | + decoder = DECODERS.get(codec); |
| 53 | + } |
| 54 | + if (decoder != null) { |
| 55 | + return decoder.apply(input); |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + ClassLoader loader = codec.getClass().getClassLoader(); |
| 60 | + Class<?> jsonOps = Class.forName("com.mojang.serialization.JsonOps", true, loader); |
| 61 | + Object operations = jsonOps.getField("INSTANCE").get(null); |
| 62 | + Method parse = findParse(codec.getClass(), operations, input); |
| 63 | + Object result = parse.invoke(codec, operations, input); |
| 64 | + return unwrapResult(result); |
| 65 | + } catch (InvocationTargetException exception) { |
| 66 | + Throwable cause = exception.getCause() == null ? exception : exception.getCause(); |
| 67 | + throw new IllegalArgumentException(message(cause), cause); |
| 68 | + } catch (ReflectiveOperationException exception) { |
| 69 | + throw linkageFailure("decode legacy ore-pattern settings", codec.getClass(), exception); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static Method findParse(Class<?> codecType, Object operations, JsonElement input) |
| 74 | + throws NoSuchMethodException { |
| 75 | + for (Method method : codecType.getMethods()) { |
| 76 | + if (!"parse".equals(method.getName()) || method.getParameterCount() != 2) continue; |
| 77 | + Class<?>[] parameters = method.getParameterTypes(); |
| 78 | + if (parameters[0].isInstance(operations) |
| 79 | + && (input == null || parameters[1].isInstance(input) |
| 80 | + || parameters[1] == Object.class)) { |
| 81 | + return method; |
| 82 | + } |
| 83 | + } |
| 84 | + throw new NoSuchMethodException(codecType.getName() |
| 85 | + + " has no JSON-compatible parse(operations, input) method"); |
| 86 | + } |
| 87 | + |
| 88 | + private static Object unwrapResult(Object dataResult) throws ReflectiveOperationException { |
| 89 | + if (dataResult == null) { |
| 90 | + throw new IllegalArgumentException("Codec returned no result"); |
| 91 | + } |
| 92 | + Object value = dataResult.getClass().getMethod("result").invoke(dataResult); |
| 93 | + if (value instanceof Optional && ((Optional<?>) value).isPresent()) { |
| 94 | + return ((Optional<?>) value).get(); |
| 95 | + } |
| 96 | + Object error = dataResult.getClass().getMethod("error").invoke(dataResult); |
| 97 | + String detail = error instanceof Optional && ((Optional<?>) error).isPresent() |
| 98 | + ? String.valueOf(((Optional<?>) error).get()) : "unknown codec error"; |
| 99 | + throw new IllegalArgumentException(detail); |
| 100 | + } |
| 101 | + |
| 102 | + private static IllegalStateException linkageFailure(String action, Class<?> codecType, |
| 103 | + ReflectiveOperationException exception) { |
| 104 | + String origin = "unknown origin"; |
| 105 | + if (codecType.getProtectionDomain() != null |
| 106 | + && codecType.getProtectionDomain().getCodeSource() != null) { |
| 107 | + origin = String.valueOf(codecType.getProtectionDomain().getCodeSource().getLocation()); |
| 108 | + } |
| 109 | + return new IllegalStateException("Cannot " + action + " using " + codecType.getName() |
| 110 | + + " from " + origin, exception); |
| 111 | + } |
| 112 | + |
| 113 | + private static String message(Throwable failure) { |
| 114 | + String text = failure.getMessage(); |
| 115 | + return text == null || text.trim().isEmpty() ? failure.getClass().getSimpleName() : text; |
| 116 | + } |
| 117 | + |
| 118 | + private static final class CodecInvocationHandler<A> implements InvocationHandler { |
| 119 | + private final Function<JsonElement, A> decoder; |
| 120 | + |
| 121 | + private CodecInvocationHandler(Function<JsonElement, A> decoder) { |
| 122 | + this.decoder = decoder; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable { |
| 127 | + if (method.getDeclaringClass() == Object.class) { |
| 128 | + switch (method.getName()) { |
| 129 | + case "toString": return "OreSpawn legacy Codec bridge"; |
| 130 | + case "hashCode": return System.identityHashCode(proxy); |
| 131 | + case "equals": return proxy == arguments[0]; |
| 132 | + default: throw new UnsupportedOperationException(method.toString()); |
| 133 | + } |
| 134 | + } |
| 135 | + if ("parse".equals(method.getName()) && arguments != null && arguments.length == 2 |
| 136 | + && arguments[1] instanceof JsonElement) { |
| 137 | + try { |
| 138 | + return dataResult(method.getReturnType(), decoder.apply((JsonElement) arguments[1]), null); |
| 139 | + } catch (RuntimeException exception) { |
| 140 | + return dataResult(method.getReturnType(), null, message(exception)); |
| 141 | + } |
| 142 | + } |
| 143 | + throw new UnsupportedOperationException("OreSpawn's legacy Codec bridge supports JSON parse only: " |
| 144 | + + method); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static Object dataResult(Class<?> resultType, Object value, String error) |
| 149 | + throws ReflectiveOperationException { |
| 150 | + if (error == null) { |
| 151 | + return invokeStaticFactory(resultType, "success", value, null); |
| 152 | + } |
| 153 | + return invokeStaticFactory(resultType, "error", error, () -> error); |
| 154 | + } |
| 155 | + |
| 156 | + private static Object invokeStaticFactory(Class<?> type, String name, Object direct, |
| 157 | + Supplier<String> supplied) throws ReflectiveOperationException { |
| 158 | + for (Method method : type.getMethods()) { |
| 159 | + if (!name.equals(method.getName()) || !Modifier.isStatic(method.getModifiers()) |
| 160 | + || method.getParameterCount() != 1) continue; |
| 161 | + Class<?> parameter = method.getParameterTypes()[0]; |
| 162 | + if (supplied != null && Supplier.class.isAssignableFrom(parameter)) { |
| 163 | + return method.invoke(null, supplied); |
| 164 | + } |
| 165 | + if (direct == null || parameter.isInstance(direct) || parameter == Object.class) { |
| 166 | + return method.invoke(null, direct); |
| 167 | + } |
| 168 | + } |
| 169 | + throw new NoSuchMethodException(type.getName() + "." + name + "(value)"); |
| 170 | + } |
| 171 | +} |
0 commit comments