diff --git a/src/main/java/io/rpg/config/model/ActionConfigBundle.java b/src/main/java/io/rpg/config/model/ActionConfigBundle.java index 0ddde31b..27750fce 100644 --- a/src/main/java/io/rpg/config/model/ActionConfigBundle.java +++ b/src/main/java/io/rpg/config/model/ActionConfigBundle.java @@ -218,9 +218,6 @@ Result validateForQuizAction() { Result validateForDialogueAction() { ErrorMessageBuilder builder = new ErrorMessageBuilder(); - if (description == null) { - builder.append("No description provided"); - } if (assetPath == null) { builder.append("No asset path provided"); } else if (!Files.isRegularFile(Path.of(assetPath))) { diff --git a/src/main/java/io/rpg/config/model/GameWorldConfig.java b/src/main/java/io/rpg/config/model/GameWorldConfig.java index 7caa8564..91c943f5 100644 --- a/src/main/java/io/rpg/config/model/GameWorldConfig.java +++ b/src/main/java/io/rpg/config/model/GameWorldConfig.java @@ -7,6 +7,7 @@ import com.kkafara.rt.Result; import io.rpg.util.ErrorMessageBuilder; import io.rpg.util.PathUtils; +import io.rpg.util.RuntimeConstantRegistryInstance; import java.nio.file.Files; import java.nio.file.Path; @@ -172,7 +173,7 @@ public Result validateStageOne() { assetDirPath = Path.of(assetDir); } - PathUtils.init(assetDirPath); + RuntimeConstantRegistryInstance.get().setAssetDirPath(assetDirPath); if (playerConfig == null) { builder.append("No player config provided"); @@ -240,6 +241,7 @@ public Result validate() { if (stageOneValidationResult.isErr()) { builder.combine(stageOneValidationResult.getErr().getMessage()); } + if (locationConfigs.size() < 1) { builder.append("No location configs loaded"); } diff --git a/src/main/java/io/rpg/util/PathUtils.java b/src/main/java/io/rpg/util/PathUtils.java index 4629d6e7..ec5c17f7 100644 --- a/src/main/java/io/rpg/util/PathUtils.java +++ b/src/main/java/io/rpg/util/PathUtils.java @@ -5,7 +5,6 @@ import java.util.Optional; public class PathUtils { - private static Path root; /** * Resolves path to the asset. @@ -27,7 +26,7 @@ public static Optional resolvePathToAsset(String pathStr) { return Optional.of(assetPath.toString()); } - assetPath = root.resolve(pathStr); + assetPath = RuntimeConstantRegistryInstance.get().getAssetDirPath().resolve(pathStr); if (Files.isRegularFile(assetPath)) { return Optional.of(assetPath.toString()); @@ -42,10 +41,4 @@ public static String resolvePathToJFXFormat(String path) { } return path; } - - public static void init(Path rootPath) { - if (root == null) { - root = rootPath; - } - } } diff --git a/src/main/java/io/rpg/util/RuntimeConstantRegistry.java b/src/main/java/io/rpg/util/RuntimeConstantRegistry.java new file mode 100644 index 00000000..674ad10c --- /dev/null +++ b/src/main/java/io/rpg/util/RuntimeConstantRegistry.java @@ -0,0 +1,29 @@ +package io.rpg.util; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.nio.file.Path; + +public final class RuntimeConstantRegistry { + private final Logger logger = LogManager.getLogger(RuntimeConstantRegistry.class); + + private Path assetDirPath = null; + + RuntimeConstantRegistry() {} + + @NotNull + public Path getAssetDirPath() { + if (assetDirPath != null) return assetDirPath; + throw new IllegalStateException("Attempt to get asset dir before it was set"); + } + + public void setAssetDirPath(Path assetDirPath) { + if (this.assetDirPath != null) { + logger.warn("Overwriting asset directory path in constant registry. Old value: " + this.assetDirPath + + ". New value: " + assetDirPath); + } + this.assetDirPath = assetDirPath; + } +} diff --git a/src/main/java/io/rpg/util/RuntimeConstantRegistryInstance.java b/src/main/java/io/rpg/util/RuntimeConstantRegistryInstance.java new file mode 100644 index 00000000..65ef2746 --- /dev/null +++ b/src/main/java/io/rpg/util/RuntimeConstantRegistryInstance.java @@ -0,0 +1,14 @@ +package io.rpg.util; + +public final class RuntimeConstantRegistryInstance { + + private static class StaticInitializer { + private static final RuntimeConstantRegistry INSTANCE = new RuntimeConstantRegistry(); + } + + private RuntimeConstantRegistryInstance() {} + + public static RuntimeConstantRegistry get() { + return StaticInitializer.INSTANCE; + } +} diff --git a/src/main/java/io/rpg/wrapper/WrapperController.java b/src/main/java/io/rpg/wrapper/WrapperController.java index 99844489..41e12c5b 100644 --- a/src/main/java/io/rpg/wrapper/WrapperController.java +++ b/src/main/java/io/rpg/wrapper/WrapperController.java @@ -5,7 +5,11 @@ import io.rpg.Initializer; import io.rpg.Main; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Optional; + +import io.rpg.config.ConfigConstants; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -14,6 +18,7 @@ import javafx.scene.control.TextArea; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; +import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -102,10 +107,20 @@ private void onLoadClick() { String path = pathOptional.get(); printLine("Selected: " + path); - startButton.setDisable(false); - loadGame(path) - .ifOk(g -> g.setOnEnd(() -> show(stage))); + // simple check if the user did not pick just a random file + Path _path = Path.of(path); + if (!Files.isReadable(_path) || !Files.isDirectory(_path)) { + printLine(_path.toString() + " is not readable or is not a directory"); + } else { + startButton.setDisable(false); + loadGame(path).ifOk( + g -> { + game = g; + game.setOnEnd(() -> show(stage)); + } + ); + } } private Result loadGame(String path) { @@ -128,18 +143,17 @@ private Result loadGame(String path) { } else if (initializationResult.isOkValueNull()) { printLine("Initialization returned null value"); return Result.err(); - } else { - game = initializationResult.getOk(); - printLine("File was correctly loaded. Press START to begin game"); } - return Result.ok(game); + return Result.ok(initializationResult.getOk()); } @FXML private void onStartClick() { - startButton.setDisable(true); - game.start(stage); + if (game != null) { + startButton.setDisable(true); + game.start(stage); + } } private void printLine(String line) {