mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-04 15:27:30 +07:00
Created packet serializers
Created packet serializers and moved serialization code from NetworkSynchronizer into SerializationManager
This commit is contained in:
183
core/src/com/riiablo/engine/server/SerializationManager.java
Normal file
183
core/src/com/riiablo/engine/server/SerializationManager.java
Normal file
@ -0,0 +1,183 @@
|
||||
package com.riiablo.engine.server;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.artemis.Component;
|
||||
import com.artemis.ComponentManager;
|
||||
import com.artemis.ComponentMapper;
|
||||
import com.artemis.utils.Bag;
|
||||
import com.badlogic.gdx.utils.ByteArray;
|
||||
import com.badlogic.gdx.utils.IntArray;
|
||||
import com.badlogic.gdx.utils.ObjectMap;
|
||||
import com.riiablo.engine.Dirty;
|
||||
import com.riiablo.engine.server.component.Angle;
|
||||
import com.riiablo.engine.server.component.CofAlphas;
|
||||
import com.riiablo.engine.server.component.CofComponents;
|
||||
import com.riiablo.engine.server.component.CofTransforms;
|
||||
import com.riiablo.engine.server.component.Player;
|
||||
import com.riiablo.engine.server.component.Position;
|
||||
import com.riiablo.engine.server.component.Velocity;
|
||||
import com.riiablo.engine.server.component.serializer.AngleSerializer;
|
||||
import com.riiablo.engine.server.component.serializer.CofAlphasSerializer;
|
||||
import com.riiablo.engine.server.component.serializer.CofComponentsSerializer;
|
||||
import com.riiablo.engine.server.component.serializer.CofTransformsSerializer;
|
||||
import com.riiablo.engine.server.component.serializer.FlatBuffersSerializer;
|
||||
import com.riiablo.engine.server.component.serializer.PlayerSerializer;
|
||||
import com.riiablo.engine.server.component.serializer.PositionSerializer;
|
||||
import com.riiablo.engine.server.component.serializer.VelocitySerializer;
|
||||
import com.riiablo.net.packet.d2gs.CofAlphasP;
|
||||
import com.riiablo.net.packet.d2gs.CofComponentsP;
|
||||
import com.riiablo.net.packet.d2gs.CofTransformsP;
|
||||
import com.riiablo.net.packet.d2gs.D2GS;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
|
||||
import net.mostlyoriginal.api.system.core.PassiveSystem;
|
||||
|
||||
public class SerializationManager extends PassiveSystem {
|
||||
private static final String TAG = "SerializationManager";
|
||||
private static final boolean DEBUG = true;
|
||||
private static final boolean DEBUG_DESERIALIZE = DEBUG && true;
|
||||
|
||||
private static final int INITIAL_SIZE = 64;
|
||||
public final Bag<Component> components = new Bag<>(INITIAL_SIZE);
|
||||
public final ByteArray dataType = new ByteArray(INITIAL_SIZE);
|
||||
public final IntArray data = new IntArray(INITIAL_SIZE);
|
||||
|
||||
private ObjectMap<Class<? extends Component>, FlatBuffersSerializer> serializers;
|
||||
private Class<? extends Component>[] deserializers;
|
||||
private final Sync sync = new Sync();
|
||||
|
||||
protected ComponentMapper<CofComponents> mCofComponents;
|
||||
protected ComponentMapper<CofTransforms> mCofTransforms;
|
||||
protected ComponentMapper<CofAlphas> mCofAlphas;
|
||||
protected ComponentMapper<Position> mPosition;
|
||||
protected ComponentMapper<Velocity> mVelocity;
|
||||
protected ComponentMapper<Angle> mAngle;
|
||||
protected ComponentMapper[] cm;
|
||||
|
||||
protected ComponentManager componentManager;
|
||||
protected CofManager cofs;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void initialize() {
|
||||
serializers = new ObjectMap<>();
|
||||
serializers.put(CofComponents.class, new CofComponentsSerializer());
|
||||
serializers.put(CofTransforms.class, new CofTransformsSerializer());
|
||||
serializers.put(CofAlphas.class, new CofAlphasSerializer());
|
||||
serializers.put(Position.class, new PositionSerializer());
|
||||
serializers.put(Velocity.class, new VelocitySerializer());
|
||||
serializers.put(Angle.class, new AngleSerializer());
|
||||
serializers.put(Player.class, new PlayerSerializer());
|
||||
|
||||
deserializers = (Class<? extends Component>[]) new Class[SyncData.names.length];
|
||||
deserializers[SyncData.CofComponentsP] = CofComponents.class;
|
||||
deserializers[SyncData.CofTransformsP] = CofTransforms.class;
|
||||
deserializers[SyncData.CofAlphasP] = CofAlphas.class;
|
||||
deserializers[SyncData.PositionP] = Position.class;
|
||||
deserializers[SyncData.VelocityP] = Velocity.class;
|
||||
deserializers[SyncData.AngleP] = Angle.class;
|
||||
deserializers[SyncData.PlayerP] = Player.class;
|
||||
|
||||
cm = new ComponentMapper[SyncData.names.length];
|
||||
cm[SyncData.CofComponentsP] = null;
|
||||
cm[SyncData.CofTransformsP] = null;
|
||||
cm[SyncData.CofAlphasP] = null;
|
||||
cm[SyncData.PositionP] = mPosition;
|
||||
cm[SyncData.VelocityP] = mVelocity;
|
||||
cm[SyncData.AngleP] = mAngle;
|
||||
cm[SyncData.PlayerP] = null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public int serialize(FlatBufferBuilder builder, int entityId) {
|
||||
dataType.clear();
|
||||
data.clear();
|
||||
components.clear();
|
||||
|
||||
componentManager.getComponentsFor(entityId, components);
|
||||
for (Component c : components) {
|
||||
FlatBuffersSerializer serializer = serializers.get(c.getClass());
|
||||
if (serializer == null) continue;
|
||||
dataType.add(serializer.getDataType());
|
||||
data.add(serializer.putData(builder, c));
|
||||
}
|
||||
|
||||
assert dataType.size == data.size;
|
||||
|
||||
final int dataTypeSize = dataType.size;
|
||||
final byte[] dataType = this.dataType.items;
|
||||
Sync.startDataTypeVector(builder, dataTypeSize);
|
||||
for (int i = 0; i < dataTypeSize; i++) builder.addByte(dataType[i]);
|
||||
int dataTypeOffset = builder.endVector();
|
||||
|
||||
final int dataSize = data.size;
|
||||
final int[] data = this.data.items;
|
||||
Sync.startDataVector(builder, dataSize);
|
||||
for (int i = 0; i < dataSize; i++) builder.addOffset(data[i]);
|
||||
int dataOffset = builder.endVector();
|
||||
|
||||
return Sync.createSync(builder, entityId, dataTypeOffset, dataOffset);
|
||||
}
|
||||
|
||||
public void deserialize(int entityId, D2GS packet) {
|
||||
packet.data(sync);
|
||||
deserialize(entityId, sync); // TODO: for each sync in d2gs
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void deserialize(int entityId, Sync sync) {
|
||||
int tFlags = Dirty.NONE;
|
||||
int aFlags = Dirty.NONE;
|
||||
|
||||
byte dataType;
|
||||
//int entityId = sync.entityId(); // FIXME: use something like this for client-side (resolve id)
|
||||
for (int i = 0, len = sync.dataTypeLength(); i < len; i++) {
|
||||
switch (dataType = sync.dataType(i)) {
|
||||
case SyncData.CofComponentsP: {
|
||||
Class<? extends Component> clazz = deserializers[dataType];
|
||||
CofComponentsSerializer serializer = (CofComponentsSerializer) serializers.get(clazz);
|
||||
CofComponentsP table = serializer.getTable(sync, i);
|
||||
for (int j = 0, s = table.componentLength(); j < s; j++) {
|
||||
cofs.setComponent(entityId, j, table.component(j));
|
||||
}
|
||||
// if (DEBUG_DESERIALIZE) Gdx.app.log(TAG, " " + DebugUtils.toByteArray(ArrayUtils.toByteArray(mCofComponents.get(entityId).component)));
|
||||
break;
|
||||
}
|
||||
case SyncData.CofTransformsP: {
|
||||
Class<? extends Component> clazz = deserializers[dataType];
|
||||
CofTransformsSerializer serializer = (CofTransformsSerializer) serializers.get(clazz);
|
||||
CofTransformsP table = serializer.getTable(sync, i);
|
||||
for (int j = 0, s = table.transformLength(); j < s; j++) {
|
||||
tFlags |= cofs.setTransform(entityId, j, (byte) table.transform(j));
|
||||
}
|
||||
// if (DEBUG_DESERIALIZE) Gdx.app.log(TAG, " " + DebugUtils.toByteArray(mCofTransforms.get(entityId).transform));
|
||||
break;
|
||||
}
|
||||
case SyncData.CofAlphasP: {
|
||||
Class<? extends Component> clazz = deserializers[dataType];
|
||||
CofAlphasSerializer serializer = (CofAlphasSerializer) serializers.get(clazz);
|
||||
CofAlphasP table = serializer.getTable(sync, i);
|
||||
for (int j = 0, s = table.alphaLength(); j < s; j++) {
|
||||
aFlags |= cofs.setAlpha(entityId, j, table.alpha(j));
|
||||
}
|
||||
// if (DEBUG_DESERIALIZE) Gdx.app.log(TAG, " " + Arrays.toString(mCofAlphas.get(entityId).alpha));
|
||||
break;
|
||||
}
|
||||
case SyncData.ClassP:
|
||||
case SyncData.PlayerP:
|
||||
break;
|
||||
default: {
|
||||
Class<? extends Component> clazz = deserializers[dataType];
|
||||
FlatBuffersSerializer serializer = serializers.get(clazz);
|
||||
serializer.getData(sync, i, cm[dataType].get(entityId));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cofs.updateTransform(entityId, tFlags);
|
||||
cofs.updateAlpha(entityId, aFlags);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.riiablo.engine.server.component.Angle;
|
||||
import com.riiablo.net.packet.d2gs.AngleP;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
|
||||
public class AngleSerializer implements FlatBuffersSerializer<Angle, AngleP> {
|
||||
public static final AngleP table = new AngleP();
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return SyncData.AngleP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putData(FlatBufferBuilder builder, Angle c) {
|
||||
Vector2 angle = c.target;
|
||||
return AngleP.createAngleP(builder, angle.x, angle.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AngleP getTable(Sync sync, int j) {
|
||||
sync.data(table, j);
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Angle getData(Sync sync, int j, Angle c) {
|
||||
getTable(sync, j);
|
||||
Vector2 angle = c.target;
|
||||
angle.x = table.x();
|
||||
angle.y = table.y();
|
||||
return c;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.riiablo.engine.server.component.CofAlphas;
|
||||
import com.riiablo.net.packet.d2gs.CofAlphasP;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
|
||||
public class CofAlphasSerializer implements FlatBuffersSerializer<CofAlphas, CofAlphasP> {
|
||||
public static final CofAlphasP table = new CofAlphasP();
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return SyncData.CofAlphasP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putData(FlatBufferBuilder builder, CofAlphas c) {
|
||||
int vectorOffset = CofAlphasP.createAlphaVector(builder, c.alpha);
|
||||
return CofAlphasP.createCofAlphasP(builder, vectorOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CofAlphasP getTable(Sync sync, int i) {
|
||||
sync.data(table, i);
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CofAlphas getData(Sync sync, int j, CofAlphas c) {
|
||||
getTable(sync, j);
|
||||
float[] alpha = c.alpha;
|
||||
for (int i = 0, s = table.alphaLength(); i < s; i++) {
|
||||
alpha[i] = table.alpha(i);
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.riiablo.engine.server.component.CofComponents;
|
||||
import com.riiablo.net.packet.d2gs.CofComponentsP;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
|
||||
public class CofComponentsSerializer implements FlatBuffersSerializer<CofComponents, CofComponentsP> {
|
||||
public static final CofComponentsP table = new CofComponentsP();
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return SyncData.CofComponentsP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putData(FlatBufferBuilder builder, CofComponents c) {
|
||||
int[] component = c.component;
|
||||
CofComponentsP.startComponentVector(builder, component.length);
|
||||
for (int i = component.length - 1; i >= 0; i--) {
|
||||
builder.addByte((byte) component[i]);
|
||||
}
|
||||
|
||||
int vectorOffset = builder.endVector();
|
||||
return CofComponentsP.createCofComponentsP(builder, vectorOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CofComponentsP getTable(Sync sync, int j) {
|
||||
sync.data(table, j);
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CofComponents getData(Sync sync, int j, CofComponents c) {
|
||||
getTable(sync, j);
|
||||
int[] component = c.component;
|
||||
for (int i = 0, s = table.componentLength(); i < s; i++) {
|
||||
component[i] = table.component(i);
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.riiablo.engine.server.component.CofTransforms;
|
||||
import com.riiablo.net.packet.d2gs.CofTransformsP;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
|
||||
public class CofTransformsSerializer implements FlatBuffersSerializer<CofTransforms, CofTransformsP> {
|
||||
public static final CofTransformsP table = new CofTransformsP();
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return SyncData.CofTransformsP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putData(FlatBufferBuilder builder, CofTransforms c) {
|
||||
int vectorOffset = CofTransformsP.createTransformVector(builder, c.transform);
|
||||
return CofTransformsP.createCofTransformsP(builder, vectorOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CofTransformsP getTable(Sync sync, int i) {
|
||||
sync.data(table, i);
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CofTransforms getData(Sync sync, int j, CofTransforms c) {
|
||||
getTable(sync, j);
|
||||
byte[] transform = c.transform;
|
||||
for (int i = 0, s = table.transformLength(); i < s; i++) {
|
||||
transform[i] = (byte) table.transform(i);
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import com.google.flatbuffers.Table;
|
||||
|
||||
import com.artemis.Component;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
|
||||
public interface FlatBuffersSerializer<T extends Component, S extends Table> {
|
||||
byte getDataType();
|
||||
int putData(FlatBufferBuilder builder, T c);
|
||||
S getTable(Sync sync, int j);
|
||||
T getData(Sync sync, int j, T c);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.riiablo.codec.D2S;
|
||||
import com.riiablo.engine.server.component.Player;
|
||||
import com.riiablo.net.packet.d2gs.PlayerP;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
|
||||
public class PlayerSerializer implements FlatBuffersSerializer<Player, PlayerP> {
|
||||
public static final PlayerP table = new PlayerP();
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return SyncData.PlayerP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putData(FlatBufferBuilder builder, Player c) {
|
||||
D2S.Header d2sHeader = c.data.getD2S().header;
|
||||
int charNameOffset = builder.createString(d2sHeader.name);
|
||||
return PlayerP.createPlayerP(builder, d2sHeader.charClass, charNameOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerP getTable(Sync sync, int j) {
|
||||
sync.data(table, j);
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getData(Sync sync, int j, Player c) {
|
||||
throw new UnsupportedOperationException("Not supported!");
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.riiablo.engine.server.component.Position;
|
||||
import com.riiablo.net.packet.d2gs.PositionP;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
|
||||
public class PositionSerializer implements FlatBuffersSerializer<Position, PositionP> {
|
||||
public static final PositionP table = new PositionP();
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return SyncData.PositionP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putData(FlatBufferBuilder builder, Position c) {
|
||||
Vector2 position = c.position;
|
||||
return PositionP.createPositionP(builder, position.x, position.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PositionP getTable(Sync sync, int j) {
|
||||
sync.data(table, j);
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getData(Sync sync, int j, Position c) {
|
||||
getTable(sync, j);
|
||||
Vector2 position = c.position;
|
||||
position.x = table.x();
|
||||
position.y = table.y();
|
||||
return c;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.riiablo.engine.server.component.serializer;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.riiablo.engine.server.component.Velocity;
|
||||
import com.riiablo.net.packet.d2gs.Sync;
|
||||
import com.riiablo.net.packet.d2gs.SyncData;
|
||||
import com.riiablo.net.packet.d2gs.VelocityP;
|
||||
|
||||
public class VelocitySerializer implements FlatBuffersSerializer<Velocity, VelocityP> {
|
||||
public static final VelocityP table = new VelocityP();
|
||||
|
||||
@Override
|
||||
public byte getDataType() {
|
||||
return SyncData.VelocityP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int putData(FlatBufferBuilder builder, Velocity c) {
|
||||
Vector2 velocity = c.velocity;
|
||||
return VelocityP.createVelocityP(builder, velocity.x, velocity.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VelocityP getTable(Sync sync, int j) {
|
||||
sync.data(table, j);
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Velocity getData(Sync sync, int j, Velocity c) {
|
||||
getTable(sync, j);
|
||||
Vector2 velocity = c.velocity;
|
||||
velocity.x = table.x();
|
||||
velocity.y = table.y();
|
||||
return c;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user