mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Pin persisted enum codes
- Give HydrologyFeatureType, ReachStyle and PondKind explicit codes equal to their old ordinals - Add PersistedCodes lookup tables that reject duplicate, out-of-range and unknown codes - Write and read codes in HydrologyPlanCodec; unknown codes decode as malformed plans - Write and read codes in GenerationSemanticIndex shard records and region summaries - Order summary river types and the river feature comparator by code - Pin the code tables and a captured plan byte fixture in tests
This commit is contained in:
1 parent
f6fbf9147f
commit
9c95e7901c
10 files changed
+351
-44
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package art.arcane.iris.generation.hydrology;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.ToIntFunction;
|
||||
|
||||
public final class PersistedCodes<E extends Enum<E>> {
|
||||
private static final int MAX_CODE = 255;
|
||||
|
||||
private final String type;
|
||||
private final E[] byCode;
|
||||
|
||||
private PersistedCodes(String type, E[] byCode) {
|
||||
this.type = type;
|
||||
this.byCode = byCode;
|
||||
}
|
||||
|
||||
public static <E extends Enum<E>> PersistedCodes<E> of(Class<E> type, ToIntFunction<E> code) {
|
||||
E[] constants = type.getEnumConstants();
|
||||
int size = 0;
|
||||
for (E constant : constants) {
|
||||
int value = code.applyAsInt(constant);
|
||||
if (value < 0 || value > MAX_CODE) {
|
||||
throw new IllegalStateException(type.getSimpleName() + "." + constant.name() + " has code " + value
|
||||
+ ", outside 0.." + MAX_CODE);
|
||||
}
|
||||
size = Math.max(size, value + 1);
|
||||
}
|
||||
E[] byCode = Arrays.copyOf(constants, size);
|
||||
Arrays.fill(byCode, null);
|
||||
for (E constant : constants) {
|
||||
int value = code.applyAsInt(constant);
|
||||
if (byCode[value] != null) {
|
||||
throw new IllegalStateException(type.getSimpleName() + "." + constant.name() + " reuses code " + value + " of "
|
||||
+ type.getSimpleName() + "." + byCode[value].name());
|
||||
}
|
||||
byCode[value] = constant;
|
||||
}
|
||||
return new PersistedCodes<>(type.getSimpleName(), byCode);
|
||||
}
|
||||
|
||||
public E decode(int code) {
|
||||
E constant = code >= 0 && code < byCode.length ? byCode[code] : null;
|
||||
if (constant == null) {
|
||||
throw new IllegalStateException("Unknown " + type + " code " + code);
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
}
|
||||
+5
-7
@@ -28,8 +28,6 @@ public final class HydrologyPlanCodec {
|
||||
private static final int POND_BYTES = 2 * Long.BYTES + 1 + 15 * Integer.BYTES;
|
||||
private static final int STATION_BYTES = 5 * Integer.BYTES;
|
||||
private static final int INLET_BYTES = Long.BYTES + 5 * Integer.BYTES + 2 * STATION_BYTES;
|
||||
private static final ReachStyle[] STYLES = ReachStyle.values();
|
||||
private static final PondKind[] KINDS = PondKind.values();
|
||||
|
||||
private HydrologyPlanCodec() {
|
||||
}
|
||||
@@ -102,13 +100,13 @@ public final class HydrologyPlanCodec {
|
||||
writeFloats(out, reach.halfWidth());
|
||||
writeFloats(out, reach.depth());
|
||||
for (ReachStyle style : reach.style()) {
|
||||
out.writeByte(style.ordinal());
|
||||
out.writeByte(style.code());
|
||||
}
|
||||
}
|
||||
|
||||
private static void writePond(DataOutputStream out, Pond pond) throws IOException {
|
||||
out.writeLong(pond.id());
|
||||
out.writeByte(pond.kind().ordinal());
|
||||
out.writeByte(pond.kind().code());
|
||||
out.writeInt(pond.level());
|
||||
out.writeInt(pond.floor());
|
||||
out.writeInt(pond.spillX());
|
||||
@@ -190,7 +188,7 @@ public final class HydrologyPlanCodec {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(new Body(x, z, reaches, ponds, inlets));
|
||||
} catch (BufferUnderflowException | IllegalArgumentException malformed) {
|
||||
} catch (BufferUnderflowException | IllegalArgumentException | IllegalStateException malformed) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -211,14 +209,14 @@ public final class HydrologyPlanCodec {
|
||||
float[] depth = readFloats(in, points);
|
||||
ReachStyle[] style = new ReachStyle[points];
|
||||
for (int point = 0; point < points; point++) {
|
||||
style[point] = STYLES[index(in.get(), STYLES.length)];
|
||||
style[point] = ReachStyle.fromCode(Byte.toUnsignedInt(in.get()));
|
||||
}
|
||||
return new Reach(id, tier, downstreamId, xs, zs, grade, halfWidth, depth, style, catchment);
|
||||
}
|
||||
|
||||
private static Pond readPond(ByteBuffer in) {
|
||||
long id = in.getLong();
|
||||
PondKind kind = KINDS[index(in.get(), KINDS.length)];
|
||||
PondKind kind = PondKind.fromCode(Byte.toUnsignedInt(in.get()));
|
||||
int level = in.getInt();
|
||||
int floor = in.getInt();
|
||||
int spillX = in.getInt();
|
||||
|
||||
+29
-11
@@ -1,23 +1,41 @@
|
||||
package art.arcane.iris.generation.hydrology.query;
|
||||
|
||||
import art.arcane.iris.generation.hydrology.PersistedCodes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public enum HydrologyFeatureType {
|
||||
STREAM,
|
||||
RIVER,
|
||||
MAJOR_RIVER,
|
||||
CASCADE,
|
||||
WATERFALL,
|
||||
MOUTH,
|
||||
POND,
|
||||
LAKE,
|
||||
DAM,
|
||||
SEA_INLET,
|
||||
CAVE_POOL;
|
||||
STREAM(0),
|
||||
RIVER(1),
|
||||
MAJOR_RIVER(2),
|
||||
CASCADE(3),
|
||||
WATERFALL(4),
|
||||
MOUTH(5),
|
||||
POND(6),
|
||||
LAKE(7),
|
||||
DAM(8),
|
||||
SEA_INLET(9),
|
||||
CAVE_POOL(10);
|
||||
|
||||
private static final List<String> KEYS = keyList();
|
||||
private static final PersistedCodes<HydrologyFeatureType> CODES =
|
||||
PersistedCodes.of(HydrologyFeatureType.class, HydrologyFeatureType::code);
|
||||
|
||||
private final int code;
|
||||
|
||||
HydrologyFeatureType(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int code() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public static HydrologyFeatureType fromCode(int code) {
|
||||
return CODES.decode(code);
|
||||
}
|
||||
|
||||
public String key() {
|
||||
return name().toLowerCase(Locale.ROOT);
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
package art.arcane.iris.generation.hydrology.water;
|
||||
|
||||
import art.arcane.iris.generation.hydrology.PersistedCodes;
|
||||
|
||||
public enum PondKind {
|
||||
NATURAL,
|
||||
SEEDED,
|
||||
CHAINED,
|
||||
LAKE
|
||||
NATURAL(0),
|
||||
SEEDED(1),
|
||||
CHAINED(2),
|
||||
LAKE(3);
|
||||
|
||||
private static final PersistedCodes<PondKind> CODES = PersistedCodes.of(PondKind.class, PondKind::code);
|
||||
|
||||
private final int code;
|
||||
|
||||
PondKind(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int code() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public static PondKind fromCode(int code) {
|
||||
return CODES.decode(code);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,28 @@
|
||||
package art.arcane.iris.generation.hydrology.water;
|
||||
|
||||
import art.arcane.iris.generation.hydrology.PersistedCodes;
|
||||
|
||||
public enum ReachStyle {
|
||||
RUN,
|
||||
RIFFLE,
|
||||
CASCADE,
|
||||
WATERFALL,
|
||||
POND_OUTLET,
|
||||
MOUTH
|
||||
RUN(0),
|
||||
RIFFLE(1),
|
||||
CASCADE(2),
|
||||
WATERFALL(3),
|
||||
POND_OUTLET(4),
|
||||
MOUTH(5);
|
||||
|
||||
private static final PersistedCodes<ReachStyle> CODES = PersistedCodes.of(ReachStyle.class, ReachStyle::code);
|
||||
|
||||
private final int code;
|
||||
|
||||
ReachStyle(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int code() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public static ReachStyle fromCode(int code) {
|
||||
return CODES.decode(code);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public final class ChunkGenerationSemantics {
|
||||
.thenComparingInt(point -> point.position().y());
|
||||
private static final Comparator<RiverFeatureOccurrence> RIVER_FEATURE_COMPARATOR = Comparator
|
||||
.comparing(RiverFeatureOccurrence::profileKey)
|
||||
.thenComparing(RiverFeatureOccurrence::type)
|
||||
.thenComparingInt(occurrence -> occurrence.type().code())
|
||||
.thenComparingInt(occurrence -> occurrence.position().x())
|
||||
.thenComparingInt(occurrence -> occurrence.position().z())
|
||||
.thenComparingInt(occurrence -> occurrence.position().y())
|
||||
|
||||
@@ -1750,14 +1750,14 @@ public final class GenerationSemanticIndex {
|
||||
} else if (riverTypeCount > HydrologyFeatureType.values().length) {
|
||||
throw invalid(source, "invalid summary river type count " + riverTypeCount);
|
||||
}
|
||||
int previousOrdinal = -1;
|
||||
int previousCode = -1;
|
||||
for (int index = 0; index < riverTypeCount; index++) {
|
||||
int ordinal = input.readUnsignedByte();
|
||||
if (ordinal <= previousOrdinal || ordinal >= HydrologyFeatureType.values().length) {
|
||||
throw invalid(source, "invalid summary river type ordinal " + ordinal);
|
||||
int code = input.readUnsignedByte();
|
||||
if (code <= previousCode) {
|
||||
throw invalid(source, "summary river type codes are not in canonical order");
|
||||
}
|
||||
riverTypes.add(HydrologyFeatureType.values()[ordinal]);
|
||||
previousOrdinal = ordinal;
|
||||
riverTypes.add(RegionShard.riverFeatureType(source, code));
|
||||
previousCode = code;
|
||||
}
|
||||
LongOpenHashSet activations = readActivations(source, input, "summary activation");
|
||||
LongOpenHashSet sealedActivations = readActivations(source, input, "sealed summary activation");
|
||||
@@ -1791,8 +1791,8 @@ public final class GenerationSemanticIndex {
|
||||
}
|
||||
}
|
||||
output.writeByte(riverTypes.size());
|
||||
for (HydrologyFeatureType type : riverTypes) {
|
||||
output.writeByte(type.ordinal());
|
||||
for (int code : riverTypes.stream().mapToInt(HydrologyFeatureType::code).sorted().toArray()) {
|
||||
output.writeByte(code);
|
||||
}
|
||||
writeActivations(output, activations);
|
||||
writeActivations(output, sealedActivations);
|
||||
@@ -2707,7 +2707,7 @@ public final class GenerationSemanticIndex {
|
||||
output.writeShort(semantics.riverFeatures().size());
|
||||
for (ChunkGenerationSemantics.RiverFeatureOccurrence occurrence : semantics.riverFeatures()) {
|
||||
output.writeShort(paletteIndexes.get(occurrence.profileKey()));
|
||||
output.writeByte(occurrence.type().ordinal());
|
||||
output.writeByte(occurrence.type().code());
|
||||
output.writeLong(occurrence.featureId());
|
||||
output.writeInt(occurrence.position().x());
|
||||
output.writeInt(occurrence.position().y());
|
||||
@@ -2916,21 +2916,17 @@ public final class GenerationSemanticIndex {
|
||||
return;
|
||||
}
|
||||
ChunkGenerationSemantics.RiverFeatureOccurrence previous = null;
|
||||
HydrologyFeatureType[] types = HydrologyFeatureType.values();
|
||||
for (int index = 0; index < count; index++) {
|
||||
int paletteIndex = input.readUnsignedShort();
|
||||
if (paletteIndex >= palette.length) {
|
||||
throw invalid(file, "river profile key palette index is out of range");
|
||||
}
|
||||
int typeOrdinal = input.readUnsignedByte();
|
||||
if (typeOrdinal >= types.length) {
|
||||
throw invalid(file, "river feature type ordinal is out of range");
|
||||
}
|
||||
HydrologyFeatureType type = riverFeatureType(file, input.readUnsignedByte());
|
||||
paletteUsed[paletteIndex] = true;
|
||||
ChunkGenerationSemantics.RiverFeatureOccurrence occurrence =
|
||||
new ChunkGenerationSemantics.RiverFeatureOccurrence(
|
||||
palette[paletteIndex],
|
||||
types[typeOrdinal],
|
||||
type,
|
||||
input.readLong(),
|
||||
new ChunkGenerationSemantics.BlockPosition(
|
||||
input.readInt(),
|
||||
@@ -2948,6 +2944,14 @@ public final class GenerationSemanticIndex {
|
||||
}
|
||||
}
|
||||
|
||||
private static HydrologyFeatureType riverFeatureType(Path file, int code) throws IOException {
|
||||
try {
|
||||
return HydrologyFeatureType.fromCode(code);
|
||||
} catch (IllegalStateException unknown) {
|
||||
throw invalid(file, unknown.getMessage(), unknown);
|
||||
}
|
||||
}
|
||||
|
||||
private static void skipLegacyRiverFeatures(
|
||||
Path file,
|
||||
DataInputStream input,
|
||||
|
||||
Reference in new issue
Block a user