Skip to content

Commit 8787712

Browse files
committed
Don't blame us forge!
- Abort loading of mixins if mod loading exceptions are present
1 parent 0a7cc00 commit 8787712

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

common/src/main/java/com/railwayteam/railways/util/MethodVarHandleUtils.java

+47-4
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,66 @@
1818

1919
package com.railwayteam.railways.util;
2020

21+
import com.google.common.cache.Cache;
22+
import com.google.common.cache.CacheBuilder;
23+
import org.jetbrains.annotations.Nullable;
24+
2125
import java.lang.invoke.MethodHandles;
26+
import java.lang.invoke.VarHandle;
27+
import java.util.concurrent.ExecutionException;
2228

2329
@SuppressWarnings("unchecked")
2430
public class MethodVarHandleUtils {
31+
private static final Cache<VarHandleInfo, VarHandle> varHandleCache = CacheBuilder.newBuilder().build();
32+
2533
private static final MethodHandles.Lookup lookup = MethodHandles.lookup();
26-
34+
2735
public static <T> T getStaticField(Class<?> clazz, String fieldName, Class<T> type) throws NoSuchFieldException, IllegalAccessException {
28-
return (T) lookup.findStaticVarHandle(clazz, fieldName, type).get();
36+
T value = null;
37+
38+
try {
39+
value = (T) varHandleCache.get(
40+
new VarHandleInfo(clazz, fieldName, type),
41+
() -> lookup.findStaticVarHandle(clazz, fieldName, type)
42+
).get();
43+
} catch (ExecutionException ignored) {}
44+
45+
return value;
2946
}
3047

3148
public static <T> T getStaticField(Class<?> clazz, String fieldName, Class<T> type, T defaultValue) {
3249
T returnValue = defaultValue;
33-
50+
3451
try {
3552
returnValue = getStaticField(clazz, fieldName, type);
3653
} catch (NoSuchFieldException | IllegalAccessException ignored) {}
37-
54+
3855
return returnValue;
3956
}
57+
58+
public static <T, U> T getPrivateField(U instance, Class<U> clazz, String fieldName, Class<T> type, T defaultValue) {
59+
T returnValue = defaultValue;
60+
61+
VarHandle handle = findPrivateFieldVarHandle(new VarHandleInfo(clazz, fieldName, type));
62+
if (handle != null) {
63+
returnValue = (T) handle.get(instance);
64+
}
65+
66+
return returnValue;
67+
}
68+
69+
@Nullable
70+
public static VarHandle findPrivateFieldVarHandle(VarHandleInfo info) {
71+
try {
72+
return varHandleCache.get(info, () -> {
73+
MethodHandles.Lookup privateLookup = MethodHandles.privateLookupIn(info.clazz(), lookup);
74+
return privateLookup.findVarHandle(info.clazz(), info.fieldName(), info.type());
75+
});
76+
} catch (ExecutionException ignored) {
77+
return null;
78+
}
79+
}
80+
81+
public record VarHandleInfo(Class<?> clazz, String fieldName, Class<?> type) {
82+
}
4083
}

forge/src/main/java/com/railwayteam/railways/forge/mixin/CRMixinPlugin.java

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import com.railwayteam.railways.forge.asm.ContainerLevelAccessASM;
2222
import com.railwayteam.railways.forge.asm.RollingModeEnumAdder;
2323
import com.railwayteam.railways.util.ConditionalMixinManager;
24+
import com.railwayteam.railways.util.MethodVarHandleUtils;
25+
import net.minecraftforge.fml.ModLoader;
26+
import net.minecraftforge.fml.ModLoadingException;
2427
import org.objectweb.asm.tree.ClassNode;
2528
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
2629
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
@@ -37,6 +40,16 @@ public void onLoad(String mixinPackage) { } // NO-OP
3740

3841
@Override
3942
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
43+
//noinspection unchecked
44+
Class<List<ModLoadingException>> modLoadingExceptionListClass = (Class<List<ModLoadingException>>)(Class<?>) List.class;
45+
46+
List<ModLoadingException> modLoadingExceptionList = MethodVarHandleUtils.getPrivateField(
47+
ModLoader.get(), ModLoader.class, "loadingExceptions", modLoadingExceptionListClass, null);
48+
49+
if (!modLoadingExceptionList.isEmpty()) {
50+
return false;
51+
}
52+
4053
return ConditionalMixinManager.shouldApply(mixinClassName);
4154
}
4255

0 commit comments

Comments
 (0)