Skip to content

Commit

Permalink
Finish porting to LF and cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Deftu committed Dec 11, 2024
1 parent 39fec52 commit e79bae9
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 160 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {

toolkitLoomHelper {
// Adds OneConfig to our project
useOneConfig(mcData, "commands", "config", "config-impl", "events", "internal", "ui")
useOneConfig(mcData, "commands", "config", "config-impl", "events", "internal", "ui", "utils")

// Removes the server configs from IntelliJ IDEA, leaving only client runs.
// If you're developing a server-side mod, you can remove this line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

package org.polyfrost.crashpatch.mixin;

import org.polyfrost.crashpatch.crashes.ModIdentifier;
import org.polyfrost.crashpatch.identifier.ModIdentifier;
import org.polyfrost.crashpatch.hooks.CrashReportHook;
import org.polyfrost.crashpatch.hooks.StacktraceDeobfuscator;
import net.minecraft.crash.CrashReport;
import net.minecraftforge.fml.common.ModContainer;
import org.polyfrost.crashpatch.identifier.ModMetadata;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -32,7 +33,7 @@ public String getSuspectedCrashPatchMods() {

@Inject(method = "populateEnvironment", at = @At("TAIL"))
private void afterPopulateEnvironment(CallbackInfo ci) {
ModContainer susMod = ModIdentifier.INSTANCE.identifyFromStacktrace(cause);
ModMetadata susMod = ModIdentifier.INSTANCE.identifyFromStacktrace(cause);
crashpatch$suspectedMod = (susMod == null ? "Unknown" : susMod.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOEx
String[] list = crashpatch$wrapFormattedStringToWidth(crashpatch$getText(), width).split("\n");
int width = -1;
for (String text : list) {
width = Math.max(width, fontRendererObj.getStringWidth(text));
width = Math.max(width, this.fontRendererObj.getStringWidth(text));
}

int left = (this.width / 2) - width / 2;
if ((width == -1 || (left < mouseX && left + width > mouseX)) && (mouseY > 5 && mouseY < 15 + ((list.length - 1) * (fontRendererObj.FONT_HEIGHT + 2)))) {
if ((width == -1 || (left < mouseX && left + width > mouseX)) && (mouseY > 5 && mouseY < 15 + ((list.length - 1) * (this.fontRendererObj.FONT_HEIGHT + 2)))) {
UDesktop.browse(URI.create("https://discord.gg/eh7tNFezct"));
}
}
Expand All @@ -54,8 +55,8 @@ protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOEx
@Unique
public void crashpatch$drawSplitCenteredString(String text, int x, int y, int color) {
for (String line : crashpatch$wrapFormattedStringToWidth(text, width).split("\n")) {
drawCenteredString(fontRendererObj, line, x, y, color);
y += fontRendererObj.FONT_HEIGHT + 2;
drawCenteredString(this.fontRendererObj, line, x, y, color);
y += this.fontRendererObj.FONT_HEIGHT + 2;
}
}

Expand Down Expand Up @@ -91,7 +92,7 @@ protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOEx
case ' ':
l = k;
default:
j += fontRendererObj.getCharWidth(c0);
j += this.fontRendererObj.getCharWidth(c0);

if (flag) {
++j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.polyfrost.crashpatch.mixin;

import org.polyfrost.crashpatch.CrashPatch;
import org.polyfrost.universal.UDesktop;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
Expand All @@ -9,9 +10,9 @@
import net.minecraftforge.fml.common.DuplicateModsFoundException;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.ModContainer;
import org.polyfrost.crashpatch.CrashPatchOldKt;
import org.spongepowered.asm.mixin.Mixin;
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.CallbackInfo;
Expand Down Expand Up @@ -40,7 +41,7 @@ private void onInit(CallbackInfo ci) {
protected void actionPerformed(GuiButton button) {
switch (button.id) {
case 0:
UDesktop.open(new File(CrashPatchOldKt.getMcDir(), "mods"));
UDesktop.open(new File(CrashPatch.getMcDir(), "mods"));
break;
case 1:
FMLCommonHandler.instance().exitJava(0, false);
Expand All @@ -53,16 +54,16 @@ private void onDrawScreen(int mouseX, int mouseY, float partialTicks, CallbackIn
ci.cancel();
drawDefaultBackground();
int offset = 10;
offset += drawSplitString("There are duplicate mods in your mod folder!", width / 2, offset, width, Color.RED.getRGB());
offset += crashpatch$drawSplitString("There are duplicate mods in your mod folder!", width / 2, offset, width, Color.RED.getRGB());

for (Map.Entry<ModContainer, File> modContainerFileEntry : dupes.dupes.entries()) {
offset += 10;
offset += drawSplitString(String.format("%s : %s", modContainerFileEntry.getKey().getModId(), modContainerFileEntry.getValue().getName()), width / 2, offset, width, Color.YELLOW.getRGB());
offset += crashpatch$drawSplitString(String.format("%s : %s", modContainerFileEntry.getKey().getModId(), modContainerFileEntry.getValue().getName()), width / 2, offset, width, Color.YELLOW.getRGB());
}

offset += 10;

drawSplitString(EnumChatFormatting.BOLD + "To fix this, go into your mods folder by clicking the button below or going to " + CrashPatchOldKt.getMcDir().getAbsolutePath() + " and deleting the duplicate mods.", width / 2, offset, width, Color.BLUE.getRGB());
crashpatch$drawSplitString(EnumChatFormatting.BOLD + "To fix this, go into your mods folder by clicking the button below or going to " + CrashPatch.getMcDir().getAbsolutePath() + " and deleting the duplicate mods.", width / 2, offset, width, Color.BLUE.getRGB());

for (GuiButton guiButton : this.buttonList) {
guiButton.drawButton(this.mc, mouseX, mouseY);
Expand All @@ -72,8 +73,9 @@ private void onDrawScreen(int mouseX, int mouseY, float partialTicks, CallbackIn
}
}

private static int drawSplitString(String str, int x, int y, int wrapWidth, int textColor) {
str = trimStringNewline(str);
@Unique
private static int crashpatch$drawSplitString(String str, int x, int y, int wrapWidth, int textColor) {
str = crashpatch$trimStringNewline(str);
int y2 = y;
for (String s : Minecraft.getMinecraft().fontRendererObj.listFormattedStringToWidth(str, wrapWidth)) {
Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow(s, (float) (x - Minecraft.getMinecraft().fontRendererObj.getStringWidth(s) / 2), (float) y2, textColor);
Expand All @@ -82,10 +84,12 @@ private static int drawSplitString(String str, int x, int y, int wrapWidth, int
return y2 - y;
}

private static String trimStringNewline(String text) {
@Unique
private static String crashpatch$trimStringNewline(String text) {
while (text != null && text.endsWith("\n")) {
text = text.substring(0, text.length() - 1);
}
return text;
}

}
89 changes: 45 additions & 44 deletions src/main/java/org/polyfrost/crashpatch/mixin/MixinMinecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL14;
import org.polyfrost.crashpatch.CrashPatch;
import org.polyfrost.crashpatch.config.CrashPatchConfig;
import org.polyfrost.crashpatch.CrashPatchConfig;
import org.polyfrost.crashpatch.crashes.StateManager;
import org.polyfrost.crashpatch.gui.CrashUI;
import org.polyfrost.crashpatch.hooks.MinecraftHook;
Expand All @@ -52,6 +52,7 @@
import java.util.Queue;
import java.util.concurrent.FutureTask;

@SuppressWarnings("AccessStaticViaInstance")
@Mixin(value = Minecraft.class, priority = -9000)
public abstract class MixinMinecraft implements MinecraftHook {

Expand Down Expand Up @@ -161,7 +162,7 @@ public void run(CallbackInfo ci) {
}
try {
while (running) {
if (!hasCrashed || crashReporter == null) {
if (!this.hasCrashed || this.crashReporter == null) {
try {
if (CrashPatch.INSTANCE.getRequestedCrash()) {
CrashPatch.INSTANCE.setRequestedCrash(false);
Expand All @@ -174,24 +175,24 @@ public void run(CallbackInfo ci) {
addGraphicsAndWorldToCrashReport(e.getCrashReport());
crashpatch$addInfoToCrash(e.getCrashReport());
crashpatch$resetGameState();
logger.fatal("Reported exception thrown!", e);
this.logger.fatal("Reported exception thrown!", e);
crashpatch$displayCrashScreen(e.getCrashReport());
} catch (Throwable e) {
crashpatch$clientCrashCount++;
CrashReport report = new CrashReport("Unexpected error", e);
addGraphicsAndWorldToCrashReport(report);
crashpatch$addInfoToCrash(report);
crashpatch$resetGameState();
logger.fatal("Unreported exception thrown!", e);
this.logger.fatal("Unreported exception thrown!", e);
crashpatch$displayCrashScreen(report);
}
} else {
crashpatch$serverCrashCount++;
crashpatch$addInfoToCrash(crashReporter);
crashpatch$addInfoToCrash(this.crashReporter);
freeMemory();
crashpatch$displayCrashScreen(crashReporter);
hasCrashed = false;
crashReporter = null;
crashpatch$displayCrashScreen(this.crashReporter);
this.hasCrashed = false;
this.crashReporter = null;
}
}
} catch (MinecraftError ignored) {
Expand All @@ -213,27 +214,27 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
crashpatch$letDie = true;
}
if ((crashpatch$clientCrashCount >= CrashPatchConfig.INSTANCE.getCrashLimit() || crashpatch$serverCrashCount >= CrashPatchConfig.INSTANCE.getCrashLimit())) {
logger.error("Crash limit reached, exiting game");
this.logger.error("Crash limit reached, exiting game");
crashpatch$letDie = true;
}
displayCrashReport(report);
crashpatch$recoveredFromCrash = true;
try {

// Reset hasCrashed, debugCrashKeyPressTime, and crashIntegratedServerNextTick
hasCrashed = false;
debugCrashKeyPressTime = -1;
this.hasCrashed = false;
this.debugCrashKeyPressTime = -1;

// Vanilla does this when switching to main menu but not our custom crash screen
// nor the out of memory screen (see https://bugs.mojang.com/browse/MC-128953)
gameSettings.showDebugInfo = false;
this.gameSettings.showDebugInfo = false;

// Display the crash screen
// crashpatch$runGUILoop(new GuiCrashScreen(report));
displayGuiScreen(new CrashUI(report).create());
} catch (Throwable t) {
// The crash screen has crashed. Report it normally instead.
logger.error("An uncaught exception occured while displaying the crash screen, making normal report instead", t);
this.logger.error("An uncaught exception occured while displaying the crash screen, making normal report instead", t);
displayCrashReport(report);
System.exit(report.getFile() != null ? -1 : -2);
}
Expand All @@ -252,41 +253,41 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
// Free up memory such that this works properly in case of an OutOfMemoryError
int originalMemoryReserveSize = -1;
try { // In case another mod actually deletes the memoryReserve field
if (memoryReserve != null) {
originalMemoryReserveSize = memoryReserve.length;
memoryReserve = new byte[0];
if (this.memoryReserve != null) {
originalMemoryReserveSize = this.memoryReserve.length;
this.memoryReserve = new byte[0];
}
} catch (Throwable ignored) {
}

StateManager.INSTANCE.resetStates();

if (crashpatch$clientCrashCount >= CrashPatchConfig.INSTANCE.getLeaveLimit() || crashpatch$serverCrashCount >= CrashPatchConfig.INSTANCE.getLeaveLimit()) {
logger.error("Crash limit reached, exiting world");
this.logger.error("Crash limit reached, exiting world");
CrashUI.Companion.setLeaveWorldCrash(true);
if (getNetHandler() != null) {
getNetHandler().getNetworkManager().closeChannel(new ChatComponentText("[CrashPatch] Client crashed"));
}
loadWorld(null);

if (entityRenderer.isShaderActive()) {
entityRenderer.stopUseShader();
if (this.entityRenderer.isShaderActive()) {
this.entityRenderer.stopUseShader();
}

scheduledTasks.clear(); // TODO: Figure out why this isn't necessary for vanilla disconnect
this.scheduledTasks.clear(); // TODO: Figure out why this isn't necessary for vanilla disconnect
}

crashpatch$resetState();

if (originalMemoryReserveSize != -1) {
try {
memoryReserve = new byte[originalMemoryReserveSize];
this.memoryReserve = new byte[originalMemoryReserveSize];
} catch (Throwable ignored) {
}
}
System.gc();
} catch (Throwable t) {
logger.error("Failed to reset state after a crash", t);
this.logger.error("Failed to reset state after a crash", t);
try {
StateManager.INSTANCE.resetStates();
crashpatch$resetState();
Expand All @@ -303,19 +304,19 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
}
displayCrashReport(report);
try {
mcResourceManager = new SimpleReloadableResourceManager(metadataSerializer_);
renderEngine = new TextureManager(mcResourceManager);
mcResourceManager.registerReloadListener(renderEngine);
this.mcResourceManager = new SimpleReloadableResourceManager(this.metadataSerializer_);
this.renderEngine = new TextureManager(this.mcResourceManager);
this.mcResourceManager.registerReloadListener(this.renderEngine);

mcLanguageManager = new LanguageManager(metadataSerializer_, gameSettings.language);
mcResourceManager.registerReloadListener(mcLanguageManager);
this.mcLanguageManager = new LanguageManager(this.metadataSerializer_, this.gameSettings.language);
this.mcResourceManager.registerReloadListener(this.mcLanguageManager);

refreshResources(); // TODO: Why is this necessary?
fontRendererObj = new FontRenderer(gameSettings, new ResourceLocation("textures/font/ascii.png"), renderEngine, false);
mcResourceManager.registerReloadListener(fontRendererObj);
this.fontRendererObj = new FontRenderer(this.gameSettings, new ResourceLocation("textures/font/ascii.png"), this.renderEngine, false);
this.mcResourceManager.registerReloadListener(this.fontRendererObj);

mcSoundHandler = new SoundHandler(mcResourceManager, gameSettings);
mcResourceManager.registerReloadListener(mcSoundHandler);
this.mcSoundHandler = new SoundHandler(this.mcResourceManager, this.gameSettings);
this.mcResourceManager.registerReloadListener(this.mcSoundHandler);

//try { // this is necessary for some GUI stuff. if it works, cool, if not, it's not a big deal
// //EventManager.INSTANCE.register(Notifications.INSTANCE);
Expand All @@ -325,7 +326,7 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
//}
//todo do we need a polyui equivalent

running = true;
this.running = true;
try {
//noinspection deprecation
SplashProgress.pause();// Disable the forge splash progress screen
Expand All @@ -336,7 +337,7 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
crashpatch$runGUILoop(new CrashUI(report, CrashUI.GuiType.INIT));
} catch (Throwable t) {
if (!crashpatch$letDie) {
logger.error("An uncaught exception occured while displaying the init error screen, making normal report instead", t);
this.logger.error("An uncaught exception occured while displaying the init error screen, making normal report instead", t);
crashpatch$letDie = true;
}
displayCrashReport(report);
Expand All @@ -349,21 +350,21 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {
private void crashpatch$runGUILoop(CrashUI crashUI) throws Throwable {
GuiScreen screen = crashUI.create();
displayGuiScreen(screen);
while (running && currentScreen != null) {
while (this.running && this.currentScreen != null) {
if (Display.isCreated() && Display.isCloseRequested()) {
System.exit(0);
}
//EventManager.INSTANCE.post(new RenderEvent.Start()); todo
leftClickCounter = 10000;
currentScreen.handleInput();
currentScreen.updateScreen();
this.leftClickCounter = 10000;
this.currentScreen.handleInput();
this.currentScreen.updateScreen();

GlStateManager.pushMatrix();
GlStateManager.clear(16640);
framebufferMc.bindFramebuffer(true);
this.framebufferMc.bindFramebuffer(true);
GlStateManager.enableTexture2D();

GlStateManager.viewport(0, 0, displayWidth, displayHeight);
GlStateManager.viewport(0, 0, this.displayWidth, this.displayHeight);

ScaledResolution scaledResolution = new ScaledResolution(((Minecraft) (Object) this));
GlStateManager.clear(256);
Expand All @@ -377,20 +378,20 @@ private void onGUIDisplay(GuiScreen i, CallbackInfo ci) {

int width = scaledResolution.getScaledWidth();
int height = scaledResolution.getScaledHeight();
int mouseX = Mouse.getX() * width / displayWidth;
int mouseY = height - Mouse.getY() * height / displayHeight - 1;
int mouseX = Mouse.getX() * width / this.displayWidth;
int mouseY = height - Mouse.getY() * height / this.displayHeight - 1;
Gui.drawRect(0, 0, width, height, Color.WHITE.getRGB()); // DO NOT REMOVE THIS! FOR SOME REASON NANOVG DOESN'T RENDER WITHOUT IT
currentScreen.drawScreen(mouseX, mouseY, 0);
this.currentScreen.drawScreen(mouseX, mouseY, 0);
if (crashUI.getShouldCrash()) {
crashpatch$letDie = true;
throw Objects.requireNonNull(crashUI.getThrowable());
}

framebufferMc.unbindFramebuffer();
this.framebufferMc.unbindFramebuffer();
GlStateManager.popMatrix();

GlStateManager.pushMatrix();
framebufferMc.framebufferRender(displayWidth, displayHeight);
this.framebufferMc.framebufferRender(this.displayWidth, this.displayHeight);
GlStateManager.popMatrix();

//EventManager.INSTANCE.post(new RenderEvent(Stage.END, 0)); todo
Expand Down
Loading

0 comments on commit e79bae9

Please sign in to comment.