SharedAccessSystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Content.Shared.Access.Components;
  2. using Content.Shared.Roles;
  3. using Robust.Shared.GameStates;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Serialization;
  6. namespace Content.Shared.Access.Systems
  7. {
  8. public abstract class SharedAccessSystem : EntitySystem
  9. {
  10. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<AccessComponent, MapInitEvent>(OnAccessInit);
  15. SubscribeLocalEvent<AccessComponent, GetAccessTagsEvent>(OnGetAccessTags);
  16. }
  17. private void OnAccessInit(EntityUid uid, AccessComponent component, MapInitEvent args)
  18. {
  19. // Add all tags in groups to the list of tags.
  20. foreach (var group in component.Groups)
  21. {
  22. if (!_prototypeManager.TryIndex<AccessGroupPrototype>(group, out var proto))
  23. continue;
  24. component.Tags.UnionWith(proto.Tags);
  25. Dirty(uid, component);
  26. }
  27. }
  28. private void OnGetAccessTags(EntityUid uid, AccessComponent component, ref GetAccessTagsEvent args)
  29. {
  30. if (!component.Enabled)
  31. return;
  32. args.Tags.UnionWith(component.Tags);
  33. }
  34. public void SetAccessEnabled(EntityUid uid, bool val, AccessComponent? component = null)
  35. {
  36. if (!Resolve(uid, ref component, false))
  37. return;
  38. component.Enabled = val;
  39. Dirty(uid, component);
  40. }
  41. /// <summary>
  42. /// Replaces the set of access tags we have with the provided set.
  43. /// </summary>
  44. /// <param name="access">The new access tags</param>
  45. public bool TrySetTags(EntityUid uid, IEnumerable<ProtoId<AccessLevelPrototype>> newTags, AccessComponent? access = null)
  46. {
  47. if (!Resolve(uid, ref access))
  48. return false;
  49. access.Tags.Clear();
  50. access.Tags.UnionWith(newTags);
  51. Dirty(uid, access);
  52. return true;
  53. }
  54. /// <summary>
  55. /// Gets the set of access tags.
  56. /// </summary>
  57. /// <param name="access">The new access tags</param>
  58. public IEnumerable<ProtoId<AccessLevelPrototype>>? TryGetTags(EntityUid uid, AccessComponent? access = null)
  59. {
  60. return !Resolve(uid, ref access) ? null : access.Tags;
  61. }
  62. public bool TryAddGroups(EntityUid uid, IEnumerable<ProtoId<AccessGroupPrototype>> newGroups, AccessComponent? access = null)
  63. {
  64. if (!Resolve(uid, ref access))
  65. return false;
  66. foreach (var group in newGroups)
  67. {
  68. if (!_prototypeManager.TryIndex<AccessGroupPrototype>(group, out var proto))
  69. continue;
  70. access.Tags.UnionWith(proto.Tags);
  71. }
  72. Dirty(uid, access);
  73. return true;
  74. }
  75. /// <summary>
  76. /// Set the access on an <see cref="AccessComponent"/> to the access for a specific job.
  77. /// </summary>
  78. /// <param name="uid">The ID of the entity with the access component.</param>
  79. /// <param name="prototype">The job prototype to use access from.</param>
  80. /// <param name="extended">Whether to apply extended job access.</param>
  81. /// <param name="access">The access component.</param>
  82. public void SetAccessToJob(
  83. EntityUid uid,
  84. JobPrototype prototype,
  85. bool extended,
  86. AccessComponent? access = null)
  87. {
  88. if (!Resolve(uid, ref access))
  89. return;
  90. access.Tags.Clear();
  91. access.Tags.UnionWith(prototype.Access);
  92. Dirty(uid, access);
  93. TryAddGroups(uid, prototype.AccessGroups, access);
  94. if (extended)
  95. {
  96. access.Tags.UnionWith(prototype.ExtendedAccess);
  97. TryAddGroups(uid, prototype.ExtendedAccessGroups, access);
  98. }
  99. }
  100. }
  101. }