From 2aab5ae9a33e540526c4db6d5f2aea465b4c709b Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 8 Oct 2026 08:44:18 +0200 Subject: [PATCH] Show pack modules' settings in studio and reload them when their jars change - SchemaBuilder renders the settings the pack's modules add: each feature at a dimension, region, biome or object placement becomes a property under its id and, for built-ins, its alias, with a constraint that an object holds only one of the two. Each settings class is one definition; options carry their kind, bounds, defaults and descriptions, and references complete from the same lists Iris's own fields use. - The code workspace writes a schema for each module snippet type under snippet//. - A pack whose modules fail to load gets no workspace; the error is logged instead of writing schemas without the modules. - PackModuleSet forgets loaded or failed modules once the jars in /modules change, and the workspace then reloads the pack data, so studio picks up rebuilt module jars. The studio hotload watcher now also reacts to .jar files. - Placement tests name Iris's built-in states by their own keys, since those resolve once per JVM with whichever platform a test bound first. --- .../service/ModdedStudioHotloadService.java | 2 +- .../iris/core/modules/PackModuleSet.java | 46 +++- .../iris/core/project/IrisCodeWorkspace.java | 76 +++++- .../core/project/ModuleSchemaRenderer.java | 216 ++++++++++++++++++ .../iris/core/project/SchemaBuilder.java | 90 ++++++-- .../engine/platform/BukkitChunkGenerator.java | 2 +- .../core/project/IrisCodeWorkspaceTest.java | 18 ++ .../core/project/SchemaBuilderParityTest.java | 70 ++++++ .../IrisObjectPlacementRegressionTest.java | 17 +- 9 files changed, 496 insertions(+), 41 deletions(-) create mode 100644 core/src/main/java/art/arcane/iris/core/project/ModuleSchemaRenderer.java diff --git a/adapters/modded-common/src/main/java/art/arcane/iris/modded/service/ModdedStudioHotloadService.java b/adapters/modded-common/src/main/java/art/arcane/iris/modded/service/ModdedStudioHotloadService.java index 4edeee53d..9d077b40e 100644 --- a/adapters/modded-common/src/main/java/art/arcane/iris/modded/service/ModdedStudioHotloadService.java +++ b/adapters/modded-common/src/main/java/art/arcane/iris/modded/service/ModdedStudioHotloadService.java @@ -264,7 +264,7 @@ public final class ModdedStudioHotloadService implements ModdedTickableService, watch.folder = new ReactiveFolder( engine.getData().getDataFolder(), (KList created, KList changed, KList deleted) -> hotload(dimensionId, generator, engine), - new KList<>(".iob", ".json"), + new KList<>(".iob", ".json", ".jar"), new KList<>(".iris"), new KList<>()); return; diff --git a/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java b/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java index 37b2faff6..312486fb6 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java +++ b/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java @@ -22,20 +22,28 @@ import art.arcane.iris.spi.IrisLogging; import com.volmit.host.LoadedPack; import java.io.File; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; /** * The modules of one pack folder, loaded with {@link IrisModuleHost} the first time something needs * them and closed with the pack data that owns them. A failed load is reported once and remembered, so - * every later use fails with the same error instead of loading the jars again. + * every later use fails with the same error instead of loading the jars again. Studio can load them + * again after the jars in {@code /modules} change. */ public final class PackModuleSet implements AutoCloseable { private enum State { NOT_LOADED, LOADED, FAILED, CLOSED } + private static final String MODULES_FOLDER = "modules"; + private static final String JAR_EXTENSION = ".jar"; + private final File packFolder; private State state = State.NOT_LOADED; private LoadedPack pack; private ModuleSettingsDecoder settings; private PackModulesException failure; + private List loadedJars = List.of(); public PackModuleSet(File packFolder) { this.packFolder = packFolder; @@ -52,14 +60,35 @@ public final class PackModuleSet implements AutoCloseable { return settings; } + /** + * Forgets the loaded modules, or the failure to load them, when the pack's module jars changed since, + * so the next use loads the jars as they are now. Only for studio: anything built from the old modules, + * such as resources read with their settings, must be reloaded too. + * + * @return whether the jars changed + */ + public synchronized boolean reloadIfJarsChanged() { + if (state == State.NOT_LOADED || state == State.CLOSED || jarFingerprint().equals(loadedJars)) { + return false; + } + release(State.NOT_LOADED); + return true; + } + @Override public synchronized void close() { + release(State.CLOSED); + } + + private void release(State next) { if (state == State.LOADED) { pack.close(); } pack = null; settings = null; - state = State.CLOSED; + failure = null; + loadedJars = List.of(); + state = next; } private void requireLoaded() { @@ -73,6 +102,7 @@ public final class PackModuleSet implements AutoCloseable { } private void load() { + loadedJars = jarFingerprint(); try { LoadedPack loaded = LoadedPack.load(packFolder.toPath(), IrisModuleHost.get()); pack = loaded; @@ -85,4 +115,16 @@ public final class PackModuleSet implements AutoCloseable { throw failure; } } + + /** Each module jar's name, size and modification time, in name order. */ + private List jarFingerprint() { + File[] jars = new File(packFolder, MODULES_FOLDER).listFiles(file -> file.isFile() && file.getName().endsWith(JAR_EXTENSION)); + if (jars == null) { + return List.of(); + } + return Arrays.stream(jars) + .sorted(Comparator.comparing(File::getName)) + .map(jar -> jar.getName() + ":" + jar.length() + ":" + jar.lastModified()) + .toList(); + } } diff --git a/core/src/main/java/art/arcane/iris/core/project/IrisCodeWorkspace.java b/core/src/main/java/art/arcane/iris/core/project/IrisCodeWorkspace.java index debbcc9cd..0a28268c0 100644 --- a/core/src/main/java/art/arcane/iris/core/project/IrisCodeWorkspace.java +++ b/core/src/main/java/art/arcane/iris/core/project/IrisCodeWorkspace.java @@ -21,6 +21,7 @@ package art.arcane.iris.core.project; import art.arcane.iris.core.IrisSettings; import art.arcane.iris.core.loader.IrisData; import art.arcane.iris.core.loader.ResourceLoader; +import art.arcane.iris.core.modules.PackModulesException; import art.arcane.iris.engine.object.annotations.Snippet; import art.arcane.iris.spi.IrisLogging; import art.arcane.iris.util.common.plugin.VolmitSender; @@ -31,6 +32,7 @@ import art.arcane.volmlib.util.format.Form; import art.arcane.volmlib.util.io.IO; import art.arcane.volmlib.util.json.JSONArray; import art.arcane.volmlib.util.json.JSONObject; +import com.volmit.host.schema.ObjectType; import org.dom4j.Document; import org.dom4j.Element; @@ -38,6 +40,7 @@ import java.awt.Desktop; import java.awt.GraphicsEnvironment; import java.io.File; import java.io.IOException; +import java.io.UncheckedIOException; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -46,6 +49,10 @@ import java.util.UUID; @SuppressWarnings("ALL") public class IrisCodeWorkspace { + private static final String SNIPPET_SCHEMA_FOLDER = ".iris/schema/snippet/"; + private static final String SCHEMA_SUFFIX = "-schema.json"; + private static final int SNIPPET_MATCH_DEPTH = 8; + private final IrisProject project; public IrisCodeWorkspace(IrisProject project) { @@ -95,6 +102,9 @@ public class IrisCodeWorkspace { private boolean updateWorkspace(boolean immediateSchemas) { project.getPath().mkdirs(); File ws = getCodeWorkspaceFile(); + if (!loadsPackModules(IrisData.get(project.getPath()))) { + return false; + } // Render before touching the file: a config-generation failure has nothing to do with // the existing workspace file, and deleting it before a rethrowing rebuild silently @@ -125,6 +135,23 @@ public class IrisCodeWorkspace { return false; } + /** + * Schemas describe the pack's modules or nothing, so a pack whose modules fail to load gets no workspace. + * Modules whose jars changed are loaded again, and the pack's resources with them. + */ + private boolean loadsPackModules(IrisData data) { + if (data.getModules().reloadIfJarsChanged()) { + data.hotloaded(); + } + try { + data.getModules().pack(); + return true; + } catch (PackModulesException failure) { + IrisLogging.error("Not opening the code workspace of " + project.getName() + ": " + failure.getMessage()); + return false; + } + } + public JSONObject createCodeWorkspaceConfig() { return createCodeWorkspaceConfig(false); } @@ -180,17 +207,8 @@ public class IrisCodeWorkspace { for (Class snippetClass : sortedSnippets(dm.resolveSnippets())) { try { String snipType = snippetClass.getDeclaredAnnotation(Snippet.class).value(); - JSONObject schemaEntry = new JSONObject(); - KList fileMatches = new KList<>(); - - for (int depth = 1; depth < 8; depth++) { - fileMatches.add("/snippet/" + snipType + Form.repeat("/*", depth) + ".json"); - } - - schemaEntry.put("fileMatch", new JSONArray(fileMatches.toArray())); - schemaEntry.put("url", "./.iris/schema/snippet/" + snipType + "-schema.json"); - schemaEntries.add(schemaEntry); - File schemaFile = new File(dm.getDataFolder(), ".iris/schema/snippet/" + snipType + "-schema.json"); + schemaEntries.add(snippetSchemaEntry(snipType)); + File schemaFile = new File(dm.getDataFolder(), SNIPPET_SCHEMA_FOLDER + snipType + SCHEMA_SUFFIX); if (immediateSchemas) { IO.writeAll(schemaFile, new SchemaBuilder(snippetClass, dm).construct().toString(4)); } else { @@ -211,6 +229,10 @@ public class IrisCodeWorkspace { } } + for (ObjectType type : dm.getModules().pack().schema().snippets()) { + schemaEntries.add(moduleSnippetSchema(dm, type, immediateSchemas)); + } + schemaEntries.sort(Comparator.comparing(entry -> entry.getString("url"))); for (JSONObject entry : schemaEntries) { schemas.put(entry); @@ -229,6 +251,38 @@ public class IrisCodeWorkspace { return ws; } + /** A module's snippet type gets its own schema for the files under {@code snippet//}, as Iris's snippets do. */ + private static JSONObject moduleSnippetSchema(IrisData data, ObjectType type, boolean immediate) { + String name = type.snippet().orElseThrow(); + JSONObject entry = snippetSchemaEntry(name); + File schemaFile = new File(data.getDataFolder(), SNIPPET_SCHEMA_FOLDER + name + SCHEMA_SUFFIX); + if (immediate) { + writeSchema(schemaFile, SchemaBuilder.moduleSnippet(type, data)); + } else { + J.attemptAsync(() -> writeSchema(schemaFile, SchemaBuilder.moduleSnippet(type, data))); + } + return entry; + } + + private static JSONObject snippetSchemaEntry(String snippetFolder) { + KList fileMatches = new KList<>(); + for (int depth = 1; depth < SNIPPET_MATCH_DEPTH; depth++) { + fileMatches.add("/snippet/" + snippetFolder + Form.repeat("/*", depth) + ".json"); + } + JSONObject entry = new JSONObject(); + entry.put("fileMatch", new JSONArray(fileMatches.toArray())); + entry.put("url", "./" + SNIPPET_SCHEMA_FOLDER + snippetFolder + SCHEMA_SUFFIX); + return entry; + } + + private static void writeSchema(File schemaFile, JSONObject schema) { + try { + IO.writeAll(schemaFile, schema.toString(4)); + } catch (IOException failure) { + throw new UncheckedIOException("Could not write schema " + schemaFile.getAbsolutePath(), failure); + } + } + private void updateIntelliJSchemaMappings(JSONArray schemas) { File schemasFile = new File(project.getPath(), ".idea" + File.separator + "jsonSchemas.xml"); Document doc = IO.read(schemasFile); diff --git a/core/src/main/java/art/arcane/iris/core/project/ModuleSchemaRenderer.java b/core/src/main/java/art/arcane/iris/core/project/ModuleSchemaRenderer.java new file mode 100644 index 000000000..0f404b94d --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/project/ModuleSchemaRenderer.java @@ -0,0 +1,216 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.project; + +import art.arcane.volmlib.util.json.JSONArray; +import art.arcane.volmlib.util.json.JSONObject; +import com.volmit.FeatureAttachment.Target; +import com.volmit.config.Reference; +import com.volmit.host.schema.FeatureSchema; +import com.volmit.host.schema.ObjectType; +import com.volmit.host.schema.PackSchema; +import com.volmit.host.schema.SchemaOption; +import com.volmit.host.schema.TextFormat; +import com.volmit.host.schema.ValueKind; + +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; + +/** + * Renders the settings a pack's modules add, described by IrisApi as a {@link PackSchema}, into the JSON + * schema studio writes: each feature as a property of its target under its id and, for built-ins, its + * alias, each settings class once as a definition, references as completion lists. + */ +final class ModuleSchemaRenderer { + private static final String DEFINITION_PREFIX = "module-"; + private static final String DEFINITION_REF = "#/definitions/"; + private static final String RESOURCE_ID_PATTERN = "^[a-z0-9_.-]+:[a-z0-9_./-]+$"; + private static final String BLOCK_STATE_PATTERN = "^[a-z0-9_.-]+:[a-z0-9_./-]+(\\[[a-z0-9_]+=[a-z0-9_.-]+(,[a-z0-9_]+=[a-z0-9_.-]+)*\\])?$"; + private static final String SNIPPET_PATTERN = "^snippet/"; + + /** Points a string property at the completion list of a reference. */ + interface ReferenceCompletions { + void complete(JSONObject property, Reference reference, TextFormat format); + } + + private final PackSchema schema; + private final Map definitions; + private final ReferenceCompletions references; + private final Function> snippetFiles; + + /** {@code snippetFiles} lists the snippet references a pack has for a snippet folder, such as {@code snippet/thick}. */ + ModuleSchemaRenderer(PackSchema schema, Map definitions, ReferenceCompletions references, + Function> snippetFiles) { + this.schema = schema; + this.definitions = definitions; + this.references = references; + this.snippetFiles = snippetFiles; + } + + /** + * Adds every feature at {@code target} to {@code properties} under its id and alias, and to + * {@code constraints} that one JSON object may not hold both. + */ + void addFeatures(Target target, JSONObject properties, JSONArray constraints) { + for (FeatureSchema feature : schema.fields(target)) { + JSONObject property = featureProperty(feature); + properties.put(feature.key(), property); + feature.alias().ifPresent(alias -> { + properties.put(alias, property); + constraints.put(notBoth(alias, feature.key())); + }); + } + } + + /** The schema of a snippet file of {@code type}, which holds one object of it. */ + JSONObject snippetFileSchema(ObjectType type) { + JSONObject file = objectSchema(type); + file.put("$schema", "http://json-schema.org/draft-07/schema#"); + JSONObject fileDefinitions = new JSONObject(); + definitions.forEach(fileDefinitions::put); + file.put("definitions", fileDefinitions); + return file; + } + + private JSONObject featureProperty(FeatureSchema feature) { + JSONObject property = new JSONObject(); + property.put("$ref", DEFINITION_REF + definition(feature.settings().name())); + String description = feature.name() + " (" + feature.id() + "), from module " + feature.module().name() + + " (" + feature.module().id() + ")" + (feature.settings().description().isEmpty() ? "" : ". " + feature.settings().description()); + property.put("description", description); + return property; + } + + private static JSONObject notBoth(String alias, String key) { + JSONObject both = new JSONObject(); + both.put("required", new JSONArray(List.of(alias, key))); + JSONObject not = new JSONObject(); + not.put("not", both); + return not; + } + + /** The definition key of a settings class, built the first time a property names it. */ + private String definition(String typeName) { + String key = DEFINITION_PREFIX + typeName.replace('.', '-').replace('$', '-').toLowerCase(Locale.ROOT); + if (definitions.containsKey(key)) { + return key; + } + definitions.put(key, new JSONObject()); + ObjectType type = schema.type(typeName); + definitions.put(key, type.snippet().isPresent() ? objectOrSnippet(type) : objectSchema(type)); + return key; + } + + private JSONObject objectOrSnippet(ObjectType type) { + String folder = type.snippet().orElseThrow(); + JSONObject reference = new JSONObject(); + reference.put("type", "string"); + JSONArray files = new JSONArray(snippetFiles.apply(folder)); + if (files.length() > 0) { + reference.put("examples", files); + } + reference.put("pattern", SNIPPET_PATTERN); + reference.put("description", "A snippet file under snippet/" + folder + "/, written as snippet/"); + JSONObject either = new JSONObject(); + either.put("anyOf", new JSONArray(List.of(objectSchema(type), reference))); + either.put("description", type.description()); + return either; + } + + private JSONObject objectSchema(ObjectType type) { + JSONObject object = new JSONObject(); + object.put("type", "object"); + object.put("description", type.description()); + JSONObject properties = new JSONObject(); + JSONArray required = new JSONArray(); + for (SchemaOption option : type.options()) { + properties.put(option.name(), optionSchema(option)); + if (option.required()) { + required.put(option.name()); + } + } + object.put("properties", properties); + if (required.length() > 0) { + object.put("required", required); + } + return object; + } + + private JSONObject optionSchema(SchemaOption option) { + JSONObject property = valueSchema(option.kind()); + if (!option.description().isEmpty()) { + property.put("description", option.description()); + } + option.minimum().ifPresent(minimum -> property.put("minimum", minimum)); + option.maximum().ifPresent(maximum -> property.put("maximum", maximum)); + option.defaultValue().ifPresent(value -> property.put("default", JSONObject.wrap(value))); + return property; + } + + private JSONObject valueSchema(ValueKind kind) { + JSONObject value = new JSONObject(); + switch (kind) { + case ValueKind.BooleanKind ignored -> value.put("type", "boolean"); + case ValueKind.IntegerKind ignored -> value.put("type", "integer"); + case ValueKind.DecimalKind ignored -> value.put("type", "number"); + case ValueKind.TextKind text -> { + value.put("type", "string"); + patternOf(text.format()).ifPresent(pattern -> value.put("pattern", pattern)); + } + case ValueKind.ReferenceKind reference -> { + value.put("type", "string"); + references.complete(value, reference.reference(), reference.format()); + } + case ValueKind.EnumKind enumKind -> { + value.put("type", "string"); + value.put("enum", new JSONArray(enumKind.constants().stream().map(ValueKind.EnumConstant::name).toList())); + value.put("enumDescriptions", new JSONArray(enumKind.constants().stream().map(ValueKind.EnumConstant::description).toList())); + } + case ValueKind.ListKind list -> { + value.put("type", "array"); + value.put("items", valueSchema(list.element())); + if (list.unique()) { + value.put("uniqueItems", true); + } + } + case ValueKind.MapKind map -> { + value.put("type", "object"); + value.put("additionalProperties", valueSchema(map.value())); + } + case ValueKind.ObjectKind object -> value.put("$ref", DEFINITION_REF + definition(object.type())); + } + return value; + } + + private static Optional patternOf(TextFormat format) { + return switch (format) { + case PLAIN -> Optional.empty(); + case RESOURCE_ID -> Optional.of(RESOURCE_ID_PATTERN); + case BLOCK_STATE -> Optional.of(BLOCK_STATE_PATTERN); + }; + } + + /** The pattern a block state written as text matches, for completions that also accept properties. */ + static String blockStatePattern() { + return BLOCK_STATE_PATTERN; + } +} diff --git a/core/src/main/java/art/arcane/iris/core/project/SchemaBuilder.java b/core/src/main/java/art/arcane/iris/core/project/SchemaBuilder.java index df0ea3e4b..a2ca557fc 100644 --- a/core/src/main/java/art/arcane/iris/core/project/SchemaBuilder.java +++ b/core/src/main/java/art/arcane/iris/core/project/SchemaBuilder.java @@ -25,6 +25,10 @@ import art.arcane.iris.spi.PlatformNumericRange; import art.arcane.iris.core.loader.IrisData; import art.arcane.iris.core.loader.IrisRegistrant; import art.arcane.iris.core.loader.ResourceLoader; +import art.arcane.iris.core.modules.ModuleTargets; +import art.arcane.iris.engine.object.prefab.IrisObject; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.region.IrisRegion; import art.arcane.iris.core.structure.StructureSchemaKeys; import art.arcane.iris.engine.framework.ListFunction; import art.arcane.iris.engine.object.annotations.ArrayType; @@ -52,6 +56,9 @@ import art.arcane.volmlib.util.collection.KList; import art.arcane.volmlib.util.collection.KMap; import art.arcane.volmlib.util.json.JSONArray; import art.arcane.volmlib.util.json.JSONObject; +import com.volmit.config.Reference; +import com.volmit.host.schema.ObjectType; +import com.volmit.host.schema.TextFormat; import org.jetbrains.annotations.NotNull; import java.awt.GraphicsEnvironment; @@ -81,6 +88,7 @@ public class SchemaBuilder { private final IrisData data; private JSONArray potionTypes; private JSONArray enchantTypes; + private ModuleSchemaRenderer moduleSchemas; public SchemaBuilder(Class root, IrisData data) { this.data = data; @@ -89,6 +97,11 @@ public class SchemaBuilder { this.root = root; } + /** The schema of a module snippet file, which holds one object of {@code type}. */ + public static JSONObject moduleSnippet(ObjectType type, IrisData data) { + return new SchemaBuilder(type.getClass(), data).moduleSchemas().snippetFileSchema(type); + } + public JSONObject construct() { JSONObject schema = new JSONObject(); schema.put("$schema", "http://json-schema.org/draft-07/schema#"); @@ -336,6 +349,7 @@ public class SchemaBuilder { } buildProperties(properties, required, extended, c); + addModuleFeatures(c, properties, extended); if (required.length() > 0) { o.put("required", required); @@ -350,6 +364,58 @@ public class SchemaBuilder { return buildSnippet(o, c); } + /** The pack modules' settings at the target whose JSON {@code c} is, such as a biome's. */ + private void addModuleFeatures(Class c, JSONObject properties, JSONArray constraints) { + ModuleTargets.ofResource(c).ifPresent(target -> moduleSchemas().addFeatures(target, properties, constraints)); + } + + private ModuleSchemaRenderer moduleSchemas() { + if (moduleSchemas == null) { + moduleSchemas = new ModuleSchemaRenderer(data.getModules().pack().schema(), definitions, + this::completeReference, data::getPossibleSnippets); + } + return moduleSchemas; + } + + /** Completion for a module option that names something, from the same lists Iris's own fields use. */ + private void completeReference(JSONObject property, Reference reference, TextFormat format) { + switch (reference) { + case BLOCK -> { + if (format == TextFormat.BLOCK_STATE) { + putRegistryEnumOrPrefixRef(property, "enum-block-state", "enum-block-type", this::blockTypes, + ModuleSchemaRenderer.blockStatePattern()); + } else { + putRegistryEnumRef(property, "enum-block-type", this::blockTypes); + } + } + case ITEM -> putRegistryEnumRef(property, "enum-item-type", this::itemTypes); + case ENTITY -> putRegistryEnumRef(property, "enum-entity-type", this::entityTypes); + case BIOME -> putRegistryEnumRef(property, "enum-biome-type", this::biomeTypes); + case STRUCTURE -> putRegistryEnumRef(property, "enum-vanilla-structure", this::vanillaStructures); + case POTION_EFFECT -> putRegistryEnumRef(property, "enum-potion-effect-type", this::potionTypes); + case ENCHANTMENT -> putRegistryEnumRef(property, "enum-enchantment", this::enchantTypes); + case PACK_BIOME -> putPackResourceRef(property, IrisBiome.class); + case PACK_REGION -> putPackResourceRef(property, IrisRegion.class); + case PACK_OBJECT -> putPackResourceRef(property, IrisObject.class); + } + } + + /** Points {@code property} at the keys of the pack's resources of {@code type}; null when the pack has no such loader. */ + private ResourceLoader putPackResourceRef(JSONObject property, Class type) { + ResourceLoader loader = data.getLoaders().get(type); + if (loader == null) { + return null; + } + String key = "erz" + loader.getFolderName(); + if (!definitions.containsKey(key)) { + JSONObject keys = new JSONObject(); + keys.put("enum", new JSONArray(loader.getPossibleKeys())); + definitions.put(key, keys); + } + property.put("$ref", "#/definitions/" + key); + return loader; + } + private void buildProperties(JSONObject properties, JSONArray required, JSONArray extended, Class c) { for (Field k : c.getDeclaredFields()) { if (Modifier.isStatic(k.getModifiers()) || Modifier.isFinal(k.getModifiers()) || Modifier.isTransient(k.getModifiers())) { @@ -429,19 +495,10 @@ public class SchemaBuilder { if (k.isAnnotationPresent(RegistryListResource.class)) { RegistryListResource rr = k.getDeclaredAnnotation(RegistryListResource.class); - ResourceLoader loader = data.getLoaders().get(rr.value()); + ResourceLoader loader = putPackResourceRef(prop, rr.value()); if (loader != null) { - String key = "erz" + loader.getFolderName(); - - if (!definitions.containsKey(key)) { - JSONObject j = new JSONObject(); - j.put("enum", new JSONArray(loader.getPossibleKeys())); - definitions.put(key, j); - } - fancyType = "Iris " + loader.getResourceTypeName(); - prop.put("$ref", "#/definitions/" + key); description.add(SYMBOL_TYPE__N + " Must be a valid " + loader.getFolderName() + " (use ctrl+space for auto complete!)"); } else { IrisLogging.error("Cannot find Registry Loader for type " + rr.value() + " used in " + k.getDeclaringClass().getCanonicalName() + " in field " + k.getName()); @@ -638,20 +695,11 @@ public class SchemaBuilder { if (k.isAnnotationPresent(RegistryListResource.class)) { RegistryListResource rr = k.getDeclaredAnnotation(RegistryListResource.class); - ResourceLoader loader = data.getLoaders().get(rr.value()); + JSONObject items = new JSONObject(); + ResourceLoader loader = putPackResourceRef(items, rr.value()); if (loader != null) { fancyType = "List<" + loader.getResourceTypeName() + ">"; - String key = "erz" + loader.getFolderName(); - - if (!definitions.containsKey(key)) { - JSONObject j = new JSONObject(); - j.put("enum", new JSONArray(loader.getPossibleKeys())); - definitions.put(key, j); - } - - JSONObject items = new JSONObject(); - items.put("$ref", "#/definitions/" + key); prop.put("items", items); description.add(SYMBOL_TYPE__N + " Must be a valid " + loader.getResourceTypeName() + " (use ctrl+space for auto complete!)"); } else { diff --git a/core/src/main/java/art/arcane/iris/engine/platform/BukkitChunkGenerator.java b/core/src/main/java/art/arcane/iris/engine/platform/BukkitChunkGenerator.java index 078678cb2..18dd0cef3 100644 --- a/core/src/main/java/art/arcane/iris/engine/platform/BukkitChunkGenerator.java +++ b/core/src/main/java/art/arcane/iris/engine/platform/BukkitChunkGenerator.java @@ -152,7 +152,7 @@ public class BukkitChunkGenerator extends ChunkGenerator implements PlatformChun this.folder = new ReactiveFolder( dataLocation, (_a, _b, _c) -> hotloadFromWatcher(), - new KList<>(".iob", ".json"), + new KList<>(".iob", ".json", ".jar"), new KList<>(".iris"), new KList<>() ); diff --git a/core/src/test/java/art/arcane/iris/core/project/IrisCodeWorkspaceTest.java b/core/src/test/java/art/arcane/iris/core/project/IrisCodeWorkspaceTest.java index 8a1063d1d..7b5abf0e5 100644 --- a/core/src/test/java/art/arcane/iris/core/project/IrisCodeWorkspaceTest.java +++ b/core/src/test/java/art/arcane/iris/core/project/IrisCodeWorkspaceTest.java @@ -20,6 +20,7 @@ import org.mockito.Answers; import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.attribute.FileTime; import java.util.ArrayList; import java.util.Collections; @@ -89,6 +90,23 @@ public class IrisCodeWorkspaceTest { stamp.toMillis(), Files.getLastModifiedTime(file.toPath()).toMillis()); } + @Test + public void aPackWhoseModulesFailToLoadGetsNoWorkspaceUntilItsJarsAreFixed() throws Exception { + File pack = temporaryFolder.newFolder("broken-modules"); + Path brokenJar = Files.createDirectories(pack.toPath().resolve("modules")).resolve("broken.jar"); + Files.writeString(brokenJar, "not a jar"); + IrisCodeWorkspace workspace = new IrisCodeWorkspace(new IrisProject(pack)); + + assertFalse(workspace.updateWorkspace()); + data = IrisData.get(pack); + assertFalse(workspace.getCodeWorkspaceFile().exists()); + + Files.delete(brokenJar); + + assertTrue(workspace.updateWorkspace()); + assertTrue(workspace.getCodeWorkspaceFile().isFile()); + } + @Test public void workspaceSchemaEntriesAreEmittedInStableSortedOrder() throws Exception { File pack = temporaryFolder.newFolder("sorted"); diff --git a/core/src/test/java/art/arcane/iris/core/project/SchemaBuilderParityTest.java b/core/src/test/java/art/arcane/iris/core/project/SchemaBuilderParityTest.java index 9441e73ed..94cbba93b 100644 --- a/core/src/test/java/art/arcane/iris/core/project/SchemaBuilderParityTest.java +++ b/core/src/test/java/art/arcane/iris/core/project/SchemaBuilderParityTest.java @@ -59,11 +59,25 @@ import art.arcane.volmlib.util.collection.KList; import art.arcane.volmlib.util.collection.KMap; import art.arcane.volmlib.util.json.JSONArray; import art.arcane.volmlib.util.json.JSONObject; +import art.arcane.iris.core.modules.PackModuleSet; +import com.volmit.FeatureAttachment.Target; +import com.volmit.IrisFeature; +import com.volmit.IrisModule; +import com.volmit.ModuleInfo; +import com.volmit.config.Feature; +import com.volmit.host.LoadedPack; +import com.volmit.host.ModuleHost; +import com.volmit.host.schema.PackSchema; +import com.volmit.stage.Stage; +import com.volmit.stage.StageGraph; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -240,6 +254,23 @@ public class SchemaBuilderParityTest { assertFalse(properties.has("surfaceOpeningClearance")); } + @Test + public void objectPlacementSchemaRendersModuleSettingsUnderTheirIdAndAlias() throws IOException { + JSONObject schema = new SchemaBuilder(IrisObjectPlacement.class, structureSchemaData(glowModuleSchema())).construct(); + JSONObject properties = rootProperties(schema); + String definition = definitionKey(properties.getJSONObject("example:glow")); + JSONObject level = schema.getJSONObject("definitions").getJSONObject(definition) + .getJSONObject("properties").getJSONObject("level"); + + assertEquals(definition, definitionKey(properties.getJSONObject("glow"))); + assertEquals("integer", level.getString("type")); + assertEquals(1, level.getInt("minimum")); + assertEquals(15, level.getInt("maximum")); + assertEquals(7, level.getInt("default")); + assertEquals("How bright the glow is", level.getString("description")); + assertTrue(schema.toString().contains("{\"not\":{\"required\":[\"glow\",\"example:glow\"]}}")); + } + @Test public void dimensionSchemaExposesSurfaceSupportDefaults() { JSONObject properties = rootProperties( @@ -285,7 +316,17 @@ public class SchemaBuilderParityTest { } private static IrisData structureSchemaData() { + return structureSchemaData(new PackSchema(List.of(), List.of())); + } + + /** {@code modules} is what the pack's modules add to its JSON. */ + private static IrisData structureSchemaData(PackSchema modules) { IrisData data = mock(IrisData.class); + LoadedPack pack = mock(LoadedPack.class); + PackModuleSet moduleSet = mock(PackModuleSet.class); + when(pack.schema()).thenReturn(modules); + when(moduleSet.pack()).thenReturn(pack); + when(data.getModules()).thenReturn(moduleSet); ResourceLoader structureLoader = mock(ResourceLoader.class); ResourceLoader pieceLoader = mock(ResourceLoader.class); ResourceLoader expressionLoader = mock(ResourceLoader.class); @@ -592,4 +633,33 @@ public class SchemaBuilderParityTest { public void reportError(Throwable error) { } } + + private static PackSchema glowModuleSchema() throws IOException { + Path root = Files.createTempDirectory("glow-pack"); + try (LoadedPack pack = LoadedPack.load(Files.createDirectory(root.resolve("pack")), + ModuleHost.of(List.of(new GlowModule())).copyingJarsTo(Files.createDirectory(root.resolve("copies"))))) { + return pack.schema(); + } + } + + @Feature(id = "example:glow", name = "Glow", alias = "glow", targets = Target.OBJECT_PLACEMENT) + public static final class GlowSettings implements IrisFeature { + @com.volmit.config.Desc("How bright the glow is") + @com.volmit.config.MinNumber(1) + @com.volmit.config.MaxNumber(15) + private int level = 7; + } + + public static final class GlowModule implements IrisModule { + @Override + public ModuleInfo info() { + return ModuleInfo.of("example:glow", "Glow", "1"); + } + + @Override + public void install(StageGraph graph) { + graph.define(Stage.placement("example:glow/light")).when(GlowSettings.class).run((context, glow) -> { + }); + } + } } diff --git a/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java b/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java index 9c228db0b..e60de969d 100644 --- a/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java +++ b/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java @@ -19,6 +19,7 @@ import art.arcane.iris.core.loader.IrisData; import art.arcane.iris.core.modules.BuiltInPlacementPack; import art.arcane.iris.core.modules.ModuleObjectPlacement; import art.arcane.iris.core.modules.ObjectPlacementResult; +import art.arcane.iris.core.modules.PlatformBlockStates; import art.arcane.iris.engine.IrisComplex; import art.arcane.iris.engine.framework.Engine; import art.arcane.iris.engine.framework.NativeStructureVolume; @@ -65,8 +66,6 @@ import static org.mockito.Mockito.when; public class IrisObjectPlacementRegressionTest { private static final int ANCHOR_Y = 80; - private static final String SNOW_LAYER = "minecraft:snow[layers=1]"; - private static final String CAVE_AIR = "minecraft:cave_air"; @Rule public TemporaryFolder folder = new TemporaryFolder(); @@ -143,7 +142,7 @@ public class IrisObjectPlacementRegressionTest { int resultY = lineObject(3).place(0, ANCHOR_Y, 0, placer, placement, new RNG(2L), data); // The object's own blocks replace the bored air below, so only the extension stays air. - List airWrites = placer.writesOf(CAVE_AIR); + List airWrites = placer.writesOf(keyOf(IrisObject.States.AIR)); assertEquals(3, airWrites.size()); assertTrue(airWrites.stream().allMatch(write -> write.y() == resultY + 1)); assertTrue(airWrites.stream().allMatch(write -> write.x() == 0)); @@ -196,7 +195,7 @@ public class IrisObjectPlacementRegressionTest { object.place(0, ANCHOR_Y, 0, placer, placement, new RNG(2L), data); - List snowWrites = placer.writesOf(SNOW_LAYER); + List snowWrites = placer.writesOf(keyOf(IrisObject.States.SNOW_LAYERS[0])); assertEquals(1, snowWrites.size()); assertEquals(ANCHOR_Y, snowWrites.get(0).y()); } @@ -502,7 +501,7 @@ public class IrisObjectPlacementRegressionTest { List center = placer.writes().stream().filter(write -> write.x() == 0 && write.z() == 0).toList(); assertEquals(List.of(82, 81, 83), center.stream().map(BlockWrite::y).toList()); assertEquals(solid.key(), center.get(0).state().key()); - assertEquals(SNOW_LAYER, center.get(2).state().key()); + assertEquals(keyOf(IrisObject.States.SNOW_LAYERS[0]), center.get(2).state().key()); } @Test @@ -574,6 +573,14 @@ public class IrisObjectPlacementRegressionTest { return object; } + /** + * The key a placement writes a built-in state under. Iris resolves its built-in states once per JVM, with + * whichever platform a test bound first, so tests name them by their own keys. + */ + private static String keyOf(PlatformBlockState builtIn) { + return PlatformBlockStates.namespaced(builtIn.key()); + } + private static PlatformBlockState state(String key, boolean solid) { PlatformBlockState state = mock(PlatformBlockState.class); when(state.key()).thenReturn(key);