diff --git a/src/main/java/ca/spottedleaf/leafprofiler/client/ClientProfilerInstance.java b/src/main/java/ca/spottedleaf/leafprofiler/client/ClientProfilerInstance.java index 3257515..be9a439 100644 --- a/src/main/java/ca/spottedleaf/leafprofiler/client/ClientProfilerInstance.java +++ b/src/main/java/ca/spottedleaf/leafprofiler/client/ClientProfilerInstance.java @@ -31,6 +31,8 @@ public final class ClientProfilerInstance implements ProfilerFiller { } } + private static final double LARGE_TICK_THRESHOLD = 1.0 - 0.05; + private final LProfilerRegistry registry = new LProfilerRegistry(); private final TickAccumulator tickAccumulator = new TickAccumulator(TimeUnit.SECONDS.toNanos(1L)); @@ -44,20 +46,23 @@ public final class ClientProfilerInstance implements ProfilerFiller { private LeafProfiler delayedFrameProfiler; private LeafProfiler frameProfiler; - private static final double LARGE_TICK_THRESHOLD = 1.0 - 0.05; - private long tick; private final List largeTicks = new ArrayList<>(); private static record LargeTick(long tickNum, LeafProfiler.ProfilingData profile) {} - public void startProfiler() { - this.delayedFrameProfiler = new LeafProfiler(this.registry, new LProfileGraph()); + public ClientProfilerInstance() { + this.reset(); } - public void stopProfiler() { - this.delayedFrameProfiler = null; + public void reset() { + this.previousTickStart = TickTime.DEADLINE_NOT_SET; + this.tickStart = TickTime.DEADLINE_NOT_SET; + this.tickStartCPU = TickTime.DEADLINE_NOT_SET; + this.tick = 0L; + this.largeTicks.clear(); + this.delayedFrameProfiler = new LeafProfiler(this.registry, new LProfileGraph()); } @Override diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/profiler/MinecraftMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/profiler/MinecraftMixin.java index 091a5f6..a59079e 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/profiler/MinecraftMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/profiler/MinecraftMixin.java @@ -3,23 +3,19 @@ package ca.spottedleaf.moonrise.mixin.profiler; import ca.spottedleaf.leafprofiler.client.ClientProfilerInstance; import com.mojang.blaze3d.platform.WindowEventHandler; import net.minecraft.client.Minecraft; -import net.minecraft.util.profiling.ContinuousProfiler; +import net.minecraft.util.profiling.InactiveProfiler; import net.minecraft.util.profiling.ProfilerFiller; import net.minecraft.util.profiling.SingleTickProfiler; import net.minecraft.util.thread.ReentrantBlockableEventLoop; -import org.spongepowered.asm.mixin.Final; 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 org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(Minecraft.class) abstract class MinecraftMixin extends ReentrantBlockableEventLoop implements WindowEventHandler { - @Shadow - @Final - private ContinuousProfiler fpsPieProfiler; - public MinecraftMixin(String string) { super(string); } @@ -28,17 +24,25 @@ abstract class MinecraftMixin extends ReentrantBlockableEventLoop impl private final ClientProfilerInstance leafProfiler = new ClientProfilerInstance(); /** - * @reason Use our own profiler for client + * @reason Insert our own profiler for client * @author Spottedleaf */ - @Overwrite - public ProfilerFiller constructProfiler(final boolean shouldRenderFPSPie, final SingleTickProfiler singleTickProfiler) { - if (shouldRenderFPSPie) { - this.fpsPieProfiler.enable(); - } else { - this.fpsPieProfiler.disable(); + @Inject( + method = "constructProfiler", + cancellable = true, + at = @At( + value = "RETURN" + ) + ) + public void addOurProfiler(final boolean shouldRenderFPSPie, final SingleTickProfiler singleTickProfiler, + final CallbackInfoReturnable cir) { + final ProfilerFiller ret = cir.getReturnValue(); + + if (ret == null || ret == InactiveProfiler.INSTANCE) { + this.leafProfiler.reset(); + return; } - return this.leafProfiler; + cir.setReturnValue(ProfilerFiller.tee(this.leafProfiler, ret)); } } diff --git a/src/main/resources/moonrise.mixins.json b/src/main/resources/moonrise.mixins.json index afd7c60..42e6226 100644 --- a/src/main/resources/moonrise.mixins.json +++ b/src/main/resources/moonrise.mixins.json @@ -115,6 +115,7 @@ "collisions.LiquidBlockRendererMixin", "collisions.ParticleMixin", "loading_screen.LevelLoadStatusManagerMixin", + "profiler.MinecraftMixin", "render.SectionRenderDispatcherMixin", "serverlist.ClientConnectionMixin", "serverlist.ServerAddressResolverMixin",