DoAfterComponent.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Threading.Tasks;
  2. using Robust.Shared.GameStates;
  3. using Robust.Shared.Serialization;
  4. namespace Content.Shared.DoAfter;
  5. [RegisterComponent, NetworkedComponent]
  6. [Access(typeof(SharedDoAfterSystem))]
  7. public sealed partial class DoAfterComponent : Component
  8. {
  9. [DataField("nextId")]
  10. public ushort NextId;
  11. [DataField("doAfters")]
  12. public Dictionary<ushort, DoAfter> DoAfters = new();
  13. // Used by obsolete async do afters
  14. public readonly Dictionary<ushort, TaskCompletionSource<DoAfterStatus>> AwaitedDoAfters = new();
  15. }
  16. [Serializable, NetSerializable]
  17. public sealed class DoAfterComponentState : ComponentState
  18. {
  19. public readonly ushort NextId;
  20. public readonly Dictionary<ushort, DoAfter> DoAfters;
  21. public DoAfterComponentState(IEntityManager entManager, DoAfterComponent component)
  22. {
  23. NextId = component.NextId;
  24. // Cursed test bugs - See CraftingTests.CancelCraft
  25. // The following is wrapped in an if DEBUG. This is tests don't (de)serialize net messages and just copy objects
  26. // by reference. This means that the server will directly modify cached server states on the client's end.
  27. // Crude fix at the moment is to used modified state handling while in debug mode Otherwise, this test cannot work.
  28. #if !DEBUG
  29. DoAfters = component.DoAfters;
  30. #else
  31. DoAfters = new();
  32. foreach (var (id, doAfter) in component.DoAfters)
  33. {
  34. var newDoAfter = new DoAfter(entManager, doAfter);
  35. DoAfters.Add(id, newDoAfter);
  36. }
  37. #endif
  38. }
  39. }
  40. [Serializable, NetSerializable]
  41. public enum DoAfterStatus : byte
  42. {
  43. Invalid,
  44. Running,
  45. Cancelled,
  46. Finished,
  47. }