From 7532b3e0fbf57ec33e14f593d04716585e3be150 Mon Sep 17 00:00:00 2001 From: miozune Date: Tue, 9 Aug 2022 06:25:17 +0900 Subject: [PATCH] Add limit for number of logs generated (#5) * Add limit for number of logs generated * Extract common code * better GTNH mode * Open issue tracker for both opened and closed issues --- .../bettercrashes/BetterCrashesConfig.java | 21 +++- .../mixins/minecraft/MinecraftMixin.java | 18 ++- .../bettercrashes/utils/GuiCrashScreen.java | 62 +--------- .../utils/GuiInitErrorScreen.java | 62 +--------- .../bettercrashes/utils/GuiProblemScreen.java | 113 +++++++++++++++++- .../assets/bettercrashes/lang/en_US.lang | 2 + .../assets/bettercrashes/lang/zh_CN.lang | 2 + 7 files changed, 161 insertions(+), 119 deletions(-) diff --git a/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java b/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java index af01e3c..e459bfa 100644 --- a/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java +++ b/src/main/java/vfyjxf/bettercrashes/BetterCrashesConfig.java @@ -1,5 +1,6 @@ package vfyjxf.bettercrashes; +import cpw.mods.fml.common.Loader; import java.io.File; import net.minecraftforge.common.config.Configuration; @@ -9,7 +10,9 @@ public class BetterCrashesConfig { public static final String GENERAL = "General"; - public static boolean isGTNH = true; + public static boolean isGTNH = Loader.isModLoaded("dreamcraft"); + public static int crashLogLimitClient = 30; + public static int crashLogLimitServer = 30; public static void init(File file) { config = new Configuration(file); @@ -19,8 +22,20 @@ public class BetterCrashesConfig { public static void syncConfig() { config.setCategoryComment(GENERAL, "General config"); - isGTNH = config.get(GENERAL, "isGTNH", false, "Set to false if you're playing outside of GTNH") - .getBoolean(); + crashLogLimitClient = config.getInt( + "crashLogLimitClient", + GENERAL, + 30, + 0, + Integer.MAX_VALUE, + "Maximum number of crash logs generated per restart for client. Suppresses too many logs generated by continuous crashes."); + crashLogLimitServer = config.getInt( + "crashLogLimitServer", + GENERAL, + 30, + 0, + Integer.MAX_VALUE, + "Maximum number of crash logs generated per restart for server. Suppresses too many logs generated by continuous crashes."); if (config.hasChanged()) { config.save(); diff --git a/src/main/java/vfyjxf/bettercrashes/mixins/minecraft/MinecraftMixin.java b/src/main/java/vfyjxf/bettercrashes/mixins/minecraft/MinecraftMixin.java index edafc16..3bcd37e 100644 --- a/src/main/java/vfyjxf/bettercrashes/mixins/minecraft/MinecraftMixin.java +++ b/src/main/java/vfyjxf/bettercrashes/mixins/minecraft/MinecraftMixin.java @@ -38,6 +38,7 @@ 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 vfyjxf.bettercrashes.BetterCrashesConfig; import vfyjxf.bettercrashes.utils.CrashUtils; import vfyjxf.bettercrashes.utils.GuiCrashScreen; import vfyjxf.bettercrashes.utils.GuiInitErrorScreen; @@ -212,7 +213,9 @@ public abstract class MinecraftMixin { */ public void displayCrashScreen(CrashReport report) { try { - CrashUtils.outputReport(report); + if (shouldGenerateCrashLog()) { + CrashUtils.outputReport(report); + } // Reset hasCrashed, debugCrashKeyPressTime, and crashIntegratedServerNextTick hasCrashed = false; @@ -295,7 +298,9 @@ public abstract class MinecraftMixin { * @param report */ public void displayInitErrorScreen(CrashReport report) { - CrashUtils.outputReport(report); + if (shouldGenerateCrashLog()) { + CrashUtils.outputReport(report); + } try { mcResourceManager = new SimpleReloadableResourceManager(metadataSerializer_); renderEngine = new TextureManager(mcResourceManager); @@ -397,6 +402,13 @@ public abstract class MinecraftMixin { */ @Overwrite public void displayCrashReport(CrashReport report) { - CrashUtils.outputReport(report); + if (shouldGenerateCrashLog()) { + CrashUtils.outputReport(report); + } + } + + public boolean shouldGenerateCrashLog() { + return clientCrashCount <= BetterCrashesConfig.crashLogLimitClient + && serverCrashCount <= BetterCrashesConfig.crashLogLimitServer; } } diff --git a/src/main/java/vfyjxf/bettercrashes/utils/GuiCrashScreen.java b/src/main/java/vfyjxf/bettercrashes/utils/GuiCrashScreen.java index 8959ed9..e4f1df9 100644 --- a/src/main/java/vfyjxf/bettercrashes/utils/GuiCrashScreen.java +++ b/src/main/java/vfyjxf/bettercrashes/utils/GuiCrashScreen.java @@ -6,8 +6,6 @@ package vfyjxf.bettercrashes.utils; -import static vfyjxf.bettercrashes.BetterCrashesConfig.isGTNH; - import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.gui.GuiButton; @@ -15,7 +13,6 @@ import net.minecraft.client.gui.GuiMainMenu; import net.minecraft.client.gui.GuiOptionButton; import net.minecraft.client.resources.I18n; import net.minecraft.crash.CrashReport; -import org.apache.commons.lang3.StringUtils; @SideOnly(Side.CLIENT) public class GuiCrashScreen extends GuiProblemScreen { @@ -46,59 +43,12 @@ public class GuiCrashScreen extends GuiProblemScreen { } @Override - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - if (detectedUnsupportedModNames == null) { - detectedUnsupportedModNames = getUnsupportedMods(); - } - boolean hasUnsupportedMods = !detectedUnsupportedModNames.isEmpty(); + protected String getScreenTitle() { + return I18n.format("bettercrashes.gui.crash.title"); + } - drawDefaultBackground(); - drawCenteredString( - fontRendererObj, - I18n.format("bettercrashes.gui.crash.title"), - width / 2, - height / 4 - 40 - (hasUnsupportedMods ? 16 : 0), - 0xFFFFFF); - - int textColor = 0xD0D0D0; - int x = width / 2 - 155; - int y = height / 4; - if (hasUnsupportedMods) { - y -= 32; - } - - drawString(fontRendererObj, I18n.format("bettercrashes.gui.crash.summary"), x, y, textColor); - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph1"), x, y += 18, textColor); - - drawCenteredString(fontRendererObj, getModListString(), width / 2, y += 11, 0xE0E000); - - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph2"), x, y += 11, textColor); - - drawCenteredString( - fontRendererObj, - report.getFile() != null - ? "\u00A7n" + report.getFile().getName() - : I18n.format("bettercrashes.gui.common.reportSaveFailed"), - width / 2, - y += 11, - 0x00FF00); - - y += 12; - y += drawLongString( - fontRendererObj, - I18n.format("bettercrashes.gui.common.paragraph3" + (isGTNH ? "_gtnh" : "")), - x, - y, - 340, - textColor); - - if (hasUnsupportedMods) { - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph4_gtnh"), x, y += 10, textColor); - drawCenteredString( - fontRendererObj, StringUtils.join(detectedUnsupportedModNames, ", "), width / 2, y += 11, 0xE0E000); - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph5_gtnh"), x, y += 12, textColor); - } - - super.drawScreen(mouseX, mouseY, partialTicks); + @Override + protected String getScreenSummary() { + return I18n.format("bettercrashes.gui.crash.summary"); } } diff --git a/src/main/java/vfyjxf/bettercrashes/utils/GuiInitErrorScreen.java b/src/main/java/vfyjxf/bettercrashes/utils/GuiInitErrorScreen.java index b52f608..27ba6ce 100644 --- a/src/main/java/vfyjxf/bettercrashes/utils/GuiInitErrorScreen.java +++ b/src/main/java/vfyjxf/bettercrashes/utils/GuiInitErrorScreen.java @@ -6,14 +6,11 @@ package vfyjxf.bettercrashes.utils; -import static vfyjxf.bettercrashes.BetterCrashesConfig.isGTNH; - import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.resources.I18n; import net.minecraft.crash.CrashReport; -import org.apache.commons.lang3.StringUtils; @SideOnly(Side.CLIENT) public class GuiInitErrorScreen extends GuiProblemScreen { @@ -48,59 +45,12 @@ public class GuiInitErrorScreen extends GuiProblemScreen { } @Override - public void drawScreen(int mouseX, int mouseY, float partialTicks) { - if (detectedUnsupportedModNames == null) { - detectedUnsupportedModNames = getUnsupportedMods(); - } - boolean hasUnsupportedMods = !detectedUnsupportedModNames.isEmpty(); + protected String getScreenTitle() { + return I18n.format("bettercrashes.gui.init_error.title"); + } - drawDefaultBackground(); - drawCenteredString( - fontRendererObj, - I18n.format("bettercrashes.gui.init_error.title"), - width / 2, - height / 4 - 40 - (hasUnsupportedMods ? 16 : 0), - 0xFFFFFF); - - int textColor = 0xD0D0D0; - int x = width / 2 - 155; - int y = height / 4; - if (hasUnsupportedMods) { - y -= 32; - } - - drawString(fontRendererObj, I18n.format("bettercrashes.gui.init_error.summary"), x, y, textColor); - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph1"), x, y += 18, textColor); - - drawCenteredString(fontRendererObj, getModListString(), width / 2, y += 11, 0xE0E000); - - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph2"), x, y += 11, textColor); - - drawCenteredString( - fontRendererObj, - report.getFile() != null - ? "\u00A7n" + report.getFile().getName() - : I18n.format("bettercrashes.gui.common.reportSaveFailed"), - width / 2, - y += 11, - 0x00FF00); - - y += 12; - y += drawLongString( - fontRendererObj, - I18n.format("bettercrashes.gui.common.paragraph3" + (isGTNH ? "_gtnh" : "")), - x, - y, - 340, - textColor); - - if (hasUnsupportedMods) { - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph4_gtnh"), x, y += 10, textColor); - drawCenteredString( - fontRendererObj, StringUtils.join(detectedUnsupportedModNames, ", "), width / 2, y += 11, 0xE0E000); - drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph5_gtnh"), x, y += 12, textColor); - } - - super.drawScreen(mouseX, mouseY, partialTicks); + @Override + protected String getScreenSummary() { + return I18n.format("bettercrashes.gui.init_error.summary"); } } diff --git a/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java b/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java index a5edc6a..079ce14 100644 --- a/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java +++ b/src/main/java/vfyjxf/bettercrashes/utils/GuiProblemScreen.java @@ -6,12 +6,15 @@ package vfyjxf.bettercrashes.utils; +import static vfyjxf.bettercrashes.BetterCrashesConfig.isGTNH; + import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.ModContainer; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import java.io.IOException; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -29,13 +32,28 @@ import vfyjxf.bettercrashes.BetterCrashesConfig; @SideOnly(Side.CLIENT) public abstract class GuiProblemScreen extends GuiScreen { + private static Field fieldClientCrashCount = null; + private static Field fieldServerCrashCount = null; + + static { + try { + // these are actually reachable, see MinecraftMixin + fieldClientCrashCount = Minecraft.class.getDeclaredField("clientCrashCount"); + fieldClientCrashCount.setAccessible(true); + fieldServerCrashCount = Minecraft.class.getDeclaredField("serverCrashCount"); + fieldServerCrashCount.setAccessible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + protected final CrashReport report; private volatile String hasteLink = null; private String modListString; protected static final List UNSUPPORTED_MOD_IDS = Arrays.asList(); protected List detectedUnsupportedModNames; private static final String GTNH_ISSUE_TRACKER = - "https://github.com/GTNewHorizons/GT-New-Horizons-Modpack/labels/Type%3A%20Crash"; + "https://github.com/GTNewHorizons/GT-New-Horizons-Modpack/issues?q=label%3A%22Type%3A+Crash%22"; public GuiProblemScreen(CrashReport report) { this.report = report; @@ -121,6 +139,72 @@ public abstract class GuiProblemScreen extends GuiScreen { @Override protected void keyTyped(char typedChar, int keyCode) {} + protected abstract String getScreenTitle(); + + protected abstract String getScreenSummary(); + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + if (detectedUnsupportedModNames == null) { + detectedUnsupportedModNames = getUnsupportedMods(); + } + boolean hasUnsupportedMods = !detectedUnsupportedModNames.isEmpty(); + + drawDefaultBackground(); + drawCenteredString( + fontRendererObj, + getScreenTitle(), + width / 2, + height / 4 - 40 - (hasUnsupportedMods ? 16 : 0), + 0xFFFFFF); + + int textColor = 0xD0D0D0; + int x = width / 2 - 155; + int y = height / 4; + if (hasUnsupportedMods) { + y -= 32; + } + + drawString(fontRendererObj, getScreenSummary(), x, y, textColor); + drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph1"), x, y += 18, textColor); + + drawCenteredString(fontRendererObj, getModListString(), width / 2, y += 11, 0xE0E000); + + if (isCrashLogExpectedToBeGenerated()) { + drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph2"), x, y += 11, textColor); + + drawCenteredString( + fontRendererObj, + report.getFile() != null + ? "\u00A7n" + report.getFile().getName() + : I18n.format("bettercrashes.gui.common.reportSaveFailed"), + width / 2, + y += 11, + 0x00FF00); + } else { + drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph6"), x, y += 11, textColor); + drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph7"), x, y += 11, textColor); + } + + y += 12; + y += drawLongString( + fontRendererObj, + I18n.format("bettercrashes.gui.common.paragraph3" + (isGTNH ? "_gtnh" : "")), + x, + y, + 340, + textColor); + + if (hasUnsupportedMods) { + drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph4_gtnh"), x, y += 10, textColor); + drawCenteredString( + fontRendererObj, StringUtils.join(detectedUnsupportedModNames, ", "), width / 2, y += 11, 0xE0E000); + drawString(fontRendererObj, I18n.format("bettercrashes.gui.common.paragraph5_gtnh"), x, y += 12, textColor); + } + + super.drawScreen(mouseX, mouseY, partialTicks); + } + protected String getModListString() { if (modListString == null) { final Set suspectedMods = ((IPatchedCrashReport) report).getSuspectedMods(); @@ -164,4 +248,31 @@ public abstract class GuiProblemScreen extends GuiScreen { } return list; } + + private int getClientCrashCount() { + if (fieldClientCrashCount != null) { + try { + return (int) fieldClientCrashCount.get(Minecraft.getMinecraft()); + } catch (Exception e) { + e.printStackTrace(); + } + } + return 0; + } + + private int getServerCrashCount() { + if (fieldServerCrashCount != null) { + try { + return (int) fieldServerCrashCount.get(Minecraft.getMinecraft()); + } catch (Exception e) { + e.printStackTrace(); + } + } + return 0; + } + + protected boolean isCrashLogExpectedToBeGenerated() { + return getClientCrashCount() <= BetterCrashesConfig.crashLogLimitClient + && getServerCrashCount() <= BetterCrashesConfig.crashLogLimitServer; + } } diff --git a/src/main/resources/assets/bettercrashes/lang/en_US.lang b/src/main/resources/assets/bettercrashes/lang/en_US.lang index 41bce88..8eb1ab3 100644 --- a/src/main/resources/assets/bettercrashes/lang/en_US.lang +++ b/src/main/resources/assets/bettercrashes/lang/en_US.lang @@ -15,6 +15,8 @@ bettercrashes.gui.common.paragraph3=You're encouraged to send this report's link bettercrashes.gui.common.paragraph3_gtnh=You're encouraged to send this report's link to the GTNH issue tracker to help us fix the issue, AFTER searching the tracker for similar issue already reported. Clicking the "Upload and Copy link" button can upload report and copy its link to clipboard. Since BetterCrashes is installed, you can keep playing despite the crash. bettercrashes.gui.common.paragraph4_gtnh=GTNH does not support the following mod(s): bettercrashes.gui.common.paragraph5_gtnh=When sending report, add mention to these as well. +bettercrashes.gui.common.paragraph6=Number of crash logs exceeded limit and log was not generated. +bettercrashes.gui.common.paragraph7=Limit can be changed via config. bettercrashes.gui.crash.title=Minecraft crashed! bettercrashes.gui.crash.summary=Minecraft ran into a problem and crashed. diff --git a/src/main/resources/assets/bettercrashes/lang/zh_CN.lang b/src/main/resources/assets/bettercrashes/lang/zh_CN.lang index 69bdba4..917d925 100644 --- a/src/main/resources/assets/bettercrashes/lang/zh_CN.lang +++ b/src/main/resources/assets/bettercrashes/lang/zh_CN.lang @@ -14,6 +14,8 @@ bettercrashes.gui.common.paragraph3=我们建议您将此报告的链接发送 #bettercrashes.gui.common.paragraph3_gtnh=You're encouraged to send this report's link to the GTNH issue tracker to help us fix the issue, AFTER searching the tracker for similar issue already reported. Clicking the "Upload and Copy link" button can upload report and copy its link to clipboard. Since BetterCrashes is installed, you can keep playing despite the crash. #bettercrashes.gui.common.paragraph4_gtnh=GTNH does not support the following mod(s): #bettercrashes.gui.common.paragraph5_gtnh=When sending report, add mention to these as well. +#bettercrashes.gui.common.paragraph6=Number of crash logs exceeded limit and log was not generated. +#bettercrashes.gui.common.paragraph7=Limit can be changed via config. bettercrashes.gui.crash.title=Minecraft崩溃啦! bettercrashes.gui.crash.summary=Minecraft遇到了一个问题并崩溃了。