diff --git a/src/sporemodder/HashManager.java b/src/sporemodder/HashManager.java index 3ce3b4e..871d69b 100644 --- a/src/sporemodder/HashManager.java +++ b/src/sporemodder/HashManager.java @@ -74,8 +74,6 @@ public static HashManager get() { private NameRegistry extraRegistry; private final HashMap registries = new HashMap(); - - private String registryError; @Override public void initialize(Properties properties) { @@ -88,23 +86,23 @@ public void initialize(Properties properties) { try { fileRegistry.read(PathManager.get().getProgramFile(fileRegistry.getFileName())); } catch (Exception e) { - registryError = "The file name registry (reg_file.txt) is corrupt or missing."; + UIManager.get().setInitializationError("The file name registry (reg_file.txt) is corrupt or missing."); } try { typeRegistry.read(PathManager.get().getProgramFile(typeRegistry.getFileName())); } catch (Exception e) { - registryError = "The types registry (reg_type.txt) is corrupt or missing."; + UIManager.get().setInitializationError("The types registry (reg_type.txt) is corrupt or missing."); } try { propRegistry.read(PathManager.get().getProgramFile(propRegistry.getFileName())); } catch (Exception e) { - registryError = "The property registry (reg_property.txt) is corrupt or missing."; + UIManager.get().setInitializationError("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")); } catch (Exception e) { - registryError = "The simulator attributes registry (reg_simulator.txt or reg_simulator_stub.txt) is corrupt or missing."; + UIManager.get().setInitializationError("The simulator attributes registry (reg_simulator.txt or reg_simulator_stub.txt) is corrupt or missing."); } CnvUnit.loadNameRegistry(); @@ -115,12 +113,6 @@ 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 aa7485e..50e5a8a 100644 --- a/src/sporemodder/MainApp.java +++ b/src/sporemodder/MainApp.java @@ -188,6 +188,8 @@ private void init(boolean testInit) { e.printStackTrace(); } } + + pathManager.loadSettings(settings); // The path managers might be used in other manager initialization methods, // so we ensure we initialize them first. @@ -268,7 +270,7 @@ public void start(Stage primaryStage) { return; } - hashManager.showInitializationError(); + uiManager.showInitializationError(); if (uiManager.isFirstTime()) { gameManager.showFirstTimeDialog(); diff --git a/src/sporemodder/PathManager.java b/src/sporemodder/PathManager.java index 8318e95..b1e10b7 100644 --- a/src/sporemodder/PathManager.java +++ b/src/sporemodder/PathManager.java @@ -20,7 +20,9 @@ package sporemodder; import java.io.File; +import java.io.IOException; import java.net.URISyntaxException; +import java.nio.file.Files; import java.util.Objects; import java.util.Properties; @@ -33,6 +35,11 @@ public class PathManager extends AbstractManager { private File programFolder; /** The folder where SporeModder projects are. */ private File projectsFolder; + private boolean isDefaultProjectsFolder; + /** Path that will get saved as the Projects folder */ + public String nextProjectsFolder; + + private static final String PROPERTY_projectsPath = "projectsFolderPath"; /** @@ -73,8 +80,62 @@ public void initialize(Properties properties) { // run in ide programFolder = new File(System.getProperty("user.dir")); } - - projectsFolder = new File(programFolder, "Projects"); + } + + public void loadSettings(Properties properties) { + String path = properties.getProperty(PROPERTY_projectsPath); + if (path != null) { + projectsFolder = new File(path); + if (!Files.isDirectory(projectsFolder.toPath())) { + UIManager.get().setInitializationError("The path to the projects folder does not exist: " + path); + projectsFolder = null; + } + } + if (projectsFolder == null) { + projectsFolder = getDefaultProjectsFolder(); + } + // Check if the custom path points to the same folder + try { + isDefaultProjectsFolder = Files.isSameFile(projectsFolder.toPath(), getDefaultProjectsFolder().toPath()); + } catch (IOException e) { + e.printStackTrace(); + isDefaultProjectsFolder = path == null || path.isBlank(); + } + } + + @Override public void saveSettings(Properties properties) { + if (nextProjectsFolder != null) { + // non null but blank means reset to default + if (nextProjectsFolder.isBlank()) { + properties.remove(PROPERTY_projectsPath); + } else { + properties.put(PROPERTY_projectsPath, nextProjectsFolder); + } + } else if (!isDefaultProjectsFolder) { + properties.put(PROPERTY_projectsPath, projectsFolder.getAbsolutePath()); + } + } + + public File getDefaultProjectsFolder() { + return new File(programFolder, "Projects"); + } + + public String getProjectsFolderStringForSettings() { + if (nextProjectsFolder != null) { + return nextProjectsFolder; + } else if (isDefaultProjectsFolder) { + return ""; + } else { + return projectsFolder.getAbsolutePath(); + } + } + + public boolean isDefaultProjectsFolder() { + return isDefaultProjectsFolder; + } + + public void setNextProjectsFolder(String path) { + nextProjectsFolder = path; } /** Returns the folder where the program files are. */ diff --git a/src/sporemodder/UIManager.java b/src/sporemodder/UIManager.java index c4f6d84..7a83cc3 100644 --- a/src/sporemodder/UIManager.java +++ b/src/sporemodder/UIManager.java @@ -121,6 +121,8 @@ public static class UILoadMessage { private Image programIcon; private double dpiScaling; + + private String initializationError; /** @@ -135,6 +137,16 @@ public Stage getPrimaryStage() { return primaryStage; } + public void setInitializationError(String initializationError) { + this.initializationError = initializationError; + } + + public void showInitializationError() { + if (initializationError != null) { + UIManager.get().showDialog(Alert.AlertType.ERROR, initializationError); + } + } + @Override public void initialize(Properties properties) { diff --git a/src/sporemodder/view/dialogs/ProgramSettingsUI.fxml b/src/sporemodder/view/dialogs/ProgramSettingsUI.fxml index 29a8b9c..91e88ef 100644 --- a/src/sporemodder/view/dialogs/ProgramSettingsUI.fxml +++ b/src/sporemodder/view/dialogs/ProgramSettingsUI.fxml @@ -6,12 +6,40 @@ + - + + + + + +