Reworked data management

This commit is contained in:
Felix Cravic 2020-04-28 17:36:17 +02:00
parent 08015586a0
commit b128074c75
18 changed files with 43 additions and 43 deletions

View File

@ -62,7 +62,7 @@ public interface DataContainer {
return; return;
} }
SerializableData data = DataReader.readData(array, true); SerializableData data = DataReader.readCompressedData(array);
setData(data); setData(data);
if (callback != null) if (callback != null)

View File

@ -7,6 +7,6 @@ public abstract class DataType<T> {
public abstract void encode(PacketWriter packetWriter, T value); public abstract void encode(PacketWriter packetWriter, T value);
public abstract T decode(PacketReader packetReader, byte[] value); public abstract T decode(PacketReader packetReader);
} }

View File

@ -11,7 +11,7 @@ public class BooleanData extends DataType<Boolean> {
} }
@Override @Override
public Boolean decode(PacketReader packetReader, byte[] value) { public Boolean decode(PacketReader packetReader) {
return packetReader.readBoolean(); return packetReader.readBoolean();
} }
} }

View File

@ -11,7 +11,7 @@ public class ByteData extends DataType<Byte> {
} }
@Override @Override
public Byte decode(PacketReader packetReader, byte[] value) { public Byte decode(PacketReader packetReader) {
return packetReader.readByte(); return packetReader.readByte();
} }
} }

View File

@ -12,7 +12,7 @@ public class CharacterData extends DataType<Character> {
} }
@Override @Override
public Character decode(PacketReader packetReader, byte[] value) { public Character decode(PacketReader packetReader) {
return packetReader.readChar(); return packetReader.readChar();
} }
} }

View File

@ -12,7 +12,7 @@ public class DoubleData extends DataType<Double> {
} }
@Override @Override
public Double decode(PacketReader packetReader, byte[] value) { public Double decode(PacketReader packetReader) {
return packetReader.readDouble(); return packetReader.readDouble();
} }
} }

View File

@ -12,7 +12,7 @@ public class FloatData extends DataType<Float> {
} }
@Override @Override
public Float decode(PacketReader packetReader, byte[] value) { public Float decode(PacketReader packetReader) {
return packetReader.readFloat(); return packetReader.readFloat();
} }
} }

View File

@ -12,7 +12,7 @@ public class IntegerData extends DataType<Integer> {
} }
@Override @Override
public Integer decode(PacketReader packetReader, byte[] value) { public Integer decode(PacketReader packetReader) {
return packetReader.readVarInt(); return packetReader.readVarInt();
} }
} }

View File

@ -12,7 +12,7 @@ public class ItemStackData extends DataType<ItemStack> {
} }
@Override @Override
public ItemStack decode(PacketReader packetReader, byte[] value) { public ItemStack decode(PacketReader packetReader) {
return packetReader.readSlot(); return packetReader.readSlot();
} }
} }

View File

@ -11,7 +11,7 @@ public class LongData extends DataType<Long> {
} }
@Override @Override
public Long decode(PacketReader packetReader, byte[] value) { public Long decode(PacketReader packetReader) {
return packetReader.readLong(); return packetReader.readLong();
} }
} }

View File

@ -22,7 +22,7 @@ public class SerializableDataData extends DataType<SerializableData> {
} }
@Override @Override
public SerializableData decode(PacketReader packetReader, byte[] value) { public SerializableData decode(PacketReader packetReader) {
return DataReader.readData(value, false); return DataReader.readData(packetReader.getBuffer());
} }
} }

View File

@ -12,7 +12,7 @@ public class ShortData extends DataType<Short> {
} }
@Override @Override
public Short decode(PacketReader packetReader, byte[] value) { public Short decode(PacketReader packetReader) {
return packetReader.readShort(); return packetReader.readShort();
} }
} }

View File

@ -12,7 +12,7 @@ public class StringData extends DataType<String> {
} }
@Override @Override
public String decode(PacketReader packetReader, byte[] value) { public String decode(PacketReader packetReader) {
return packetReader.readSizedString(); return packetReader.readSizedString();
} }
} }

View File

