diff options
author | Linnea Gräf <nea@nea.moe> | 2024-01-04 17:45:29 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-01-04 17:55:39 +0100 |
commit | 61a5635e993b46d3ae2e76edd1b104987ff92a3f (patch) | |
tree | 3091150988d9ab47ff37aac482263aa0ce0e797f /src | |
parent | 62519450e2dc3434560ab2e9db1f308c0ca79f0c (diff) | |
download | neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.tar.gz neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.tar.bz2 neuhax-61a5635e993b46d3ae2e76edd1b104987ff92a3f.zip |
Add Optifine cache
Diffstat (limited to 'src')
5 files changed, 154 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/sky/mixin/PatchCustomItemModelCache.java b/src/main/java/moe/nea/sky/mixin/PatchCustomItemModelCache.java new file mode 100644 index 0000000..d9a6187 --- /dev/null +++ b/src/main/java/moe/nea/sky/mixin/PatchCustomItemModelCache.java @@ -0,0 +1,44 @@ +package moe.nea.sky.mixin; + +import moe.nea.sky.features.fopt.CacheResult; +import moe.nea.sky.features.fopt.OptifineCustomItemCache; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.optifine.CustomItemProperties; +import net.optifine.CustomItems; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +@Pseudo +@Mixin(value = CustomItems.class, remap = false) +public class PatchCustomItemModelCache { + + @Inject(method = "getCustomItemProperties", at = @At("HEAD"), cancellable = true) + private static void overrideCustomItemProperties( + ItemStack itemStack, int type, + CallbackInfoReturnable<CustomItemProperties> cir) { + CacheResult cacheHit = OptifineCustomItemCache.retrieveCacheHit(itemStack, type); + if (cacheHit != null) { + cir.setReturnValue(cacheHit.getCustomItemProperties()); + } + } + + @Inject(method = "getCustomItemProperties", at = @At(value = "RETURN", ordinal = 2), + locals = LocalCapture.CAPTURE_FAILHARD) + private static void storeCustomItemProperties( + ItemStack itemStack, int type, CallbackInfoReturnable<CustomItemProperties> cir, Item item, int itemId, CustomItemProperties[] cips, int i, CustomItemProperties cip) { + OptifineCustomItemCache.storeCacheElement(itemStack, type, new CacheResult(cip)); + } + + @Inject(method = "getCustomItemProperties", at = @At(value = "RETURN", ordinal = 3)) + private static void storeCustomItemProperties( + ItemStack itemStack, int type, CallbackInfoReturnable<CustomItemProperties> cir) { + OptifineCustomItemCache.storeCacheElement(itemStack, type, new CacheResult(null)); + } + + +} diff --git a/src/main/kotlin/moe/nea/sky/NEUHax.kt b/src/main/kotlin/moe/nea/sky/NEUHax.kt index 93e1a6f..734fdab 100644 --- a/src/main/kotlin/moe/nea/sky/NEUHax.kt +++ b/src/main/kotlin/moe/nea/sky/NEUHax.kt @@ -11,8 +11,10 @@ package moe.nea.sky import io.github.moulberry.notenoughupdates.NotEnoughUpdates +import io.github.moulberry.notenoughupdates.util.brigadier.BrigadierRoot import moe.nea.sky.commands.NEUHaxCommand import moe.nea.sky.config.HaxConfigNeuConfig +import moe.nea.sky.features.fopt.OptifineCustomItemCache import moe.nea.sky.features.gui.Enchanting import moe.nea.sky.features.gui.Melody import moe.nea.sky.features.world.AutoFishing @@ -51,11 +53,13 @@ object NEUHax { AutoFishing, YawSnapping, Melody, + OptifineCustomItemCache, ).forEach { MinecraftForge.EVENT_BUS.register(it) } ClientCommandHandler.instance.registerCommand(NEUHaxCommand) ClientCommandHandler.instance.registerCommand(CommandActionRegistry) + BrigadierRoot.updateHooks() } }
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt b/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt new file mode 100644 index 0000000..ce922a3 --- /dev/null +++ b/src/main/kotlin/moe/nea/sky/features/fopt/OptifineCustomItemCacheKey.kt @@ -0,0 +1,101 @@ +package moe.nea.sky.features.fopt + +import io.github.moulberry.notenoughupdates.deps.com.mojang.brigadier.arguments.IntegerArgumentType +import io.github.moulberry.notenoughupdates.events.RegisterBrigadierCommandEvent +import io.github.moulberry.notenoughupdates.util.brigadier.get +import io.github.moulberry.notenoughupdates.util.brigadier.thenArgumentExecute +import io.github.moulberry.notenoughupdates.util.brigadier.thenExecute +import io.github.moulberry.notenoughupdates.util.brigadier.withHelp +import moe.nea.sky.util.showMessage +import net.minecraft.command.ICommandSender +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import net.minecraftforge.fml.common.gameevent.TickEvent +import net.optifine.CustomItemProperties + +class OptifineCustomItemCacheKey(val itemstack: ItemStack, val type: Int) { + override fun equals(other: Any?): Boolean { + if (other !is OptifineCustomItemCacheKey) return false + return itemstack === other.itemstack && type == other.type + } + + override fun hashCode(): Int { + return System.identityHashCode(itemstack) + type * 31 + } +} + +class CacheResult(val customItemProperties: CustomItemProperties?) + +class Histogram<T>(val maxSize: Int) : Iterable<T> { + private val dequeue = ArrayDeque<T>() + fun append(element: T) { + dequeue.addLast(element) + if (dequeue.size > maxSize) { + dequeue.removeFirst() + } + } + + val size get() = dequeue.size + + override fun iterator(): Iterator<T> { + return dequeue.iterator() + } +} + +data class CacheStats(var cacheHits: Int = 0, var cacheMisses: Int = 0, var cacheCount: Int = 0) +object OptifineCustomItemCache { + private val map: MutableMap<OptifineCustomItemCacheKey, CacheResult> = mutableMapOf() + private val histogram = Histogram<CacheStats>(100) + private var cacheStats = CacheStats() + + fun dumpStats(target: ICommandSender, length: Int) { + target.showMessage { + text("Optifine Cache Size:") + histogram.forEachIndexed { index, stats -> + val ago = histogram.size - index + if (ago <= length) + text("§b${ago}§e tick${s(ago)} ago: §a${stats.cacheHits}§e-§c${stats.cacheMisses}§e-§b${stats.cacheCount}") + } + } + } + + @SubscribeEvent + fun onCommands(event: RegisterBrigadierCommandEvent) { + event.command("nhopticache") { + thenExecute { + dumpStats(source, Int.MAX_VALUE) + } + thenArgumentExecute("count", IntegerArgumentType.integer()) { + dumpStats(source, this[it]) + }.withHelp("Show stats about the last count ticks") + }.withHelp("Show stats about the optifine item cache") + } + + + @SubscribeEvent + fun onTick(event: TickEvent.ClientTickEvent) { + if (event.phase == TickEvent.Phase.END) { + cacheStats.cacheCount = map.size + histogram.append(cacheStats) + cacheStats = CacheStats() + map.clear() + } + } + + @JvmStatic + fun retrieveCacheHit(itemstack: ItemStack, type: Int): CacheResult? { + val cacheResult = map[OptifineCustomItemCacheKey(itemstack, type)] + if (cacheResult == null) { + cacheStats.cacheMisses++ + } else { + cacheStats.cacheHits++ + } + return cacheResult + } + + @JvmStatic + fun storeCacheElement(itemstack: ItemStack, type: Int, properties: CacheResult) { + map[OptifineCustomItemCacheKey(itemstack, type)] = properties + } + +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/sky/util/chatutils.kt b/src/main/kotlin/moe/nea/sky/util/chatutils.kt index df54cc8..025ee92 100644 --- a/src/main/kotlin/moe/nea/sky/util/chatutils.kt +++ b/src/main/kotlin/moe/nea/sky/util/chatutils.kt @@ -80,6 +80,10 @@ object CommandActionRegistry : CommandBase() { class MessageTarget { val aggregate = mutableListOf<IChatComponent>() + fun s(count: Int): String { + return if (count == 1) "" else "s" + } + fun text(string: String): ChatComponentText { return component(ChatComponentText(string)) } diff --git a/src/main/resources/mixins.neuhax.json b/src/main/resources/mixins.neuhax.json index b494695..45a5dcb 100644 --- a/src/main/resources/mixins.neuhax.json +++ b/src/main/resources/mixins.neuhax.json @@ -10,6 +10,7 @@ "MixinGenericBlockHighlighter", "MixinSkytilsFunnny", "MixinUltrasequencerItem", + "PatchCustomItemModelCache", "config.MixinHaxConfigEnchanting", "config.MixinHaxConfigFishing", "config.MixinHaxConfigNeuConfig", |