Improve YMLWrapper

This commit is contained in:
2025-10-26 09:14:34 +01:00
parent 4405d9c25d
commit aec03e41a1
14 changed files with 163 additions and 314 deletions
+2
View File
@@ -23,4 +23,6 @@ plugins {
dependencies {
api(project(":CommonCore:SQL"))
implementation("org.yaml:snakeyaml:2.2")
}
@@ -28,6 +28,7 @@ import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@ToString
@@ -94,8 +95,10 @@ public final class GameModeConfig<M, ST, W> {
public final List<Integer> EnterStages;
public final Techhider<M> Techhider;
public GameModeConfig(YMLWrapper<M, ST, W> loader) {
configFile = loader.getFile();
public GameModeConfig(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper, Function<File, String> defaultGameName) {
YMLWrapper<M, ST, W> loader = new YMLWrapper<>(file, materialMapper, schematicTypeMapper, winconditionMapper);
configFile = file;
loaded = loader.canLoad();
Server = new Server(loader.with("Server"));
@@ -116,7 +119,7 @@ public final class GameModeConfig<M, ST, W> {
Times = new Times(loader.with("Times"));
// Arena would be here to be in config order but needs Schematic.Size and EnterStages loaded afterwards
Schematic = new Schematic<>(loader.with("Schematic"));
GameName = loader.getString("GameName", loader.getDefaultGameName());
GameName = loader.getString("GameName", defaultGameName.apply(file));
ActiveMonths = loader.getIntList("ActiveMonths");
TeamChatPrefix = loader.getString("TeamChatPrefix", "+");
Blue = new Blue(loader.with("Blue"));
@@ -20,8 +20,11 @@
package de.steamwar.data;
import de.steamwar.sql.SchematicType;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -29,34 +32,75 @@ import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class YMLWrapper<M, ST, W> {
public final class YMLWrapper<M, ST, W> {
public static final Function<String, String> ToString = Function.identity();
public static final Function<String, SchematicType> ToSchematicType = SchematicType::fromDB;
protected final File file;
public final Function<String, M> materialMapper;
public final Function<String, ST> schematicTypeMapper;
public final Function<String, W> winconditionMapper;
public static final Function<File, String> ToStaticWarGear = __ -> "WarGear";
public static final Function<File, String> ToInternalName = file -> file.getName().replace(".yml", "");
protected YMLWrapper(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
private final File file;
final Function<String, M> materialMapper;
final Function<String, ST> schematicTypeMapper;
final Function<String, W> winconditionMapper;
private final boolean canLoad;
private final Map<String, Object> document;
public YMLWrapper(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
this.file = file;
this.materialMapper = materialMapper;
this.schematicTypeMapper = schematicTypeMapper;
this.winconditionMapper = winconditionMapper;
Yaml yaml = new Yaml();
Map<String, Object> document = Collections.emptyMap();
boolean canLoad = false;
if (file != null && file.exists() && file.isFile()) {
try {
document = yaml.load(new FileReader(file));
canLoad = true;
} catch (IOException e) {
// Ignore
}
}
this.document = document;
this.canLoad = canLoad;
}
public final File getFile() {
return file;
private YMLWrapper(boolean canLoad, Map<String, Object> document, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
this.file = null;
this.materialMapper = materialMapper;
this.schematicTypeMapper = schematicTypeMapper;
this.winconditionMapper = winconditionMapper;
this.canLoad = canLoad;
this.document = document;
}
public abstract boolean canLoad();
public boolean canLoad() {
return canLoad;
}
public abstract YMLWrapper<M, ST, W> with(String path);
public YMLWrapper<M, ST, W> with(String path) {
if (document.containsKey(path)) {
Object value = document.get(path);
if (value instanceof Map) {
return new YMLWrapper<>(true, (Map) value, materialMapper, schematicTypeMapper, winconditionMapper);
}
}
return new YMLWrapper<>(false, Collections.emptyMap(), materialMapper, schematicTypeMapper, winconditionMapper);
}
public abstract String getDefaultGameName();
public abstract <T> T get(String path, T defaultValue, Function<Object, T> mapper);
public <T> T get(String path, T defaultValue, Function<Object, T> mapper) {
Object value = this.document.get(path);
if (value == null) return defaultValue;
try {
return mapper.apply(value);
} catch (ClassCastException e) {
return defaultValue;
}
}
public String getString(String path, String defaultValue) {
return get(path, defaultValue, Objects::toString);
@@ -74,16 +118,24 @@ public abstract class YMLWrapper<M, ST, W> {
return get(path, defaultValue, Boolean.class::cast);
}
public final ST getSchematicType(String path, String defaultValue) {
public ST getSchematicType(String path, String defaultValue) {
String schematicType = getString(path, defaultValue);
return schematicTypeMapper.apply(schematicType);
}
public final M getMaterial(String path, String defaultValue) {
public M getMaterial(String path, String defaultValue) {
return materialMapper.apply(getString(path, defaultValue).toUpperCase());
}
public abstract <T> List<T> get(String path, Function<Object, List<T>> mapper);
public <T> List<T> get(String path, Function<Object, List<T>> mapper) {
Object value = this.document.get(path);
if (value == null) return Collections.emptyList();
try {
return Collections.unmodifiableList(mapper.apply(value));
} catch (ClassCastException e) {
return Collections.emptyList();
}
}
public List<String> getStringList(String path) {
return get(path, o -> (List<String>) o);
@@ -93,7 +145,7 @@ public abstract class YMLWrapper<M, ST, W> {
return get(path, o -> (List<Integer>) o);
}
public final List<ST> getSchematicTypeList(String path) {
public List<ST> getSchematicTypeList(String path) {
List<String> list = getStringList(path);
if (list.isEmpty()) {
return Collections.emptyList();
@@ -102,7 +154,7 @@ public abstract class YMLWrapper<M, ST, W> {
}
}
public final List<M> getMaterialList(String path) {
public List<M> getMaterialList(String path) {
List<String> list = getStringList(path);
if (list.isEmpty()) {
return Collections.emptyList();
@@ -111,5 +163,12 @@ public abstract class YMLWrapper<M, ST, W> {
}
}
public abstract List<Map<?, ?>> getMapList(String path);
public List<Map<?, ?>> getMapList(String path) {
Object value = this.document.get(path);
if (value instanceof List) {
return Collections.unmodifiableList((List<Map<?, ?>>) value);
} else {
return Collections.emptyList();
}
}
}