Skip to content

Commit 9d0ee86

Browse files
committed
refactor SlimeSerializer
1 parent f9d1919 commit 9d0ee86

File tree

3 files changed

+81
-78
lines changed

3 files changed

+81
-78
lines changed

slime-loader/src/main/java/net/roxymc/slime/serializer/SlimeSerializer.java

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.kyori.adventure.nbt.ListBinaryTag;
1212
import net.roxymc.slime.CompoundBinaryTagHolder;
1313
import net.roxymc.slime.loader.SlimeLoader;
14+
import net.roxymc.slime.util.function.IOBiConsumer;
1415
import net.roxymc.slime.util.function.IOBiFunction;
1516
import net.roxymc.slime.util.function.IOConsumer;
1617
import net.roxymc.slime.util.function.IOFunction;
@@ -32,10 +33,8 @@ public SlimeSerializer(SlimeLoader loader) {
3233
public static SlimeSerializer forVersion(SlimeLoader loader, int version) {
3334
Preconditions.checkArgument(
3435
version >= 12,
35-
String.format(
36-
"Serializers below version 12 (%s) are not supported. See: https://github.com/roxymc-net/SlimeLoader#legacy-slime-versions",
37-
version
38-
)
36+
"Serializers below version 12 (%s) are not supported. See: https://github.com/roxymc-net/SlimeLoader#legacy-slime-versions",
37+
version
3938
);
4039

4140
return switch (version) {
@@ -51,7 +50,7 @@ public void serialize(World world, ByteArrayDataOutput out) throws IOException {
5150
out.writeInt(world.version());
5251
}
5352

54-
protected void serializeCompressed(byte[] bytes, ByteArrayDataOutput out) {
53+
protected void writeCompressed(byte[] bytes, ByteArrayDataOutput out) {
5554
byte[] compressed = Zstd.compress(bytes);
5655

5756
out.writeInt(compressed.length);
@@ -60,32 +59,44 @@ protected void serializeCompressed(byte[] bytes, ByteArrayDataOutput out) {
6059
out.write(compressed);
6160
}
6261

63-
protected void serializeCompressed(IOConsumer<ByteArrayDataOutput> consumer, ByteArrayDataOutput out) throws IOException {
64-
ByteArrayDataOutput tempOut = ByteStreams.newDataOutput();
65-
consumer.accept(tempOut);
62+
protected void writeCompressed(IOConsumer<ByteArrayDataOutput> consumer, ByteArrayDataOutput out) throws IOException {
63+
ByteArrayDataOutput output = ByteStreams.newDataOutput();
64+
consumer.accept(output);
6665

67-
serializeCompressed(tempOut.toByteArray(), out);
66+
writeCompressed(output.toByteArray(), out);
6867
}
6968

70-
protected void serializeCompound(CompoundBinaryTag tag, ByteArrayDataOutput out) throws IOException {
71-
serializeCompound(tag, out, true);
69+
protected <T> void writeCompressed(T value, IOBiConsumer<T, ByteArrayDataOutput> consumer, ByteArrayDataOutput out) throws IOException {
70+
writeCompressed(output -> consumer.accept(value, output), out);
7271
}
7372

74-
protected void serializeCompound(CompoundBinaryTag tag, ByteArrayDataOutput out, boolean writeLength) throws IOException {
75-
ByteArrayDataOutput tagOut = ByteStreams.newDataOutput();
76-
BinaryTagIO.writer().write(tag, tagOut);
73+
protected <T> void writeArray(T[] array, ByteArrayDataOutput out, IOBiConsumer<T, ByteArrayDataOutput> serializer) throws IOException {
74+
out.writeInt(array.length);
7775

78-
byte[] bytes = tagOut.toByteArray();
79-
80-
if (writeLength) {
81-
out.writeInt(bytes.length);
76+
for (T element : array) {
77+
serializer.accept(element, out);
8278
}
79+
}
80+
81+
protected void writeCompound(CompoundBinaryTag tag, ByteArrayDataOutput out) throws IOException {
82+
ByteArrayDataOutput output = ByteStreams.newDataOutput();
83+
writeRawCompound(tag, output);
84+
85+
byte[] bytes = output.toByteArray();
8386

87+
out.writeInt(bytes.length);
8488
out.write(bytes);
8589
}
8690

87-
protected <T extends CompoundBinaryTagHolder> void serializeCompoundList(T[] array, ByteArrayDataOutput out, String key) throws IOException {
91+
protected void writeRawCompound(CompoundBinaryTag tag, ByteArrayDataOutput out) throws IOException {
92+
if (!tag.isEmpty()) {
93+
BinaryTagIO.writer().write(tag, out);
94+
}
95+
}
96+
97+
protected <T extends CompoundBinaryTagHolder> void writeCompoundArray(T[] array, ByteArrayDataOutput out, String key) throws IOException {
8898
ListBinaryTag.Builder<CompoundBinaryTag> builder = ListBinaryTag.builder(BinaryTagTypes.COMPOUND);
99+
89100
for (T element : array) {
90101
builder.add(element.tag());
91102
}
@@ -94,12 +105,12 @@ protected <T extends CompoundBinaryTagHolder> void serializeCompoundList(T[] arr
94105
.put(key, builder.build())
95106
.build();
96107

97-
serializeCompound(tag, out);
108+
writeCompound(tag, out);
98109
}
99110

100111
public abstract World deserialize(ByteArrayDataInput in) throws IOException;
101112

102-
protected byte[] deserializeCompressed(ByteArrayDataInput in) {
113+
protected byte[] readCompressed(ByteArrayDataInput in) {
103114
int compressedLength = in.readInt();
104115
int length = in.readInt();
105116

@@ -109,37 +120,43 @@ protected byte[] deserializeCompressed(ByteArrayDataInput in) {
109120
return Zstd.decompress(compressed, length);
110121
}
111122

112-
protected <T> T deserializeCompressed(IOFunction<ByteArrayDataInput, T> function, ByteArrayDataInput in) throws IOException {
113-
return deserializeCompressed((length, dataIn) -> function.apply(dataIn), in);
123+
protected <T> T readCompressed(IOFunction<ByteArrayDataInput, T> function, ByteArrayDataInput in) throws IOException {
124+
return readCompressed((length, input) -> function.apply(input), in);
114125
}
115126

116-
protected <T> T deserializeCompressed(IOBiFunction<Integer, ByteArrayDataInput, T> function, ByteArrayDataInput in) throws IOException {
117-
byte[] bytes = deserializeCompressed(in);
127+
protected <T> T readCompressed(IOBiFunction<Integer, ByteArrayDataInput, T> function, ByteArrayDataInput in) throws IOException {
128+
byte[] bytes = readCompressed(in);
118129

119130
return function.apply(bytes.length, ByteStreams.newDataInput(bytes));
120131
}
121132

122-
protected CompoundBinaryTag deserializeCompound(ByteArrayDataInput in) throws IOException {
133+
protected <T> T[] readArray(
134+
IntFunction<T[]> arrayBuilder, ByteArrayDataInput in, IOFunction<ByteArrayDataInput, T> deserializer
135+
) throws IOException {
123136
int length = in.readInt();
124137

125-
return deserializeCompound(length, in);
138+
T[] array = arrayBuilder.apply(length);
139+
for (int i = 0; i < length; i++) {
140+
array[i] = deserializer.apply(in);
141+
}
142+
143+
return array;
126144
}
127145

128-
protected CompoundBinaryTag deserializeCompound(int length, ByteArrayDataInput in) throws IOException {
129-
if (length == 0) {
130-
return CompoundBinaryTag.empty();
131-
}
146+
protected CompoundBinaryTag readCompound(ByteArrayDataInput in) throws IOException {
147+
int length = in.readInt();
132148

133-
byte[] bytes = new byte[length];
134-
in.readFully(bytes);
149+
return readRawCompound(length, in);
150+
}
135151

136-
return BinaryTagIO.reader().read(ByteStreams.newDataInput(bytes));
152+
protected CompoundBinaryTag readRawCompound(int length, ByteArrayDataInput in) throws IOException {
153+
return length == 0 ? CompoundBinaryTag.empty() : BinaryTagIO.reader().read(in);
137154
}
138155

139-
protected <T extends CompoundBinaryTagHolder> T[] deserializeCompoundList(
156+
protected <T extends CompoundBinaryTagHolder> T[] readCompoundArray(
140157
IntFunction<T[]> arrayBuilder, ByteArrayDataInput in, String key, Function<CompoundBinaryTag, T> deserializer
141158
) throws IOException {
142-
CompoundBinaryTag tag = deserializeCompound(in);
159+
CompoundBinaryTag tag = readCompound(in);
143160
ListBinaryTag list = tag.getList(key, BinaryTagTypes.COMPOUND);
144161

145162
T[] array = arrayBuilder.apply(list.size());

slime-loader/src/main/java/net/roxymc/slime/serializer/SlimeSerializer_v12.java

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ public byte version() {
3636
public void serialize(World world, ByteArrayDataOutput out) throws IOException {
3737
super.serialize(world, out);
3838

39-
serializeCompressed(chunksOut -> serializeChunks(world.chunks(), chunksOut), out);
40-
serializeCompressed(dataOut -> serializeCompound(world.tag(), dataOut, false), out);
39+
writeCompressed(world.chunks(), this::serializeChunks, out);
40+
writeCompressed(world.tag(), this::writeRawCompound, out);
4141
}
4242

4343
protected void serializeChunks(Chunk[] chunks, ByteArrayDataOutput out) throws IOException {
44-
out.writeInt(chunks.length);
45-
46-
for (Chunk chunk : chunks) {
47-
serializeChunk(chunk, out);
48-
}
44+
writeArray(chunks, out, this::serializeChunk);
4945
}
5046

5147
protected void serializeChunk(Chunk chunk, ByteArrayDataOutput out) throws IOException {
@@ -56,15 +52,11 @@ protected void serializeChunk(Chunk chunk, ByteArrayDataOutput out) throws IOExc
5652
serializeHeightmaps(chunk.heightmaps(), out);
5753
serializeBlockEntities(chunk.blockEntities(), out);
5854
serializeEntities(chunk.entities(), out);
59-
serializeCompound(chunk.tag(), out);
55+
writeCompound(chunk.tag(), out);
6056
}
6157

6258
protected void serializeSections(Section[] sections, ByteArrayDataOutput out) throws IOException {
63-
out.writeInt(sections.length);
64-
65-
for (Section section : sections) {
66-
serializeSection(section, out);
67-
}
59+
writeArray(sections, out, this::serializeSection);
6860
}
6961

7062
protected void serializeSection(Section section, ByteArrayDataOutput out) throws IOException {
@@ -83,23 +75,23 @@ protected void serializeLightData(byte @Nullable [] bytes, ByteArrayDataOutput o
8375
}
8476

8577
protected void serializeBlockStates(BlockStates blockStates, ByteArrayDataOutput out) throws IOException {
86-
serializeCompound(blockStates.tag(), out);
78+
writeCompound(blockStates.tag(), out);
8779
}
8880

8981
protected void serializeBiomes(Biomes biomes, ByteArrayDataOutput out) throws IOException {
90-
serializeCompound(biomes.tag(), out);
82+
writeCompound(biomes.tag(), out);
9183
}
9284

9385
protected void serializeHeightmaps(Heightmaps heightmaps, ByteArrayDataOutput out) throws IOException {
94-
serializeCompound(heightmaps.tag(), out);
86+
writeCompound(heightmaps.tag(), out);
9587
}
9688

9789
protected void serializeBlockEntities(BlockEntity[] blockEntities, ByteArrayDataOutput out) throws IOException {
98-
serializeCompoundList(blockEntities, out, TILE_ENTITIES);
90+
writeCompoundArray(blockEntities, out, TILE_ENTITIES);
9991
}
10092

10193
protected void serializeEntities(Entity[] entities, ByteArrayDataOutput out) throws IOException {
102-
serializeCompoundList(entities, out, ENTITIES);
94+
writeCompoundArray(entities, out, ENTITIES);
10395
}
10496
//</editor-fold>
10597

@@ -108,21 +100,14 @@ protected void serializeEntities(Entity[] entities, ByteArrayDataOutput out) thr
108100
public World deserialize(ByteArrayDataInput in) throws IOException {
109101
int worldVersion = in.readInt();
110102

111-
Chunk[] chunks = deserializeCompressed(this::deserializeChunks, in);
112-
CompoundBinaryTag extraData = deserializeCompressed((length, dataIn) -> this.deserializeCompound(length, dataIn), in);
103+
Chunk[] chunks = readCompressed(this::deserializeChunks, in);
104+
CompoundBinaryTag extraData = readCompressed(this::readRawCompound, in);
113105

114106
return loader.deserializers().world().deserialize(worldVersion, chunks, extraData);
115107
}
116108

117109
protected Chunk[] deserializeChunks(ByteArrayDataInput in) throws IOException {
118-
int length = in.readInt();
119-
120-
Chunk[] chunks = new Chunk[length];
121-
for (int i = 0; i < length; i++) {
122-
chunks[i] = deserializeChunk(in);
123-
}
124-
125-
return chunks;
110+
return readArray(Chunk[]::new, in, this::deserializeChunk);
126111
}
127112

128113
protected Chunk deserializeChunk(ByteArrayDataInput in) throws IOException {
@@ -133,20 +118,13 @@ protected Chunk deserializeChunk(ByteArrayDataInput in) throws IOException {
133118
Heightmaps heightmaps = deserializeHeightmaps(in);
134119
BlockEntity[] blockEntities = deserializeBlockEntities(in);
135120
Entity[] entities = deserializeEntities(in);
136-
CompoundBinaryTag extraData = deserializeCompound(in);
121+
CompoundBinaryTag extraData = readCompound(in);
137122

138123
return loader.deserializers().chunk().deserialize(x, z, sections, heightmaps, blockEntities, entities, extraData);
139124
}
140125

141126
protected Section[] deserializeSections(ByteArrayDataInput in) throws IOException {
142-
int length = in.readInt();
143-
144-
Section[] sections = new Section[length];
145-
for (int i = 0; i < length; i++) {
146-
sections[i] = deserializeSection(in);
147-
}
148-
149-
return sections;
127+
return readArray(Section[]::new, in, this::deserializeSection);
150128
}
151129

152130
protected Section deserializeSection(ByteArrayDataInput in) throws IOException {
@@ -171,23 +149,23 @@ protected Section deserializeSection(ByteArrayDataInput in) throws IOException {
171149
}
172150

173151
protected BlockStates deserializeBlockStates(ByteArrayDataInput in) throws IOException {
174-
return loader.deserializers().blockStates().deserialize(deserializeCompound(in));
152+
return loader.deserializers().blockStates().deserialize(readCompound(in));
175153
}
176154

177155
protected Biomes deserializeBiomes(ByteArrayDataInput in) throws IOException {
178-
return loader.deserializers().biomes().deserialize(deserializeCompound(in));
156+
return loader.deserializers().biomes().deserialize(readCompound(in));
179157
}
180158

181159
protected Heightmaps deserializeHeightmaps(ByteArrayDataInput in) throws IOException {
182-
return loader.deserializers().heightmaps().deserialize(deserializeCompound(in));
160+
return loader.deserializers().heightmaps().deserialize(readCompound(in));
183161
}
184162

185163
protected BlockEntity[] deserializeBlockEntities(ByteArrayDataInput in) throws IOException {
186-
return deserializeCompoundList(BlockEntity[]::new, in, TILE_ENTITIES, loader.deserializers().blockEntity()::deserialize);
164+
return readCompoundArray(BlockEntity[]::new, in, TILE_ENTITIES, loader.deserializers().blockEntity()::deserialize);
187165
}
188166

189167
protected Entity[] deserializeEntities(ByteArrayDataInput in) throws IOException {
190-
return deserializeCompoundList(Entity[]::new, in, ENTITIES, loader.deserializers().entity()::deserialize);
168+
return readCompoundArray(Entity[]::new, in, ENTITIES, loader.deserializers().entity()::deserialize);
191169
}
192170
//</editor-fold>
193171
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package net.roxymc.slime.util.function;
2+
3+
import java.io.IOException;
4+
5+
@FunctionalInterface
6+
public interface IOBiConsumer<T, U> {
7+
void accept(T t, U u) throws IOException;
8+
}

0 commit comments

Comments
 (0)