Support 1.21.9. (#347)

This commit is contained in:
ZekerZhayard authored and GitHub committed 2025-10-07 08:31:05 +09:00
1 parent 1fbe258365
commit 1f3d19ac6e
60 files changed
+635 -722

No files matched your search

@@ -1,14 +1,10 @@
package customskinloader.utils;
import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.UUID;
import java.util.function.Supplier;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.authlib.yggdrasil.response.MinecraftProfilePropertiesResponse;
@@ -42,42 +38,25 @@ public class TextureUtil {
}
}
private final static MethodHandles.Lookup IMPL_LOOKUP = ((Supplier<MethodHandles.Lookup>) () -> {
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field theUnsafeField = unsafeClass.getDeclaredField("theUnsafe");
theUnsafeField.setAccessible(true);
Object theUnsafe = theUnsafeField.get(null);
Method getObjectMethod = unsafeClass.getMethod("getObject", Object.class, long.class);
Method staticFieldBaseMethod = unsafeClass.getMethod("staticFieldBase", Field.class);
Method staticFieldOffsetMethod = unsafeClass.getMethod("staticFieldOffset", Field.class);
Field implLookupField = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
return (MethodHandles.Lookup) getObjectMethod.invoke(theUnsafe, staticFieldBaseMethod.invoke(theUnsafe, implLookupField), staticFieldOffsetMethod.invoke(theUnsafe, implLookupField));
} catch (Throwable t) {
throw new RuntimeException(t);
}
}).get();
// Some of the classes in Authlib used after Minecraft 23w31a were changed to record classes,
// resulting in changes to method names, so reflection is used here to be compatible with these changes.
public enum AuthlibField {
PROPERTY_NAME(Property.class, "name", String.class),
PROPERTY_VALUE(Property.class, "value", String.class),
PROPERTY_SIGNATURE(Property.class, "signature", String.class),
MINECRAFT_PROFILE_PROPERTIES_RESPONSE_ID(MinecraftProfilePropertiesResponse.class, "id", UUID.class),
MINECRAFT_PROFILE_PROPERTIES_RESPONSE_NAME(MinecraftProfilePropertiesResponse.class, "name", String.class),
MINECRAFT_PROFILE_PROPERTIES_RESPONSE_PROPERTIES(MinecraftProfilePropertiesResponse.class, "properties", PropertyMap.class),
MINECRAFT_TEXTURES_PAYLOAD_TEXTURES(MinecraftTexturesPayload.class, "textures", Map.class);
GAME_PROFILE_ID(GameProfile.class, "id"),
GAME_PROFILE_NAME(GameProfile.class, "name"),
GAME_PROFILE_PROPERTIES(GameProfile.class, "properties"),
PROPERTY_VALUE(Property.class, "value"),
PROPERTY_MAP_PROPERTIES(PropertyMap.class, "properties"),
MINECRAFT_PROFILE_PROPERTIES_RESPONSE_ID(MinecraftProfilePropertiesResponse.class, "id"),
MINECRAFT_PROFILE_PROPERTIES_RESPONSE_NAME(MinecraftProfilePropertiesResponse.class, "name"),
MINECRAFT_PROFILE_PROPERTIES_RESPONSE_PROPERTIES(MinecraftProfilePropertiesResponse.class, "properties"),
MINECRAFT_TEXTURES_PAYLOAD_TEXTURES(MinecraftTexturesPayload.class, "textures");
private final MethodHandle getter;
private final MethodHandle setter;
private final Field field;
AuthlibField(Class<?> clazz, String name, Class<?> returnType) {
AuthlibField(Class<?> clazz, String name) {
try {
this.getter = IMPL_LOOKUP.findGetter(clazz, name, returnType);
this.setter = IMPL_LOOKUP.findSetter(clazz, name, returnType);
this.field = clazz.getDeclaredField(name);
this.field.setAccessible(true);
} catch (Throwable t) {
throw new RuntimeException(t);
}
@@ -86,7 +65,7 @@ public class TextureUtil {
@SuppressWarnings("unchecked")
public <R> R get(Object o) {
try {
return (R) this.getter.invokeWithArguments(o);
return (R) this.field.get(o);
} catch (Throwable t) {
throw new RuntimeException(t);
}
@@ -94,7 +73,7 @@ public class TextureUtil {
public void set(Object o, Object value) {
try {
this.setter.invokeWithArguments(o, value);
this.field.set(o, value);
} catch (Throwable t) {
throw new RuntimeException(t);
}