|
| 1 | +package org.dimdev.vanillafix.mixins.client; |
| 2 | + |
| 3 | +import net.minecraft.client.Minecraft; |
| 4 | +import net.minecraft.client.gui.GuiMemoryErrorScreen; |
| 5 | +import net.minecraft.client.gui.GuiScreen; |
| 6 | +import net.minecraft.crash.CrashReport; |
| 7 | +import net.minecraft.init.Bootstrap; |
| 8 | +import net.minecraft.profiler.ISnooperInfo; |
| 9 | +import net.minecraft.util.IThreadListener; |
| 10 | +import net.minecraft.util.MinecraftError; |
| 11 | +import net.minecraft.util.ReportedException; |
| 12 | +import net.minecraftforge.fml.relauncher.Side; |
| 13 | +import net.minecraftforge.fml.relauncher.SideOnly; |
| 14 | +import org.apache.logging.log4j.Logger; |
| 15 | +import org.dimdev.vanillafix.GuiCrashScreen; |
| 16 | +import org.lwjgl.LWJGLException; |
| 17 | +import org.spongepowered.asm.mixin.Final; |
| 18 | +import org.spongepowered.asm.mixin.Mixin; |
| 19 | +import org.spongepowered.asm.mixin.Overwrite; |
| 20 | +import org.spongepowered.asm.mixin.Shadow; |
| 21 | + |
| 22 | +import javax.annotation.Nullable; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.text.SimpleDateFormat; |
| 26 | +import java.util.Date; |
| 27 | + |
| 28 | +@SuppressWarnings({"unused", "NonConstantFieldWithUpperCaseName", "RedundantThrows"}) // Shadow |
| 29 | +@SideOnly(Side.CLIENT) |
| 30 | +@Mixin(Minecraft.class) |
| 31 | +public abstract class MixinMinecraft implements IThreadListener, ISnooperInfo { |
| 32 | + |
| 33 | + @Shadow volatile boolean running; |
| 34 | + @Shadow private boolean hasCrashed; |
| 35 | + @Shadow private CrashReport crashReporter; |
| 36 | + |
| 37 | + @Shadow private void init() throws LWJGLException, IOException {} |
| 38 | + |
| 39 | + @Shadow private void runGameLoop() throws IOException {} |
| 40 | + |
| 41 | + @Shadow public void freeMemory() {} |
| 42 | + |
| 43 | + @Shadow public void displayGuiScreen(@Nullable GuiScreen guiScreenIn) {} |
| 44 | + |
| 45 | + @Shadow public CrashReport addGraphicsAndWorldToCrashReport(CrashReport theCrash) { return null; } |
| 46 | + |
| 47 | + @Shadow @Final private static Logger LOGGER; |
| 48 | + |
| 49 | + @Shadow public void shutdownMinecraftApplet() {} |
| 50 | + |
| 51 | + @Shadow public void displayCrashReport(CrashReport crashReportIn) {} |
| 52 | + |
| 53 | + @SuppressWarnings("CallToSystemGC") |
| 54 | + @Overwrite |
| 55 | + public void run() { |
| 56 | + running = true; |
| 57 | + |
| 58 | + try { |
| 59 | + init(); |
| 60 | + } catch (Throwable throwable) { |
| 61 | + CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Initializing game"); |
| 62 | + crashreport.makeCategory("Initialization"); |
| 63 | + displayCrashReport(addGraphicsAndWorldToCrashReport(crashreport)); // TODO: GUI for this too |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + while (running) { |
| 69 | + if (!hasCrashed || crashReporter == null) { |
| 70 | + try { |
| 71 | + runGameLoop(); |
| 72 | + } catch (OutOfMemoryError e) { |
| 73 | + freeMemory(); |
| 74 | + displayGuiScreen(new GuiMemoryErrorScreen()); |
| 75 | + System.gc(); |
| 76 | + } catch (ReportedException e) { |
| 77 | + addGraphicsAndWorldToCrashReport(e.getCrashReport()); |
| 78 | + freeMemory(); |
| 79 | + LOGGER.fatal("Reported exception thrown!", e); |
| 80 | + displayCrashScreen(e.getCrashReport()); |
| 81 | + } catch (Throwable e) { |
| 82 | + CrashReport report = addGraphicsAndWorldToCrashReport(new CrashReport("Unexpected error", e)); |
| 83 | + freeMemory(); |
| 84 | + LOGGER.fatal("Unreported exception thrown!", e); |
| 85 | + displayCrashScreen(report); |
| 86 | + } |
| 87 | + } else { |
| 88 | + displayCrashReport(crashReporter); |
| 89 | + } |
| 90 | + } |
| 91 | + } catch (MinecraftError ignored) { |
| 92 | + } finally { |
| 93 | + shutdownMinecraftApplet(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public void displayCrashScreen(CrashReport report) { |
| 98 | + try { |
| 99 | + File crashReportsDir = new File(Minecraft.getMinecraft().mcDataDir, "crash-reports"); |
| 100 | + File crashReportSaveFile = new File(crashReportsDir, "crash-" + new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss").format(new Date()) + "-client.txt"); |
| 101 | + |
| 102 | + // Print the report in bootstrap |
| 103 | + Bootstrap.printToSYSOUT(report.getCompleteReport()); |
| 104 | + |
| 105 | + // Save the report and print file in bootstrap |
| 106 | + File reportFile = null; |
| 107 | + if (report.getFile() != null) { |
| 108 | + reportFile = report.getFile(); |
| 109 | + } else if (report.saveToFile(crashReportSaveFile)) { |
| 110 | + reportFile = crashReportSaveFile; |
| 111 | + } |
| 112 | + |
| 113 | + if (reportFile != null) { |
| 114 | + Bootstrap.printToSYSOUT("Recoverable game crash! Crash report saved to: " + reportFile); |
| 115 | + } else { |
| 116 | + Bootstrap.printToSYSOUT("Recoverable game crash! Crash report could not be saved."); |
| 117 | + } |
| 118 | + |
| 119 | + // Display the crash screen |
| 120 | + displayGuiScreen(new GuiCrashScreen(reportFile, report)); |
| 121 | + |
| 122 | + // Keep running |
| 123 | + hasCrashed = false; |
| 124 | + } catch (Throwable e) { |
| 125 | + LOGGER.error("The crash screen threw an error, reverting to default crash report", e); |
| 126 | + displayCrashReport(report); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments