Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: cleanup code & fixes #110

Merged
merged 5 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/java/io/rpg/config/model/ActionConfigBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ Result<Void, Exception> validateForQuizAction() {

Result<Void, Exception> 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))) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/rpg/config/model/GameWorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -172,7 +173,7 @@ public Result<Void, Exception> validateStageOne() {
assetDirPath = Path.of(assetDir);
}

PathUtils.init(assetDirPath);
RuntimeConstantRegistryInstance.get().setAssetDirPath(assetDirPath);

if (playerConfig == null) {
builder.append("No player config provided");
Expand Down Expand Up @@ -240,6 +241,7 @@ public Result<Void, Exception> validate() {
if (stageOneValidationResult.isErr()) {
builder.combine(stageOneValidationResult.getErr().getMessage());
}

if (locationConfigs.size() < 1) {
builder.append("No location configs loaded");
}
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/io/rpg/util/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Optional;

public class PathUtils {
private static Path root;

/**
* Resolves path to the asset.
Expand All @@ -27,7 +26,7 @@ public static Optional<String> 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());
Expand All @@ -42,10 +41,4 @@ public static String resolvePathToJFXFormat(String path) {
}
return path;
}

public static void init(Path rootPath) {
if (root == null) {
root = rootPath;
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/rpg/util/RuntimeConstantRegistry.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/io/rpg/util/RuntimeConstantRegistryInstance.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
32 changes: 23 additions & 9 deletions src/main/java/io/rpg/wrapper/WrapperController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Game, Void> loadGame(String path) {
Expand All @@ -128,18 +143,17 @@ private Result<Game, Void> 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) {
Expand Down