Simplified config!

This commit is contained in:
Andrew Lalis 2022-07-28 01:25:14 +02:00
parent 1bb4b08df8
commit 7df8120c16
5 changed files with 33 additions and 23 deletions

View File

@ -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). 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. 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: 3. Run the game by double-clicking the JAR file, or entering `java -jar <jarfile>` in a terminal. This should generate a `config.yaml` file.
```yaml
serverHost: localhost
serverPort: 25565
username: myUsername
input:
mouseSensitivity: 0.005
display:
fullscreen: true
captureCursor: true
fov: 80
```
4. Set the `serverHost`, `serverPort`, and `username` properties accordingly for the server you want to join. 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
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. 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.

View File

@ -7,6 +7,7 @@ import nl.andrewl.aos2_client.model.ClientPlayer;
import nl.andrewl.aos2_client.model.OtherPlayer; import nl.andrewl.aos2_client.model.OtherPlayer;
import nl.andrewl.aos2_client.render.GameRenderer; import nl.andrewl.aos2_client.render.GameRenderer;
import nl.andrewl.aos2_client.sound.SoundManager; 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.config.Config;
import nl.andrewl.aos_core.model.Player; import nl.andrewl.aos_core.model.Player;
import nl.andrewl.aos_core.model.Projectile; import nl.andrewl.aos_core.model.Projectile;
@ -20,7 +21,6 @@ import org.joml.Vector3f;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.awt.image.RenderedImage;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
@ -271,7 +271,7 @@ public class Client implements Runnable {
if (args.length > 0) { if (args.length > 0) {
configPaths.add(Path.of(args[0].trim())); 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 client = new Client(clientConfig);
client.run(); client.run();
} }

View File

@ -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

View File

@ -19,10 +19,11 @@ public final class Config {
* @param paths The paths to load from. * @param paths The paths to load from.
* @param fallback A default configuration object to use if no config could * @param fallback A default configuration object to use if no config could
* be loaded from any of the paths. * be loaded from any of the paths.
* @param defaultConfigFile The default config file to save.
* @return The configuration object. * @return The configuration object.
* @param <T> The type of the configuration object. * @param <T> The type of the configuration object.
*/ */
public static <T> T loadConfig(Class<T> configType, List<Path> paths, T fallback) { public static <T> T loadConfig(Class<T> configType, List<Path> paths, T fallback, String defaultConfigFile) {
for (var path : paths) { for (var path : paths) {
if (Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path)) { if (Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path)) {
try (var reader = Files.newBufferedReader(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; return fallback;
} }
public static <T> T loadConfig(Class<T> configType, List<Path> paths) { public static <T> T loadConfig(Class<T> configType, List<Path> paths, String defaultConfigFile) {
var cfg = loadConfig(configType, paths, null); var cfg = loadConfig(configType, paths, null, defaultConfigFile);
if (cfg == null) { if (cfg == null) {
throw new RuntimeException("Could not load config from any of the supplied paths."); throw new RuntimeException("Could not load config from any of the supplied paths.");
} }
return cfg; return cfg;
} }
public static <T> T loadConfig(Class<T> configType, T fallback, Path... paths) { public static <T> T loadConfig(Class<T> configType, T fallback, String defaultConfigFile, Path... paths) {
return loadConfig(configType, List.of(paths), fallback); return loadConfig(configType, List.of(paths), fallback, defaultConfigFile);
} }
public static List<Path> getCommonConfigPaths() { public static List<Path> getCommonConfigPaths() {
List<Path> paths = new ArrayList<>(); List<Path> 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.yaml"));
paths.add(Path.of("config.yml")); 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.yaml"));
paths.add(Path.of("cfg.yml")); paths.add(Path.of("cfg.yml"));
return paths; return paths;

View File

@ -180,7 +180,7 @@ public class Server implements Runnable {
if (args.length > 0) { if (args.length > 0) {
configPaths.add(Path.of(args[0].trim())); 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); Server server = new Server(cfg);
new Thread(server).start(); new Thread(server).start();
ServerCli.start(server); ServerCli.start(server);