From 4730875dc82b03051377b781c8c5412ce2635087 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Thu, 9 Jun 2022 22:55:26 +0200 Subject: [PATCH 1/5] chore: create RuntimeConstantRegistry --- .../io/rpg/config/model/GameWorldConfig.java | 4 ++- src/main/java/io/rpg/util/PathUtils.java | 9 +----- .../io/rpg/util/RuntimeConstantRegistry.java | 30 +++++++++++++++++++ .../util/RuntimeConstantRegistryInstance.java | 14 +++++++++ 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/rpg/util/RuntimeConstantRegistry.java create mode 100644 src/main/java/io/rpg/util/RuntimeConstantRegistryInstance.java diff --git a/src/main/java/io/rpg/config/model/GameWorldConfig.java b/src/main/java/io/rpg/config/model/GameWorldConfig.java index 7caa856..91c943f 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 4629d6e..ec5c17f 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 0000000..c657e03 --- /dev/null +++ b/src/main/java/io/rpg/util/RuntimeConstantRegistry.java @@ -0,0 +1,30 @@ +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; +import java.util.Optional; + +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 0000000..65ef274 --- /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; + } +} From 95c496b4c1ea0f9d9905fa572240c2447c876ff2 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Thu, 9 Jun 2022 22:56:30 +0200 Subject: [PATCH 2/5] refact: remove unused imports --- src/main/java/io/rpg/util/RuntimeConstantRegistry.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/io/rpg/util/RuntimeConstantRegistry.java b/src/main/java/io/rpg/util/RuntimeConstantRegistry.java index c657e03..674ad10 100644 --- a/src/main/java/io/rpg/util/RuntimeConstantRegistry.java +++ b/src/main/java/io/rpg/util/RuntimeConstantRegistry.java @@ -5,7 +5,6 @@ import org.jetbrains.annotations.NotNull; import java.nio.file.Path; -import java.util.Optional; public final class RuntimeConstantRegistry { private final Logger logger = LogManager.getLogger(RuntimeConstantRegistry.class); From 58ca9b7c588b433340ba7d13a4c7ee21cfc07d42 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Thu, 9 Jun 2022 23:15:06 +0200 Subject: [PATCH 3/5] chore: add check for invalid config file This can be easily passed by, but the most basic case is covered --- .../java/io/rpg/wrapper/WrapperController.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/rpg/wrapper/WrapperController.java b/src/main/java/io/rpg/wrapper/WrapperController.java index 9984448..5354782 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; @@ -104,8 +109,15 @@ private void onLoadClick() { 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)) { + printLine("Selected path does not point to a readable file. Try again."); + } else if (!_path.getFileName().toString().equals(ConfigConstants.ROOT)) { + printLine("Selected path does not point to: " + ConfigConstants.ROOT + " file."); + } else { + loadGame(path).ifOk(g -> g.setOnEnd(() -> show(stage))); + } } private Result loadGame(String path) { @@ -130,7 +142,6 @@ private Result loadGame(String path) { return Result.err(); } else { game = initializationResult.getOk(); - printLine("File was correctly loaded. Press START to begin game"); } return Result.ok(game); From bbcefce95385bb4de4b1e91c3b3923bc98f9c50f Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Thu, 9 Jun 2022 23:31:08 +0200 Subject: [PATCH 4/5] fix: validateForDialogueAction --- src/main/java/io/rpg/config/model/ActionConfigBundle.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/io/rpg/config/model/ActionConfigBundle.java b/src/main/java/io/rpg/config/model/ActionConfigBundle.java index 0ddde31..27750fc 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))) { From 485561a3a16bc1cc3b81984b4c222706f10b2b3b Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Thu, 9 Jun 2022 23:31:25 +0200 Subject: [PATCH 5/5] fix: handle bad path in WrapperController --- .../io/rpg/wrapper/WrapperController.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/rpg/wrapper/WrapperController.java b/src/main/java/io/rpg/wrapper/WrapperController.java index 5354782..41e12c5 100644 --- a/src/main/java/io/rpg/wrapper/WrapperController.java +++ b/src/main/java/io/rpg/wrapper/WrapperController.java @@ -107,16 +107,19 @@ private void onLoadClick() { String path = pathOptional.get(); printLine("Selected: " + path); - startButton.setDisable(false); // simple check if the user did not pick just a random file Path _path = Path.of(path); - if (!Files.isReadable(_path)) { - printLine("Selected path does not point to a readable file. Try again."); - } else if (!_path.getFileName().toString().equals(ConfigConstants.ROOT)) { - printLine("Selected path does not point to: " + ConfigConstants.ROOT + " file."); + if (!Files.isReadable(_path) || !Files.isDirectory(_path)) { + printLine(_path.toString() + " is not readable or is not a directory"); } else { - loadGame(path).ifOk(g -> g.setOnEnd(() -> show(stage))); + startButton.setDisable(false); + loadGame(path).ifOk( + g -> { + game = g; + game.setOnEnd(() -> show(stage)); + } + ); } } @@ -140,17 +143,17 @@ private Result loadGame(String path) { } else if (initializationResult.isOkValueNull()) { printLine("Initialization returned null value"); return Result.err(); - } else { - game = initializationResult.getOk(); } - 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) {