1
0

EntityWhitelistSystem.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Item;
  3. using Content.Shared.Roles;
  4. using Content.Shared.Tag;
  5. namespace Content.Shared.Whitelist;
  6. public sealed class EntityWhitelistSystem : EntitySystem
  7. {
  8. [Dependency] private readonly IComponentFactory _factory = default!;
  9. [Dependency] private readonly SharedRoleSystem _roles = default!;
  10. [Dependency] private readonly TagSystem _tag = default!;
  11. private EntityQuery<ItemComponent> _itemQuery;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. _itemQuery = GetEntityQuery<ItemComponent>();
  16. }
  17. /// <inheritdoc cref="IsValid(Content.Shared.Whitelist.EntityWhitelist,Robust.Shared.GameObjects.EntityUid)"/>
  18. public bool IsValid(EntityWhitelist list, [NotNullWhen(true)] EntityUid? uid)
  19. {
  20. return uid != null && IsValid(list, uid.Value);
  21. }
  22. /// <summary>
  23. /// Checks whether a given entity is allowed by a whitelist and not blocked by a blacklist.
  24. /// If a blacklist is provided and it matches then this returns false.
  25. /// If a whitelist is provided and it does not match then this returns false.
  26. /// If either list is null it does not get checked.
  27. /// </summary>
  28. public bool CheckBoth([NotNullWhen(true)] EntityUid? uid, EntityWhitelist? blacklist = null, EntityWhitelist? whitelist = null)
  29. {
  30. if (uid == null)
  31. return false;
  32. if (blacklist != null && IsValid(blacklist, uid))
  33. return false;
  34. return whitelist == null || IsValid(whitelist, uid);
  35. }
  36. /// <summary>
  37. /// Checks whether a given entity satisfies a whitelist.
  38. /// </summary>
  39. public bool IsValid(EntityWhitelist list, EntityUid uid)
  40. {
  41. if (list.Components != null)
  42. {
  43. if (list.Registrations == null)
  44. {
  45. var regs = StringsToRegs(list.Components);
  46. list.Registrations = new List<ComponentRegistration>();
  47. list.Registrations.AddRange(regs);
  48. }
  49. }
  50. if (list.MindRoles != null)
  51. {
  52. var regs = StringsToRegs(list.MindRoles);
  53. foreach (var role in regs)
  54. {
  55. if ( _roles.MindHasRole(uid, role.Type, out _))
  56. {
  57. if (!list.RequireAll)
  58. return true;
  59. }
  60. else if (list.RequireAll)
  61. return false;
  62. }
  63. }
  64. if (list.Registrations != null && list.Registrations.Count > 0)
  65. {
  66. foreach (var reg in list.Registrations)
  67. {
  68. if (HasComp(uid, reg.Type))
  69. {
  70. if (!list.RequireAll)
  71. return true;
  72. }
  73. else if (list.RequireAll)
  74. return false;
  75. }
  76. }
  77. if (list.Sizes != null && _itemQuery.TryComp(uid, out var itemComp))
  78. {
  79. if (list.Sizes.Contains(itemComp.Size))
  80. return true;
  81. }
  82. if (list.Tags != null)
  83. {
  84. return list.RequireAll
  85. ? _tag.HasAllTags(uid, list.Tags)
  86. : _tag.HasAnyTag(uid, list.Tags);
  87. }
  88. return list.RequireAll;
  89. }
  90. /// The following are a list of "helper functions" that are basically the same as each other
  91. /// to help make code that uses EntityWhitelist a bit more readable because at the moment
  92. /// it is quite clunky having to write out component.Whitelist == null ? true : _whitelist.IsValid(component.Whitelist, uid)
  93. /// several times in a row and makes comparisons easier to read
  94. /// <summary>
  95. /// Helper function to determine if Whitelist is not null and entity is on list
  96. /// </summary>
  97. public bool IsWhitelistPass(EntityWhitelist? whitelist, EntityUid uid)
  98. {
  99. if (whitelist == null)
  100. return false;
  101. return IsValid(whitelist, uid);
  102. }
  103. /// <summary>
  104. /// Helper function to determine if Whitelist is not null and entity is not on the list
  105. /// </summary>
  106. public bool IsWhitelistFail(EntityWhitelist? whitelist, EntityUid uid)
  107. {
  108. if (whitelist == null)
  109. return false;
  110. return !IsValid(whitelist, uid);
  111. }
  112. /// <summary>
  113. /// Helper function to determine if Whitelist is either null or the entity is on the list
  114. /// </summary>
  115. public bool IsWhitelistPassOrNull(EntityWhitelist? whitelist, EntityUid uid)
  116. {
  117. if (whitelist == null)
  118. return true;
  119. return IsValid(whitelist, uid);
  120. }
  121. /// <summary>
  122. /// Helper function to determine if Whitelist is either null or the entity is not on the list
  123. /// </summary>
  124. public bool IsWhitelistFailOrNull(EntityWhitelist? whitelist, EntityUid uid)
  125. {
  126. if (whitelist == null)
  127. return true;
  128. return !IsValid(whitelist, uid);
  129. }
  130. /// <summary>
  131. /// Helper function to determine if Blacklist is not null and entity is on list
  132. /// Duplicate of equivalent Whitelist function
  133. /// </summary>
  134. public bool IsBlacklistPass(EntityWhitelist? blacklist, EntityUid uid)
  135. {
  136. return IsWhitelistPass(blacklist, uid);
  137. }
  138. /// <summary>
  139. /// Helper function to determine if Blacklist is not null and entity is not on the list
  140. /// Duplicate of equivalent Whitelist function
  141. /// </summary>
  142. public bool IsBlacklistFail(EntityWhitelist? blacklist, EntityUid uid)
  143. {
  144. return IsWhitelistFail(blacklist, uid);
  145. }
  146. /// <summary>
  147. /// Helper function to determine if Blacklist is either null or the entity is on the list
  148. /// Duplicate of equivalent Whitelist function
  149. /// </summary>
  150. public bool IsBlacklistPassOrNull(EntityWhitelist? blacklist, EntityUid uid)
  151. {
  152. return IsWhitelistPassOrNull(blacklist, uid);
  153. }
  154. /// <summary>
  155. /// Helper function to determine if Blacklist is either null or the entity is not on the list
  156. /// Duplicate of equivalent Whitelist function
  157. /// </summary>
  158. public bool IsBlacklistFailOrNull(EntityWhitelist? blacklist, EntityUid uid)
  159. {
  160. return IsWhitelistFailOrNull(blacklist, uid);
  161. }
  162. private List<ComponentRegistration> StringsToRegs(string[]? input)
  163. {
  164. var list = new List<ComponentRegistration>();
  165. if (input == null || input.Length == 0)
  166. return list;
  167. foreach (var name in input)
  168. {
  169. var availability = _factory.GetComponentAvailability(name);
  170. if (_factory.TryGetRegistration(name, out var registration)
  171. && availability == ComponentAvailability.Available)
  172. {
  173. list.Add(registration);
  174. }
  175. else if (availability == ComponentAvailability.Unknown)
  176. {
  177. Log.Error($"StringsToRegs failed: Unknown component name {name} passed to EntityWhitelist!");
  178. }
  179. }
  180. return list;
  181. }
  182. }