Skip to content

Commit

Permalink
Fix initial crash on app init() if there are missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
emd4600 committed Oct 12, 2024
1 parent 1ccbf4e commit f2afd84
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
38 changes: 26 additions & 12 deletions src/sporemodder/HashManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -73,6 +74,8 @@ public static HashManager get() {
private NameRegistry extraRegistry;

private final HashMap<String, NameRegistry> registries = new HashMap<String, NameRegistry>();

private String registryError;

@Override
public void initialize(Properties properties) {
Expand All @@ -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();

Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/sporemodder/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ public void start(Stage primaryStage) {
Platform.exit();
return;
}

hashManager.showInitializationError();

if (uiManager.isFirstTime()) {
gameManager.showFirstTimeDialog();
Expand Down
5 changes: 4 additions & 1 deletion src/sporemodder/util/ModBundlesList.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public Collection<ModBundle> getAll() {
}

public void loadList() {
if (!Files.exists(getFile())) {
return;
}
try {
Files.readAllLines(getFile()).forEach(line -> {
ModBundle modBundle = null;
Expand All @@ -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());
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/sporemodder/util/ProjectsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f2afd84

Please sign in to comment.