2022-01-13 23:38:45 -08:00
|
|
|
|
using Mirror;
|
|
|
|
|
using QSB.WorldSync;
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
namespace QSB.Messaging;
|
|
|
|
|
|
|
|
|
|
public abstract class QSBWorldObjectMessage<T> : QSBMessage where T : IWorldObject
|
2021-12-10 23:28:42 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// set automatically by SendMessage
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal int ObjectId;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// set automatically by ShouldReceive
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected T WorldObject { get; private set; }
|
|
|
|
|
|
|
|
|
|
public override void Serialize(NetworkWriter writer)
|
2021-12-10 23:28:42 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
base.Serialize(writer);
|
|
|
|
|
writer.Write(ObjectId);
|
|
|
|
|
}
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override void Deserialize(NetworkReader reader)
|
|
|
|
|
{
|
|
|
|
|
base.Deserialize(reader);
|
|
|
|
|
ObjectId = reader.Read<int>();
|
|
|
|
|
}
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override bool ShouldReceive
|
|
|
|
|
{
|
|
|
|
|
get
|
2021-12-10 23:28:42 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
if (!QSBWorldSync.AllObjectsReady)
|
2021-12-10 23:28:42 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
return false;
|
2022-02-27 04:40:44 -08:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
|
|
|
|
|
WorldObject = ObjectId.GetWorldObject<T>();
|
|
|
|
|
return true;
|
2021-12-10 23:28:42 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public abstract class QSBWorldObjectMessage<T, D> : QSBWorldObjectMessage<T> where T : IWorldObject
|
|
|
|
|
{
|
2022-03-10 17:39:00 -08:00
|
|
|
|
protected D Data { get; private set; }
|
2022-03-10 17:28:52 -08:00
|
|
|
|
protected QSBWorldObjectMessage(D data) => Data = data;
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override void Serialize(NetworkWriter writer)
|
|
|
|
|
{
|
|
|
|
|
base.Serialize(writer);
|
|
|
|
|
writer.Write(Data);
|
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override void Deserialize(NetworkReader reader)
|
|
|
|
|
{
|
|
|
|
|
base.Deserialize(reader);
|
|
|
|
|
Data = reader.Read<D>();
|
2021-12-10 23:28:42 -08:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|