Comments + utils methods for BinaryWriter

This commit is contained in:
themode 2020-09-02 19:06:11 +02:00
parent 74cfc1994d
commit 925c0a89f6
10 changed files with 58 additions and 68 deletions

View File

@ -44,7 +44,6 @@ import java.util.UUID;
public class PlayerInit {
private static volatile InstanceContainer instanceContainer;
private static volatile InstanceContainer netherTest;
private static volatile Inventory inventory;
@ -52,28 +51,17 @@ public class PlayerInit {
StorageLocation storageLocation = MinecraftServer.getStorageManager().getLocation("instance_data", new StorageOptions().setCompression(true));
ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo();
NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator();
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageLocation);
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD, storageLocation);
instanceContainer.enableAutoChunkLoad(true);
//instanceContainer.setChunkDecider((x,y) -> (pos) -> pos.getY()>40?(short)0:(short)1);
instanceContainer.setChunkGenerator(noiseTestGenerator);
netherTest = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
netherTest.enableAutoChunkLoad(true);
netherTest.setChunkGenerator(noiseTestGenerator);
InstanceContainer end = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
end.enableAutoChunkLoad(true);
end.setChunkGenerator(noiseTestGenerator);
// Load some chunks beforehand
final int loopStart = -10;
final int loopEnd = 10;
for (int x = loopStart; x < loopEnd; x++)
for (int z = loopStart; z < loopEnd; z++) {
instanceContainer.loadChunk(x, z);
netherTest.loadChunk(x, z);
end.loadChunk(x, z);
}
inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");

View File

@ -1,19 +1,8 @@
package fr.themode.demo.commands;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandProcessor;
import net.minestom.server.command.CommandSender;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.ChunkGenerator;
import net.minestom.server.instance.ChunkPopulator;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.InstanceManager;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.world.DimensionType;
import net.minestom.server.world.biomes.Biome;
import java.util.Arrays;
import java.util.List;
public class SimpleCommand implements CommandProcessor {
@Override
@ -31,35 +20,7 @@ public class SimpleCommand implements CommandProcessor {
if (!sender.isPlayer())
return false;
final int word = 2;
ChunkGenerator chunkGeneratorDemo = new ChunkGenerator() {
@Override
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
batch.setBlockStateId(x, 1, z, (short) word);
}
}
}
@Override
public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
Arrays.fill(biomes, Biome.PLAINS);
}
@Override
public List<ChunkPopulator> getPopulators() {
return null;
}
};
InstanceManager instanceManager = MinecraftServer.getInstanceManager();
InstanceContainer instanceContainer = instanceManager.createInstanceContainer(DimensionType.OVERWORLD);
instanceContainer.enableAutoChunkLoad(true);
instanceContainer.setChunkGenerator(chunkGeneratorDemo);
sender.asPlayer().setInstance(instanceContainer);
System.out.println(MinecraftServer.getInstanceManager().getInstances().size());
sender.asPlayer().getInstance().saveChunksToStorage(() -> System.out.println("END SAVE"));
System.gc();

View File

@ -3,11 +3,15 @@ package net.minestom.server.data;
import java.util.Collections;
import java.util.Set;
/**
* Represent an object which contain key/value based data
*/
public interface Data {
Data EMPTY = new Data() {
@Override
public <T> void set(String key, T value, Class<T> type) { }
public <T> void set(String key, T value, Class<T> type) {
}
@Override
public <T> T get(String key) {
@ -89,7 +93,7 @@ public interface Data {
*
* @return true if the data does not contain anything, false otherwise
*/
boolean isEmpty();
boolean isEmpty();
/**
* Clone this data

View File

@ -4,6 +4,9 @@ import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* {@link Data} implementation which use a {@link ConcurrentHashMap}
*/
public class DataImpl implements Data {
protected final ConcurrentHashMap<String, Object> data = new ConcurrentHashMap<>();

View File

@ -6,6 +6,9 @@ import net.minestom.server.reader.DataReader;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
/**
* Represent a {@link Data} object which can be serialized and read back by the {@link DataReader}
*/
public interface SerializableData extends Data {
DataManager DATA_MANAGER = MinecraftServer.getDataManager();

View File

@ -1,7 +1,5 @@
package net.minestom.server.data;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
import net.minestom.server.utils.PrimitiveConversion;
@ -10,6 +8,9 @@ import net.minestom.server.utils.binary.BinaryWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* {@link SerializableData} implementation based on {@link DataImpl}
*/
public class SerializableDataImpl extends DataImpl implements SerializableData {
private ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
@ -91,10 +92,8 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
// The buffer containing all the index info (class name to class index)
BinaryWriter indexWriter = new BinaryWriter();
SerializableData.writeDataIndexHeader(indexWriter, typeToIndexMap);
// Merge the index buffer & the main data buffer
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(indexWriter.getBuffer(), binaryWriter.getBuffer());
// Change the main writer buffer, so it contains both the indexes and the data
binaryWriter.setBuffer(finalBuffer);
// Set the header
binaryWriter.writeAtStart(indexWriter);
}
return binaryWriter.toByteArray();

View File

@ -1,8 +1,6 @@
package net.minestom.server.instance;
import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
@ -204,10 +202,8 @@ public class DynamicChunk extends Chunk {
SerializableData.writeDataIndexHeader(indexWriter, typeToIndexMap);
}
// Create the final buffer (data index buffer followed by the chunk buffer)
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(indexWriter.getBuffer(), binaryWriter.getBuffer());
// Change the main writer buffer
binaryWriter.setBuffer(finalBuffer);
// Add the hasIndex field & the index header if it has it
binaryWriter.writeAtStart(indexWriter);
return binaryWriter.toByteArray();
}

View File

@ -72,7 +72,7 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
if (buf.readableBytes() != 0) {
int i = Utils.readVarInt(buf);
final int i = Utils.readVarInt(buf);
if (i == 0) {
out.add(buf.readRetainedSlice(buf.readableBytes()));

View File

@ -15,6 +15,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/**
* Class used to read from a byte array
* WARNING: not thread-safe
*/
public class BinaryReader extends InputStream {
private final ByteBuf buffer;

View File

@ -16,6 +16,10 @@ import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.function.Consumer;
/**
* Class used to write to a byte array
* WARNING: not thread-safe
*/
public class BinaryWriter extends OutputStream {
private ByteBuf buffer;
@ -252,6 +256,34 @@ public class BinaryWriter extends OutputStream {
return bytes;
}
/**
* Add a {@link BinaryWriter}'s {@link ByteBuf} at the beginning of this writer
*
* @param headerWriter the {@link BinaryWriter} to add at the beginning
*/
public void writeAtStart(BinaryWriter headerWriter) {
// Get the buffer of the header
final ByteBuf headerBuf = headerWriter.getBuffer();
// Merge both the headerBuf and this buffer
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(headerBuf, buffer);
// Change the buffer used by this writer
setBuffer(finalBuffer);
}
/**
* Add a {@link BinaryWriter}'s {@link ByteBuf} at the end of this writer
*
* @param footerWriter the {@link BinaryWriter} to add at the end
*/
public void writeAtEnd(BinaryWriter footerWriter) {
// Get the buffer of the footer
final ByteBuf footerBuf = footerWriter.getBuffer();
// Merge both this buffer and the footerBuf
final ByteBuf finalBuffer = Unpooled.wrappedBuffer(buffer, footerBuf);
// Change the buffer used by this writer
setBuffer(finalBuffer);
}
/**
* Get the raw buffer used by this binary writer
*