diff --git a/src/main/java/org/embeddedt/modernfix/ModernFixClient.java b/src/main/java/org/embeddedt/modernfix/ModernFixClient.java index ef61b7ae..9ade2cf7 100644 --- a/src/main/java/org/embeddedt/modernfix/ModernFixClient.java +++ b/src/main/java/org/embeddedt/modernfix/ModernFixClient.java @@ -1,7 +1,6 @@ package org.embeddedt.modernfix; import com.mojang.datafixers.util.Pair; -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.ConnectScreen; @@ -107,7 +106,19 @@ public class ModernFixClient { * * This is to ensure we can perform ID fixup on already constructed managers. */ - public static Set allEntityDatas = Collections.newSetFromMap(new WeakHashMap<>()); + public static final Set allEntityDatas = Collections.newSetFromMap(new WeakHashMap<>()); + + private static final Field entriesArrayField; + static { + Field field; + try { + field = SynchedEntityData.class.getDeclaredField("entriesArray"); + field.setAccessible(true); + } catch(ReflectiveOperationException e) { + field = null; + } + entriesArrayField = field; + } /** * Extremely hacky method to detect and correct mismatched entity data parameter IDs on the client and server. @@ -142,12 +153,28 @@ public class ModernFixClient { if(fixNeeded) { dataEntries = new ArrayList<>(allEntityDatas); for(SynchedEntityData manager : dataEntries) { - Int2ObjectMap> fixedMap = new Int2ObjectOpenHashMap<>(); + Int2ObjectOpenHashMap> fixedMap = new Int2ObjectOpenHashMap<>(); List> items = new ArrayList<>(manager.itemsById.values()); for(SynchedEntityData.DataItem item : items) { fixedMap.put(item.getAccessor().id, item); } - manager.itemsById = fixedMap; + manager.lock.writeLock().lock(); + try { + manager.itemsById.replaceAll((id, parameter) -> fixedMap.get((int)id)); + if(entriesArrayField != null) { + try { + SynchedEntityData.DataItem[] dataArray = new SynchedEntityData.DataItem[items.size()]; + for(int i = 0; i < dataArray.length; i++) { + dataArray[i] = fixedMap.get(i); + } + entriesArrayField.set(manager, dataArray); + } catch(ReflectiveOperationException e) { + ModernFix.LOGGER.error(e); + } + } + } finally { + manager.lock.writeLock().unlock(); + } } } allEntityDatas.clear(); diff --git a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index 1e525ee4..842f1b9c 100644 --- a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -56,6 +56,7 @@ public class ModernFixEarlyConfig { disableIfModPresent("mixin.perf.thread_priorities", "smoothboot"); disableIfModPresent("mixin.perf.async_jei", "modernui"); disableIfModPresent("mixin.perf.compress_biome_container", "chocolate"); + disableIfModPresent("mixin.bugfix.mc218112", "performant"); } private void disableIfModPresent(String configName, String... ids) { diff --git a/src/main/java/org/embeddedt/modernfix/textures/StbStitcher.java b/src/main/java/org/embeddedt/modernfix/textures/StbStitcher.java index cc386233..d2fbb230 100644 --- a/src/main/java/org/embeddedt/modernfix/textures/StbStitcher.java +++ b/src/main/java/org/embeddedt/modernfix/textures/StbStitcher.java @@ -11,12 +11,119 @@ import org.lwjgl.stb.STBRPNode; import org.lwjgl.stb.STBRPRect; import org.lwjgl.stb.STBRectPack; +import static java.lang.invoke.MethodHandles.*; +import static java.lang.invoke.MethodType.*; + +import java.lang.invoke.MethodHandle; +import java.sql.Ref; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; /* Source: https://github.com/GTNewHorizons/lwjgl3ify/blob/f21364cd3d178aef863458a2faa1f5718a4e350d/src/main/java/me/eigenraven/lwjgl3ify/textures/StbStitcher.java */ public class StbStitcher { + /* Most of this logic is to allow use of LWJGL versions where coordinates are short and versions where they are int */ + private static final MethodHandle MH_rect_shortSet, MH_rect_intSet, MH_rect_intX, MH_rect_intY, MH_rect_shortX, + MH_rect_shortY; + + static { + MethodHandle shortM = null, intM = null; + List exceptions = new ArrayList<>(); + try { + intM = publicLookup().findVirtual(STBRPRect.class, "set", methodType(STBRPRect.class, + int.class, /* id */ + int.class, + int.class, + int.class, + int.class, + boolean.class)); + } catch(ReflectiveOperationException e) { + exceptions.add(e); + } + try { + shortM = publicLookup().findVirtual(STBRPRect.class, "set", methodType(STBRPRect.class, + int.class, /* id */ + short.class, + short.class, + short.class, + short.class, + boolean.class)); + } catch(ReflectiveOperationException e) { + exceptions.add(e); + } + if(shortM == null && intM == null) { + IllegalStateException e = new IllegalStateException("An STBRPRect set method could not be located"); + exceptions.forEach(e::addSuppressed); + throw e; + } + MH_rect_shortSet = shortM; + MH_rect_intSet = intM; + /* Now look for X methods */ + exceptions.clear(); + try { + intM = publicLookup().findVirtual(STBRPRect.class, "x", methodType(int.class)); + } catch(ReflectiveOperationException e) { + exceptions.add(e); + } + try { + shortM = publicLookup().findVirtual(STBRPRect.class, "x", methodType(short.class)); + } catch(ReflectiveOperationException e) { + exceptions.add(e); + } + if(shortM == null && intM == null) { + IllegalStateException e = new IllegalStateException("An STBRPRect x() method could not be located"); + exceptions.forEach(e::addSuppressed); + throw e; + } + MH_rect_shortX = shortM; + MH_rect_intX = intM; + /* Assume that Y is the same */ + try { + if(MH_rect_shortX != null) { + MH_rect_shortY = publicLookup().findVirtual(STBRPRect.class, "y", methodType(short.class)); + MH_rect_intY = null; + } else { /* it must be int */ + MH_rect_intY = publicLookup().findVirtual(STBRPRect.class, "y", methodType(int.class)); + MH_rect_shortY = null; + } + } catch(ReflectiveOperationException e) { + throw new IllegalStateException("An STBRPRect y() method could not be located", e); + } + } + + private static STBRPRect setWrapper(STBRPRect rect, int id, int width, int height, int x, int y, boolean was_packed) { + try { + if(MH_rect_shortSet != null) + return (STBRPRect)MH_rect_shortSet.invokeExact(rect, id, (short)width, (short)height, (short)0, (short)0, false); + else + return (STBRPRect)MH_rect_intSet.invokeExact(rect, id, width, height, 0, 0, false); + } catch(Throwable e) { + throw new AssertionError(e); + } + } + + private static int getX(STBRPRect rect) { + try { + if(MH_rect_shortX != null) + return (short)MH_rect_shortX.invokeExact(rect); + else + return (int)MH_rect_intX.invokeExact(rect); + } catch(Throwable e) { + throw new AssertionError(e); + } + } + + private static int getY(STBRPRect rect) { + try { + if(MH_rect_shortX != null) + return (short)MH_rect_shortY.invokeExact(rect); + else + return (int)MH_rect_intY.invokeExact(rect); + } catch(Throwable e) { + throw new AssertionError(e); + } + } + public static Pair, List> packRects(Stitcher.Holder[] holders) { int holderSize = holders.length; @@ -36,7 +143,9 @@ public class StbStitcher { int height = holder.height; // The ID here is just the array index, for easy lookup later - rectBuf.get(j).set(j, (short)width, (short)height, (short)0, (short)0, false); + STBRPRect rect = rectBuf.get(j); + + setWrapper(rect, j, width, height, 0, 0, false); sqSize += (width * height); } @@ -63,7 +172,7 @@ public class StbStitcher { } // Initialize the sprite now with the position and size that we've calculated so far - infoList.add(new LoadableSpriteInfo(holder.spriteInfo, width, height, rect.x(), rect.y())); + infoList.add(new LoadableSpriteInfo(holder.spriteInfo, width, height, getX(rect), getY(rect))); //holder.spriteInfo.initSprite(size, size, rect.x(), rect.y(), false); } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index a366f6f0..400ca921 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -11,3 +11,4 @@ public net.minecraft.server.MinecraftServer f_129726_ # nextTickTime public net.minecraft.client.Minecraft f_90999_ # progressListener public-f net.minecraft.network.syncher.EntityDataAccessor f_135010_ # id public-f net.minecraft.network.syncher.SynchedEntityData f_135345_ # itemsById +public-f net.minecraft.network.syncher.SynchedEntityData f_135346_ # lock