Use class for reading extension.yml

This commit is contained in:
Konicai 2022-08-03 00:20:27 -04:00
parent a5dc70a3b5
commit aa7d0f4a57
No known key found for this signature in database
GPG key ID: 710D09287708C823

View file

@ -25,14 +25,18 @@
package org.geysermc.geyser.extension; package org.geysermc.geyser.extension;
import lombok.Getter;
import lombok.Setter;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.extension.ExtensionDescription; import org.geysermc.geyser.api.extension.ExtensionDescription;
import org.geysermc.geyser.api.extension.exception.InvalidDescriptionException; import org.geysermc.geyser.api.extension.exception.InvalidDescriptionException;
import org.geysermc.geyser.text.GeyserLocale; import org.geysermc.geyser.text.GeyserLocale;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
import java.io.Reader; import java.io.Reader;
import java.util.*; import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public record GeyserExtensionDescription(@NonNull String name, public record GeyserExtensionDescription(@NonNull String name,
@ -43,28 +47,27 @@ public record GeyserExtensionDescription(@NonNull String name,
@NonNull String version, @NonNull String version,
@NonNull List<String> authors) implements ExtensionDescription { @NonNull List<String> authors) implements ExtensionDescription {
private static final Yaml YAML = new Yaml(); private static final Yaml YAML = new Yaml(new CustomClassLoaderConstructor(Source.class.getClassLoader()));
public static final Pattern NAME_PATTERN = Pattern.compile("^[A-Za-z_.-]*$"); public static final Pattern NAME_PATTERN = Pattern.compile("^[A-Za-z_.-]*$");
public static final Pattern API_VERSION_PATTERN = Pattern.compile("^\\d+\\.\\d+\\.\\d+$"); public static final Pattern API_VERSION_PATTERN = Pattern.compile("^\\d+\\.\\d+\\.\\d+$");
@NonNull @NonNull
@SuppressWarnings("unchecked")
public static GeyserExtensionDescription fromYaml(Reader reader) throws InvalidDescriptionException { public static GeyserExtensionDescription fromYaml(Reader reader) throws InvalidDescriptionException {
Map<String, Object> map; Source source;
try { try {
map = YAML.loadAs(reader, HashMap.class); source = YAML.loadAs(reader, Source.class);
} catch (Exception e) { } catch (Exception e) {
throw new InvalidDescriptionException(e); throw new InvalidDescriptionException(e);
} }
String name = require(map, "name"); String name = require(source::getName, "name");
if (!NAME_PATTERN.matcher(name).matches()) { if (!NAME_PATTERN.matcher(name).matches()) {
throw new InvalidDescriptionException("Invalid extension name, must match: " + NAME_PATTERN.pattern()); throw new InvalidDescriptionException("Invalid extension name, must match: " + NAME_PATTERN.pattern());
} }
String version = String.valueOf(map.get("version")); String version = String.valueOf(source.version);
String main = require(map, "main"); String main = require(source::getMain, "main");
String apiVersion = require(map, "api"); String apiVersion = require(source::getApi, "api");
if (!API_VERSION_PATTERN.matcher(apiVersion).matches()) { if (!API_VERSION_PATTERN.matcher(apiVersion).matches()) {
throw new InvalidDescriptionException(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_api_format", name, apiVersion)); throw new InvalidDescriptionException(GeyserLocale.getLocaleStringLog("geyser.extensions.load.failed_api_format", name, apiVersion));
} }
@ -74,26 +77,33 @@ public record GeyserExtensionDescription(@NonNull String name,
int patchApi = Integer.parseUnsignedInt(api[2]); int patchApi = Integer.parseUnsignedInt(api[2]);
List<String> authors = new ArrayList<>(); List<String> authors = new ArrayList<>();
if (map.containsKey("author")) { if (source.author != null) {
authors.add(String.valueOf(map.get("author"))); authors.add(source.author);
} }
if (map.containsKey("authors")) { if (source.authors != null) {
try { authors.addAll(source.authors);
authors.addAll((Collection<? extends String>) map.get("authors"));
} catch (Exception e) {
throw new InvalidDescriptionException("Invalid authors format, should be a list of strings", e);
}
} }
return new GeyserExtensionDescription(name, main, majorApi, minorApi, patchApi, version, authors); return new GeyserExtensionDescription(name, main, majorApi, minorApi, patchApi, version, authors);
} }
@NonNull @NonNull
private static String require(Map<String, Object> desc, String key) throws InvalidDescriptionException { private static String require(Supplier<String> supplier, String name) throws InvalidDescriptionException {
Object value = desc.get(key); String value = supplier.get();
if (value instanceof String) { if (value == null) {
return (String) value; throw new InvalidDescriptionException("Extension description is missing string property '" + name + "'");
} }
throw new InvalidDescriptionException("Extension description is missing string property '" + key + "'"); return value;
}
@Getter
@Setter
public static class Source {
String name;
String main;
String api;
String version;
String author;
List<String> authors;
} }
} }