using Cyber.Entities;
using UnityEngine.Networking;
namespace Cyber.Networking.Messages {
///
/// Contains sync data to sync stuff with.
///
public class SyncPkt : MessageBase {
///
/// The Sync Packet ID of this packet.
///
public int SyncPacketID;
///
/// The Array of SyncID added in this SyncPkt
///
public int[] SyncedSyncBases;
private SyncDB SyncDB;
///
/// Creates a SyncPkt on the serverside.
///
/// SyncDB to sync from.
/// The ID's of the SyncBases to sync.
/// ID of the sync packet itself.
public SyncPkt(SyncDB syncDB, int[] syncBases, int syncPacketID) {
SyncPacketID = syncPacketID;
SyncDB = syncDB;
SyncedSyncBases = syncBases;
}
///
/// Creates SyncPkt for deserializing.
///
/// SyncBase to sync to.
public SyncPkt(SyncDB syncDB) {
SyncDB = syncDB;
}
///
/// Deserializes the SynkPkt with ONLY the Sync Packet ID.
///
///
public override void Deserialize(NetworkReader reader) {
SyncPacketID = reader.ReadInt32();
}
///
/// Applies the SyncPkt.
///
///
public void ApplySync(NetworkReader reader) {
byte[][] ByteArray = new byte[4][];
ByteArray[0] = reader.ReadBytesAndSize();
ByteArray[1] = reader.ReadBytesAndSize();
ByteArray[2] = reader.ReadBytesAndSize();
ByteArray[3] = reader.ReadBytesAndSize();
SyncedSyncBases = NetworkHelper.DeserializeIntArray(ByteArray);
foreach (int syncId in SyncedSyncBases) {
SyncDB.Get(syncId).Deserialize(reader);
}
}
///
/// Serializes the SyncPkt and writes everything it needs.
///
///
public override void Serialize(NetworkWriter writer) {
writer.Write(SyncPacketID);
byte[][] ByteArray = NetworkHelper.SerializeIntArray(SyncedSyncBases);
writer.WriteBytesFull(ByteArray[0]);
writer.WriteBytesFull(ByteArray[1]);
writer.WriteBytesFull(ByteArray[2]);
writer.WriteBytesFull(ByteArray[3]);
foreach (int syncId in SyncedSyncBases) {
SyncDB.Get(syncId).Serialize(writer);
}
}
}
}