Serve module pattern views over the studio bridge

ModuleViews finds a pack's studio views of one kind; StudioScenes and the
new StudioPatterns sit on it. The bridge lists pattern views, their
entries, and lays out one tile of an entry at a depth (PatternEncoding:
JSON header with the block keys, then an i16 pick per column).
This commit is contained in:
piexel committed 2026-11-06 02:39:45 +01:00
1 parent 5dd753c589
commit 7eff4d0754
6 files changed
+297 -42

No files matched your search

@@ -0,0 +1,67 @@
/*
* 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.studio.bridge;
import com.google.gson.Gson;
import com.volmit.block.BlockState;
import com.volmit.tooling.BlockPattern;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* A {@link BlockPattern} as the studio reads it, little endian:
* <pre>
* u32 header length | header, UTF-8 JSON | padding to 4 bytes | i16 picks[columns²]
* </pre>
* The header holds the columns across, the blocks between columns, and the block keys the picks index; a pick of
* {@link BlockPattern#NO_BLOCK} is a column the pattern places nothing at.
*/
final class PatternEncoding {
private static final int ALIGNMENT_BYTES = 4;
private PatternEncoding() {
}
static byte[] encode(BlockPattern pattern, int blocksPerColumn, Gson gson) {
List<String> blocks = pattern.blocks().stream().map(BlockState::key).toList();
byte[] header = gson.toJson(new Header(pattern.columns(), blocksPerColumn, blocks, BlockPattern.NO_BLOCK))
.getBytes(StandardCharsets.UTF_8);
int columns = pattern.columns();
ByteBuffer buffer = ByteBuffer.allocate(aligned(Integer.BYTES + header.length) + columns * columns * Short.BYTES)
.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(header.length).put(header);
buffer.position(aligned(buffer.position()));
for (int row = 0; row < columns; row++) {
for (int column = 0; column < columns; column++) {
buffer.putShort((short) pattern.pickAt(column, row));
}
}
return buffer.array();
}
private static int aligned(int bytes) {
return (bytes + ALIGNMENT_BYTES - 1) / ALIGNMENT_BYTES * ALIGNMENT_BYTES;
}
private record Header(int columns, int blocksPerColumn, List<String> blocks, int noBlock) {
}
}
@@ -33,6 +33,7 @@ import art.arcane.iris.core.studio.saved.SavedBiomeLabels;
import art.arcane.iris.core.studio.saved.SavedTile;
import art.arcane.iris.core.studio.saved.SavedWorld;
import art.arcane.iris.core.studio.saved.SavedWorlds;
import art.arcane.iris.core.studio.scene.StudioPatterns;
import art.arcane.iris.core.studio.scene.StudioScenes;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.render.RenderType;
@@ -45,6 +46,9 @@ import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import com.volmit.object.GeneratedObject;
import com.volmit.tooling.BlockPattern;
import com.volmit.tooling.PatternArea;
import com.volmit.tooling.PatternEntry;
import com.volmit.tooling.SceneEntry;
import java.io.IOException;
@@ -94,6 +98,8 @@ public final class StudioBridge implements AutoCloseable {
private static final double LARGEST_BLOCKS_PER_PIXEL = 4_096D;
// Each pixel reads the chunk it lands in, so a saved world is shown no farther out than a pixel per chunk.
private static final int LARGEST_SAVED_BLOCKS_PER_PIXEL = 16;
// A palette is picked block by block, so far out a column stands for the blocks around it; past this it says little.
private static final int LARGEST_PATTERN_BLOCKS_PER_COLUMN = 64;
// Noise has detail finer than a block, so it may be looked at closer than the world.
private static final double SMALLEST_NOISE_BLOCKS_PER_PIXEL = 1D / 64D;
private static final long ENTITY_WAIT_MILLIS = 3_000L;
@@ -222,6 +228,9 @@ public final class StudioBridge implements AutoCloseable {
case "saved-worlds" -> sendJson(exchange, STATUS_OK, savedWorlds(exchange));
case "saved-start" -> sendJson(exchange, STATUS_OK, savedStart(exchange, query));
case "saved-tile" -> sendBytes(exchange, "application/octet-stream", savedTile(get(exchange).engine(), query));
case "patterns" -> sendJson(exchange, STATUS_OK, StudioPatterns.of(get(exchange).engine()).views());
case "pattern-entries" -> sendJson(exchange, STATUS_OK, patternEntries(get(exchange).engine(), query));
case "pattern" -> sendBytes(exchange, "application/octet-stream", pattern(get(exchange).engine(), query));
case "noise-sources" -> sendJson(exchange, STATUS_OK, NoiseSources.of(get(exchange).engine()).list());
case "noise-tile" -> sendBytes(exchange, "application/octet-stream", noiseTile(get(exchange).engine(), query));
case "events" -> streamEvents(exchange);
@@ -332,6 +341,35 @@ public final class StudioBridge implements AutoCloseable {
return SceneEncoding.encode(scene.orElseThrow(() -> new RequestError(STATUS_NOT_FOUND, "No scene " + key)), gson);
}
private static List<PatternEntry> patternEntries(Engine engine, Query query) {
try {
return StudioPatterns.of(engine).entries(query.text("view"));
} catch (NoSuchElementException unknown) {
throw new RequestError(STATUS_NOT_FOUND, unknown.getMessage());
}
}
private byte[] pattern(Engine engine, Query query) {
int columns = query.whole("size", DEFAULT_TILE_SIZE);
int blocksPerColumn = query.whole("scale", 1);
if (columns < 1 || columns > LARGEST_TILE_SIZE) {
throw new RequestError(STATUS_BAD_REQUEST, "A pattern is 1 to " + LARGEST_TILE_SIZE + " columns across");
}
if (blocksPerColumn < 1 || blocksPerColumn > LARGEST_PATTERN_BLOCKS_PER_COLUMN) {
throw new RequestError(STATUS_BAD_REQUEST, "The scale is 1 to " + LARGEST_PATTERN_BLOCKS_PER_COLUMN + " whole blocks per column");
}
String key = query.text("key");
int tileBlocks = columns * blocksPerColumn;
PatternArea area = new PatternArea(query.whole("x", 0) * tileBlocks, query.whole("z", 0) * tileBlocks, columns, blocksPerColumn);
Optional<BlockPattern> pattern;
try {
pattern = StudioPatterns.of(engine).pattern(query.text("view"), key, query.whole("depth", 0), area);
} catch (NoSuchElementException unknown) {
throw new RequestError(STATUS_NOT_FOUND, unknown.getMessage());
}
return PatternEncoding.encode(pattern.orElseThrow(() -> new RequestError(STATUS_NOT_FOUND, "No pattern " + key)), blocksPerColumn, gson);
}
private static List<SavedWorlds.Info> savedWorlds(HttpExchange exchange) {
requireMethod(exchange, "GET");
return SavedWorlds.shared().list();
@@ -0,0 +1,81 @@
/*
* 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.studio.scene;
import art.arcane.iris.engine.framework.Engine;
import com.volmit.host.StudioViewRegistration;
import com.volmit.tooling.StudioView;
import com.volmit.world.PackWorld;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Function;
/** The studio views of one kind the modules of an engine's pack registered, asked for that engine's world. */
final class ModuleViews<V extends StudioView> {
private final List<StudioViewRegistration> registrations;
private final Class<V> kind;
private final String kindName;
private final PackWorld world;
private ModuleViews(List<StudioViewRegistration> registrations, Class<V> kind, String kindName, PackWorld world) {
this.registrations = registrations;
this.kind = kind;
this.kindName = kindName;
this.world = world;
}
static <V extends StudioView> ModuleViews<V> of(Engine engine, Class<V> kind, String kindName) {
List<StudioViewRegistration> ofKind = engine.getData().getModules().pack().catalog().studioViews().stream()
.filter(registration -> kind.isInstance(registration.view()))
.toList();
return new ModuleViews<>(ofKind, kind, kindName, engine.getGeneration().world());
}
List<ViewInfo> infos() {
return registrations.stream()
.map(registration -> new ViewInfo(registration.view().id().toString(), registration.view().label(), registration.view().description()))
.toList();
}
/**
* What {@code ask} gives for the view {@code viewId}; a failure inside the module names the module and the task.
*
* @throws NoSuchElementException when no module registered a view {@code viewId} of this kind
*/
<T> T ask(String viewId, String task, Function<V, T> ask) {
StudioViewRegistration registration = registrations.stream()
.filter(candidate -> candidate.view().id().toString().equals(viewId))
.findFirst()
.orElseThrow(() -> new NoSuchElementException("No module shows a " + kindName + " " + viewId));
try {
return ask.apply(kind.cast(registration.view()));
} catch (RuntimeException failure) {
throw new IllegalStateException("The " + registration + " could not " + task, failure);
}
}
PackWorld world() {
return world;
}
/** A studio view as the studio lists it. */
public record ViewInfo(String id, String label, String description) {
}
}
@@ -0,0 +1,60 @@
/*
* 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.studio.scene;
import art.arcane.iris.engine.framework.Engine;
import com.volmit.tooling.BlockPattern;
import com.volmit.tooling.PatternArea;
import com.volmit.tooling.PatternEntry;
import com.volmit.tooling.PatternView;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
/** The studio pattern views the modules of an engine's pack registered, asked for that engine's world. */
public final class StudioPatterns {
private final ModuleViews<PatternView> views;
private StudioPatterns(ModuleViews<PatternView> views) {
this.views = views;
}
public static StudioPatterns of(Engine engine) {
return new StudioPatterns(ModuleViews.of(engine, PatternView.class, "pattern view"));
}
public List<ModuleViews.ViewInfo> views() {
return views.infos();
}
/** @throws NoSuchElementException when no module registered a pattern view {@code viewId} */
public List<PatternEntry> entries(String viewId) {
return views.ask(viewId, "list its entries", view -> view.entries(views.world()));
}
/**
* What an entry places at a depth across an area; empty when the view has no such entry.
*
* @throws NoSuchElementException when no module registered a pattern view {@code viewId}
*/
public Optional<BlockPattern> pattern(String viewId, String key, int depth, PatternArea area) {
return views.ask(viewId, "lay out " + key, view -> view.pattern(views.world(), key, depth, area));
}
}
@@ -19,10 +19,9 @@
package art.arcane.iris.core.studio.scene;
import art.arcane.iris.engine.framework.Engine;
import com.volmit.host.SceneViewRegistration;
import com.volmit.object.GeneratedObject;
import com.volmit.tooling.SceneEntry;
import com.volmit.world.PackWorld;
import com.volmit.tooling.SceneView;
import java.util.List;
import java.util.NoSuchElementException;
@@ -30,61 +29,31 @@ import java.util.Optional;
/** The studio scene views the modules of an engine's pack registered, asked for that engine's world. */
public final class StudioScenes {
private final List<SceneViewRegistration> registrations;
private final PackWorld world;
private final ModuleViews<SceneView> views;
private StudioScenes(List<SceneViewRegistration> registrations, PackWorld world) {
this.registrations = registrations;
this.world = world;
private StudioScenes(ModuleViews<SceneView> views) {
this.views = views;
}
public static StudioScenes of(Engine engine) {
return new StudioScenes(engine.getData().getModules().pack().catalog().sceneViews(), engine.getGeneration().world());
return new StudioScenes(ModuleViews.of(engine, SceneView.class, "scene view"));
}
public List<SceneViewInfo> views() {
return registrations.stream()
.map(registration -> new SceneViewInfo(registration.view().id().toString(), registration.view().label(),
registration.view().description()))
.toList();
public List<ModuleViews.ViewInfo> views() {
return views.infos();
}
/** @throws NoSuchElementException when no module registered a view {@code viewId} */
/** @throws NoSuchElementException when no module registered a scene view {@code viewId} */
public List<SceneEntry> entries(String viewId) {
SceneViewRegistration registration = registration(viewId);
try {
return registration.view().entries(world);
} catch (RuntimeException failure) {
throw new IllegalStateException(failureOf(registration, "list its entries"), failure);
}
return views.ask(viewId, "list its entries", view -> view.entries(views.world()));
}
/**
* The blocks of one variant of an entry; empty when the view has no such entry or variant.
*
* @throws NoSuchElementException when no module registered a view {@code viewId}
* @throws NoSuchElementException when no module registered a scene view {@code viewId}
*/
public Optional<GeneratedObject> scene(String viewId, String key, int variant) {
SceneViewRegistration registration = registration(viewId);
try {
return registration.view().scene(world, key, variant);
} catch (RuntimeException failure) {
throw new IllegalStateException(failureOf(registration, "build " + key), failure);
}
}
private SceneViewRegistration registration(String viewId) {
return registrations.stream()
.filter(registration -> registration.view().id().toString().equals(viewId))
.findFirst()
.orElseThrow(() -> new NoSuchElementException("No module shows a scene view " + viewId));
}
private static String failureOf(SceneViewRegistration registration, String task) {
return "The " + registration + " could not " + task;
}
/** A scene view as the studio lists it. */
public record SceneViewInfo(String id, String label, String description) {
return views.ask(viewId, "build " + key, view -> view.scene(views.world(), key, variant));
}
}
@@ -0,0 +1,40 @@
package art.arcane.iris.core.studio.bridge;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.volmit.block.BlockState;
import com.volmit.tooling.BlockPattern;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
public class PatternEncodingTest {
@Test
public void picksFollowTheHeaderRowByRowWithNothingAsNoBlock() {
BlockState grass = BlockState.parse("minecraft:grass_block[snowy=false]");
BlockState dirt = BlockState.parse("minecraft:dirt");
BlockPattern pattern = BlockPattern.of(2, new BlockState[]{grass, null, dirt, grass});
byte[] encoded = PatternEncoding.encode(pattern, 4, new Gson());
ByteBuffer body = ByteBuffer.wrap(encoded).order(ByteOrder.LITTLE_ENDIAN);
int headerLength = body.getInt();
JsonObject header = JsonParser.parseString(new String(encoded, 4, headerLength, StandardCharsets.UTF_8)).getAsJsonObject();
assertEquals("[\"minecraft:grass_block[snowy=false]\",\"minecraft:dirt\"]", header.get("blocks").toString());
assertEquals(2, header.get("columns").getAsInt());
assertEquals(4, header.get("blocksPerColumn").getAsInt());
assertEquals(-1, header.get("noBlock").getAsInt());
int picksAt = (4 + headerLength + 3) / 4 * 4;
assertEquals(picksAt + 4 * Short.BYTES, encoded.length);
body.position(picksAt);
assertEquals(0, body.getShort());
assertEquals(-1, body.getShort());
assertEquals(1, body.getShort());
assertEquals(0, body.getShort());
}
}