Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions src/main/java/org/dimdev/rift/mixin/optifine/MixinVanillaPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
import net.minecraft.resources.VanillaPack;
import net.minecraft.util.ResourceLocation;

import org.dimdev.riftloader.OptifineLoader;
import org.dimdev.utils.ReflectionUtils;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.file.Files;
Expand All @@ -34,7 +30,7 @@ public class MixinVanillaPack {
*/
@Nullable
@Overwrite(constraints = "OPTIFINE(1+)")
protected InputStream getInputStreamVanilla(ResourcePackType type, ResourceLocation location) {
public InputStream getInputStreamVanilla(ResourcePackType type, ResourceLocation location) {
String pathString = type.getDirectoryName() + "/" + location.getNamespace() + "/" + location.getPath();

InputStream in = optifineResourceFisher.apply(pathString);
Expand Down Expand Up @@ -64,18 +60,13 @@ protected InputStream getInputStreamVanilla(ResourcePackType type, ResourceLocat
//Ironically Optifine does all this via reflection too, so we're actually no slower than how it otherwise works
try {
//Fish for the type
Class<?> transformer = Class.forName(OptifineLoader.OPTIFINE_TRANSFORMER);
//Then the instance field Optifine helpfully hangs onto
Field instanceField = ReflectionUtils.findField(transformer, transformer);
Object instance = instanceField.get(null);
if (instance == null) throw new IllegalStateException("Transformer hasn't loaded yet?");
Class<?> transformer = Class.forName("net.optifine.reflect.ReflectorForge");
//Now fish for the method
Method method = transformer.getDeclaredMethod("getOptiFineResource", String.class);
Method method = transformer.getDeclaredMethod("getOptiFineResourceStream", String.class);
MethodHandle handle = MethodHandles.lookup().unreflect(method);
optifineResourceFisher = path -> {
try {
byte[] resource = (byte[]) handle.invoke(instance, path);
return resource != null ? new ByteArrayInputStream(resource) : null;
return (InputStream) handle.invoke(path);
} catch (Throwable t) {
throw new RuntimeException("Error getting resource from Optifine", t);
}
Expand Down