浏览代码

Merge branch 'main' into 1.17

malte0811 3 年之前
父节点
当前提交
d4330d2639

+ 17 - 0
common/src/main/java/malte0811/ferritecore/ducks/BlockStateCacheAccess.java

@@ -0,0 +1,17 @@
+package malte0811.ferritecore.ducks;
+
+import net.minecraft.world.phys.shapes.VoxelShape;
+
+import javax.annotation.Nullable;
+
+// This should be an accessor Mixin, but some part of the toolchain does not handle setters for fields of inner classes
+// properly
+public interface BlockStateCacheAccess {
+    VoxelShape getCollisionShape();
+
+    void setCollisionShape(VoxelShape newShape);
+
+    VoxelShape[] getOcclusionShapes();
+
+    void setOcclusionShapes(@Nullable VoxelShape[] newShapes);
+}

+ 1 - 1
common/src/main/java/malte0811/ferritecore/impl/BlockStateCacheImpl.java

@@ -2,10 +2,10 @@ package malte0811.ferritecore.impl;
 
 import com.google.common.base.Suppliers;
 import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
+import malte0811.ferritecore.ducks.BlockStateCacheAccess;
 import malte0811.ferritecore.hash.ArrayVoxelShapeHash;
 import malte0811.ferritecore.hash.VoxelShapeHash;
 import malte0811.ferritecore.mixin.blockstatecache.ArrayVSAccess;
-import malte0811.ferritecore.mixin.blockstatecache.BlockStateCacheAccess;
 import malte0811.ferritecore.mixin.blockstatecache.SliceShapeAccess;
 import malte0811.ferritecore.util.Constants;
 import net.minecraft.world.level.block.state.BlockBehaviour.BlockStateBase;

+ 0 - 27
common/src/main/java/malte0811/ferritecore/mixin/blockstatecache/BlockStateCacheAccess.java

@@ -1,27 +0,0 @@
-package malte0811.ferritecore.mixin.blockstatecache;
-
-import net.minecraft.world.phys.shapes.VoxelShape;
-import org.spongepowered.asm.mixin.Mixin;
-import org.spongepowered.asm.mixin.Mutable;
-import org.spongepowered.asm.mixin.gen.Accessor;
-
-import javax.annotation.Nullable;
-
-// TODO this does not work in Dev, but BB$BSB$C causes build issues
-@Mixin(targets = "net.minecraft.world.level.block.state.BlockBehaviour.BlockStateBase.Cache")
-public interface BlockStateCacheAccess {
-    @Accessor
-    VoxelShape getCollisionShape();
-
-    @Accessor
-    @Mutable
-    void setCollisionShape(VoxelShape newShape);
-
-    @Accessor
-    @Nullable
-    VoxelShape[] getOcclusionShapes();
-
-    @Accessor
-    @Mutable
-    void setOcclusionShapes(@Nullable VoxelShape[] newShapes);
-}

+ 43 - 0
common/src/main/java/malte0811/ferritecore/mixin/blockstatecache/BlockStateCacheMixin.java

@@ -0,0 +1,43 @@
+package malte0811.ferritecore.mixin.blockstatecache;
+
+import malte0811.ferritecore.ducks.BlockStateCacheAccess;
+import net.minecraft.world.phys.shapes.VoxelShape;
+import org.jetbrains.annotations.Nullable;
+import org.spongepowered.asm.mixin.Final;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Mutable;
+import org.spongepowered.asm.mixin.Shadow;
+
+@Mixin(targets = "net.minecraft.world.level.block.state.BlockBehaviour$BlockStateBase$Cache")
+public class BlockStateCacheMixin implements BlockStateCacheAccess {
+    @Shadow
+    @Final
+    @Mutable
+    protected VoxelShape collisionShape;
+
+    @Shadow
+    @Final
+    @Mutable
+    @Nullable
+    VoxelShape[] occlusionShapes;
+
+    @Override
+    public VoxelShape getCollisionShape() {
+        return this.collisionShape;
+    }
+
+    @Override
+    public void setCollisionShape(VoxelShape newShape) {
+        this.collisionShape = newShape;
+    }
+
+    @Override
+    public VoxelShape[] getOcclusionShapes() {
+        return this.occlusionShapes;
+    }
+
+    @Override
+    public void setOcclusionShapes(@Nullable VoxelShape[] newShapes) {
+        this.occlusionShapes = newShapes;
+    }
+}

