aboutsummaryrefslogtreecommitdiff
path: root/src/compat/citresewn/java/ConditionDirectAccessHelper.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-09-12 13:34:11 +0200
committerLinnea Gräf <nea@nea.moe>2024-09-12 13:34:11 +0200
commit52623b715d395f65f96e09f4492285935b797b4b (patch)
tree1b96097a17dd9c81671478b11d127ce3278ddada /src/compat/citresewn/java/ConditionDirectAccessHelper.kt
parentab809e197f3e8c9a51745ee9510bc65c952c0cc8 (diff)
downloadFirmament-fix/citresewn.tar.gz
Firmament-fix/citresewn.tar.bz2
Firmament-fix/citresewn.zip
Isolate CIT resewn compatfix/citresewn
Diffstat (limited to 'src/compat/citresewn/java/ConditionDirectAccessHelper.kt')
-rw-r--r--src/compat/citresewn/java/ConditionDirectAccessHelper.kt59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/compat/citresewn/java/ConditionDirectAccessHelper.kt b/src/compat/citresewn/java/ConditionDirectAccessHelper.kt
new file mode 100644
index 0000000..af97a40
--- /dev/null
+++ b/src/compat/citresewn/java/ConditionDirectAccessHelper.kt
@@ -0,0 +1,59 @@
+package moe.nea.firmament.compat.citresewn
+
+import java.lang.invoke.MethodHandles
+import java.util.function.BiPredicate
+import java.util.function.Function
+import shcm.shsupercm.fabric.citresewn.defaults.cit.conditions.ConditionNBT
+
+object ConditionNBTMixin {
+ class Helper<StringMatcher> {
+ // TODO: make lambdametafactory work by way of modifying the actual modifiers
+
+ val stringMatcherType = ConditionNBT::class.java.getDeclaredField("matchString").type
+
+ val accessMatcher = run {
+ val matchStringF = ConditionNBT::class.java.getDeclaredField("matchString");
+ matchStringF.isAccessible = true
+ val l = MethodHandles.privateLookupIn(ConditionNBT::class.java, MethodHandles.lookup())
+// val mt = MethodType.methodType(stringMatcherType, ConditionNBT::class.java)
+// val callsite = LambdaMetafactory.metafactory(
+// l, "apply",
+// MethodType.methodType(Function::class.java),
+// MethodType.methodType(java.lang.Object::class.java, java.lang.Object::class.java),
+// l.unreflectGetter(matchStringF),
+// mt
+// )
+ val getter = l.unreflectGetter(matchStringF)
+ Function<ConditionNBT, StringMatcher> { getter.invoke(it) as StringMatcher }
+ }
+ val directCaller = run {
+ val matchM = stringMatcherType.getDeclaredMethod("matches", String::class.java);
+ matchM.isAccessible = true
+ val l = MethodHandles.privateLookupIn(ConditionNBT::class.java, MethodHandles.lookup())
+// val mt = MethodType.methodType(java.lang.Boolean.TYPE, stringMatcherType, String::class.java)
+// val callsite = LambdaMetafactory.metafactory(
+// l, "test",
+// MethodType.methodType(BiPredicate::class.java),
+// mt,
+// l.unreflect(matchM),
+// mt
+// )
+ val func = l.unreflect(matchM)
+ BiPredicate<StringMatcher, String> { a, b -> func.invoke(a, b) as Boolean }
+ }
+
+ fun test(condition: ConditionNBT, text: String): Boolean {
+ return directCaller.test(accessMatcher.apply(condition), text) as Boolean
+ }
+ }
+
+ val helper = Helper<Any>()
+
+ @JvmStatic
+ fun invokeDirectConditionNBTStringMatch(
+ nbt: ConditionNBT,
+ text: String,
+ ): Boolean {
+ return helper.test(nbt, text)
+ }
+}