using Cyber.Console;
using Cyber.Entities;
using UnityEngine.Networking;
namespace Cyber.Networking.Messages {
///
/// Contains sync data to sync stuff with.
///
public class SyncPkt : MessageBase {
///
/// Timestamp of the sync packet when sent from the server.
///
public double Timestamp;
///
/// The Sync Packet ID of this packet.
///
public int SyncPacketID;
///
/// The Array of SyncID added in this SyncPkt
///
public int[] SyncedSyncBases;
///
/// The Array of SyncIDs that have their hashes in this syncpacket
///
public int[] ChecksummedSyncBases;
///
/// The Array of Checksums that are sent with .
///
public int[] Checksums;
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[] checksummedSyncBases, int[] checksums, int syncPacketID, double timestamp) {
SyncPacketID = syncPacketID;
SyncDB = syncDB;
SyncedSyncBases = syncBases;
ChecksummedSyncBases = checksummedSyncBases;
Checksums = checksums;
Timestamp = timestamp;
}
///
/// Creates SyncPkt for deserializing.
///
/// SyncBase to sync to.
public SyncPkt(SyncDB syncDB) {
SyncDB = syncDB;
ChecksummedSyncBases = new int[0];
Checksums = new int[0];
}
///
/// Deserializes the SynkPkt with ONLY the Sync Packet ID.
///
///
public override void Deserialize(NetworkReader reader) {
SyncPacketID = reader.ReadInt32();
Timestamp = reader.ReadDouble();
}
///
/// 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);
}
bool checksums = reader.ReadBoolean();
if (checksums) {
byte[][] SummedIdsByteArray = new byte[4][];
SummedIdsByteArray[0] = reader.ReadBytesAndSize();
SummedIdsByteArray[1] = reader.ReadBytesAndSize();
SummedIdsByteArray[2] = reader.ReadBytesAndSize();
SummedIdsByteArray[3] = reader.ReadBytesAndSize();
byte[][] SumsByteArray = new byte[4][];
SumsByteArray[0] = reader.ReadBytesAndSize();
SumsByteArray[1] = reader.ReadBytesAndSize();
SumsByteArray[2] = reader.ReadBytesAndSize();
SumsByteArray[3] = reader.ReadBytesAndSize();
ChecksummedSyncBases = NetworkHelper.DeserializeIntArray(SummedIdsByteArray);
Checksums = NetworkHelper.DeserializeIntArray(SumsByteArray);
}
}
///
/// Serializes the SyncPkt and writes everything it needs.
///
///
public override void Serialize(NetworkWriter writer) {
writer.Write(SyncPacketID);
writer.Write((double) Timestamp);
byte[][] SyncedIdsByteArray = NetworkHelper.SerializeIntArray(SyncedSyncBases);
writer.WriteBytesFull(SyncedIdsByteArray[0]);
writer.WriteBytesFull(SyncedIdsByteArray[1]);
writer.WriteBytesFull(SyncedIdsByteArray[2]);
writer.WriteBytesFull(SyncedIdsByteArray[3]);
foreach (int syncId in SyncedSyncBases) {
SyncDB.Get(syncId).Serialize(writer);
}
if (ChecksummedSyncBases.Length == 0) {
writer.Write(false);
} else {
writer.Write(true);
byte[][] SummedIdsByteArray = NetworkHelper.SerializeIntArray(ChecksummedSyncBases);
writer.WriteBytesFull(SummedIdsByteArray[0]);
writer.WriteBytesFull(SummedIdsByteArray[1]);
writer.WriteBytesFull(SummedIdsByteArray[2]);
writer.WriteBytesFull(SummedIdsByteArray[3]);
byte[][] SumsByteArray = NetworkHelper.SerializeIntArray(Checksums);
writer.WriteBytesFull(SumsByteArray[0]);
writer.WriteBytesFull(SumsByteArray[1]);
writer.WriteBytesFull(SumsByteArray[2]);
writer.WriteBytesFull(SumsByteArray[3]);
}
}
}
}