mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Switch the studio between packs from the title bar
- List the workspace packs with their names and versions in the pack menu - Open the chosen pack as the studio world and reconnect to its bridge - Close the bridge of the world the studio switched away from - Keep Disconnect in the menu for the world list
This commit is contained in:
1 parent
26c7ed9c40
commit
fe99d5bda5
14 files changed
+450
-17
No files matched your search
@@ -98,6 +98,9 @@ public final class StudioBridge implements AutoCloseable {
|
||||
private static final long ASSET_CACHE_SECONDS = 86_400L;
|
||||
private static final String OVERRIDE_PREFIX = "set.";
|
||||
private static final int LARGEST_BODY_BYTES = 4 * 1024 * 1024;
|
||||
private static final long PACK_OPEN_TIMEOUT_MINUTES = 5;
|
||||
/** How long a bridge the studio switched away from stays up, so its answer naming the new bridge gets out first. */
|
||||
private static final long REPLACED_BRIDGE_GRACE_SECONDS = 5;
|
||||
private static final String VIEW_BIOMES = "biomes";
|
||||
private static final String VIEW_REGIONS = "regions";
|
||||
private static final String VIEW_RIVERS = "rivers";
|
||||
@@ -277,6 +280,8 @@ public final class StudioBridge implements AutoCloseable {
|
||||
case "saved-worlds" -> sendJson(exchange, STATUS_OK, List.of());
|
||||
case "events" -> streamEvents(exchange);
|
||||
case "teleport" -> teleport(exchange, query);
|
||||
case "packs" -> sendJson(exchange, STATUS_OK, packs(exchange));
|
||||
case "open-pack" -> openPack(exchange, query);
|
||||
case "open" -> throw new RequestError(STATUS_NOT_FOUND, "Opening a pack file from the studio is not available in Iris v4");
|
||||
default -> throw new RequestError(STATUS_NOT_FOUND, "No such endpoint: " + endpoint);
|
||||
}
|
||||
@@ -645,6 +650,41 @@ public final class StudioBridge implements AutoCloseable {
|
||||
sendJson(exchange, STATUS_OK, new MessageBody(null));
|
||||
}
|
||||
|
||||
private List<StudioPack> packs(HttpExchange exchange) {
|
||||
requireMethod(exchange, "GET");
|
||||
StudioPackSwitch packSwitch = StudioBridges.packSwitch();
|
||||
return packSwitch == null ? List.of() : packSwitch.packs();
|
||||
}
|
||||
|
||||
/** Replaces the studio world with one of another pack and answers with the new world's bridge, then closes this one. */
|
||||
private void openPack(HttpExchange exchange, Query query) throws IOException {
|
||||
requireMethod(exchange, "POST");
|
||||
StudioPackSwitch packSwitch = StudioBridges.packSwitch();
|
||||
if (packSwitch == null) {
|
||||
throw new RequestError(STATUS_NOT_FOUND, "This server cannot switch packs from the studio");
|
||||
}
|
||||
StudioBridge opened = awaitPackOpen(packSwitch.open(query.text("pack"), world.openerId()));
|
||||
sendJson(exchange, STATUS_OK, new OpenedBridge(opened.worldName(), opened.port(), opened.token()));
|
||||
if (opened != this) {
|
||||
CompletableFuture.runAsync(() -> StudioBridges.close(worldName()),
|
||||
CompletableFuture.delayedExecutor(REPLACED_BRIDGE_GRACE_SECONDS, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
private static StudioBridge awaitPackOpen(CompletableFuture<StudioBridge> opening) {
|
||||
try {
|
||||
return opening.get(PACK_OPEN_TIMEOUT_MINUTES, TimeUnit.MINUTES);
|
||||
} catch (ExecutionException failure) {
|
||||
Throwable cause = failure.getCause() == null ? failure : failure.getCause();
|
||||
throw new RequestError(STATUS_CONFLICT, "Could not open the pack: " + cause.getMessage());
|
||||
} catch (TimeoutException slow) {
|
||||
throw new RequestError(STATUS_UNAVAILABLE, "The pack is still opening; the server will show it when it is ready");
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RequestError(STATUS_UNAVAILABLE, "The bridge stopped while the pack opened");
|
||||
}
|
||||
}
|
||||
|
||||
private void streamEvents(HttpExchange exchange) throws IOException {
|
||||
requireMethod(exchange, "GET");
|
||||
Headers headers = exchange.getResponseHeaders();
|
||||
@@ -797,6 +837,9 @@ public final class StudioBridge implements AutoCloseable {
|
||||
private record ErrorBody(String error) {
|
||||
}
|
||||
|
||||
private record OpenedBridge(String world, int port, String token) {
|
||||
}
|
||||
|
||||
private record MessageBody(String message) {
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ public final class StudioBridges {
|
||||
private static final Path CONNECTION_FOLDER = Path.of(System.getProperty("user.home"), ".iris", "studio", "bridges");
|
||||
private static final String OWNER_ONLY = "rw-------";
|
||||
private static final Map<String, StudioBridge> OPEN = new ConcurrentHashMap<>();
|
||||
private static volatile StudioPackSwitch packSwitch;
|
||||
|
||||
private StudioBridges() {
|
||||
}
|
||||
@@ -61,6 +62,24 @@ public final class StudioBridges {
|
||||
return bridge;
|
||||
}
|
||||
|
||||
/** Lets the studio switch packs through {@code packSwitch}; without one the studio offers no other pack. */
|
||||
public static void usePackSwitch(StudioPackSwitch packSwitch) {
|
||||
StudioBridges.packSwitch = packSwitch;
|
||||
}
|
||||
|
||||
static StudioPackSwitch packSwitch() {
|
||||
return packSwitch;
|
||||
}
|
||||
|
||||
public static synchronized void close(String worldName) {
|
||||
StudioBridge bridge = OPEN.remove(worldName);
|
||||
if (bridge == null) {
|
||||
return;
|
||||
}
|
||||
bridge.close();
|
||||
withdraw(bridge);
|
||||
}
|
||||
|
||||
public static synchronized void closeAll() {
|
||||
StudioWindow.closeAll();
|
||||
for (StudioBridge bridge : OPEN.values()) {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.studio.bridge;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A pack the studio can open: its folder, and the name and version its dimension file gives.
|
||||
*
|
||||
* @param key the pack's folder, which names its dimension file too
|
||||
*/
|
||||
public record StudioPack(String key, String name, int version) {
|
||||
private static final int UNVERSIONED = 1;
|
||||
|
||||
/** The packs in the workspace by name; a folder without a readable dimension file of its own name is no pack. */
|
||||
public static List<StudioPack> inWorkspace(File workspace) {
|
||||
File[] folders = workspace.listFiles(File::isDirectory);
|
||||
if (folders == null) {
|
||||
return List.of();
|
||||
}
|
||||
return Arrays.stream(folders)
|
||||
.map(StudioPack::read)
|
||||
.filter(Objects::nonNull)
|
||||
.sorted(Comparator.comparing(StudioPack::name, String.CASE_INSENSITIVE_ORDER))
|
||||
.toList();
|
||||
}
|
||||
|
||||
static StudioPack read(File folder) {
|
||||
String key = folder.getName();
|
||||
File dimensionFile = new File(folder, "dimensions/" + key + ".json");
|
||||
if (!dimensionFile.isFile()) {
|
||||
return null;
|
||||
}
|
||||
try (Reader reader = Files.newBufferedReader(dimensionFile.toPath(), StandardCharsets.UTF_8)) {
|
||||
JsonObject dimension = JsonParser.parseReader(reader).getAsJsonObject();
|
||||
return new StudioPack(key, textOr(dimension.get("name"), key), versionOf(dimension.get("version")));
|
||||
} catch (IOException | JsonParseException | IllegalStateException | NumberFormatException unreadable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String textOr(JsonElement element, String fallback) {
|
||||
return element != null && element.isJsonPrimitive() && !element.getAsString().isBlank() ? element.getAsString() : fallback;
|
||||
}
|
||||
|
||||
private static int versionOf(JsonElement element) {
|
||||
return element != null && element.isJsonPrimitive() ? element.getAsInt() : UNVERSIONED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.studio.bridge;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/** Opens the studio on another pack, on a platform that can replace the open studio world. */
|
||||
public interface StudioPackSwitch {
|
||||
/** The packs the studio can open, in the order to list them. */
|
||||
List<StudioPack> packs();
|
||||
|
||||
/**
|
||||
* Replaces the open studio world with one of {@code pack} and opens a bridge to the new world.
|
||||
*
|
||||
* @param openerId the player the old studio was opened for, who moves to the new one; null for the console
|
||||
*/
|
||||
CompletableFuture<StudioBridge> open(String pack, UUID openerId);
|
||||
}
|
||||
@@ -51,6 +51,10 @@ final class StudioWorld {
|
||||
return name;
|
||||
}
|
||||
|
||||
UUID openerId() {
|
||||
return openerId;
|
||||
}
|
||||
|
||||
/** The world's engine and what the bridge draws it with; null while the world has no running engine. */
|
||||
synchronized Session session() {
|
||||
if (session != null && isRunning(session.engine())) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package art.arcane.iris.studio.bridge;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StudioPackTest {
|
||||
@Rule
|
||||
public TemporaryFolder workspace = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void aPackIsNamedAndVersionedByItsDimensionFile() throws IOException {
|
||||
dimension("natureplanet", "{\"name\": \"Etopia\", \"version\": 100}");
|
||||
|
||||
assertEquals(List.of(new StudioPack("natureplanet", "Etopia", 100)), StudioPack.inWorkspace(workspace.getRoot()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commentsInTheDimensionFileAreRead() throws IOException {
|
||||
dimension("overworld", "{\n // the pack's name\n \"name\": \"Overworld\",\n \"version\": 3\n}");
|
||||
|
||||
assertEquals(List.of(new StudioPack("overworld", "Overworld", 3)), StudioPack.inWorkspace(workspace.getRoot()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aDimensionWithoutNameOrVersionFallsBackToItsFolderAndTheFirstVersion() throws IOException {
|
||||
dimension("bare", "{}");
|
||||
|
||||
assertEquals(List.of(new StudioPack("bare", "bare", 1)), StudioPack.inWorkspace(workspace.getRoot()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void foldersWithoutTheirDimensionFileAreNoPacks() throws IOException {
|
||||
workspace.newFolder("notes");
|
||||
dimension("broken", "{\"name\": ");
|
||||
dimension("zeta", "{\"name\": \"zeta\"}");
|
||||
dimension("alpha", "{\"name\": \"Alpha\"}");
|
||||
|
||||
assertEquals(List.of("Alpha", "zeta"), StudioPack.inWorkspace(workspace.getRoot()).stream().map(StudioPack::name).toList());
|
||||
}
|
||||
|
||||
private void dimension(String pack, String json) throws IOException {
|
||||
File dimensions = new File(workspace.getRoot(), pack + "/dimensions");
|
||||
Files.createDirectories(dimensions.toPath());
|
||||
Files.writeString(new File(dimensions, pack + ".json").toPath(), json, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user