+ 11 - 14
common/src/main/java/malte0811/ferritecore/mixin/dedupmultipart/MixinMultipartModel.java

@@ -2,14 +2,10 @@ package malte0811.ferritecore.mixin.dedupmultipart;
 
 import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
 import net.minecraft.client.resources.model.MultiPartBakedModel;
-import net.minecraft.world.level.block.state.BlockState;
-import org.spongepowered.asm.mixin.Final;
 import org.spongepowered.asm.mixin.Mixin;
-import org.spongepowered.asm.mixin.Shadow;
 import org.spongepowered.asm.mixin.injection.At;
 import org.spongepowered.asm.mixin.injection.Redirect;
 
-import java.util.BitSet;
 import java.util.Map;
 
 /**
@@ -22,25 +18,26 @@ import java.util.Map;
  * concurrent get-calls can (and will) crash, so we need to synchronize them.<br>
  * It is not clear if this implementation (naive synchronization on the cache) is optimal w.r.t.
  * runtime/parallelization, in my experience this part of the code is not runtime-critical enough to put significant
- * effort into fancy parallelization solutions (may change in the future).
+ * effort into fancy parallelization solutions (may change in the future).<br>
+ * The increased priority takes care of compatibility with sodium's prio 1000 overwrite. Some versions of sodium come
+ * with their own synchronization, but disabling the FC synchronization when sodium's is present is near-impossible. Any
+ * decent JIT should be able to remove the inner synchronization since both are on the same final field, so performance
+ * should not be an issue.
  */
-// Non-final fields: Work around Java/Mixin limitations
 // Unresolved reference: Forge adds a parameter to getQuads, so the usual remapping process breaks and I need to specify
 // SRG and intermediary names directly, which confuses the MCDev IntelliJ plugin
-@SuppressWarnings({"SynchronizeOnNonFinalField", "UnresolvedMixinReference"})
-@Mixin(MultiPartBakedModel.class)
+// Sync on local/parameter: The parameter is actually always a final field, but which one it is depends on whether
+// sodium is installed or not.
+@SuppressWarnings({"UnresolvedMixinReference", "SynchronizationOnLocalVariableOrMethodParameter"})
+@Mixin(value = MultiPartBakedModel.class, priority = 1100)
 public class MixinMultipartModel {
-    @Shadow
-    @Final
-    private Map<BlockState, BitSet> selectorCache;
-
     @Redirect(
             method = {"method_4707", "func_200117_a", "getQuads"},
             at = @At(value = "INVOKE", target = "Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"),
             remap = false
     )
     public <K, V> V redirectCacheGet(Map<K, V> map, K key) {
-        synchronized (selectorCache) {
+        synchronized (map) {
             return map.get(key);
         }
     }
@@ -51,7 +48,7 @@ public class MixinMultipartModel {
             remap = false
     )
     public <K, V> V redirectCachePut(Map<K, V> map, K key, V value) {
-        synchronized (selectorCache) {
+        synchronized (map) {
             return map.put(key, value);
         }
     }

+ 1 - 1
common/src/main/resources/ferritecore.blockstatecache.mixin.json

@@ -13,7 +13,7 @@
     "ArrayVSAccess",
     "BitSetDVSAccess",
     "BlockStateBaseMixin",
-    "BlockStateCacheAccess",
+    "BlockStateCacheMixin",
     "DiscreteVSAccess",
     "SliceShapeAccess",
     "SubShapeAccess",