Merge wave1/b

This commit is contained in:
piexel committed 2026-10-09 14:43:33 +02:00
commit b1414a1753
4 files changed
+121 -5

No files matched your search

@@ -43,7 +43,7 @@ import java.util.stream.Stream;
/**
* The one {@link ModuleHost} every pack load uses: Iris's built-in modules, the built-in module API
* package exposed to packs, Iris's own JSON fields, the reserved {@code iris} namespace, Iris's pack object
* package exposed to packs, Iris's own JSON fields, Iris's pack object
* model as host types, the pack folders Iris reads itself, a jar copy directory under the plugin's cache, and
* Iris's log.
*/
@@ -52,7 +52,6 @@ public final class IrisModuleHost {
public static final String BUILT_IN_API_PACKAGE = "art.arcane.iris.builtin";
/** Iris's pack object model: built-in settings may hold these classes, which Iris reads and describes itself. */
public static final String HOST_TYPE_PACKAGE = "art.arcane.iris.engine.object";
static final String RESERVED_NAMESPACE = "iris";
private static final String[] JAR_COPY_FOLDER = {"cache", "module-jars"};
private static final String TEMPORARY_DIRECTORY_PROPERTY = "java.io.tmpdir";
@@ -82,7 +81,6 @@ public final class IrisModuleHost {
private static ModuleHost build() {
return ModuleHost.of(builtInModules())
.exposing(BUILT_IN_API_PACKAGE)
.reservingNamespaces(RESERVED_NAMESPACE)
.reservingFields(ModuleTargets.reservedFields())
.describingHostTypesIn(HOST_TYPE_PACKAGE)
.reservingFolders(IrisData.ownFolderNames().toArray(String[]::new))
@@ -27,6 +27,7 @@ import com.volmit.host.ModuleHost;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
@@ -38,10 +39,25 @@ public final class BuiltInPack {
private BuiltInPack() {
}
/** A new empty pack folder, to put module jars into (see {@link PackModuleJar}) before {@link #serve(IrisData, TemporaryFolder, Path, List)}. */
public static Path newPackFolder(TemporaryFolder folder) throws IOException {
return folder.newFolder().toPath();
}
/** Loads the pack and lets the mocked {@code data} serve it, as a pack's data serves its modules. Close it after the test. */
public static LoadedPack serve(IrisData data, TemporaryFolder folder, List<IrisModule> packModules) throws IOException {
return serve(data, folder, newPackFolder(folder), packModules);
}
/**
* Like {@link #serve(IrisData, TemporaryFolder, List)} for a pack folder whose {@code modules} folder may hold module jars:
* they load through the pack loader after the compiled-in built-ins and {@code packModules}, as in a real pack. The jars see the
* same packages a real pack's modules see.
*/
public static LoadedPack serve(IrisData data, TemporaryFolder folder, Path packFolder, List<IrisModule> packModules) throws IOException {
List<IrisModule> modules = Stream.concat(IrisModuleHost.builtInModules().stream(), packModules.stream()).toList();
LoadedPack pack = LoadedPack.load(folder.newFolder().toPath(), ModuleHost.of(modules)
LoadedPack pack = LoadedPack.load(packFolder, ModuleHost.of(modules)
.exposing(IrisModuleHost.BUILT_IN_API_PACKAGE)
.describingHostTypesIn(IrisModuleHost.HOST_TYPE_PACKAGE)
.copyingJarsTo(folder.newFolder().toPath()));
PackModuleSet moduleSet = mock(PackModuleSet.class);
@@ -25,6 +25,11 @@ public final class PackModuleJar {
/** @param sources Java sources by binary class name; {@code entryClass} is the module's entry point */
public static void write(Path pack, String jarName, String entryClass, Map<String, String> sources) throws IOException {
write(pack, jarName, List.of(entryClass), sources);
}
/** A jar of several modules: {@code entryClasses} are listed one per line, in order. */
public static void write(Path pack, String jarName, List<String> entryClasses, Map<String, String> sources) throws IOException {
Path work = Files.createTempDirectory("iris-pack-module-src");
Path classes = Files.createDirectories(work.resolve("classes"));
compile(work, classes, sources);
@@ -32,7 +37,7 @@ public final class PackModuleJar {
try (JarOutputStream output = new JarOutputStream(Files.newOutputStream(jar))) {
addClasses(output, classes);
output.putNextEntry(new JarEntry(SERVICE_ENTRY));
output.write((entryClass + "\n").getBytes(StandardCharsets.UTF_8));
output.write((String.join("\n", entryClasses) + "\n").getBytes(StandardCharsets.UTF_8));
output.closeEntry();
}
}
@@ -0,0 +1,97 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.modules;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.engine.modifier.CarveModule;
import com.volmit.host.LoadedPack;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* Module jars in a test pack's {@code modules} folder load next to the compiled-in built-ins. This is the path the built-ins
* take when they move into a pack module jar one by one: a jar may list several modules, the {@code iris} namespace is free,
* and a module id stays unique across the jar and the compiled-in modules.
*/
public class PackModuleJarNextToBuiltInsTest {
private static final String MODULE_TEMPLATE = """
package pack.%1$s;
import com.volmit.*;
import com.volmit.stage.*;
public final class %2$s implements IrisModule {
public ModuleInfo info() { return ModuleInfo.of("%3$s", "%2$s", "1"); }
public void install(StageGraph graph) {
graph.define(Stage.placement("%3$s/work")).run(context -> {});
}
}
""";
private static final String FIRST_ID = "iris:ported";
private static final String SECOND_ID = "example:companion";
private static final String PREPARE_STAGE = "iris:prepare/slope";
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void aJarOfSeveralModulesLoadsNextToTheCompiledInBuiltIns() throws IOException {
Path packFolder = BuiltInPack.newPackFolder(folder);
PackModuleJar.write(packFolder, "builtins.jar", List.of("pack.ported.Ported", "pack.companion.Companion"),
Map.of("pack.ported.Ported", module("ported", "Ported", FIRST_ID),
"pack.companion.Companion", module("companion", "Companion", SECOND_ID)));
try (LoadedPack pack = BuiltInPack.serve(mock(IrisData.class), folder, packFolder, List.of())) {
assertEquals(List.of(FIRST_ID, SECOND_ID), pack.packModules().stream().map(module -> module.info().id().toString()).toList());
List<String> stages = pack.placement().stages().stream().map(stage -> stage.stage().id().toString()).toList();
assertTrue(stages.toString(), stages.contains(PREPARE_STAGE));
assertTrue(stages.toString(), stages.contains(FIRST_ID + "/work"));
assertTrue(stages.toString(), stages.contains(SECOND_ID + "/work"));
}
}
@Test
public void aJarModuleWithTheIdOfACompiledInBuiltInFailsAsADuplicate() throws IOException {
String builtInId = new CarveModule().info().id().toString();
Path packFolder = BuiltInPack.newPackFolder(folder);
PackModuleJar.write(packFolder, "carve.jar", "pack.ported.Ported",
Map.of("pack.ported.Ported", module("ported", "Ported", builtInId)));
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
() -> BuiltInPack.serve(mock(IrisData.class), folder, packFolder, List.of()));
assertEquals("Duplicate module " + builtInId + " from " + Path.of("modules", "carve.jar") + " (pack.ported.Ported); already loaded from host classpath ("
+ CarveModule.class.getName() + ")", failure.getMessage());
}
private static String module(String packageName, String className, String moduleId) {
return MODULE_TEMPLATE.formatted(packageName, className, moduleId);
}
}