From 2f19d2cd51a80fc56383f45b0228634884a3ba36 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 18 Sep 2021 10:57:22 +0900 Subject: [PATCH] error trap to inform players wtf went wrong --- src/net/torvald/terrarum/App.java | 115 ++++++++++-------- src/net/torvald/terrarum/GameCrashHandler.kt | 66 ++++++++++ .../torvald/terrarum/gameworld/GameWorld.kt | 4 +- 3 files changed, 131 insertions(+), 54 deletions(-) create mode 100644 src/net/torvald/terrarum/GameCrashHandler.kt diff --git a/src/net/torvald/terrarum/App.java b/src/net/torvald/terrarum/App.java index d55a69ed5..8a8a11302 100644 --- a/src/net/torvald/terrarum/App.java +++ b/src/net/torvald/terrarum/App.java @@ -291,67 +291,78 @@ public class App implements ApplicationListener { System.out.println(csiG+TerrarumAppConfiguration.COPYRIGHT_LICENSE_TERMS_SHORT+csi0); - // load configs - getDefaultDirectory(); - createDirs(); - readConfigJson(); - updateListOfSavegames(); - - setGamepadButtonLabels(); + try { - try { processor = GetCpuName.getModelName(); } - catch (IOException e1) { processor = "Unknown CPU"; } - try { processorVendor = GetCpuName.getCPUID(); } - catch (IOException e2) { processorVendor = "Unknown CPU"; } + // load configs + getDefaultDirectory(); + createDirs(); + readConfigJson(); + updateListOfSavegames(); + + setGamepadButtonLabels(); - ShaderProgram.pedantic = false; - - scr = new TerrarumScreenSize(getConfigInt("screenwidth"), getConfigInt("screenheight")); - int width = scr.getWidth(); - int height = scr.getHeight(); - - Lwjgl3ApplicationConfiguration appConfig = new Lwjgl3ApplicationConfiguration(); - //appConfig.useGL30 = false; // https://stackoverflow.com/questions/46753218/libgdx-should-i-use-gl30 - appConfig.useVsync(getConfigBoolean("usevsync")); - appConfig.setResizable(false); - appConfig.setWindowedMode(width, height); - int fps = Math.min(GLOBAL_FRAMERATE_LIMIT, getConfigInt("displayfps")); - if (fps <= 0) fps = GLOBAL_FRAMERATE_LIMIT; - appConfig.setIdleFPS(fps); - appConfig.setForegroundFPS(fps); - appConfig.setTitle(GAME_NAME); - //appConfig.forceExit = true; // it seems KDE 5 likes this one better... - // (Plasma freezes upon app exit. with forceExit = true, it's only frozen for a minute; with forceExit = false, it's indefinite) - //appConfig.samples = 4; // force the AA on, if the graphics driver didn't do already - - // load app icon - int[] appIconSizes = new int[]{256,128,64,32,16}; - ArrayList appIconPaths = new ArrayList<>(); - for (int size : appIconSizes) { - String name = "assets/appicon" + size + ".png"; - if (new File("./" + name).exists()) { - appIconPaths.add("./" + name); + try { + processor = GetCpuName.getModelName(); + } + catch (IOException e1) { + processor = "Unknown CPU"; + } + try { + processorVendor = GetCpuName.getCPUID(); + } + catch (IOException e2) { + processorVendor = "Unknown CPU"; } - } - Object[] iconPathsTemp = appIconPaths.toArray(); - appConfig.setWindowIcon(Arrays.copyOf(iconPathsTemp, iconPathsTemp.length, String[].class)); - //if (args.length == 1 && args[0].equals("isdev=true")) { + ShaderProgram.pedantic = false; + + scr = new TerrarumScreenSize(getConfigInt("screenwidth"), getConfigInt("screenheight")); + int width = scr.getWidth(); + int height = scr.getHeight(); + + Lwjgl3ApplicationConfiguration appConfig = new Lwjgl3ApplicationConfiguration(); + //appConfig.useGL30 = false; // https://stackoverflow.com/questions/46753218/libgdx-should-i-use-gl30 + appConfig.useVsync(getConfigBoolean("usevsync")); + appConfig.setResizable(false); + appConfig.setWindowedMode(width, height); + int fps = Math.min(GLOBAL_FRAMERATE_LIMIT, getConfigInt("displayfps")); + if (fps <= 0) fps = GLOBAL_FRAMERATE_LIMIT; + appConfig.setIdleFPS(fps); + appConfig.setForegroundFPS(fps); + appConfig.setTitle(GAME_NAME); + //appConfig.forceExit = true; // it seems KDE 5 likes this one better... + // (Plasma freezes upon app exit. with forceExit = true, it's only frozen for a minute; with forceExit = false, it's indefinite) + //appConfig.samples = 4; // force the AA on, if the graphics driver didn't do already + + // load app icon + int[] appIconSizes = new int[]{256, 128, 64, 32, 16}; + ArrayList appIconPaths = new ArrayList<>(); + for (int size : appIconSizes) { + String name = "assets/appicon" + size + ".png"; + if (new File("./" + name).exists()) { + appIconPaths.add("./" + name); + } + } + + Object[] iconPathsTemp = appIconPaths.toArray(); + appConfig.setWindowIcon(Arrays.copyOf(iconPathsTemp, iconPathsTemp.length, String[].class)); + IS_DEVELOPMENT_BUILD = true; - // safe area box - //KeyToggler.INSTANCE.forceSet(Input.Keys.F11, true); - //} - //else { - // System.err.println("Game not started using DEBUG MODE -- current build of the game will display black screen without debug mode"); - //} - - // set some more configuration vars - MULTITHREAD = THREAD_COUNT >= 3 && getConfigBoolean("multithread"); - new Lwjgl3Application(new App(appConfig), appConfig); + // set some more configuration vars + MULTITHREAD = THREAD_COUNT >= 3 && getConfigBoolean("multithread"); + + new Lwjgl3Application(new App(appConfig), appConfig); + } + catch (Throwable e) { + if (Gdx.app != null) { + Gdx.app.exit(); + } + new GameCrashHandler(e); + } } @Override diff --git a/src/net/torvald/terrarum/GameCrashHandler.kt b/src/net/torvald/terrarum/GameCrashHandler.kt new file mode 100644 index 000000000..155346df4 --- /dev/null +++ b/src/net/torvald/terrarum/GameCrashHandler.kt @@ -0,0 +1,66 @@ +package net.torvald.terrarum + +import java.awt.BorderLayout +import java.awt.Color +import java.awt.Dimension +import java.awt.Font +import java.awt.Font.BOLD +import java.awt.Font.PLAIN +import java.io.OutputStream +import java.io.PrintStream +import javax.swing.* + +/** + * Created by minjaesong on 2021-09-18. + */ +class GameCrashHandler(e: Throwable) : JFrame() { + + val textArea = JTextArea() + + private val outputStream = object : OutputStream() { + override fun write(p0: Int) { + textArea.append("${p0.toChar()}") + } + } + + private val printStream = object : PrintStream(outputStream) {} + + init { + val border = JPanel() + border.layout = BorderLayout(18,18) + border.isVisible = true + border.background = Color(63,79,93) + + + val title = JLabel("Help! A blackhole ate my game!") + title.font = Font("SansSerif", BOLD, 18) + title.foreground = Color.WHITE + title.isVisible = true + title.horizontalAlignment = SwingConstants.CENTER + border.add(title, BorderLayout.NORTH) + + + textArea.font = Font("Monospaced", PLAIN, 14) + textArea.isVisible = true + textArea.isEditable = false + border.add(JScrollPane(textArea), BorderLayout.CENTER) + + + this.layout = BorderLayout(32,32) + this.size = Dimension(1280, 720) + this.isVisible = true + this.defaultCloseOperation = EXIT_ON_CLOSE + this.contentPane.background = Color(63,79,93) + this.add(JLabel(), BorderLayout.EAST) + this.add(JLabel(), BorderLayout.WEST) + this.add(JLabel(), BorderLayout.SOUTH) + this.add(JLabel(), BorderLayout.NORTH) + this.add(border, BorderLayout.CENTER) + this.title = TerrarumAppConfiguration.GAME_NAME + + + e.printStackTrace(printStream) + e.printStackTrace() + } + +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index ce84ddfab..748bd3a05 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -213,7 +213,7 @@ open class GameWorld() : Disposable { return tileNumberToNameMap[layerWall.unsafeGetTile(x, y).toLong()]!! } catch (e: NullPointerException) { - val msg = "No tile name mapping for wall ${layerWall.unsafeGetTile(x, y)} in ($x, $y)" + val msg = "No tile name mapping for wall ${layerWall.unsafeGetTile(x, y)} in ($x, $y) from $layerWall" throw NoSuchElementException(msg) } } @@ -228,7 +228,7 @@ open class GameWorld() : Disposable { return tileNumberToNameMap[layerTerrain.unsafeGetTile(x, y).toLong()]!! } catch (e: NullPointerException) { - val msg = "No tile name mapping for terrain ${layerTerrain.unsafeGetTile(x, y)} in ($x, $y)" + val msg = "No tile name mapping for terrain ${layerTerrain.unsafeGetTile(x, y)} in ($x, $y) from $layerTerrain" throw NoSuchElementException(msg) } }