Skip to content

Commit

Permalink
Allow changing the Projects folder in the program settings
Browse files Browse the repository at this point in the history
  • Loading branch information
emd4600 committed Oct 12, 2024
1 parent f2afd84 commit 92c7ee6
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 33 deletions.
16 changes: 4 additions & 12 deletions src/sporemodder/HashManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ 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 @@ -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();
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/sporemodder/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -268,7 +270,7 @@ public void start(Stage primaryStage) {
return;
}

hashManager.showInitializationError();
uiManager.showInitializationError();

if (uiManager.isFirstTime()) {
gameManager.showFirstTimeDialog();
Expand Down
65 changes: 63 additions & 2 deletions src/sporemodder/PathManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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";


/**
Expand Down Expand Up @@ -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. */
Expand Down
12 changes: 12 additions & 0 deletions src/sporemodder/UIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public static class UILoadMessage {
private Image programIcon;

private double dpiScaling;

private String initializationError;


/**
Expand All @@ -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) {

Expand Down
30 changes: 29 additions & 1 deletion src/sporemodder/view/dialogs/ProgramSettingsUI.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,40 @@
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<VBox fx:id="mainNode" prefWidth="600.0" styleClass="dialog-content" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sporemodder.view.dialogs.ProgramSettingsUI">
<VBox fx:id="mainNode" prefWidth="600.0" styleClass="dialog-content" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sporemodder.view.dialogs.ProgramSettingsUI">
<children>
<TitledPane fx:id="gameSettingsPanel1" animated="false" collapsible="false" layoutX="10.0" layoutY="10.0" text="General Settings">
<content>
<VBox prefWidth="465.0">
<children>
<Label text="Projects Folder (leave blank to use default folder):" />
<BorderPane>
<right>
<Button fx:id="findProjectsFolderButton" mnemonicParsing="false" text="Browse..." BorderPane.alignment="CENTER" />
</right>
<center>
<TextField fx:id="projectsFolderTextField" prefHeight="18.0" prefWidth="386.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets right="10.0" />
</BorderPane.margin>
<tooltip>
<Tooltip text="SporeModder FX will store and load its projects in this folder" />
</tooltip>
</TextField>
</center>
<VBox.margin>
<Insets bottom="10.0" />
</VBox.margin>
</BorderPane>
</children>
</VBox>
</content>
</TitledPane>
<TitledPane fx:id="gameSettingsPanel" animated="false" collapsible="false" text="Game Settings">
<content>
<VBox prefWidth="465.0">
Expand Down
66 changes: 49 additions & 17 deletions src/sporemodder/view/dialogs/ProgramSettingsUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package sporemodder.view.dialogs;

import java.io.File;
import java.nio.file.Files;
import java.util.Map;

import javafx.fxml.FXML;
Expand All @@ -35,12 +36,9 @@
import javafx.scene.control.Tooltip;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import sporemodder.FileManager;
import sporemodder.GameManager;
import sporemodder.*;
import sporemodder.GameManager.GameType;
import sporemodder.file.shaders.FXCompiler;
import sporemodder.MainApp;
import sporemodder.UIManager;
import sporemodder.util.GamePathConfiguration.GamePathType;
import sporemodder.util.SporeGame;
import sporemodder.view.Controller;
Expand Down Expand Up @@ -75,6 +73,9 @@ public class ProgramSettingsUI implements Controller {
@FXML private Control gameTypeLabel;
@FXML private Control styleLabel;
@FXML private Control fxcLabel;

@FXML private TextField projectsFolderTextField;
@FXML private Button findProjectsFolderButton;

private Dialog<ButtonType> dialog;

Expand All @@ -93,9 +94,7 @@ public Node getMainNode() {
chooser.getExtensionFilters().add(FileManager.FILEFILTER_ALL);
if (!fxcPathField.getText().isEmpty()) chooser.setInitialDirectory(new File(fxcPathField.getText()).getParentFile());

UIManager.get().setOverlay(true);
File result = chooser.showOpenDialog(UIManager.get().getScene().getWindow());
UIManager.get().setOverlay(false);

if (result != null) {
fxcPathField.setText(result.getAbsolutePath());
Expand All @@ -116,10 +115,8 @@ public Node getMainNode() {
findSporeButton.setOnAction(event -> {
DirectoryChooser chooser = new DirectoryChooser();
if (!sporeField.getText().isEmpty()) chooser.setInitialDirectory(new File(sporeField.getText()));

UIManager.get().setOverlay(true);

File result = chooser.showDialog(UIManager.get().getScene().getWindow());
UIManager.get().setOverlay(false);

if (result != null) {
result = gameMgr.findInstallationFolder(GameManager.SPORE_SPOREBIN, result);
Expand All @@ -136,10 +133,8 @@ public Node getMainNode() {
findGAButton.setOnAction(event -> {
DirectoryChooser chooser = new DirectoryChooser();
if (!gaField.getText().isEmpty()) chooser.setInitialDirectory(new File(gaField.getText()));

UIManager.get().setOverlay(true);

File result = chooser.showDialog(UIManager.get().getScene().getWindow());
UIManager.get().setOverlay(false);

if (result != null) {
result = gameMgr.findInstallationFolder(GameManager.GA_SPOREBIN, result);
Expand All @@ -162,10 +157,8 @@ public Node getMainNode() {
}
chooser.getExtensionFilters().add(FileManager.FILEFILTER_ALL);
chooser.getExtensionFilters().add(FileManager.FILEFILTER_EXE);

UIManager.get().setOverlay(true);

File result = chooser.showOpenDialog(UIManager.get().getScene().getWindow());
UIManager.get().setOverlay(false);

if (result != null) {
customPathField.setText(result.getAbsolutePath());
Expand Down Expand Up @@ -223,6 +216,25 @@ public Node getMainNode() {
if (FXCompiler.get().getFXCFile() != null) {
fxcPathField.setText(FXCompiler.get().getFXCFile().getAbsolutePath());
}


findProjectsFolderButton.setOnAction(event -> {
DirectoryChooser chooser = new DirectoryChooser();
File initialDirectory;
if (projectsFolderTextField.getText().isBlank()) {
initialDirectory = PathManager.get().getProjectsFolder();
} else {
initialDirectory = new File(projectsFolderTextField.getText());
}
chooser.setInitialDirectory(initialDirectory);

File result = chooser.showDialog(UIManager.get().getScene().getWindow());

if (result != null) {
projectsFolderTextField.setText(result.getAbsolutePath());
}
});
projectsFolderTextField.setText(PathManager.get().getProjectsFolderStringForSettings());
}

private void applyChanges() {
Expand Down Expand Up @@ -284,7 +296,27 @@ else if (!gameMgr.hasGalacticAdventures() || !path.equals(gameMgr.getGalacticAdv
} else {
FXCompiler.get().setFXCFile(new File(path));
}



String newProjectsFolder = null;
if (!projectsFolderTextField.getText().isBlank()) {
File projectsFolder = new File(projectsFolderTextField.getText());
if (!Files.isDirectory(projectsFolder.toPath())) {
UIManager.get().showDialog(AlertType.ERROR, "The Projects folder does not exist: " + projectsFolderTextField.getText());
} else {
newProjectsFolder = projectsFolderTextField.getText();
}
} else {
// Reset projects folder if it has been changed to default
if (!PathManager.get().isDefaultProjectsFolder()) {
newProjectsFolder = "";
}
}

if (newProjectsFolder != null) {
UIManager.get().showDialog("The Projects folder has been changed. The changes won't apply until you restart SporeModder FX.");
PathManager.get().setNextProjectsFolder(newProjectsFolder);
}

MainApp.get().saveSettings();
}
Expand Down Expand Up @@ -322,7 +354,7 @@ private void showInternal() {

public static void show() {
ProgramSettingsUI node = UIManager.get().loadUI("dialogs/ProgramSettingsUI");
node.dialog = new Dialog<ButtonType>();
node.dialog = new Dialog<>();
node.showInternal();
}
}

0 comments on commit 92c7ee6

Please sign in to comment.