@ -18,7 +18,7 @@ public class ItemStackArrayData extends DataType<ItemStack[]> {
} }
@Override @Override
public ItemStack[] decode(PacketReader packetReader, byte[] value) { public ItemStack[] decode(PacketReader packetReader) {
List<ItemStack> items = new ArrayList<>(); List<ItemStack> items = new ArrayList<>();
int size = packetReader.readVarInt(); int size = packetReader.readVarInt();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {

View File

@ -288,13 +288,12 @@ public class Chunk implements Viewable {
dos.writeBoolean(isCustomBlock); // Determine the type of the ID dos.writeBoolean(isCustomBlock); // Determine the type of the ID
dos.writeShort(id); dos.writeShort(id);
if (data instanceof SerializableData) { hasData = (data != null && (data instanceof SerializableData)) && hasData;
dos.writeBoolean(hasData); dos.writeBoolean(hasData);
if (hasData) { if (hasData) {
byte[] d = ((SerializableData) data).getSerializedData(); byte[] d = ((SerializableData) data).getSerializedData();
dos.writeInt(d.length); dos.writeInt(d.length);
dos.write(d); dos.write(d);
}
} }
} }
} }

View File

@ -26,6 +26,10 @@ public class InstanceManager {
return instance; return instance;
} }
public InstanceContainer createInstanceContainer(File file) {
return createInstanceContainer(Dimension.OVERWORLD, file);
}
public InstanceContainer createInstanceContainer(Dimension dimension) { public InstanceContainer createInstanceContainer(Dimension dimension) {
return createInstanceContainer(dimension, null); return createInstanceContainer(dimension, null);
} }

View File

@ -1,5 +1,6 @@
package net.minestom.server.io; package net.minestom.server.io;
import io.netty.buffer.Unpooled;
import net.minestom.server.data.Data; import net.minestom.server.data.Data;
import net.minestom.server.instance.Biome; import net.minestom.server.instance.Biome;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
@ -41,7 +42,7 @@ public class ChunkReader {
if (hasData) { if (hasData) {
int dataLength = stream.readInt(); int dataLength = stream.readInt();
byte[] dataArray = stream.readNBytes(dataLength); byte[] dataArray = stream.readNBytes(dataLength);
data = DataReader.readData(dataArray, false); data = DataReader.readData(Unpooled.wrappedBuffer(dataArray));
} }
if (isCustomBlock) { if (isCustomBlock) {

View File

@ -8,23 +8,22 @@ import net.minestom.server.data.SerializableData;
import net.minestom.server.network.packet.PacketReader; import net.minestom.server.network.packet.PacketReader;
import net.minestom.server.utils.CompressionUtils; import net.minestom.server.utils.CompressionUtils;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class DataReader { public class DataReader {
private static final DataManager DATA_MANAGER = MinecraftServer.getDataManager(); private static final DataManager DATA_MANAGER = MinecraftServer.getDataManager();
public static SerializableData readData(byte[] b, boolean shouldDecompress) { public static SerializableData readCompressedData(byte[] b) {
b = shouldDecompress ? CompressionUtils.getDecompressedData(b) : b; b = CompressionUtils.getDecompressedData(b);
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(b)); return readData(Unpooled.wrappedBuffer(b));
}
// Only uncompressed
public static SerializableData readData(ByteBuf buffer) {
SerializableData data = new SerializableData(); SerializableData data = new SerializableData();
try { try {
while (true) { while (true) {
short typeLength = stream.readShort(); short typeLength = buffer.readShort();
if (typeLength == 0xff) { if (typeLength == 0xff) {
// End of data // End of data
@ -33,29 +32,26 @@ public class DataReader {
byte[] typeCache = new byte[typeLength]; byte[] typeCache = new byte[typeLength];
for (int i = 0; i < typeLength; i++) { for (int i = 0; i < typeLength; i++) {
typeCache[i] = stream.readByte(); typeCache[i] = buffer.readByte();
} }
short nameLength = stream.readShort(); String className = new String(typeCache);
Class type = Class.forName(className);
short nameLength = buffer.readShort();
byte[] nameCache = new byte[nameLength]; byte[] nameCache = new byte[nameLength];
for (int i = 0; i < nameLength; i++) { for (int i = 0; i < nameLength; i++) {
nameCache[i] = stream.readByte(); nameCache[i] = buffer.readByte();
} }
int valueLength = stream.readInt(); ByteBuf valueCache = buffer.readBytes(buffer.readInt());
byte[] valueCache = stream.readNBytes(valueLength);
Class type = Class.forName(new String(typeCache));
String name = new String(nameCache); String name = new String(nameCache);
ByteBuf buffer = Unpooled.wrappedBuffer(valueCache); PacketReader packetReader = new PacketReader(valueCache);
PacketReader packetReader = new PacketReader(buffer); Object value = DATA_MANAGER.getDataType(type).decode(packetReader);
Object value = DATA_MANAGER.getDataType(type).decode(packetReader, valueCache);
data.set(name, value, type); data.set(name, value, type);
} }
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }