mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Persist RunningWorld.saved under the world's module-data folder
SavedWorldState reads a value when first asked for and writes it, framed with the codec version, when the engine saves its data and when the world stops, so a hotload hands the new world what the old one wrote. A rejected payload is logged and replaced by the key's initial value. Spawner cooldowns leave IrisEngineData and the EngineSpawnCooldowns facade; old cooldowns in engine data are not migrated.
This commit is contained in:
1 parent
7c90040924
commit
97410afad9
15 files changed
+656
-206
No files matched your search
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* 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.builtin.spawn.SpawnCooldowns;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
|
||||
import java.util.function.ToLongFunction;
|
||||
|
||||
/** The cooldowns of an engine's spawners, in the data the engine saves with its world. */
|
||||
final class EngineSpawnCooldowns implements SpawnCooldowns {
|
||||
private final Engine engine;
|
||||
|
||||
EngineSpawnCooldowns(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSpawn(String spawner, long intervalMilliseconds) {
|
||||
return engine.getEngineData().getCooldown(spawner).canSpawn(intervalMilliseconds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawned(String spawner) {
|
||||
engine.getEngineData().getCooldown(spawner).spawn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSpawnInChunk(int chunkX, int chunkZ, String spawner, long intervalMilliseconds) {
|
||||
return engine.getEngineData().getChunk(chunkX, chunkZ).getCooldown(spawner).canSpawn(intervalMilliseconds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawnedInChunk(int chunkX, int chunkZ, String spawner) {
|
||||
engine.getEngineData().getChunk(chunkX, chunkZ).getCooldown(spawner).spawn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forgetExpired(ToLongFunction<String> intervalMilliseconds) {
|
||||
engine.getEngineData().cleanup(intervalMilliseconds);
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,6 @@
|
||||
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.builtin.LegacyStreams;
|
||||
import art.arcane.iris.builtin.spawn.SpawnCooldowns;
|
||||
import art.arcane.iris.builtin.structure.StructureHost;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import com.volmit.ModuleLog;
|
||||
@@ -35,17 +33,14 @@ import java.util.function.Function;
|
||||
* One Iris world as modules see it: an engine runtime generating with one snapshot of its pack's resources, and the
|
||||
* state modules keep for it. {@link ModuleWorlds} makes one when the runtime is built and a new one whenever the
|
||||
* pack's resources reload under it. Iris's built-in modules reach what they need of Iris through its
|
||||
* {@linkplain #extension extensions}: the spawners' cooldowns Iris saves with the world, Iris's legacy random streams,
|
||||
* and the structure host.
|
||||
* {@linkplain #extension extensions}: the structure host.
|
||||
*/
|
||||
public final class IrisPackWorld implements PackWorld {
|
||||
private final Engine engine;
|
||||
private final IrisPackResources resources;
|
||||
private final WorldStates states;
|
||||
private final ModuleLog log;
|
||||
private final SpawnCooldowns spawnCooldowns;
|
||||
private volatile RunningWorld running;
|
||||
private final LegacyStreams legacyStreams;
|
||||
private volatile IrisRunningWorld running;
|
||||
private final Placements placements;
|
||||
private volatile IrisStructureHost structureHost;
|
||||
|
||||
@@ -54,8 +49,6 @@ public final class IrisPackWorld implements PackWorld {
|
||||
this.resources = engine.getData().getPackResources();
|
||||
this.states = new WorldStates();
|
||||
this.log = IrisApiLogger.hostLog();
|
||||
this.spawnCooldowns = new EngineSpawnCooldowns(engine);
|
||||
this.legacyStreams = new IrisLegacyStreams(engine.getData(), engine.getSeedManager());
|
||||
this.placements = new IrisPlacements(engine.getData(), engine, () -> this);
|
||||
}
|
||||
|
||||
@@ -92,14 +85,22 @@ public final class IrisPackWorld implements PackWorld {
|
||||
|
||||
@Override
|
||||
public Optional<RunningWorld> running() {
|
||||
RunningWorld known = running;
|
||||
IrisRunningWorld known = running;
|
||||
if (known == null) {
|
||||
known = new IrisRunningWorld(engine);
|
||||
known = new IrisRunningWorld(engine, log);
|
||||
running = known;
|
||||
}
|
||||
return Optional.of(known);
|
||||
}
|
||||
|
||||
/** Writes what modules saved with this world, if any asked for it. */
|
||||
void saveState() {
|
||||
IrisRunningWorld known = running;
|
||||
if (known != null) {
|
||||
known.saveState();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T state(Class<T> type, Function<? super PackWorld, ? extends T> create) {
|
||||
return states.state(type, () -> create.apply(this));
|
||||
@@ -120,12 +121,6 @@ public final class IrisPackWorld implements PackWorld {
|
||||
if (type == Engine.class) {
|
||||
return engine;
|
||||
}
|
||||
if (type == SpawnCooldowns.class) {
|
||||
return spawnCooldowns;
|
||||
}
|
||||
if (type == LegacyStreams.class) {
|
||||
return legacyStreams;
|
||||
}
|
||||
if (type == StructureHost.class) {
|
||||
return structures();
|
||||
}
|
||||
|
||||
@@ -25,11 +25,13 @@ import art.arcane.iris.util.common.scheduling.J;
|
||||
import art.arcane.volmlib.util.mantle.runtime.MantleChunk;
|
||||
import art.arcane.volmlib.util.matter.Matter;
|
||||
import art.arcane.volmlib.util.matter.slices.MarkerMatter;
|
||||
import com.volmit.ModuleLog;
|
||||
import com.volmit.config.FeatureSettings;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.placement.PlacementRecord;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.world.RunningWorld;
|
||||
import com.volmit.world.SavedKey;
|
||||
import com.volmit.world.TerrainSampler;
|
||||
|
||||
import java.util.List;
|
||||
@@ -49,9 +51,21 @@ final class IrisRunningWorld implements RunningWorld {
|
||||
private volatile TerrainSampler layout;
|
||||
private volatile FeatureSettings settings;
|
||||
private volatile Set<ResourceId> platformLootTables;
|
||||
private final SavedWorldState savedState;
|
||||
|
||||
IrisRunningWorld(Engine engine) {
|
||||
IrisRunningWorld(Engine engine, ModuleLog log) {
|
||||
this.engine = engine;
|
||||
this.savedState = new SavedWorldState(() -> engine.getWorld().worldFolder(), log);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T saved(SavedKey<T> key) {
|
||||
return savedState.saved(key);
|
||||
}
|
||||
|
||||
/** Writes the values modules keep with this world; the world calls it when it saves and when it stops. */
|
||||
void saveState() {
|
||||
savedState.saveAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -157,8 +157,20 @@ public final class ModuleWorlds {
|
||||
private void stopCurrentWorld() {
|
||||
LifecycleRunner lifecycles = startedWith;
|
||||
startedWith = null;
|
||||
if (lifecycles != null) {
|
||||
lifecycles.worldStopped(current());
|
||||
try {
|
||||
if (lifecycles != null) {
|
||||
lifecycles.worldStopped(current());
|
||||
}
|
||||
} finally {
|
||||
saveCurrentWorldState();
|
||||
}
|
||||
}
|
||||
|
||||
/** Writes what modules saved with the current world; does nothing when no world was made or none was asked for. */
|
||||
public void saveCurrentWorldState() {
|
||||
IrisPackWorld world = current;
|
||||
if (world != null) {
|
||||
world.saveState();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* 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.lifecycle.VanishedWorldStorage;
|
||||
import com.volmit.ModuleLog;
|
||||
import com.volmit.world.SavedKey;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.AtomicMoveNotSupportedException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* What modules keep with one running world ({@link com.volmit.world.RunningWorld#saved}): the world's one instance per
|
||||
* key, read from {@code <world>/iris/module-data/<namespace>/<path>.bin} when first asked for and written back by
|
||||
* {@link #saveAll()}. A file is the codec's version followed by the codec's payload.
|
||||
*/
|
||||
final class SavedWorldState {
|
||||
private static final String STORAGE_DIRECTORY = "iris/module-data";
|
||||
private static final String FILE_EXTENSION = ".bin";
|
||||
private static final String TEMPORARY_EXTENSION = ".tmp";
|
||||
|
||||
private final Supplier<File> worldFolder;
|
||||
private final ModuleLog log;
|
||||
private final Map<String, Saved<?>> values = new ConcurrentHashMap<>();
|
||||
private final Object loading = new Object();
|
||||
|
||||
SavedWorldState(Supplier<File> worldFolder, ModuleLog log) {
|
||||
this.worldFolder = worldFolder;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
<T> T saved(SavedKey<T> key) {
|
||||
Saved<?> known = values.get(key.id());
|
||||
if (known == null) {
|
||||
known = loadOnce(key);
|
||||
}
|
||||
if (known.key().type() != key.type()) {
|
||||
throw new IllegalArgumentException("Saved state " + key.id() + " is kept as " + known.key().type().getName()
|
||||
+ ", not " + key.type().getName());
|
||||
}
|
||||
return key.type().cast(known.value());
|
||||
}
|
||||
|
||||
/** Writes every value asked for so far; a value that cannot be written is reported and does not stop the others. */
|
||||
void saveAll() {
|
||||
List<Saved<?>> asked = List.copyOf(values.values());
|
||||
if (asked.isEmpty() || VanishedWorldStorage.vanished(worldFolder.get())) {
|
||||
return;
|
||||
}
|
||||
for (Saved<?> saved : asked) {
|
||||
save(saved);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> Saved<?> loadOnce(SavedKey<T> key) {
|
||||
synchronized (loading) {
|
||||
Saved<?> known = values.get(key.id());
|
||||
if (known != null) {
|
||||
return known;
|
||||
}
|
||||
Saved<T> loaded = new Saved<>(key, load(key));
|
||||
values.put(key.id(), loaded);
|
||||
return loaded;
|
||||
}
|
||||
}
|
||||
|
||||
private <T> T load(SavedKey<T> key) {
|
||||
Path file = fileOf(key);
|
||||
byte[] stored;
|
||||
try {
|
||||
stored = Files.readAllBytes(file);
|
||||
} catch (NoSuchFileException none) {
|
||||
return key.initial().get();
|
||||
} catch (IOException unreadable) {
|
||||
log.warn("Saved state " + key.id() + " could not be read from " + file + " and starts over: " + unreadable);
|
||||
return key.initial().get();
|
||||
}
|
||||
try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(stored))) {
|
||||
T value = key.codec().read(input, input.readInt());
|
||||
if (value == null) {
|
||||
throw new IOException("The codec read no value");
|
||||
}
|
||||
return value;
|
||||
} catch (IOException | RuntimeException rejected) {
|
||||
log.warn("Saved state " + key.id() + " in " + file + " was rejected and starts over: " + rejected);
|
||||
return key.initial().get();
|
||||
}
|
||||
}
|
||||
|
||||
private void save(Saved<?> saved) {
|
||||
Path file = fileOf(saved.key());
|
||||
try {
|
||||
writeAtomically(file, saved.encode());
|
||||
} catch (IOException | RuntimeException failure) {
|
||||
log.error("Saved state " + saved.key().id() + " could not be written to " + file, failure);
|
||||
}
|
||||
}
|
||||
|
||||
private Path fileOf(SavedKey<?> key) {
|
||||
String id = key.id();
|
||||
String namespace = id.substring(0, id.indexOf(':'));
|
||||
String path = id.substring(id.indexOf(':') + 1);
|
||||
return worldFolder.get().toPath().resolve(STORAGE_DIRECTORY).resolve(namespace).resolve(path + FILE_EXTENSION);
|
||||
}
|
||||
|
||||
private static void writeAtomically(Path file, byte[] content) throws IOException {
|
||||
Path parent = file.getParent();
|
||||
Files.createDirectories(parent);
|
||||
Path temporary = Files.createTempFile(parent, file.getFileName().toString(), TEMPORARY_EXTENSION);
|
||||
try {
|
||||
Files.write(temporary, content);
|
||||
try {
|
||||
Files.move(temporary, file, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (AtomicMoveNotSupportedException unsupported) {
|
||||
Files.move(temporary, file, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
} finally {
|
||||
Files.deleteIfExists(temporary);
|
||||
}
|
||||
}
|
||||
|
||||
private record Saved<T>(SavedKey<T> key, T value) {
|
||||
byte[] encode() throws IOException {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
try (DataOutputStream output = new DataOutputStream(bytes)) {
|
||||
output.writeInt(key.codec().version());
|
||||
key.codec().write(output, value);
|
||||
}
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,6 +127,7 @@ final class EngineDataStore {
|
||||
}
|
||||
|
||||
void saveEngineData() {
|
||||
saveModuleData();
|
||||
synchronized (engineDataLock) {
|
||||
if (storageVanished()) {
|
||||
return;
|
||||
@@ -144,6 +145,14 @@ final class EngineDataStore {
|
||||
}
|
||||
}
|
||||
|
||||
/** What modules keep with the world is saved wherever the engine saves its own data. */
|
||||
private void saveModuleData() {
|
||||
EngineRuntime runtime = engine.runtime;
|
||||
if (runtime != null) {
|
||||
runtime.generation().saveModuleData();
|
||||
}
|
||||
}
|
||||
|
||||
void releaseEngineData() {
|
||||
IrisData data = engine.getData();
|
||||
data.unregisterEngine(engine);
|
||||
|
||||
@@ -103,6 +103,11 @@ public final class EngineGeneration {
|
||||
return bypass;
|
||||
}
|
||||
|
||||
/** Writes what modules saved with the world, as the engine saves its data; see {@link com.volmit.world.RunningWorld#saved}. */
|
||||
public void saveModuleData() {
|
||||
modules.saveCurrentWorldState();
|
||||
}
|
||||
|
||||
/** Stops the modules' world and closes the state stages kept, such as actuators that hold engine resources. */
|
||||
public void close() {
|
||||
modules.close();
|
||||
|
||||
-36
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2022 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.engine.object.entity.spawn;
|
||||
|
||||
import art.arcane.volmlib.util.math.M;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class IrisEngineSpawnerCooldown {
|
||||
private long lastSpawn = 0;
|
||||
private String spawner;
|
||||
|
||||
public void spawn() {
|
||||
lastSpawn = M.ms();
|
||||
}
|
||||
|
||||
public boolean canSpawn(long intervalMilliseconds) {
|
||||
return M.ms() - lastSpawn > intervalMilliseconds;
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package art.arcane.iris.engine.object.entity.spawn;
|
||||
|
||||
import art.arcane.volmlib.util.collection.KMap;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.util.function.ToLongFunction;
|
||||
|
||||
@EqualsAndHashCode
|
||||
public class IrisSpawnerCooldowns {
|
||||
private final KMap<String, IrisEngineSpawnerCooldown> cooldowns = new KMap<>();
|
||||
|
||||
public IrisEngineSpawnerCooldown getCooldown(@NonNull String loadKey) {
|
||||
return cooldowns.computeIfAbsent(loadKey, k -> {
|
||||
IrisEngineSpawnerCooldown cd = new IrisEngineSpawnerCooldown();
|
||||
cd.setSpawner(loadKey);
|
||||
return cd;
|
||||
});
|
||||
}
|
||||
|
||||
/** Forgets the cooldowns that ran out and those of spawners {@code intervalMilliseconds} does not know (a negative interval). */
|
||||
public void cleanup(ToLongFunction<String> intervalMilliseconds) {
|
||||
cooldowns.values().removeIf(cd -> {
|
||||
long interval = intervalMilliseconds.applyAsLong(cd.getSpawner());
|
||||
return interval < 0 || cd.canSpawn(interval);
|
||||
});
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return cooldowns.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -18,36 +18,10 @@
|
||||
|
||||
package art.arcane.iris.engine.object.runtime;
|
||||
|
||||
import art.arcane.iris.engine.object.entity.spawn.IrisSpawnerCooldowns;
|
||||
import art.arcane.volmlib.util.cache.CacheKey;
|
||||
import art.arcane.volmlib.util.collection.KMap;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.function.ToLongFunction;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class IrisEngineData extends IrisSpawnerCooldowns {
|
||||
public class IrisEngineData {
|
||||
private IrisEngineStatistics statistics = new IrisEngineStatistics();
|
||||
private KMap<Long, IrisSpawnerCooldowns> chunks = new KMap<>();
|
||||
private Long seed = null;
|
||||
|
||||
public void removeChunk(int x, int z) {
|
||||
chunks.remove(CacheKey.key(x, z));
|
||||
}
|
||||
|
||||
public IrisSpawnerCooldowns getChunk(int x, int z) {
|
||||
return chunks.computeIfAbsent(CacheKey.key(x, z), k -> new IrisSpawnerCooldowns());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup(ToLongFunction<String> intervalMilliseconds) {
|
||||
super.cleanup(intervalMilliseconds);
|
||||
|
||||
chunks.values().removeIf(chunk -> {
|
||||
chunk.cleanup(intervalMilliseconds);
|
||||
return chunk.isEmpty();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user