This commit is contained in:
Moulberry committed 2024-08-29 10:34:49 +08:00
1 parent d2a217509f
commit 4d8f896cdf
90 files changed
+2345 -720

No files matched your search

@@ -3,18 +3,27 @@ package com.moulberry.flashback.mixin;
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.MeshData;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.playback.ReplayServer;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import com.moulberry.flashback.editor.ui.ReplayUI;
import com.moulberry.flashback.exporting.PerfectFrames;
import com.moulberry.flashback.visuals.ShaderManager;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.client.Camera;
import net.minecraft.client.CloudStatus;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
import net.minecraft.client.particle.ParticleEngine;
import net.minecraft.client.renderer.*;
@@ -34,6 +43,8 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Objects;
@Mixin(value = LevelRenderer.class, priority = 1100)
public abstract class MixinLevelRenderer {
@@ -64,8 +75,36 @@ public abstract class MixinLevelRenderer {
@Inject(method = "renderSectionLayer", at = @At("HEAD"), cancellable = true)
public void renderSectionLayer(RenderType renderType, double d, double e, double f, Matrix4f matrix4f, Matrix4f matrix4f2, CallbackInfo ci) {
EditorState editorState = EditorStateManager.getCurrent();
if (editorState != null && !editorState.replayVisuals.renderBlocks) {
ci.cancel();
if (editorState != null) {
if (!editorState.replayVisuals.renderBlocks) {
ci.cancel();
}
}
}
@Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/LevelRenderer;renderSectionLayer(Lnet/minecraft/client/renderer/RenderType;DDDLorg/joml/Matrix4f;Lorg/joml/Matrix4f;)V", ordinal = 2, shift = At.Shift.AFTER))
public void renderLevel_renderCutoutLayer(DeltaTracker deltaTracker, boolean bl, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f matrix4f, Matrix4f matrix4f2, CallbackInfo ci) {
if (Flashback.isExporting() && Flashback.EXPORT_JOB.getSettings().transparent()) {
RenderTarget main = Minecraft.getInstance().mainRenderTarget;
RenderSystem.assertOnRenderThread();
GlStateManager._disableDepthTest();
GlStateManager._depthMask(false);
GlStateManager._viewport(0, 0, main.viewWidth, main.viewHeight);
GlStateManager._disableBlend();
ShaderInstance shaderInstance = Objects.requireNonNull(ShaderManager.blitScreenRoundAlpha, "Blit shader not loaded");
shaderInstance.setSampler("DiffuseSampler", main.colorTextureId);
shaderInstance.apply();
BufferBuilder bufferBuilder = RenderSystem.renderThreadTesselator().begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLIT_SCREEN);
bufferBuilder.addVertex(0.0f, 0.0f, 0.0f);
bufferBuilder.addVertex(1.0f, 0.0f, 0.0f);
bufferBuilder.addVertex(1.0f, 1.0f, 0.0f);
bufferBuilder.addVertex(0.0f, 1.0f, 0.0f);
BufferUploader.draw(bufferBuilder.buildOrThrow());
shaderInstance.clear();
GlStateManager._enableBlend();
GlStateManager._depthMask(true);
GlStateManager._enableDepthTest();
}
}
@@ -73,7 +112,12 @@ public abstract class MixinLevelRenderer {
public void renderLevel_levelFogColor(DeltaTracker deltaTracker, boolean bl, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f matrix4f, Matrix4f matrix4f2, CallbackInfo ci) {
EditorState editorState = EditorStateManager.getCurrent();
if (editorState != null && !editorState.replayVisuals.renderSky) {
RenderSystem.clearColor(0.0f, 1.0f, 0.0f, 0.0F);
if (Flashback.isExporting() && Flashback.EXPORT_JOB.getSettings().transparent()) {
RenderSystem.clearColor(0.0f, 0.0f, 0.0f, 0.0F);
} else {
RenderSystem.clearColor(0.0f, 1.0f, 0.0f, 0.0F);
}
}
}
@@ -6,18 +6,34 @@ import com.mojang.blaze3d.pipeline.RenderTarget;
import com.moulberry.flashback.editor.ui.ReplayUI;
import net.minecraft.client.Minecraft;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL30;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(value = RenderTarget.class, priority = 1100)
public class MixinRenderTarget {
@Mixin(value = RenderTarget.class, priority = 800)
public abstract class MixinRenderTarget {
@WrapOperation(method = "_blitToScreen", at=@At(value = "INVOKE", target = "Lcom/mojang/blaze3d/platform/GlStateManager;_viewport(IIII)V", remap = false))
public void blitToScreenViewport(int left, int bottom, int width, int height, Operation<Void> original) {
if ((Object)this == Minecraft.getInstance().getMainRenderTarget() && ReplayUI.shouldModifyViewport()) {
ReplayUI.setupMainViewport();
} else {
original.call(left, bottom, width, height);
@Shadow
public int frameBufferId;
@Inject(method = "blitToScreen(IIZ)V", at = @At("HEAD"), cancellable = true)
public void blitToScreenSodium(int width, int height, boolean noBlend, CallbackInfo ci) {
if ((Object)this == Minecraft.getInstance().getMainRenderTarget() && ReplayUI.isActive()) {
var window = Minecraft.getInstance().getWindow();
int frameBottom = (ReplayUI.viewportSizeY - (ReplayUI.frameY + ReplayUI.frameHeight)) * window.framebufferHeight / ReplayUI.viewportSizeY;
int frameLeft = ReplayUI.frameX * window.framebufferWidth / ReplayUI.viewportSizeX;
int frameWidth = Math.max(1, ReplayUI.frameWidth) * window.framebufferWidth / ReplayUI.viewportSizeX;
int frameHeight = Math.max(1, ReplayUI.frameHeight) * window.framebufferHeight / ReplayUI.viewportSizeY;
GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, this.frameBufferId);
GL30.glBlitFramebuffer(0, 0, width, height, frameLeft, frameBottom, frameLeft+frameWidth, frameBottom+frameHeight,
GL11.GL_COLOR_BUFFER_BIT, GL11.GL_LINEAR);
GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, 0);
ci.cancel();
}
}
@@ -0,0 +1,31 @@
package com.moulberry.flashback.mixin.compat;
import com.bawnorton.mixinsquared.TargetHandler;
import com.moulberry.flashback.playback.ReplayServer;
import net.minecraft.server.MinecraftServer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
/*
* Prevent Fabric from modifying the world gen
* The registry that our ReplayServer uses typically does not contain server-only registries
* like minecraft:worldgen/placed_feature and so trying to modify the world gen context using
* these server-only registries is bound to error
*/
@Mixin(value = MinecraftServer.class, priority = 1500)
public class MixinFabricMinecraftServer {
@TargetHandler(
mixin = "net.fabricmc.fabric.mixin.biome.modification.MinecraftServerMixin",
name = "finalizeWorldGen"
)
@Inject(method = "@MixinSquared:Handler", at = @At("HEAD"), cancellable = true)
private void preventWorldGenChanges(CallbackInfo ci) {
if ((Object) this instanceof ReplayServer) {
ci.cancel();
}
}
}
@@ -0,0 +1,18 @@
package com.moulberry.flashback.mixin.compat.bobby;
import com.moulberry.mixinconstraints.annotations.IfModLoaded;
import de.johni0702.minecraft.bobby.FakeChunkManager;
import net.minecraft.client.multiplayer.ClientPacketListener;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
@IfModLoaded("bobby")
@Mixin(FakeChunkManager.class)
public interface FakeChunkManagerAccessor {
@Invoker(value = "getCurrentWorldOrServerName", remap = false)
static String getCurrentWorldOrServerName(ClientPacketListener clientPacketListener) {
throw new RuntimeException();
}
}
@@ -0,0 +1,42 @@
package com.moulberry.flashback.mixin.compat.bobby;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.playback.ReplayServer;
import com.moulberry.flashback.record.FlashbackMeta;
import com.moulberry.mixinconstraints.annotations.IfModLoaded;
import de.johni0702.minecraft.bobby.FakeChunkManager;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.function.Supplier;
@IfModLoaded("bobby")
@Mixin(FakeChunkManager.class)
public class MixinBobbyFakeChunkManager {
@Inject(method = "getCurrentWorldOrServerName", at = @At("HEAD"), cancellable = true)
private static void getCurrentWorldOrServerName(ClientPacketListener networkHandler, CallbackInfoReturnable<String> cir) {
ReplayServer replayServer = Flashback.getReplayServer();
if (replayServer != null) {
FlashbackMeta meta = replayServer.getMetadata();
if (meta != null && meta.bobbyWorldName != null) {
Flashback.LOGGER.info("Overriding Bobby World Name: {}", meta.bobbyWorldName);
cir.setReturnValue(meta.bobbyWorldName);
} else {
cir.setReturnValue("unknown_flashback");
}
}
}
@Inject(method = "save", at = @At("HEAD"), cancellable = true)
public void save(LevelChunk chunk, CallbackInfoReturnable<Supplier<LevelChunk>> cir) {
if (Flashback.isInReplay()) {
cir.setReturnValue(() -> chunk);
}
}
}
@@ -2,6 +2,8 @@ package com.moulberry.flashback.mixin.playback;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.playback.ReplayServer;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
@@ -12,6 +14,8 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.UUID;
@Mixin(Entity.class)
public abstract class MixinEntity {
@@ -24,10 +28,18 @@ public abstract class MixinEntity {
@Shadow
public abstract boolean isInvisibleTo(Player player);
@Shadow
public abstract UUID getUUID();
@Inject(method = "isInvisibleTo", at = @At("HEAD"), cancellable = true)
public void isInvisibleTo(Player player, CallbackInfoReturnable<Boolean> cir) {
ReplayServer replayServer = Flashback.getReplayServer();
if (replayServer != null && player == Minecraft.getInstance().player) {
EditorState editorState = EditorStateManager.getCurrent();
if (editorState != null && editorState.hideDuringExport.contains(this.getUUID())) {
cir.setReturnValue(true);
}
int localId = replayServer.getLocalPlayerId();
if (this.level.getEntity(localId) instanceof Player localPlayer) {
if ((Object)this == localPlayer) {
@@ -2,6 +2,7 @@ package com.moulberry.flashback.mixin.playback;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.state.EditorState;
@@ -10,7 +11,9 @@ import com.moulberry.flashback.editor.ui.ReplayUI;
import com.moulberry.flashback.ext.ItemInHandRendererExt;
import com.moulberry.flashback.ext.MinecraftExt;
import com.moulberry.flashback.visuals.CameraRotation;
import com.moulberry.flashback.visuals.ShaderManager;
import net.minecraft.client.Camera;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
import net.minecraft.client.multiplayer.MultiPlayerGameMode;
@@ -19,9 +22,12 @@ import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.ItemInHandRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.PostChain;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.GameType;
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
@@ -54,6 +60,39 @@ public abstract class MixinGameRenderer {
@Shadow
public abstract float getDepthFar();
@Shadow
public abstract void renderLevel(DeltaTracker deltaTracker);
@Shadow
private @Nullable PostChain postEffect;
@Shadow
private boolean effectActive;
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
public void render(DeltaTracker deltaTracker, boolean bl, CallbackInfo ci) {
if (Flashback.isExporting()) {
int i = (int)(this.minecraft.mouseHandler.xpos() * (double)this.minecraft.getWindow().getGuiScaledWidth() / (double)this.minecraft.getWindow().getScreenWidth());
int j = (int)(this.minecraft.mouseHandler.ypos() * (double)this.minecraft.getWindow().getGuiScaledHeight() / (double)this.minecraft.getWindow().getScreenHeight());
RenderSystem.viewport(0, 0, this.minecraft.getWindow().getWidth(), this.minecraft.getWindow().getHeight());
boolean bl2 = this.minecraft.isGameLoadFinished();
if (bl2 && bl && this.minecraft.level != null) {
this.minecraft.getProfiler().push("level");
this.renderLevel(deltaTracker);
this.minecraft.levelRenderer.doEntityOutline();
if (this.postEffect != null && this.effectActive) {
RenderSystem.disableBlend();
RenderSystem.disableDepthTest();
RenderSystem.resetTextureMatrix();
this.postEffect.process(deltaTracker.getGameTimeDeltaTicks());
}
this.minecraft.getMainRenderTarget().bindWrite(true);
}
ci.cancel();
}
}
@WrapOperation(method = "renderItemInHand", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/MultiPlayerGameMode;getPlayerMode()Lnet/minecraft/world/level/GameType;"))
public GameType getPlayerMode(MultiPlayerGameMode instance, Operation<GameType> original) {
if (Flashback.getSpectatingPlayer() != null) {
@@ -87,24 +126,6 @@ public abstract class MixinGameRenderer {
return CameraRotation.modifyViewQuaternion(original.call(instance));
}
// @Inject(method = "getProjectionMatrix", at = @At(value = "INVOKE", target = "Lorg/joml/Matrix4f;perspective(FFFF)Lorg/joml/Matrix4f;", shift = At.Shift.BEFORE))
// @Inject(method = "getProjectionMatrix", at = @At(value = "RETURN"))
// public void getProjectionMatrix(double d, CallbackInfoReturnable<Matrix4f> cir) {
// if (ReplayVisuals.overrideZoom) {
// cir.getReturnValue().scaleLocal(ReplayVisuals.overrideZoomAmount, ReplayVisuals.overrideZoomAmount, 1.0f);
// }
// }
// @Inject(method = "getProjectionMatrix", at = @At(value = "RETURN"), cancellable = true)
// public void getProjectionMatrix(double d, CallbackInfoReturnable<Matrix4f> cir) {
// if (ReplayVisuals.overrideZoom) {
// Window window = Minecraft.getInstance().getWindow();
// Matrix4f matrix4f = new Matrix4f().setOrtho(-10.0f, 10.0f, -10.0f, 10.0f, 0.05f, this.getDepthFar());
// cir.setReturnValue(matrix4f);
//// cir.setReturnValue(new Matrix4f().ortho());
// }
// }
@Inject(method = "tryTakeScreenshotIfNeeded", at = @At("HEAD"), cancellable = true)
public void tryTakeScreenshotIfNeeded(CallbackInfo ci) {
if (Flashback.isInReplay()) {
@@ -78,6 +78,8 @@ public abstract class MixinLevelChunk extends ChunkAccess implements LevelChunkE
return;
}
this.cachedChunkId = -1;
// Update heightmaps
this.heightmaps.get(Heightmap.Types.MOTION_BLOCKING).update(localX, y, localZ, blockState);
this.heightmaps.get(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES).update(localX, y, localZ, blockState);
@@ -1,8 +1,11 @@
package com.moulberry.flashback.mixin.playback;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.ext.ServerLevelExt;
import com.moulberry.flashback.playback.ReplayServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.PlayerChunkSender;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@@ -32,4 +35,14 @@ public class MixinPlayerChunkSender {
}
}
@Inject(method = "markChunkPendingToSend", at = @At("HEAD"), cancellable = true)
public void markChunkPendingToSend(LevelChunk levelChunk, CallbackInfo ci) {
ReplayServer replayServer = Flashback.getReplayServer();
if (replayServer != null) {
if (!((ServerLevelExt)levelChunk.getLevel()).flashback$shouldSendChunk(levelChunk.getPos().toLong())) {
ci.cancel();
}
}
}
}
@@ -0,0 +1,41 @@
package com.moulberry.flashback.mixin.replay_server;
import com.moulberry.flashback.ext.ServerLevelExt;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.core.SectionPos;
import net.minecraft.server.level.ServerLevel;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
@Mixin(ServerLevel.class)
public abstract class MixinServerLevel implements ServerLevelExt {
@Unique
private long seedHash = 0;
@Override
public void flashback$setSeedHash(long seedHash) {
this.seedHash = seedHash;
}
@Override
public long flashback$getSeedHash() {
return this.seedHash;
}
@Unique
private final LongSet validChunks = new LongOpenHashSet();
@Override
public boolean flashback$shouldSendChunk(long pos) {
return this.validChunks.contains(pos);
}
@Override
public void flashback$markChunkAsSendable(long pos) {
this.validChunks.add(pos);
}
}
@@ -25,9 +25,19 @@ public abstract class MixinEntityRenderDispatcher {
private static void renderHitbox(PoseStack poseStack, VertexConsumer vertexConsumer, Entity entity, float f, float g, float h, float i) {
}
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
public void renderBefore(Entity entity, double d, double e, double f, float g, float h, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, CallbackInfo ci) {
if (Flashback.isExporting()) {
EditorState editorState = EditorStateManager.getCurrent();
if (editorState != null && editorState.hideDuringExport.contains(entity.getUUID())) {
ci.cancel();
}
}
}
// Add a yellow outline to selected entity
@Inject(method = "render", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/PoseStack;popPose()V", shift = At.Shift.BEFORE))
public void render(Entity entity, double d, double e, double f, float g, float h, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, CallbackInfo ci) {
public void renderAfter(Entity entity, double d, double e, double f, float g, float h, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, CallbackInfo ci) {
if (Flashback.isInReplay()) {
if (ReplayUI.isEntitySelected(entity.getUUID())) {
renderHitbox(poseStack, multiBufferSource.getBuffer(RenderType.lines()), entity, h, 1.0f, 1.0f, 0.0f);
@@ -52,8 +52,13 @@ public class MixinGui {
@Inject(method = "canRenderCrosshairForSpectator", at = @At("HEAD"), cancellable = true)
public void canRenderCrosshairForSpectator(HitResult hitResult, CallbackInfoReturnable<Boolean> cir) {
if (Flashback.isExporting()) {
cir.setReturnValue(false);
return;
}
if (ReplayUI.isActive() && ReplayUI.imguiGlfw.getMouseHandledBy() == CustomImGuiImplGlfw.MouseHandledBy.GAME) {
cir.setReturnValue(true);
return;
}
if (Flashback.getSpectatingPlayer() != null) {
cir.setReturnValue(true);
@@ -0,0 +1,23 @@
package com.moulberry.flashback.mixin.visuals;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(LivingEntityRenderer.class)
public class MixinLivingEntityRenderer {
@Inject(method = "isBodyVisible", at = @At("HEAD"), cancellable = true)
public void isBodyVisible(LivingEntity livingEntity, CallbackInfoReturnable<Boolean> cir) {
EditorState editorState = EditorStateManager.getCurrent();
if (editorState != null && editorState.hideDuringExport.contains(livingEntity.getUUID())) {
cir.setReturnValue(false);
}
}
}