Make use of new @Pattern feature to centralize version-aware code

That is, most of the business code should not be aware that it is being compiled
to multiple versions even when it heavily interacts with MC, preprocessor
statements should be an escape hatch, not the norm.
Similarly, code should not be forced to do `MCVer.getWindow(mc)` instead of the
much more intuitive `mc.getWindow()`, and a new preprocessor (technically remap)
feature makes this possible by defining "search and replace"-like patterns (but
smarter in that they are type-aware) in one or more central places (the
"Patterns.java" files) which then are applied all over the code base.

In a way, this is another step in the automatic back-porting process where
preprocessor statements are used when we cannot yet do something automatically.
Previously we "merely" automatically converted between different mapping, this
new feature now also allows us to automatically perform simple refactoring
tasks like changing field access to a getter+setter (e.g. `mc.getWindow()`), or
changing how a method is called (e.g. `BufferBuilder.begin`), or changing a
method call chain (e.g. `dispatcher.camera.getYaw()`), or most other
search-and-replace-like changes and any combination of those.
The only major limitation is that the replacement itself is not smart, so
arguments must be kept in same order (or be temporarily assigned to local
variables which then can be used in any order).
This commit is contained in:
Jonas Herzig
2021-02-27 17:54:49 +01:00
parent 06a46e6f38
commit cfb9e15b8a
56 changed files with 890 additions and 872 deletions

View File

@@ -126,8 +126,8 @@ public class FFmpegWriter implements FrameConsumer<BitmapFrame> {
}
CrashReport report = CrashReport.create(t, "Exporting frame");
CrashReportSection exportDetails = report.addElement("Export details");
MCVer.addDetail(exportDetails, "Export command", settings::getExportCommand);
MCVer.addDetail(exportDetails, "Export args", commandArgs::toString);
exportDetails.add("Export command", settings::getExportCommand);
exportDetails.add("Export args", commandArgs::toString);
MCVer.getMinecraft().setCrashReport(report);
} finally {
channels.values().forEach(it -> ByteBufferPool.release(it.getByteBuffer()));

View File

@@ -1,6 +1,5 @@
package com.replaymod.render.blend;
import com.replaymod.core.versions.MCVer;
import com.replaymod.render.capturer.RenderInfo;
import com.replaymod.render.capturer.WorldRenderer;
import com.replaymod.render.frame.BitmapFrame;
@@ -8,6 +7,7 @@ import com.replaymod.render.rendering.Channel;
import com.replaymod.render.rendering.FrameCapturer;
import com.replaymod.render.utils.ByteBufferPool;
import de.johni0702.minecraft.gui.utils.lwjgl.Dimension;
import net.minecraft.client.MinecraftClient;
import java.io.IOException;
import java.util.Collections;
@@ -37,7 +37,7 @@ public class BlendFrameCapturer implements FrameCapturer<BitmapFrame> {
renderInfo.updateForNextFrame();
BlendState.getState().preFrame(framesDone);
worldRenderer.renderWorld(MCVer.getRenderPartialTicks(), null);
worldRenderer.renderWorld(MinecraftClient.getInstance().getTickDelta(), null);
BlendState.getState().postFrame(framesDone);
BitmapFrame frame = new BitmapFrame(framesDone++, new Dimension(0, 0), 0, ByteBufferPool.allocate(0));

View File

@@ -9,6 +9,7 @@ import de.johni0702.minecraft.gui.utils.lwjgl.vector.Vector3f;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormatElement;
import net.minecraft.client.render.VertexFormats;
import org.lwjgl.opengl.GL11;
//#if MC>=11500
@@ -82,7 +83,7 @@ public class BlendMeshBuilder
if (!wellBehaved) {
// In case the calling code finishes with Tessellator.getInstance().draw()
BufferBuilder_beginPosTexCol(mode);
Tessellator.getInstance().getBuffer().begin(mode, VertexFormats.POSITION_TEXTURE_COLOR);
}
//#if MC>=10809

View File

@@ -88,7 +88,7 @@ public class BlendState implements Exporter {
} catch (IOException e) {
CrashReport report = CrashReport.create(e, "Setup of blend exporter");
CrashReportSection category = report.addElement("Exporter");
addDetail(category, "Exporter", exporter::toString);
category.add("Exporter", exporter::toString);
throw new CrashException(report);
}
}
@@ -102,7 +102,7 @@ public class BlendState implements Exporter {
} catch (IOException e) {
CrashReport report = CrashReport.create(e, "Tear down of blend exporter");
CrashReportSection category = report.addElement("Exporter");
addDetail(category, "Exporter", exporter::toString);
category.add("Exporter", exporter::toString);
throw new CrashException(report);
}
}
@@ -139,8 +139,8 @@ public class BlendState implements Exporter {
} catch (IOException e) {
CrashReport report = CrashReport.create(e, "Pre frame of blend exporter");
CrashReportSection category = report.addElement("Exporter");
addDetail(category, "Exporter", exporter::toString);
addDetail(category, "Frame", () -> String.valueOf(frame));
category.add("Exporter", exporter::toString);
category.add("Frame", () -> String.valueOf(frame));
throw new CrashException(report);
}
}
@@ -154,8 +154,8 @@ public class BlendState implements Exporter {
} catch (IOException e) {
CrashReport report = CrashReport.create(e, "Post frame of blend exporter");
CrashReportSection category = report.addElement("Exporter");
addDetail(category, "Exporter", exporter::toString);
addDetail(category, "Frame", () -> String.valueOf(frame));
category.add("Exporter", exporter::toString);
category.add("Frame", () -> String.valueOf(frame));
throw new CrashException(report);
}
}

