1
0

LockSystem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using Content.Shared.Access.Components;
  2. using Content.Shared.Access.Systems;
  3. using Content.Shared.ActionBlocker;
  4. using Content.Shared.Construction.Components;
  5. using Content.Shared.DoAfter;
  6. using Content.Shared.Emag.Systems;
  7. using Content.Shared.Examine;
  8. using Content.Shared.IdentityManagement;
  9. using Content.Shared.Interaction;
  10. using Content.Shared.Popups;
  11. using Content.Shared.Storage;
  12. using Content.Shared.Storage.Components;
  13. using Content.Shared.UserInterface;
  14. using Content.Shared.Verbs;
  15. using Content.Shared.Wires;
  16. using JetBrains.Annotations;
  17. using Robust.Shared.Audio.Systems;
  18. using Robust.Shared.Utility;
  19. namespace Content.Shared.Lock;
  20. /// <summary>
  21. /// Handles (un)locking and examining of Lock components
  22. /// </summary>
  23. [UsedImplicitly]
  24. public sealed class LockSystem : EntitySystem
  25. {
  26. [Dependency] private readonly AccessReaderSystem _accessReader = default!;
  27. [Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
  28. [Dependency] private readonly ActivatableUISystem _activatableUI = default!;
  29. [Dependency] private readonly EmagSystem _emag = default!;
  30. [Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
  31. [Dependency] private readonly SharedAudioSystem _audio = default!;
  32. [Dependency] private readonly SharedPopupSystem _sharedPopupSystem = default!;
  33. [Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
  34. /// <inheritdoc />
  35. public override void Initialize()
  36. {
  37. base.Initialize();
  38. SubscribeLocalEvent<LockComponent, ComponentStartup>(OnStartup);
  39. SubscribeLocalEvent<LockComponent, ActivateInWorldEvent>(OnActivated);
  40. SubscribeLocalEvent<LockComponent, StorageOpenAttemptEvent>(OnStorageOpenAttempt);
  41. SubscribeLocalEvent<LockComponent, ExaminedEvent>(OnExamined);
  42. SubscribeLocalEvent<LockComponent, GetVerbsEvent<AlternativeVerb>>(AddToggleLockVerb);
  43. SubscribeLocalEvent<LockComponent, GotEmaggedEvent>(OnEmagged);
  44. SubscribeLocalEvent<LockComponent, LockDoAfter>(OnDoAfterLock);
  45. SubscribeLocalEvent<LockComponent, UnlockDoAfter>(OnDoAfterUnlock);
  46. SubscribeLocalEvent<LockComponent, StorageInteractAttemptEvent>(OnStorageInteractAttempt);
  47. SubscribeLocalEvent<LockedWiresPanelComponent, LockToggleAttemptEvent>(OnLockToggleAttempt);
  48. SubscribeLocalEvent<LockedWiresPanelComponent, AttemptChangePanelEvent>(OnAttemptChangePanel);
  49. SubscribeLocalEvent<LockedAnchorableComponent, UnanchorAttemptEvent>(OnUnanchorAttempt);
  50. SubscribeLocalEvent<ActivatableUIRequiresLockComponent, ActivatableUIOpenAttemptEvent>(OnUIOpenAttempt);
  51. SubscribeLocalEvent<ActivatableUIRequiresLockComponent, LockToggledEvent>(LockToggled);
  52. }
  53. private void OnStartup(EntityUid uid, LockComponent lockComp, ComponentStartup args)
  54. {
  55. _appearanceSystem.SetData(uid, LockVisuals.Locked, lockComp.Locked);
  56. }
  57. private void OnActivated(EntityUid uid, LockComponent lockComp, ActivateInWorldEvent args)
  58. {
  59. if (args.Handled || !args.Complex)
  60. return;
  61. // Only attempt an unlock by default on Activate
  62. if (lockComp.Locked && lockComp.UnlockOnClick)
  63. {
  64. TryUnlock(uid, args.User, lockComp);
  65. args.Handled = true;
  66. }
  67. else if (!lockComp.Locked && lockComp.LockOnClick)
  68. {
  69. TryLock(uid, args.User, lockComp);
  70. args.Handled = true;
  71. }
  72. }
  73. private void OnStorageOpenAttempt(EntityUid uid, LockComponent component, ref StorageOpenAttemptEvent args)
  74. {
  75. if (!component.Locked)
  76. return;
  77. if (!args.Silent)
  78. _sharedPopupSystem.PopupClient(Loc.GetString("entity-storage-component-locked-message"), uid, args.User);
  79. args.Cancelled = true;
  80. }
  81. private void OnExamined(EntityUid uid, LockComponent lockComp, ExaminedEvent args)
  82. {
  83. args.PushText(Loc.GetString(lockComp.Locked
  84. ? "lock-comp-on-examined-is-locked"
  85. : "lock-comp-on-examined-is-unlocked",
  86. ("entityName", Identity.Name(uid, EntityManager))));
  87. }
  88. /// <summary>
  89. /// Attmempts to lock a given entity
  90. /// </summary>
  91. /// <remarks>
  92. /// If the lock is set to require a do-after, a true return value only indicates that the do-after started.
  93. /// </remarks>
  94. /// <param name="uid">The entity with the lock</param>
  95. /// <param name="user">The person trying to lock it</param>
  96. /// <param name="lockComp"></param>
  97. /// <param name="skipDoAfter">If true, skip the required do-after if one is configured.</param>
  98. /// <returns>If locking was successful</returns>
  99. public bool TryLock(EntityUid uid, EntityUid user, LockComponent? lockComp = null, bool skipDoAfter = false)
  100. {
  101. if (!Resolve(uid, ref lockComp))
  102. return false;
  103. if (!CanToggleLock(uid, user, quiet: false))
  104. return false;
  105. if (!HasUserAccess(uid, user, quiet: false))
  106. return false;
  107. if (!skipDoAfter && lockComp.LockTime != TimeSpan.Zero)
  108. {
  109. return _doAfter.TryStartDoAfter(
  110. new DoAfterArgs(EntityManager, user, lockComp.LockTime, new LockDoAfter(), uid, uid)
  111. {
  112. BreakOnDamage = true,
  113. BreakOnMove = true,
  114. NeedHand = true,
  115. BreakOnDropItem = false,
  116. });
  117. }
  118. Lock(uid, user, lockComp);
  119. return true;
  120. }
  121. /// <summary>
  122. /// Forces a given entity to be locked, does not activate a do-after.
  123. /// </summary>
  124. public void Lock(EntityUid uid, EntityUid? user, LockComponent? lockComp = null)
  125. {
  126. if (!Resolve(uid, ref lockComp))
  127. return;
  128. if (user is { Valid: true })
  129. {
  130. _sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-do-lock-success",
  131. ("entityName", Identity.Name(uid, EntityManager))), uid, user);
  132. }
  133. _audio.PlayPredicted(lockComp.LockSound, uid, user);
  134. lockComp.Locked = true;
  135. _appearanceSystem.SetData(uid, LockVisuals.Locked, true);
  136. Dirty(uid, lockComp);
  137. var ev = new LockToggledEvent(true);
  138. RaiseLocalEvent(uid, ref ev, true);
  139. }
  140. /// <summary>
  141. /// Forces a given entity to be unlocked
  142. /// </summary>
  143. /// <remarks>
  144. /// This does not process do-after times.
  145. /// </remarks>
  146. /// <param name="uid">The entity with the lock</param>
  147. /// <param name="user">The person unlocking it. Can be null</param>
  148. /// <param name="lockComp"></param>
  149. public void Unlock(EntityUid uid, EntityUid? user, LockComponent? lockComp = null)
  150. {
  151. if (!Resolve(uid, ref lockComp))
  152. return;
  153. if (user is { Valid: true })
  154. {
  155. _sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-do-unlock-success",
  156. ("entityName", Identity.Name(uid, EntityManager))), uid, user.Value);
  157. }
  158. _audio.PlayPredicted(lockComp.UnlockSound, uid, user);
  159. lockComp.Locked = false;
  160. _appearanceSystem.SetData(uid, LockVisuals.Locked, false);
  161. Dirty(uid, lockComp);
  162. var ev = new LockToggledEvent(false);
  163. RaiseLocalEvent(uid, ref ev, true);
  164. }
  165. /// <summary>
  166. /// Attmempts to unlock a given entity
  167. /// </summary>
  168. /// <remarks>
  169. /// If the lock is set to require a do-after, a true return value only indicates that the do-after started.
  170. /// </remarks>
  171. /// <param name="uid">The entity with the lock</param>
  172. /// <param name="user">The person trying to unlock it</param>
  173. /// <param name="lockComp"></param>
  174. /// <param name="skipDoAfter">If true, skip the required do-after if one is configured.</param>
  175. /// <returns>If locking was successful</returns>
  176. public bool TryUnlock(EntityUid uid, EntityUid user, LockComponent? lockComp = null, bool skipDoAfter = false)
  177. {
  178. if (!Resolve(uid, ref lockComp))
  179. return false;
  180. if (!CanToggleLock(uid, user, quiet: false))
  181. return false;
  182. if (!HasUserAccess(uid, user, quiet: false))
  183. return false;
  184. if (!skipDoAfter && lockComp.UnlockTime != TimeSpan.Zero)
  185. {
  186. return _doAfter.TryStartDoAfter(
  187. new DoAfterArgs(EntityManager, user, lockComp.LockTime, new UnlockDoAfter(), uid, uid)
  188. {
  189. BreakOnDamage = true,
  190. BreakOnMove = true,
  191. NeedHand = true,
  192. BreakOnDropItem = false,
  193. });
  194. }
  195. Unlock(uid, user, lockComp);
  196. return true;
  197. }
  198. /// <summary>
  199. /// Returns true if the entity is locked.
  200. /// Entities with no lock component are considered unlocked.
  201. /// </summary>
  202. public bool IsLocked(Entity<LockComponent?> ent)
  203. {
  204. if (!Resolve(ent, ref ent.Comp, false))
  205. return false;
  206. return ent.Comp.Locked;
  207. }
  208. /// <summary>
  209. /// Raises an event for other components to check whether or not
  210. /// the entity can be locked in its current state.
  211. /// </summary>
  212. public bool CanToggleLock(EntityUid uid, EntityUid user, bool quiet = true)
  213. {
  214. if (!_actionBlocker.CanComplexInteract(user))
  215. return false;
  216. var ev = new LockToggleAttemptEvent(user, quiet);
  217. RaiseLocalEvent(uid, ref ev, true);
  218. if (ev.Cancelled)
  219. return false;
  220. var userEv = new UserLockToggleAttemptEvent(uid, quiet);
  221. RaiseLocalEvent(user, ref userEv, true);
  222. return !userEv.Cancelled;
  223. }
  224. // TODO: this should be a helper on AccessReaderSystem since so many systems copy paste it
  225. private bool HasUserAccess(EntityUid uid, EntityUid user, AccessReaderComponent? reader = null, bool quiet = true)
  226. {
  227. // Not having an AccessComponent means you get free access. woo!
  228. if (!Resolve(uid, ref reader, false))
  229. return true;
  230. if (_accessReader.IsAllowed(user, uid, reader))
  231. return true;
  232. if (!quiet)
  233. _sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-has-user-access-fail"), uid, user);
  234. return false;
  235. }
  236. private void AddToggleLockVerb(EntityUid uid, LockComponent component, GetVerbsEvent<AlternativeVerb> args)
  237. {
  238. if (!args.CanAccess || !args.CanInteract)
  239. return;
  240. AlternativeVerb verb = new()
  241. {
  242. Act = component.Locked
  243. ? () => TryUnlock(uid, args.User, component)
  244. : () => TryLock(uid, args.User, component),
  245. Text = Loc.GetString(component.Locked ? "toggle-lock-verb-unlock" : "toggle-lock-verb-lock"),
  246. Icon = !component.Locked
  247. ? new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/lock.svg.192dpi.png"))
  248. : new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/unlock.svg.192dpi.png")),
  249. };
  250. args.Verbs.Add(verb);
  251. }
  252. private void OnEmagged(EntityUid uid, LockComponent component, ref GotEmaggedEvent args)
  253. {
  254. if (!_emag.CompareFlag(args.Type, EmagType.Access))
  255. return;
  256. if (!component.Locked || !component.BreakOnAccessBreaker)
  257. return;
  258. _audio.PlayPredicted(component.UnlockSound, uid, args.UserUid);
  259. component.Locked = false;
  260. _appearanceSystem.SetData(uid, LockVisuals.Locked, false);
  261. Dirty(uid, component);
  262. var ev = new LockToggledEvent(false);
  263. RaiseLocalEvent(uid, ref ev, true);
  264. args.Repeatable = true;
  265. args.Handled = true;
  266. }
  267. private void OnDoAfterLock(EntityUid uid, LockComponent component, LockDoAfter args)
  268. {
  269. if (args.Cancelled)
  270. return;
  271. TryLock(uid, args.User, skipDoAfter: true);
  272. }
  273. private void OnDoAfterUnlock(EntityUid uid, LockComponent component, UnlockDoAfter args)
  274. {
  275. if (args.Cancelled)
  276. return;
  277. TryUnlock(uid, args.User, skipDoAfter: true);
  278. }
  279. private void OnStorageInteractAttempt(Entity<LockComponent> ent, ref StorageInteractAttemptEvent args)
  280. {
  281. if (ent.Comp.Locked)
  282. args.Cancelled = true;
  283. }
  284. private void OnLockToggleAttempt(Entity<LockedWiresPanelComponent> ent, ref LockToggleAttemptEvent args)
  285. {
  286. if (args.Cancelled)
  287. return;
  288. if (!TryComp<WiresPanelComponent>(ent, out var panel) || !panel.Open)
  289. return;
  290. if (!args.Silent)
  291. {
  292. _sharedPopupSystem.PopupClient(Loc.GetString("construction-step-condition-wire-panel-close"),
  293. ent,
  294. args.User);
  295. }
  296. args.Cancelled = true;
  297. }
  298. private void OnAttemptChangePanel(Entity<LockedWiresPanelComponent> ent, ref AttemptChangePanelEvent args)
  299. {
  300. if (args.Cancelled)
  301. return;
  302. if (!TryComp<LockComponent>(ent, out var lockComp) || !lockComp.Locked)
  303. return;
  304. _sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-generic-fail",
  305. ("target", Identity.Entity(ent, EntityManager))),
  306. ent,
  307. args.User);
  308. args.Cancelled = true;
  309. }
  310. private void OnUnanchorAttempt(Entity<LockedAnchorableComponent> ent, ref UnanchorAttemptEvent args)
  311. {
  312. if (args.Cancelled)
  313. return;
  314. if (!TryComp<LockComponent>(ent, out var lockComp) || !lockComp.Locked)
  315. return;
  316. _sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-generic-fail",
  317. ("target", Identity.Entity(ent, EntityManager))),
  318. ent,
  319. args.User);
  320. args.Cancel();
  321. }
  322. private void OnUIOpenAttempt(EntityUid uid, ActivatableUIRequiresLockComponent component, ActivatableUIOpenAttemptEvent args)
  323. {
  324. if (args.Cancelled)
  325. return;
  326. if (TryComp<LockComponent>(uid, out var lockComp) && lockComp.Locked != component.RequireLocked)
  327. {
  328. args.Cancel();
  329. if (lockComp.Locked)
  330. _sharedPopupSystem.PopupClient(Loc.GetString("entity-storage-component-locked-message"), uid, args.User);
  331. }
  332. }
  333. private void LockToggled(EntityUid uid, ActivatableUIRequiresLockComponent component, LockToggledEvent args)
  334. {
  335. if (!TryComp<LockComponent>(uid, out var lockComp) || lockComp.Locked == component.RequireLocked)
  336. return;
  337. _activatableUI.CloseAll(uid);
  338. }
  339. }