From 7df8120c16b0e31e646b0f2b4761373430b3fada Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Thu, 28 Jul 2022 01:25:14 +0200 Subject: [PATCH] Simplified config! --- README.md | 15 ++----------- .../java/nl/andrewl/aos2_client/Client.java | 4 ++-- client/src/main/resources/default-config.yaml | 14 +++++++++++++ .../nl/andrewl/aos_core/config/Config.java | 21 ++++++++++++------- .../java/nl/andrewl/aos2_server/Server.java | 2 +- 5 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 client/src/main/resources/default-config.yaml diff --git a/README.md b/README.md index 71bc744..987b627 100644 --- a/README.md +++ b/README.md @@ -8,20 +8,9 @@ _Read this guide to get started and join a server in just a minute or two!_ 1. Make sure you've got at least Java 17 installed. You can get it [here](https://adoptium.net/temurin/releases). 2. Download the `aos2-client` JAR file from the [releases page](https://github.com/andrewlalis/ace-of-shades-2/releases) that's compatible with your system. -3. Create a file named `config.yaml` in the same directory as the JAR file, and place the following text in it: -```yaml -serverHost: localhost -serverPort: 25565 -username: myUsername -input: - mouseSensitivity: 0.005 -display: - fullscreen: true - captureCursor: true - fov: 80 -``` +3. Run the game by double-clicking the JAR file, or entering `java -jar ` in a terminal. This should generate a `config.yaml` file. 4. Set the `serverHost`, `serverPort`, and `username` properties accordingly for the server you want to join. -5. Run the game by double-clicking the `aos2-client` JAR file, or enter `java -jar aos2-client-{version}.jar` in a terminal. +5. Run the game again to join the server and start playing! ## Setting up a Server Setting up a server is quite easy. Just go to the [releases page](https://github.com/andrewlalis/ace-of-shades-2/releases) and download the latest `aos2-server` JAR file. Similar to the client, it's best if you provide a `config.yaml` file to the server, in the same directory. The following snippet shows the structure and default values of a server's configuration. diff --git a/client/src/main/java/nl/andrewl/aos2_client/Client.java b/client/src/main/java/nl/andrewl/aos2_client/Client.java index 7b85a67..91848e3 100644 --- a/client/src/main/java/nl/andrewl/aos2_client/Client.java +++ b/client/src/main/java/nl/andrewl/aos2_client/Client.java @@ -7,6 +7,7 @@ import nl.andrewl.aos2_client.model.ClientPlayer; import nl.andrewl.aos2_client.model.OtherPlayer; import nl.andrewl.aos2_client.render.GameRenderer; import nl.andrewl.aos2_client.sound.SoundManager; +import nl.andrewl.aos_core.FileUtils; import nl.andrewl.aos_core.config.Config; import nl.andrewl.aos_core.model.Player; import nl.andrewl.aos_core.model.Projectile; @@ -20,7 +21,6 @@ import org.joml.Vector3f; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.awt.image.RenderedImage; import java.io.IOException; import java.nio.file.Path; import java.util.List; @@ -271,7 +271,7 @@ public class Client implements Runnable { if (args.length > 0) { configPaths.add(Path.of(args[0].trim())); } - ClientConfig clientConfig = Config.loadConfig(ClientConfig.class, configPaths, new ClientConfig()); + ClientConfig clientConfig = Config.loadConfig(ClientConfig.class, configPaths, new ClientConfig(), FileUtils.readClasspathFile("default-config.yaml")); Client client = new Client(clientConfig); client.run(); } diff --git a/client/src/main/resources/default-config.yaml b/client/src/main/resources/default-config.yaml new file mode 100644 index 0000000..27086af --- /dev/null +++ b/client/src/main/resources/default-config.yaml @@ -0,0 +1,14 @@ +# Ace of Shades 2 Client Configuration +# Set these properties to connect to a server. +serverHost: localhost +serverPort: 25565 +username: player + +# Settings for input. +input: + mouseSensitivity: 0.005 +# Settings for display. +display: + fullscreen: true + captureCursor: true + fov: 80 \ No newline at end of file diff --git a/core/src/main/java/nl/andrewl/aos_core/config/Config.java b/core/src/main/java/nl/andrewl/aos_core/config/Config.java index 289c272..cf66df9 100644 --- a/core/src/main/java/nl/andrewl/aos_core/config/Config.java +++ b/core/src/main/java/nl/andrewl/aos_core/config/Config.java @@ -19,10 +19,11 @@ public final class Config { * @param paths The paths to load from. * @param fallback A default configuration object to use if no config could * be loaded from any of the paths. + * @param defaultConfigFile The default config file to save. * @return The configuration object. * @param The type of the configuration object. */ - public static T loadConfig(Class configType, List paths, T fallback) { + public static T loadConfig(Class configType, List paths, T fallback, String defaultConfigFile) { for (var path : paths) { if (Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path)) { try (var reader = Files.newBufferedReader(path)) { @@ -32,27 +33,33 @@ public final class Config { } } } + Path outputPath = paths.size() > 0 ? paths.get(0) : Path.of("config.yaml"); + try (var writer = Files.newBufferedWriter(outputPath)) { + writer.write(defaultConfigFile); + } catch (IOException e) { + e.printStackTrace(); + } return fallback; } - public static T loadConfig(Class configType, List paths) { - var cfg = loadConfig(configType, paths, null); + public static T loadConfig(Class configType, List paths, String defaultConfigFile) { + var cfg = loadConfig(configType, paths, null, defaultConfigFile); if (cfg == null) { throw new RuntimeException("Could not load config from any of the supplied paths."); } return cfg; } - public static T loadConfig(Class configType, T fallback, Path... paths) { - return loadConfig(configType, List.of(paths), fallback); + public static T loadConfig(Class configType, T fallback, String defaultConfigFile, Path... paths) { + return loadConfig(configType, List.of(paths), fallback, defaultConfigFile); } public static List getCommonConfigPaths() { List paths = new ArrayList<>(); - paths.add(Path.of("configuration.yaml")); - paths.add(Path.of("configuration.yml")); paths.add(Path.of("config.yaml")); paths.add(Path.of("config.yml")); + paths.add(Path.of("configuration.yaml")); + paths.add(Path.of("configuration.yml")); paths.add(Path.of("cfg.yaml")); paths.add(Path.of("cfg.yml")); return paths; diff --git a/server/src/main/java/nl/andrewl/aos2_server/Server.java b/server/src/main/java/nl/andrewl/aos2_server/Server.java index 85edc41..b930351 100644 --- a/server/src/main/java/nl/andrewl/aos2_server/Server.java +++ b/server/src/main/java/nl/andrewl/aos2_server/Server.java @@ -180,7 +180,7 @@ public class Server implements Runnable { if (args.length > 0) { configPaths.add(Path.of(args[0].trim())); } - ServerConfig cfg = Config.loadConfig(ServerConfig.class, configPaths, new ServerConfig()); + ServerConfig cfg = Config.loadConfig(ServerConfig.class, configPaths, new ServerConfig(), null); Server server = new Server(cfg); new Thread(server).start(); ServerCli.start(server);