SyncObject.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 modifying 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 simply always records by default
  24. public Func<bool> IsRecording = () => true;
  25. // SyncList/Set/etc. shouldn't be modifiable if not owned.
  26. // otherwise they would silently get out of sync.
  27. // need a lambda because InitSyncObject is called in ctor, when
  28. // 'isClient' etc. aren't initialized yet.
  29. public Func<bool> IsWritable = () => true;
  30. /// <summary>Discard all the queued changes</summary>
  31. // Consider the object fully synchronized with clients
  32. public abstract void ClearChanges();
  33. /// <summary>Write a full copy of the object</summary>
  34. public abstract void OnSerializeAll(NetworkWriter writer);
  35. /// <summary>Write the changes made to the object since last sync</summary>
  36. public abstract void OnSerializeDelta(NetworkWriter writer);
  37. /// <summary>Reads a full copy of the object</summary>
  38. public abstract void OnDeserializeAll(NetworkReader reader);
  39. /// <summary>Reads the changes made to the object since last sync</summary>
  40. public abstract void OnDeserializeDelta(NetworkReader reader);
  41. /// <summary>Resets the SyncObject so that it can be re-used</summary>
  42. public abstract void Reset();
  43. }
  44. }