forked from Sk1erLLC/Patcher
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch some things to fastutil (#54)
Co-authored-by: ev chang <[email protected]>
- Loading branch information
1 parent
8247911
commit 37f2ea7
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...sk1er/patcher/mixins/performance/forge/FMLIndexedMessageToMessageCodecMixin_FastUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package club.sk1er.patcher.mixins.performance.forge; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.MessageToMessageCodec; | ||
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap; | ||
import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap; | ||
import it.unimi.dsi.fastutil.objects.Object2ByteMap; | ||
import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap; | ||
import net.minecraft.network.PacketBuffer; | ||
import net.minecraftforge.fml.common.FMLLog; | ||
import net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec; | ||
import net.minecraftforge.fml.common.network.NetworkRegistry; | ||
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; | ||
import org.apache.logging.log4j.Level; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.List; | ||
|
||
import static net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.INBOUNDPACKETTRACKER; | ||
|
||
@Mixin(value = FMLIndexedMessageToMessageCodec.class, remap = false) | ||
public abstract class FMLIndexedMessageToMessageCodecMixin_FastUtil<A> extends MessageToMessageCodec<FMLProxyPacket, A> { | ||
//#if MC==10809 | ||
@Shadow | ||
public abstract void encodeInto(ChannelHandlerContext ctx, A msg, ByteBuf target) throws Exception; | ||
@Shadow | ||
protected abstract void testMessageValidity(FMLProxyPacket msg); | ||
@Shadow | ||
public abstract void decodeInto(ChannelHandlerContext ctx, ByteBuf source, A msg); | ||
@Unique | ||
private final Byte2ObjectMap<Class<? extends A>> patcher$discriminators = new Byte2ObjectOpenHashMap<>(); | ||
@Unique | ||
private final Object2ByteMap<Class<? extends A>> patcher$types = new Object2ByteOpenHashMap<>(); | ||
|
||
/** | ||
* @author Microcontrollers | ||
* @reason Use FastUtil | ||
*/ | ||
@Overwrite | ||
public FMLIndexedMessageToMessageCodec<A> addDiscriminator(int discriminator, Class<? extends A> type) { | ||
patcher$discriminators.put((byte)discriminator, type); | ||
patcher$types.put(type, (byte)discriminator); | ||
return (FMLIndexedMessageToMessageCodec) (Object) this; | ||
} | ||
|
||
/** | ||
* @author Microcontrollers | ||
* @reason Use FastUtil | ||
*/ | ||
@Overwrite | ||
@Override | ||
protected final void encode(ChannelHandlerContext ctx, A msg, List<Object> out) throws Exception { | ||
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer()); | ||
@SuppressWarnings("unchecked") // Stupid unnecessary cast I can't seem to kill | ||
Class<? extends A> clazz = (Class<? extends A>) msg.getClass(); | ||
byte discriminator = patcher$types.get(clazz); | ||
buffer.writeByte(discriminator); | ||
this.encodeInto(ctx, msg, buffer); | ||
FMLProxyPacket proxy = new FMLProxyPacket(buffer/*.copy()*/, ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); | ||
WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get(); | ||
FMLProxyPacket old = ref == null ? null : ref.get(); | ||
if (old != null) { | ||
proxy.setDispatcher(old.getDispatcher()); | ||
} | ||
out.add(proxy); | ||
} | ||
|
||
/** | ||
* @author Microcontrollers | ||
* @reason Use FastUtil | ||
*/ | ||
@Overwrite | ||
@Override | ||
protected final void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { | ||
this.testMessageValidity(msg); | ||
ByteBuf payload = msg.payload().duplicate(); | ||
if (payload.readableBytes() < 1) { | ||
FMLLog.log(Level.ERROR, "The FMLIndexedCodec has received an empty buffer on channel %s, likely a result of a LAN server issue. Pipeline parts : %s", ctx.channel().attr(NetworkRegistry.FML_CHANNEL), ctx.pipeline().toString()); | ||
} | ||
byte discriminator = payload.readByte(); | ||
Class<? extends A> clazz = patcher$discriminators.get(discriminator); | ||
if(clazz == null) { | ||
throw new NullPointerException("Undefined message for discriminator " + discriminator + " in channel " + msg.channel()); | ||
} | ||
A newMsg = clazz.newInstance(); | ||
ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<FMLProxyPacket>(msg)); | ||
this.decodeInto(ctx, payload.slice(), newMsg); | ||
out.add(newMsg); | ||
} | ||
//#endif | ||
} |
78 changes: 78 additions & 0 deletions
78
.../java/club/sk1er/patcher/mixins/performance/forge/ItemModelMesherForgeMixin_FastUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package club.sk1er.patcher.mixins.performance.forge; | ||
|
||
import com.google.common.collect.Maps; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; | ||
import net.minecraft.client.renderer.ItemModelMesher; | ||
import net.minecraft.client.resources.model.IBakedModel; | ||
import net.minecraft.client.resources.model.ModelManager; | ||
//#if MC==10809 | ||
import net.minecraft.client.resources.model.ModelResourceLocation; | ||
//#endif | ||
import net.minecraft.item.Item; | ||
import net.minecraftforge.client.ItemModelMesherForge; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.util.Map; | ||
|
||
@Mixin(ItemModelMesherForge.class) | ||
public class ItemModelMesherForgeMixin_FastUtil extends ItemModelMesher { | ||
public ItemModelMesherForgeMixin_FastUtil(ModelManager modelManager) { | ||
super(modelManager); | ||
} | ||
|
||
//#if MC==10809 | ||
@Unique | ||
final Map<Item, Int2ObjectMap<ModelResourceLocation>> patcher$locations = Maps.newIdentityHashMap(); | ||
@Unique | ||
final Map<Item, Int2ObjectMap<IBakedModel>> patcher$models = Maps.newIdentityHashMap(); | ||
|
||
@Inject(method = "getItemModel", at = @At("HEAD"), cancellable = true) | ||
private void patcher$fastUtilGetModel(Item item, int meta, CallbackInfoReturnable<IBakedModel> cir) { | ||
Int2ObjectMap<IBakedModel> map = patcher$models.get(item); | ||
cir.setReturnValue(map == null ? null : map.get(meta)); | ||
} | ||
|
||
@Inject(method = "register", at = @At("HEAD"), cancellable = true) | ||
private void patcher$fastUtilRegisterModel(Item item, int meta, ModelResourceLocation location, CallbackInfo ci) { | ||
Int2ObjectMap<ModelResourceLocation> locs = patcher$locations.get(item); | ||
Int2ObjectMap<IBakedModel> mods = patcher$models.get(item); | ||
if (locs == null) { | ||
locs = new Int2ObjectOpenHashMap<>(); | ||
patcher$locations.put(item, locs); | ||
} | ||
if (mods == null) { | ||
mods = new Int2ObjectOpenHashMap<>(); | ||
patcher$models.put(item, mods); | ||
} | ||
locs.put(meta, location); | ||
mods.put(meta, this.getModelManager().getModel(location)); | ||
ci.cancel(); | ||
} | ||
|
||
@Inject(method = "rebuildCache", at = @At("HEAD"), cancellable = true) | ||
private void patcher$fastUtilRebuildCache(CallbackInfo ci) { | ||
final ModelManager manager = this.getModelManager(); | ||
for (Map.Entry<Item, Int2ObjectMap<ModelResourceLocation>> e : patcher$locations.entrySet()) { | ||
Int2ObjectMap<IBakedModel> mods = patcher$models.get(e.getKey()); | ||
if (mods != null) { | ||
mods.clear(); | ||
} | ||
else { | ||
mods = new Int2ObjectOpenHashMap<>(); | ||
patcher$models.put(e.getKey(), mods); | ||
} | ||
final Int2ObjectMap<IBakedModel> map = mods; | ||
for (Int2ObjectMap.Entry<ModelResourceLocation> entry : e.getValue().int2ObjectEntrySet()) { | ||
map.put(entry.getIntKey(), manager.getModel(entry.getValue())); | ||
} | ||
} | ||
ci.cancel(); | ||
} | ||
//#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters