mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 09:15:27 +00:00
Serve the pack snapshot's objects, biomes and regions to chunk, mantle and placement stages through PackResources
This commit is contained in:
1 parent
21efc385da
commit
d2b4904263
14 files changed
+665
-11
No files matched your search
@@ -31,6 +31,7 @@ import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.core.modules.IrisPackResources;
|
||||
import art.arcane.iris.core.modules.ObjectPlacementSettingsReader;
|
||||
import art.arcane.iris.core.modules.PackModuleSet;
|
||||
import art.arcane.iris.core.pack.PackDirectoryResolver;
|
||||
@@ -73,6 +74,8 @@ import art.arcane.iris.util.common.scheduling.J;
|
||||
import art.arcane.iris.util.project.context.IrisContext;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -109,6 +112,9 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
|
||||
private final boolean datapackCompiler;
|
||||
private volatile boolean closed = false;
|
||||
private final PackModuleSet modules;
|
||||
@EqualsAndHashCode.Exclude
|
||||
@ToString.Exclude
|
||||
private volatile IrisPackResources packResources;
|
||||
private ResourceLoader<IrisBiome> biomeLoader;
|
||||
private ResourceLoader<IrisLootTable> lootLoader;
|
||||
private ResourceLoader<IrisRegion> regionLoader;
|
||||
@@ -505,6 +511,7 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
|
||||
|
||||
gson = builder.create();
|
||||
loaders = replacement;
|
||||
packResources = new IrisPackResources(this);
|
||||
|
||||
for (Engine engine : getEngines()) {
|
||||
engine.hotload();
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import com.volmit.block.BlockFace;
|
||||
import com.volmit.block.BlockRegistry;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.block.BlockTrait;
|
||||
import com.volmit.resource.ResourceId;
|
||||
|
||||
/**
|
||||
* IrisApi's view of the bound platform's blocks. States convert through their keys, as {@link PlatformBlockStates}
|
||||
* does; the platform decides which blocks exist, so a key it does not know fails. Traits answer from what the
|
||||
* platform's block states can tell. Three traits ({@code GRAVITY}, {@code LOG}, {@code LEAVES}), tags and
|
||||
* attachment to a face are not offered by the platform layer yet, and asking for them throws
|
||||
* {@link UnsupportedOperationException} rather than guessing.
|
||||
*/
|
||||
final class IrisBlockRegistry implements BlockRegistry {
|
||||
@Override
|
||||
public BlockState resolve(String key) {
|
||||
return BlockState.parse(PlatformBlockStates.namespaced(platformState(key).key()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTrait(BlockState state, BlockTrait trait) {
|
||||
PlatformBlockState platform = platformState(state.key());
|
||||
return switch (trait) {
|
||||
case AIR -> platform.isAir();
|
||||
case SOLID -> platform.isSolid();
|
||||
case OCCLUDING -> platform.isOccluding();
|
||||
case FLUID -> platform.isFluid();
|
||||
case WATER -> platform.isWater();
|
||||
case FOLIAGE -> platform.isFoliage();
|
||||
case DECORATION -> platform.isDecorant();
|
||||
case ORE -> platform.isOre();
|
||||
case INVENTORY -> platform.isStorage();
|
||||
case BLOCK_ENTITY -> platform.hasTileEntity();
|
||||
case CUSTOM -> platform.isCustom();
|
||||
case NEEDS_UPDATE -> platform.isUpdatable();
|
||||
case GRAVITY, LOG, LEAVES -> throw unsupported("The " + trait + " trait");
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean inTag(BlockState state, ResourceId tag) {
|
||||
throw unsupported("Block tags");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAttach(BlockState state, BlockState support, BlockFace face) {
|
||||
throw unsupported("Attachment to a face");
|
||||
}
|
||||
|
||||
private static PlatformBlockState platformState(String key) {
|
||||
PlatformBlockState state = IrisPlatforms.get().registries().blockOrNull(PlatformBlockStates.namespaced(key), false);
|
||||
if (state == null) {
|
||||
throw new IllegalArgumentException("Unknown block: " + key);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
private static UnsupportedOperationException unsupported(String what) {
|
||||
return new UnsupportedOperationException(what + " cannot be answered yet: Iris's platform layer does not offer it");
|
||||
}
|
||||
}
|
||||
@@ -33,11 +33,9 @@ import java.util.Collection;
|
||||
/**
|
||||
* One chunk of an Iris engine as an IrisApi chunk invocation: the chunk's block hunk, Iris's terrain
|
||||
* streams, the pack's module settings and the chunk's mantle, which IrisApi gives chunk stages read-only.
|
||||
* Pack resources are not bridged yet; a stage that asks for them fails the chunk with an error saying so,
|
||||
* rather than seeing an empty pack.
|
||||
* Pack resources are the pack snapshot's, shared by every invocation of it.
|
||||
*/
|
||||
final class IrisGenerationContext implements GenerationContext {
|
||||
private static final String NOT_BRIDGED = " is not bridged to Iris yet; modules cannot use it until a built-in port needs it";
|
||||
/** Chunk stages write only their own chunk. */
|
||||
private static final int CHUNK_STAGE_REACH = 0;
|
||||
private static final int CHUNK_WIDTH = 16;
|
||||
@@ -110,6 +108,6 @@ final class IrisGenerationContext implements GenerationContext {
|
||||
|
||||
@Override
|
||||
public PackResources resources() {
|
||||
throw new UnsupportedOperationException("Pack resources for module stages" + NOT_BRIDGED);
|
||||
return engine.getData().getPackResources();
|
||||
}
|
||||
}
|
||||
@@ -31,8 +31,8 @@ import java.util.Collection;
|
||||
|
||||
/**
|
||||
* One chunk of an Iris engine as an IrisApi mantle invocation: the mantle around the chunk as far as the
|
||||
* pipeline reaches, Iris's terrain streams over the same area and the pack's module settings. Pack resources
|
||||
* are not bridged yet; a stage that asks for them fails with an error saying so.
|
||||
* pipeline reaches, Iris's terrain streams over the same area, the pack's module settings and the pack
|
||||
* snapshot's resources.
|
||||
*/
|
||||
final class IrisMantleGenerationContext implements MantleGenerationContext {
|
||||
private static final int CHUNK_SHIFT = 4;
|
||||
@@ -90,7 +90,6 @@ final class IrisMantleGenerationContext implements MantleGenerationContext {
|
||||
|
||||
@Override
|
||||
public PackResources resources() {
|
||||
throw new UnsupportedOperationException("Pack resources for module stages are not bridged to Iris yet;"
|
||||
+ " modules cannot use them until a built-in port needs them");
|
||||
return engine.getData().getPackResources();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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.core.loader.IrisRegistrant;
|
||||
import art.arcane.iris.core.loader.ResourceLoader;
|
||||
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 com.volmit.block.BlockRegistry;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.resource.ResourceKey;
|
||||
import com.volmit.world.PackBiome;
|
||||
import com.volmit.world.PackRegion;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* The resources of one loaded snapshot of an Iris pack, which every chunk, mantle and placement stage of that
|
||||
* snapshot shares. Each load or hotload of the pack's data makes a new one, with a new {@link #snapshotId()}.
|
||||
* <p>
|
||||
* Three kinds of resource are served, by the type the key asks for: {@link GeneratedObject} (a pack object, as
|
||||
* its geometry), {@link PackBiome} and {@link PackRegion}. All use the id {@code <pack namespace>:<key>} that
|
||||
* {@link PackResourceIds} gives the pack's biomes in a biome sample, so one id can name a biome and a region.
|
||||
* An id the pack lacks, or of another namespace, is absent; any other type fails with
|
||||
* {@link IllegalArgumentException}. A resource that exists but cannot be loaded fails with
|
||||
* {@link IllegalStateException}. Each resource is built once and kept for the snapshot.
|
||||
*/
|
||||
public final class IrisPackResources implements PackResources {
|
||||
private static final String SERVED_TYPES = "GeneratedObject, PackBiome and PackRegion";
|
||||
|
||||
private final PackResourceIds ids;
|
||||
private final UUID snapshotId = UUID.randomUUID();
|
||||
private final BlockRegistry blocks = new IrisBlockRegistry();
|
||||
private final Map<Class<?>, Kind<?, ?>> kinds;
|
||||
|
||||
public IrisPackResources(IrisData data) {
|
||||
this.ids = new PackResourceIds(data.getDataFolder());
|
||||
this.kinds = Map.of(
|
||||
GeneratedObject.class, new Kind<IrisObject, GeneratedObject>(ids, data::getObjectLoader, (id, object) -> object.getGeometry()),
|
||||
PackBiome.class, new Kind<IrisBiome, PackBiome>(ids, data::getBiomeLoader, this::biomeOf),
|
||||
PackRegion.class, new Kind<IrisRegion, PackRegion>(ids, data::getRegionLoader, this::regionOf));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceId packId() {
|
||||
return ids.packId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID snapshotId() {
|
||||
return snapshotId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockRegistry blocks() {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked") // The kinds are keyed by the type they serve.
|
||||
public <T> Optional<T> find(ResourceKey<T> key) {
|
||||
Kind<?, T> kind = (Kind<?, T>) kinds.get(key.type());
|
||||
if (kind == null) {
|
||||
throw new IllegalArgumentException("Iris serves pack resources of " + SERVED_TYPES + ", not " + key.type().getName());
|
||||
}
|
||||
return kind.find(key.id());
|
||||
}
|
||||
|
||||
private PackBiome biomeOf(ResourceId id, IrisBiome biome) {
|
||||
return new PackBiome(id, biome.getName(), ResourceId.parse(biome.getDerivativeKey()), biome.getRarity(), idsOf(biome.getChildren()));
|
||||
}
|
||||
|
||||
private PackRegion regionOf(ResourceId id, IrisRegion region) {
|
||||
return new PackRegion(id, region.getName(), region.getRarity(), idsOf(region.getLandBiomes()), idsOf(region.getSeaBiomes()),
|
||||
idsOf(region.getShoreBiomes()), idsOf(region.getCaveBiomes()));
|
||||
}
|
||||
|
||||
private List<ResourceId> idsOf(Collection<String> keys) {
|
||||
return keys.stream().map(ids::of).toList();
|
||||
}
|
||||
|
||||
/** One kind of pack resource: the loader that serves its keys, an index from ids to keys and what was built so far. */
|
||||
private static final class Kind<R extends IrisRegistrant, T> {
|
||||
private final PackResourceIds ids;
|
||||
private final Supplier<ResourceLoader<R>> loader;
|
||||
private final BiFunction<ResourceId, R, T> conversion;
|
||||
private final Map<ResourceId, T> built = new ConcurrentHashMap<>();
|
||||
private volatile Map<ResourceId, String> keysById;
|
||||
|
||||
Kind(PackResourceIds ids, Supplier<ResourceLoader<R>> loader, BiFunction<ResourceId, R, T> conversion) {
|
||||
this.ids = ids;
|
||||
this.loader = loader;
|
||||
this.conversion = conversion;
|
||||
}
|
||||
|
||||
Optional<T> find(ResourceId id) {
|
||||
T cached = built.get(id);
|
||||
if (cached != null) {
|
||||
return Optional.of(cached);
|
||||
}
|
||||
String key = keysById().get(id);
|
||||
if (key == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
R resource = loader.get().load(key);
|
||||
if (resource == null) {
|
||||
throw new IllegalStateException("Pack resource " + id + " exists but failed to load");
|
||||
}
|
||||
T made = conversion.apply(id, resource);
|
||||
T raced = built.putIfAbsent(id, made);
|
||||
return Optional.of(raced != null ? raced : made);
|
||||
}
|
||||
|
||||
/** The pack's keys by the ids they get, built on first use; a key whose id another key already has is left out. */
|
||||
private Map<ResourceId, String> keysById() {
|
||||
Map<ResourceId, String> index = keysById;
|
||||
if (index != null) {
|
||||
return index;
|
||||
}
|
||||
Map<ResourceId, String> indexed = new HashMap<>();
|
||||
for (String key : loader.get().getPossibleKeys()) {
|
||||
indexed.putIfAbsent(ids.of(key), key);
|
||||
}
|
||||
keysById = Map.copyOf(indexed);
|
||||
return keysById;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,8 +36,10 @@ final class IrisPlacementInputs implements PlacementContext {
|
||||
private final TerrainSampler terrain;
|
||||
private final FeatureSettings settings;
|
||||
private final BlockView blocks;
|
||||
private final PackResources resources;
|
||||
|
||||
IrisPlacementInputs(long worldSeed, TerrainSampler terrain, FeatureSettings settings, BlockView blocks) {
|
||||
IrisPlacementInputs(long worldSeed, TerrainSampler terrain, FeatureSettings settings, BlockView blocks, PackResources resources) {
|
||||
this.resources = resources;
|
||||
this.worldSeed = worldSeed;
|
||||
this.terrain = terrain;
|
||||
this.settings = settings;
|
||||
@@ -95,6 +97,6 @@ final class IrisPlacementInputs implements PlacementContext {
|
||||
|
||||
@Override
|
||||
public PackResources resources() {
|
||||
throw new UnsupportedOperationException("Pack resources for placement stages" + NOT_BRIDGED);
|
||||
return resources;
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ public final class ModuleObjectPlacement {
|
||||
geometry, new BlockPosition(x, requestedY, z));
|
||||
IrisPlacementInputs inputs = new IrisPlacementInputs(worldSeed, terrain(engine, ids, area),
|
||||
new PlacementModuleSettings(worldSettings(engine, ids), config.getModuleSettings(), source),
|
||||
new PlacerBlockView(placer, area, states));
|
||||
new PlacerBlockView(placer, area, states), data.getPackResources());
|
||||
long seed = ModuleSeeds.placement(worldSeed, objectKey, x, requestedY, z);
|
||||
PlacementOutcome outcome = placement.runBound(() -> new PlacementRunner(graph)
|
||||
.place(request, edit, inputs, seed, config.getModuleSettings().placement(source)));
|
||||
|
||||
@@ -35,6 +35,7 @@ final class PackResourceIds {
|
||||
private static final Pattern NOT_PATH = Pattern.compile("[^a-z0-9_./-]");
|
||||
private static final String REPLACEMENT = "_";
|
||||
private static final String UNNAMED_PACK = "pack";
|
||||
private static final String PACK_PATH = "pack";
|
||||
|
||||
private final String namespace;
|
||||
|
||||
@@ -43,6 +44,16 @@ final class PackResourceIds {
|
||||
this.namespace = name.isEmpty() ? UNNAMED_PACK : name;
|
||||
}
|
||||
|
||||
/** The namespace of every id this pack's resources have. */
|
||||
String namespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
/** The pack itself: its namespace with the path {@code pack}. */
|
||||
ResourceId packId() {
|
||||
return new ResourceId(namespace, PACK_PATH);
|
||||
}
|
||||
|
||||
/** {@code key} of a resource whose type its use makes clear, such as a biome in a biome sample. */
|
||||
ResourceId of(String key) {
|
||||
return new ResourceId(namespace, NOT_PATH.matcher(key.toLowerCase(Locale.ROOT)).replaceAll(REPLACEMENT));
|
||||
|
||||
Reference in new issue
Block a user