View File

@@ -21,7 +21,6 @@ import net.minecraft.client.model.ModelPart;
import java.io.IOException;
import static com.replaymod.core.versions.MCVer.*;
import static com.replaymod.render.blend.Util.isGlTextureMatrixIdentity;
public class ModelRendererExporter implements Exporter {
@@ -86,11 +85,13 @@ public class ModelRendererExporter implements Exporter {
DMesh mesh = new DMesh();
BlendMeshBuilder builder = new BlendMeshBuilder(mesh);
//#if MC>=11500
for (Cuboid box : cubeList(model)) {
// FIXME 1.15
}
// FIXME 1.15
//#elseif MC>=10809
//$$ for (Box box : model.boxes) {
//$$ box.render(builder, scale);
//$$ }
//#else
//$$ for (Box box : cubeList(model)) {
//$$ for (ModelBox box : (java.util.List<ModelBox>) model.cubeList) {
//$$ box.render(builder, scale);
//$$ }
//#endif

View File

@@ -19,7 +19,6 @@ import net.minecraft.util.crash.CrashException;
import java.util.Arrays;
import java.util.function.Consumer;
import static com.replaymod.core.versions.MCVer.addDetail;
import static com.replaymod.render.ReplayModRender.LOGGER;
public class GuiExportFailed extends GuiScreen {
@@ -33,8 +32,8 @@ public class GuiExportFailed extends GuiScreen {
// If they haven't, then this is probably a faulty ffmpeg installation and there's nothing we can do
CrashReport crashReport = CrashReport.create(e, "Exporting video");
CrashReportSection details = crashReport.addElement("Export details");
addDetail(details, "Settings", settings::toString);
addDetail(details, "FFmpeg log", e::getLog);
details.add("Settings", settings::toString);
details.add("FFmpeg log", e::getLog);
throw new CrashException(crashReport);
} else {
// If they have, ask them whether it was intentional

View File

@@ -59,7 +59,7 @@ public abstract class MixinCamera {
) {
if (getHandler() != null) {
//#if MC<11400
//$$ Entity entity = getRenderViewEntity(getMinecraft());
//$$ Entity entity = getMinecraft().getRenderViewEntity();
//#endif
orgYaw = entity.yaw;
orgPitch = entity.pitch;
@@ -79,7 +79,7 @@ public abstract class MixinCamera {
//#endif
if (getHandler() != null) {
//#if MC<11400
//$$ Entity entity = getRenderViewEntity(getMinecraft());
//$$ Entity entity = getMinecraft().getRenderViewEntity();
//#endif
RenderSettings settings = getHandler().getSettings();
if (settings.isStabilizeYaw()) {
@@ -117,7 +117,7 @@ public abstract class MixinCamera {
) {
if (getHandler() != null) {
//#if MC<11400
//$$ Entity entity = getRenderViewEntity(getMinecraft());
//$$ Entity entity = getMinecraft().getRenderViewEntity();
//#endif
entity.yaw = orgYaw;
entity.pitch = orgPitch;

View File

@@ -117,7 +117,7 @@ public abstract class MixinEntityRenderer implements EntityRendererHandler.IEnti
}
//#else
//$$ } else {
//$$ Entity currentEntity = getRenderViewEntity(MCVer.getMinecraft());
//$$ Entity currentEntity = this.mc.getRenderViewEntity();
//$$ if (currentEntity instanceof EntityPlayer && !(currentEntity instanceof CameraEntity)) {
//$$ if (renderPass == 2) { // Need to update render pass
//$$ renderPass = replayModRender_handler.data == StereoscopicOpenGlFrameCapturer.Data.LEFT_EYE ? 1 : 0;

View File

@@ -6,14 +6,9 @@ import com.replaymod.render.capturer.WorldRenderer;
import com.replaymod.render.frame.BitmapFrame;
import com.replaymod.render.processor.GlToAbsoluteDepthProcessor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.crash.CrashReport;
import net.minecraft.util.crash.CrashException;
//#if MC>=11400
import net.minecraft.util.crash.CrashReport;
import org.lwjgl.glfw.GLFW;
//#else
//$$ import org.lwjgl.opengl.Display;
//#endif
import java.util.HashMap;
import java.util.Map;
@@ -23,9 +18,6 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.replaymod.core.versions.MCVer.getMinecraft;
//#if MC>=11400
import static com.replaymod.core.versions.MCVer.getWindow;
//#endif
public class Pipeline<R extends Frame, P extends Frame> implements Runnable {
@@ -72,11 +64,7 @@ public class Pipeline<R extends Frame, P extends Frame> implements Runnable {
MinecraftClient mc = MCVer.getMinecraft();
while (!capturer.isDone() && !abort) {
//#if MC>=11400
if (GLFW.glfwWindowShouldClose(getWindow(mc).getHandle()) || ((MinecraftAccessor) mc).getCrashReporter() != null) {
//#else
//$$ if (Display.isCloseRequested() || ((MinecraftAccessor) mc).getCrashReporter() != null) {
//#endif
if (GLFW.glfwWindowShouldClose(mc.getWindow().getHandle()) || ((MinecraftAccessor) mc).getCrashReporter() != null) {
processService.shutdown();
return;
}

View File

@@ -30,10 +30,14 @@ import de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension;
import net.minecraft.client.MinecraftClient;
import com.mojang.blaze3d.platform.GLX;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.Window;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.util.crash.CrashException;
import net.minecraft.sound.SoundCategory;
import net.minecraft.client.render.RenderTickCounter;
import org.lwjgl.glfw.GLFW;
//#if MC>=11600
import net.minecraft.client.util.math.MatrixStack;
@@ -41,20 +45,16 @@ import net.minecraft.client.util.math.MatrixStack;
//#if MC>=11500
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.util.Window;
import org.lwjgl.opengl.GL11;
//#endif
//#if MC>=11400
import com.replaymod.render.EXRWriter;
import net.minecraft.client.gui.screen.Screen;
import org.lwjgl.glfw.GLFW;
import java.util.concurrent.CompletableFuture;
//#else
//$$ import net.minecraft.client.gui.ScaledResolution;
//$$ import org.lwjgl.input.Mouse;
//$$ import org.lwjgl.opengl.Display;
//$$ import static com.replaymod.core.versions.MCVer.newScaledResolution;
//#endif
//#if MC>=10800
@@ -80,6 +80,7 @@ import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
public class VideoRenderer implements RenderInfo {
private static final Identifier SOUND_RENDER_SUCCESS = new Identifier("replaymod", "render_success");
private final MinecraftClient mc = MCVer.getMinecraft();
private final RenderSettings settings;
private final ReplayHandler replayHandler;
@@ -232,7 +233,7 @@ public class VideoRenderer implements RenderInfo {
@Override
public float updateForNextFrame() {
// because the jGui lib uses Minecraft's displayWidth and displayHeight values, update these temporarily
MainWindowAccessor acc = (MainWindowAccessor) (Object) getWindow(mc);
MainWindowAccessor acc = (MainWindowAccessor) (Object) mc.getWindow();
int displayWidthBefore = acc.getFramebufferWidth();
int displayHeightBefore = acc.getFramebufferHeight();
acc.setFramebufferWidth(displayWidth);
@@ -351,10 +352,7 @@ public class VideoRenderer implements RenderInfo {
updateDisplaySize();
//#if MC<=10809
//$$ ScaledResolution scaled = newScaledResolution(mc);
//$$ gui.toMinecraft().setWorldAndResolution(mc, scaled.getScaledWidth(), scaled.getScaledHeight());
//#endif
gui.toMinecraft().init(mc, mc.getWindow().getScaledWidth(), mc.getWindow().getScaledHeight());
forceChunkLoadingHook = new ForceChunkLoadingHook(mc.worldRenderer);
@@ -401,7 +399,7 @@ public class VideoRenderer implements RenderInfo {
}
}
MCVer.playSound(new Identifier("replaymod", "render_success"));
mc.getSoundManager().play(PositionedSoundInstance.master(new SoundEvent(SOUND_RENDER_SUCCESS), 1));
try {
if (!hasFailed() && ffmpegWriter != null) {
@@ -464,12 +462,9 @@ public class VideoRenderer implements RenderInfo {
}
public boolean drawGui() {
Window window = mc.getWindow();
do {
//#if MC>=11400
if (GLFW.glfwWindowShouldClose(getWindow(mc).getHandle()) || ((MinecraftAccessor) mc).getCrashReporter() != null) {
//#else
//$$ if (Display.isCloseRequested() || ((MinecraftAccessor) mc).getCrashReporter() != null) {
//#endif
if (GLFW.glfwWindowShouldClose(window.getHandle()) || ((MinecraftAccessor) mc).getCrashReporter() != null) {
return false;
}
@@ -497,7 +492,6 @@ public class VideoRenderer implements RenderInfo {
guiFramebuffer.beginWrite(true);
//#if MC>=11500
Window window = getWindow(mc);
RenderSystem.clear(256, MinecraftClient.IS_SYSTEM_MAC);
RenderSystem.matrixMode(GL11.GL_PROJECTION);
RenderSystem.loadIdentity();
@@ -507,7 +501,7 @@ public class VideoRenderer implements RenderInfo {
RenderSystem.translatef(0, 0, -2000);
//#else
//#if MC>=11400
//$$ getWindow(mc).method_4493(
//$$ window.method_4493(
//#if MC>=11400
//$$ false
//#endif
@@ -517,12 +511,7 @@ public class VideoRenderer implements RenderInfo {
//#endif
//#endif
//#if MC>=11400
gui.toMinecraft().init(mc, getWindow(mc).getScaledWidth(), getWindow(mc).getScaledHeight());
//#else
//$$ ScaledResolution scaled = newScaledResolution(mc);
//$$ gui.toMinecraft().setWorldAndResolution(mc, scaled.getScaledWidth(), scaled.getScaledHeight());
//#endif
gui.toMinecraft().init(mc, window.getScaledWidth(), window.getScaledHeight());
// Events are polled on 1.13+ in mainWindow.update which is called later
//#if MC<11400
@@ -540,8 +529,8 @@ public class VideoRenderer implements RenderInfo {
//#endif
//#if MC>=11400
int mouseX = (int) mc.mouse.getX() * getWindow(mc).getScaledWidth() / displayWidth;
int mouseY = (int) mc.mouse.getY() * getWindow(mc).getScaledHeight() / displayHeight;
int mouseX = (int) mc.mouse.getX() * window.getScaledWidth() / displayWidth;
int mouseY = (int) mc.mouse.getY() * window.getScaledHeight() / displayHeight;
if (mc.overlay != null) {
Screen orgScreen = mc.currentScreen;
@@ -564,8 +553,8 @@ public class VideoRenderer implements RenderInfo {
mouseX, mouseY, 0);
}
//#else
//$$ int mouseX = Mouse.getX() * scaled.getScaledWidth() / mc.displayWidth;
//$$ int mouseY = scaled.getScaledHeight() - Mouse.getY() * scaled.getScaledHeight() / mc.displayHeight - 1;
//$$ int mouseX = Mouse.getX() * window.getScaledWidth() / mc.displayWidth;
//$$ int mouseY = window.getScaledHeight() - Mouse.getY() * window.getScaledHeight() / mc.displayHeight - 1;
//$$
//$$ gui.toMinecraft().updateScreen();
//$$ gui.toMinecraft().drawScreen(mouseX, mouseY, 0);
@@ -578,10 +567,10 @@ public class VideoRenderer implements RenderInfo {
popMatrix();
//#if MC>=11500
getWindow(mc).swapBuffers();
window.swapBuffers();
//#else
//#if MC>=11400
//$$ getWindow(mc).setFullscreen(false);
//$$ window.setFullscreen(false);
//#else
//$$ // if not in high performance mode, update the gui size if screen size changed
//$$ // otherwise just swap the progress gui to screen
@@ -611,13 +600,8 @@ public class VideoRenderer implements RenderInfo {
}
private boolean displaySizeChanged() {
//#if MC>=11400
int realWidth = getWindow(mc).getWidth();
int realHeight = getWindow(mc).getHeight();
//#else
//$$ int realWidth = Display.getWidth();
//$$ int realHeight = Display.getHeight();
//#endif
int realWidth = mc.getWindow().getWidth();
int realHeight = mc.getWindow().getHeight();
if (realWidth == 0 || realHeight == 0) {
// These can be zero on Windows if minimized.
// Creating zero-sized framebuffers however will throw an error, so we never want to switch to zero values.
@@ -627,13 +611,8 @@ public class VideoRenderer implements RenderInfo {
}
private void updateDisplaySize() {
//#if MC>=11400
displayWidth = getWindow(mc).getWidth();
displayHeight = getWindow(mc).getHeight();
//#else
//$$ displayWidth = Display.getWidth();
//$$ displayHeight = Display.getHeight();
//#endif
displayWidth = mc.getWindow().getWidth();
displayHeight = mc.getWindow().getHeight();
}
public int getFramesDone() {