mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-07 08:58:20 +01:00
Simplify the SerializableData interface
This commit is contained in:
parent
0f71b4c9d4
commit
2218299931
@ -27,6 +27,17 @@ public interface SerializableData extends Data {
|
|||||||
*/
|
*/
|
||||||
byte[] getSerializedData(Object2ShortMap<String> typeToIndexMap, boolean indexed);
|
byte[] getSerializedData(Object2ShortMap<String> typeToIndexMap, boolean indexed);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the data of a {@link SerializableData} when you already have the index map
|
||||||
|
* <p>
|
||||||
|
* WARNING: the data to read should not have any index to read and your index map should be COMPLETE
|
||||||
|
* Use {@link #readIndexedSerializedData(BinaryReader)} if you need to read the header
|
||||||
|
*
|
||||||
|
* @param reader the binary reader
|
||||||
|
* @param typeToIndexMap the index map
|
||||||
|
*/
|
||||||
|
void readSerializedData(BinaryReader reader, Object2ShortMap<String> typeToIndexMap);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize the data into an array of bytes
|
* Serialize the data into an array of bytes
|
||||||
* <p>
|
* <p>
|
||||||
@ -37,17 +48,9 @@ public interface SerializableData extends Data {
|
|||||||
*
|
*
|
||||||
* @return the array representation of this data object
|
* @return the array representation of this data object
|
||||||
*/
|
*/
|
||||||
byte[] getIndexedSerializedData();
|
default byte[] getIndexedSerializedData() {
|
||||||
|
return getSerializedData(new Object2ShortOpenHashMap<>(), true);
|
||||||
/**
|
}
|
||||||
* Read the data of a {@link SerializableData} when you already have the index map
|
|
||||||
* <p>
|
|
||||||
* WARNING: the data to read should not have any index to read and your index map should be COMPLETE
|
|
||||||
*
|
|
||||||
* @param reader the binary reader
|
|
||||||
* @param typeToIndexMap the index map
|
|
||||||
*/
|
|
||||||
void readSerializedData(BinaryReader reader, Object2ShortMap<String> typeToIndexMap);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the index map and the data of a serialized {@link SerializableData}
|
* Read the index map and the data of a serialized {@link SerializableData}
|
||||||
@ -55,7 +58,10 @@ public interface SerializableData extends Data {
|
|||||||
*
|
*
|
||||||
* @param reader the binary reader
|
* @param reader the binary reader
|
||||||
*/
|
*/
|
||||||
void readIndexedSerializedData(BinaryReader reader);
|
default void readIndexedSerializedData(BinaryReader reader) {
|
||||||
|
final Object2ShortMap<String> typeToIndexMap = SerializableData.readDataIndexes(reader);
|
||||||
|
readSerializedData(reader, typeToIndexMap);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the index info (class name -> class index), used to write the header for indexed serialized data
|
* Write the index info (class name -> class index), used to write the header for indexed serialized data
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package net.minestom.server.data;
|
package net.minestom.server.data;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
|
import it.unimi.dsi.fastutil.objects.Object2ShortMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap;
|
|
||||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
|
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
|
||||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
|
||||||
import net.minestom.server.utils.PrimitiveConversion;
|
import net.minestom.server.utils.PrimitiveConversion;
|
||||||
@ -112,43 +111,8 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
|
|||||||
return binaryWriter.toByteArray();
|
return binaryWriter.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] getIndexedSerializedData() {
|
|
||||||
return getSerializedData(new Object2ShortOpenHashMap<>(), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readSerializedData(BinaryReader reader, Object2ShortMap<String> typeToIndexMap) {
|
public void readSerializedData(BinaryReader reader, Object2ShortMap<String> typeToIndexMap) {
|
||||||
readIndexedData(this, typeToIndexMap, reader);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void readIndexedSerializedData(BinaryReader reader) {
|
|
||||||
readData(this, reader);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read the indexes of the data + the data
|
|
||||||
*
|
|
||||||
* @param data the object to append the data
|
|
||||||
* @param reader the reader
|
|
||||||
*/
|
|
||||||
private static void readData(SerializableData data, BinaryReader reader) {
|
|
||||||
final Object2ShortMap<String> typeToIndexMap = SerializableData.readDataIndexes(reader);
|
|
||||||
readIndexedData(data, typeToIndexMap, reader);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a buffer into a {@link SerializableData}, this will not read the data index header.
|
|
||||||
* Use {@link #readData(SerializableData, BinaryReader)} to read the whole data object (if your data contains the indexes)
|
|
||||||
* <p>
|
|
||||||
* WARNING: the {@link DataManager} needs to have all the required types as the {@link SerializableData} has
|
|
||||||
*
|
|
||||||
* @param data the object to append the data
|
|
||||||
* @param typeToIndexMap the map which index all the types contained in the data (className->classIndex)
|
|
||||||
* @param reader the reader
|
|
||||||
*/
|
|
||||||
private static void readIndexedData(SerializableData data, Object2ShortMap<String> typeToIndexMap, BinaryReader reader) {
|
|
||||||
// Map used to convert an index to the class name (opposite of typeToIndexMap)
|
// Map used to convert an index to the class name (opposite of typeToIndexMap)
|
||||||
final Short2ObjectMap<String> indexToTypeMap = new Short2ObjectOpenHashMap<>(typeToIndexMap.size());
|
final Short2ObjectMap<String> indexToTypeMap = new Short2ObjectOpenHashMap<>(typeToIndexMap.size());
|
||||||
{
|
{
|
||||||
@ -191,7 +155,8 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
|
|||||||
final Object value = DATA_MANAGER.getDataType(type).decode(reader);
|
final Object value = DATA_MANAGER.getDataType(type).decode(reader);
|
||||||
|
|
||||||
// Set the data
|
// Set the data
|
||||||
data.set(name, value, type);
|
set(name, value, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user