Skip to content

Commit

Permalink
Crash at most once after earlydisplay errors (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Jul 6, 2024
1 parent ffde017 commit 4eabaff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import static org.lwjgl.opengl.GL32C.*;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
Expand All @@ -34,6 +32,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -299,13 +298,10 @@ public String getGLVersion() {
return this.glVersion;
}

private final ReentrantLock crashLock = new ReentrantLock();

private void crashElegantly(String errorDetails) {
String qrText;
try (var is = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/glfailure.txt")))) {
qrText = is.lines().collect(Collectors.joining("\n"));
} catch (IOException ioe) {
qrText = "";
}
crashLock.lock(); // Crash at most once!

StringBuilder msgBuilder = new StringBuilder(2000);
msgBuilder.append("Failed to initialize the mod loading system and display.\n");
Expand All @@ -314,9 +310,9 @@ private void crashElegantly(String errorDetails) {
msgBuilder.append(errorDetails);
msgBuilder.append("\n\n");
msgBuilder.append("If you click yes, we will try and open " + ERROR_URL + " in your default browser");
LOGGER.error("ERROR DISPLAY\n" + msgBuilder);
LOGGER.error("ERROR DISPLAY\n{}", msgBuilder);
// we show the display on a new dedicated thread
Executors.newSingleThreadExecutor().submit(() -> {
var thread = new Thread(() -> {
var res = TinyFileDialogs.tinyfd_messageBox("Minecraft: NeoForge", msgBuilder.toString(), "yesno", "error", false);
if (res) {
try {
Expand All @@ -325,8 +321,14 @@ private void crashElegantly(String errorDetails) {
TinyFileDialogs.tinyfd_messageBox("Minecraft: NeoForge", "Sadly, we couldn't open your browser.\nVisit " + ERROR_URL, "ok", "error", false);
}
}
System.exit(1);
});
}, "crash-report");
thread.setDaemon(true);
thread.start();
try {
thread.join();
} catch (InterruptedException ignored) {}

System.exit(1);
}

/**
Expand Down
13 changes: 0 additions & 13 deletions earlydisplay/src/main/resources/glfailure.txt

This file was deleted.

0 comments on commit 4eabaff

Please sign in to comment.