mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Let modules declare pack resource kinds and validators: ResourceKind, PackResources.ids, @ResourceRef, the validation API and folders the host reserves
This commit is contained in:
1 parent
09afee8ae3
commit
ef4b640867
37 files changed
+990
-128
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package com.volmit.validation;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** Something wrong with a pack that a {@link Validator} found: how much it matters, what it concerns and what it is.
|
||||
* The message is a sentence for the pack's author; the host shows it with its subject.
|
||||
*/
|
||||
public record Problem(Severity severity, Subject subject, String message) {
|
||||
public Problem {
|
||||
Objects.requireNonNull(severity, "severity");
|
||||
Objects.requireNonNull(subject, "subject");
|
||||
Objects.requireNonNull(message, "message");
|
||||
if (message.isBlank()) throw new IllegalArgumentException("Blank problem message about " + subject);
|
||||
}
|
||||
|
||||
/** A problem that stops the pack from loading. */
|
||||
public static Problem error(Subject subject, String message) {
|
||||
return new Problem(Severity.ERROR, subject, message);
|
||||
}
|
||||
|
||||
/** A problem the host logs while the pack still loads. */
|
||||
public static Problem warning(Subject subject, String message) {
|
||||
return new Problem(Severity.WARNING, subject, message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.volmit.validation;
|
||||
|
||||
/** How much a {@link Problem} matters to the pack. */
|
||||
public enum Severity {
|
||||
/** The pack cannot be used as it is: the host refuses to load it. */
|
||||
ERROR,
|
||||
/** The pack works, but probably not as its author meant: the host logs it and loads the pack. */
|
||||
WARNING
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.volmit.validation;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** What a {@link Problem} concerns: a resource of the pack, by id, or a file of the pack, by its path. */
|
||||
public sealed interface Subject {
|
||||
/** The resource {@code id}, such as {@code overworld:castle}. */
|
||||
static Subject resource(ResourceId id) {
|
||||
return new Resource(id);
|
||||
}
|
||||
|
||||
/** The file at {@code path} below the pack root, with {@code /} between folders, such as {@code structures/castle.json}. */
|
||||
static Subject file(String path) {
|
||||
return new PackFile(path);
|
||||
}
|
||||
|
||||
/** A resource of the pack. One id can name resources of several kinds, so a file is clearer where the kind matters. */
|
||||
record Resource(ResourceId id) implements Subject {
|
||||
public Resource {
|
||||
Objects.requireNonNull(id, "id");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/** A file below the pack root. */
|
||||
record PackFile(String path) implements Subject {
|
||||
private static final Pattern SEGMENTS = Pattern.compile("[^/\\\\]+(/[^/\\\\]+)*");
|
||||
private static final Pattern RELATIVE_SEGMENT = Pattern.compile("(^|/)\\.\\.?(/|$)");
|
||||
|
||||
public PackFile {
|
||||
Objects.requireNonNull(path, "path");
|
||||
if (!SEGMENTS.matcher(path).matches() || RELATIVE_SEGMENT.matcher(path).find()) {
|
||||
throw new IllegalArgumentException("File '" + path + "' is not a path below the pack root;"
|
||||
+ " write it relative to the pack with '/' between folders, such as structures/castle.json");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.volmit.validation;
|
||||
|
||||
import com.volmit.config.PackSettings;
|
||||
import com.volmit.resource.PackResources;
|
||||
|
||||
/** The pack one {@link Validator} checks, as the host would load it, and where it reports what it finds. Valid
|
||||
* only while {@link Validator#validate} runs.
|
||||
*/
|
||||
public interface ValidationContext {
|
||||
/** The pack's resources: the ones the host serves (objects, biomes, regions) and every module resource kind.
|
||||
* {@link PackResources#ids} lists a kind's resources.
|
||||
*/
|
||||
PackResources resources();
|
||||
|
||||
/** The module settings the pack declares: every entry of every dimension, region and biome of the pack,
|
||||
* whether or not a dimension reaches it.
|
||||
*/
|
||||
PackSettings settings();
|
||||
|
||||
/** Records {@code problem}. An {@link Severity#ERROR} stops the pack from loading once every validator has run. */
|
||||
void report(Problem problem);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.volmit.validation;
|
||||
|
||||
/** Checks a pack for problems its module cares about, such as a structure that can never assemble. Modules
|
||||
* register validators with {@link com.volmit.FeatureRegistry#registerValidator}; the host runs every validator of
|
||||
* the pack wherever it validates packs (when a pack loads, when studio opens it, on request) and reports all
|
||||
* problems together. A validator reports what it finds and returns; it must not change the pack. One that throws
|
||||
* fails the validation of the pack, naming its module.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Validator {
|
||||
void validate(ValidationContext pack);
|
||||
}
|
||||
Reference in new issue
Block a user