SyncObject.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. namespace Mirror
  3. {
  4. /// <summary>SyncObjects sync state between server and client. E.g. SyncLists.</summary>
  5. // SyncObject should be a class (instead of an interface) for a few reasons:
  6. // * NetworkBehaviour stores SyncObjects in a list. structs would be a copy
  7. // and OnSerialize would use the copy instead of the original struct.
  8. // * Obsolete functions like Flush() don't need to be defined by each type
  9. // * OnDirty/IsRecording etc. default functions can be defined once here
  10. // for example, handling 'OnDirty wasn't initialized' with a default
  11. // function that throws an exception will be useful for SyncVar<T>
  12. public abstract class SyncObject
  13. {
  14. /// <summary>Used internally to set owner NetworkBehaviour's dirty mask bit when changed.</summary>
  15. public Action OnDirty;
  16. /// <summary>Used internally to check if we are currently tracking changes.</summary>
  17. // prevents ever growing .changes lists:
  18. // if a monster has no observers but we keep modifing a SyncObject,
  19. // then the changes would never be flushed and keep growing,
  20. // because OnSerialize isn't called without observers.
  21. // => Func so we can set it to () => observers.Count > 0
  22. // without depending on NetworkComponent/NetworkIdentity here.
  23. // => virtual so it sipmly always records by default
  24. public Func<bool> IsRecording = () => true;
  25. /// <summary>Discard all the queued changes</summary>
  26. // Consider the object fully synchronized with clients
  27. public abstract void ClearChanges();
  28. // Deprecated 2021-09-17
  29. [Obsolete("Deprecated: Use ClearChanges instead.")]
  30. public void Flush() => ClearChanges();
  31. /// <summary>Write a full copy of the object</summary>
  32. public abstract void OnSerializeAll(NetworkWriter writer);
  33. /// <summary>Write the changes made to the object since last sync</summary>
  34. public abstract void OnSerializeDelta(NetworkWriter writer);
  35. /// <summary>Reads a full copy of the object</summary>
  36. public abstract void OnDeserializeAll(NetworkReader reader);
  37. /// <summary>Reads the changes made to the object since last sync</summary>
  38. public abstract void OnDeserializeDelta(NetworkReader reader);
  39. /// <summary>Resets the SyncObject so that it can be re-used</summary>
  40. public abstract void Reset();
  41. }
  42. }