From f2afd84a005b355065a86b7600e02c600e19d1f3 Mon Sep 17 00:00:00 2001 From: emd4600 Date: Sat, 12 Oct 2024 11:15:38 +0200 Subject: [PATCH] Fix initial crash on app init() if there are missing files --- src/sporemodder/HashManager.java | 38 ++++++++++++++++-------- src/sporemodder/MainApp.java | 2 ++ src/sporemodder/util/ModBundlesList.java | 5 +++- src/sporemodder/util/ProjectsList.java | 3 ++ 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/sporemodder/HashManager.java b/src/sporemodder/HashManager.java index cbf9832..3ce3b4e 100644 --- a/src/sporemodder/HashManager.java +++ b/src/sporemodder/HashManager.java @@ -25,6 +25,7 @@ import java.util.Locale; import java.util.Properties; +import javafx.scene.control.Alert; import sporemodder.file.cnv.CnvUnit; import sporemodder.util.NameRegistry; @@ -73,6 +74,8 @@ public static HashManager get() { private NameRegistry extraRegistry; private final HashMap registries = new HashMap(); + + private String registryError; @Override public void initialize(Properties properties) { @@ -81,23 +84,28 @@ public void initialize(Properties properties) { decimalFormat = "#.#######"; defaultDecimalFormat = new DecimalFormat(decimalFormat, decimalSymbols); defaultDecimalFormat.setNegativePrefix("-"); - - UIManager.get().tryAction(() -> { + + try { fileRegistry.read(PathManager.get().getProgramFile(fileRegistry.getFileName())); - }, "The file name registry (reg_file.txt) is corrupt or missing."); - - UIManager.get().tryAction(() -> { + } catch (Exception e) { + registryError = "The file name registry (reg_file.txt) is corrupt or missing."; + } + try { typeRegistry.read(PathManager.get().getProgramFile(typeRegistry.getFileName())); - }, "The types registry (reg_type.txt) is corrupt or missing."); - - UIManager.get().tryAction(() -> { + } catch (Exception e) { + registryError = "The types registry (reg_type.txt) is corrupt or missing."; + } + try { propRegistry.read(PathManager.get().getProgramFile(propRegistry.getFileName())); - }, "The property registry (reg_property.txt) is corrupt or missing."); - - UIManager.get().tryAction(() -> { + } catch (Exception e) { + registryError = "The property registry (reg_property.txt) is corrupt or missing."; + } + try { simulatorRegistry.read(PathManager.get().getProgramFile(simulatorRegistry.getFileName())); simulatorRegistry.read(PathManager.get().getProgramFile("reg_simulator_stub.txt")); - }, "The simulator attributes registry (reg_simulator.txt) is corrupt or missing."); + } catch (Exception e) { + registryError = "The simulator attributes registry (reg_simulator.txt or reg_simulator_stub.txt) is corrupt or missing."; + } CnvUnit.loadNameRegistry(); @@ -107,6 +115,12 @@ public void initialize(Properties properties) { registries.put(simulatorRegistry.getFileName(), simulatorRegistry); registries.put(projectRegistry.getFileName(), projectRegistry); } + + public void showInitializationError() { + if (registryError != null) { + UIManager.get().showDialog(Alert.AlertType.ERROR, registryError); + } + } public void replaceRegistries(NameRegistry file, NameRegistry prop, NameRegistry type) { fileRegistry = file == null ? originalFileRegistry : file; diff --git a/src/sporemodder/MainApp.java b/src/sporemodder/MainApp.java index b044cee..aa7485e 100644 --- a/src/sporemodder/MainApp.java +++ b/src/sporemodder/MainApp.java @@ -267,6 +267,8 @@ public void start(Stage primaryStage) { Platform.exit(); return; } + + hashManager.showInitializationError(); if (uiManager.isFirstTime()) { gameManager.showFirstTimeDialog(); diff --git a/src/sporemodder/util/ModBundlesList.java b/src/sporemodder/util/ModBundlesList.java index db3e05a..e6dc7bb 100644 --- a/src/sporemodder/util/ModBundlesList.java +++ b/src/sporemodder/util/ModBundlesList.java @@ -45,6 +45,9 @@ public Collection getAll() { } public void loadList() { + if (!Files.exists(getFile())) { + return; + } try { Files.readAllLines(getFile()).forEach(line -> { ModBundle modBundle = null; @@ -65,7 +68,7 @@ public void loadList() { modBundles.put(modBundle.getName().toLowerCase(), modBundle); } }); - } catch (IOException e) { + } catch (Exception e) { System.err.println("Failed to read mod bundles list: " + e.getMessage()); } } diff --git a/src/sporemodder/util/ProjectsList.java b/src/sporemodder/util/ProjectsList.java index d894bb7..4eb3694 100644 --- a/src/sporemodder/util/ProjectsList.java +++ b/src/sporemodder/util/ProjectsList.java @@ -134,6 +134,9 @@ public void saveLastActiveTimesNoException() { } public void loadLastActiveTimes() { + if (!Files.exists(getTimesListFile())) { + return; + } try { Files.readAllLines(getTimesListFile()).forEach(line -> { String[] splits = line.split(" ", 2);