2021-07-30 19:22:06 +02:00
|
|
|
using Content.Server.Access.Components;
|
2021-10-22 05:31:07 +03:00
|
|
|
using Content.Server.Access.Systems;
|
2021-07-30 19:22:06 +02:00
|
|
|
using Content.Server.Storage.Components;
|
2022-02-17 21:43:24 -05:00
|
|
|
using Content.Shared.Emag.Systems;
|
|
|
|
|
using Content.Shared.Emag.Components;
|
2021-12-26 17:07:28 +13:00
|
|
|
using Content.Shared.Access.Components;
|
|
|
|
|
using Content.Shared.Access.Systems;
|
2021-07-30 19:22:06 +02:00
|
|
|
using Content.Shared.Examine;
|
|
|
|
|
using Content.Shared.Interaction;
|
2021-09-26 15:18:45 +02:00
|
|
|
using Content.Shared.Popups;
|
2021-07-30 19:22:06 +02:00
|
|
|
using Content.Shared.Storage;
|
2021-10-05 14:29:03 +11:00
|
|
|
using Content.Shared.Verbs;
|
2021-07-30 19:22:06 +02:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.Audio;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
2021-10-22 05:31:07 +03:00
|
|
|
using Robust.Shared.IoC;
|
2021-07-30 19:22:06 +02:00
|
|
|
using Robust.Shared.Localization;
|
|
|
|
|
using Robust.Shared.Player;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Lock
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles (un)locking and examining of Lock components
|
|
|
|
|
/// </summary>
|
|
|
|
|
[UsedImplicitly]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class LockSystem : EntitySystem
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-10-22 05:31:07 +03:00
|
|
|
[Dependency] private readonly AccessReaderSystem _accessReader = default!;
|
|
|
|
|
|
2021-07-30 19:22:06 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
SubscribeLocalEvent<LockComponent, ComponentStartup>(OnStartup);
|
|
|
|
|
SubscribeLocalEvent<LockComponent, ActivateInWorldEvent>(OnActivated);
|
|
|
|
|
SubscribeLocalEvent<LockComponent, ExaminedEvent>(OnExamined);
|
2022-02-10 15:30:59 +13:00
|
|
|
SubscribeLocalEvent<LockComponent, GetVerbsEvent<AlternativeVerb>>(AddToggleLockVerb);
|
2022-02-17 21:43:24 -05:00
|
|
|
SubscribeLocalEvent<LockComponent, GotEmaggedEvent>(OnEmagged);
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-02 12:00:02 +02:00
|
|
|
private void OnStartup(EntityUid uid, LockComponent lockComp, ComponentStartup args)
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-12-08 13:00:43 +01:00
|
|
|
if (EntityManager.TryGetComponent(lockComp.Owner, out AppearanceComponent? appearance))
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
|
|
|
|
appearance.SetData(StorageVisuals.CanLock, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 12:00:02 +02:00
|
|
|
private void OnActivated(EntityUid uid, LockComponent lockComp, ActivateInWorldEvent args)
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
if (args.Handled)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-07-30 19:22:06 +02:00
|
|
|
// Only attempt an unlock by default on Activate
|
|
|
|
|
if (lockComp.Locked)
|
|
|
|
|
{
|
2021-10-06 03:02:03 +11:00
|
|
|
TryUnlock(uid, args.User, lockComp);
|
|
|
|
|
args.Handled = true;
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
2021-10-02 12:00:02 +02:00
|
|
|
else if (lockComp.LockOnClick)
|
2021-08-22 19:32:24 +03:00
|
|
|
{
|
2021-10-06 03:02:03 +11:00
|
|
|
TryLock(uid, args.User, lockComp);
|
|
|
|
|
args.Handled = true;
|
2021-08-22 19:32:24 +03:00
|
|
|
}
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-02 12:00:02 +02:00
|
|
|
private void OnExamined(EntityUid uid, LockComponent lockComp, ExaminedEvent args)
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-09-15 16:58:15 +02:00
|
|
|
args.PushText(Loc.GetString(lockComp.Locked
|
|
|
|
|
? "lock-comp-on-examined-is-locked"
|
|
|
|
|
: "lock-comp-on-examined-is-unlocked",
|
2022-01-09 20:10:36 -08:00
|
|
|
("entityName", EntityManager.GetComponent<MetaDataComponent>(lockComp.Owner).EntityName)));
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public bool TryLock(EntityUid uid, EntityUid user, LockComponent? lockComp = null)
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-10-02 12:00:02 +02:00
|
|
|
if (!Resolve(uid, ref lockComp))
|
|
|
|
|
return false;
|
2021-12-03 14:05:23 +01:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!CanToggleLock(uid, user, quiet: false))
|
|
|
|
|
return false;
|
2021-12-03 14:05:23 +01:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!HasUserAccess(uid, user, quiet: false))
|
2021-10-02 12:00:02 +02:00
|
|
|
return false;
|
2021-07-30 19:22:06 +02:00
|
|
|
|
2022-01-09 20:10:36 -08:00
|
|
|
lockComp.Owner.PopupMessage(user, Loc.GetString("lock-comp-do-lock-success", ("entityName", EntityManager.GetComponent<MetaDataComponent>(lockComp.Owner).EntityName)));
|
2021-07-30 19:22:06 +02:00
|
|
|
lockComp.Locked = true;
|
2021-12-03 14:05:23 +01:00
|
|
|
|
2021-12-20 15:20:27 +01:00
|
|
|
if(lockComp.LockSound != null)
|
|
|
|
|
{
|
|
|
|
|
SoundSystem.Play(Filter.Pvs(lockComp.Owner), lockComp.LockSound.GetSound(), lockComp.Owner, AudioParams.Default.WithVolume(-5));
|
|
|
|
|
}
|
2021-09-15 16:58:15 +02:00
|
|
|
|
2021-12-08 13:00:43 +01:00
|
|
|
if (EntityManager.TryGetComponent(lockComp.Owner, out AppearanceComponent? appearanceComp))
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
|
|
|
|
appearanceComp.SetData(StorageVisuals.Locked, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 15:53:09 +01:00
|
|
|
RaiseLocalEvent(lockComp.Owner, new LockToggledEvent(true));
|
2021-08-22 19:32:24 +03:00
|
|
|
|
2021-10-02 12:00:02 +02:00
|
|
|
return true;
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-05 23:18:48 -07:00
|
|
|
public void Unlock(EntityUid uid, EntityUid user, LockComponent? lockComp = null)
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-10-02 12:00:02 +02:00
|
|
|
if (!Resolve(uid, ref lockComp))
|
2022-01-05 23:18:48 -07:00
|
|
|
return;
|
2021-07-30 19:22:06 +02:00
|
|
|
|
2022-01-09 20:10:36 -08:00
|
|
|
lockComp.Owner.PopupMessage(user, Loc.GetString("lock-comp-do-unlock-success", ("entityName", EntityManager.GetComponent<MetaDataComponent>(lockComp.Owner).EntityName)));
|
2021-07-30 19:22:06 +02:00
|
|
|
lockComp.Locked = false;
|
2021-12-03 14:05:23 +01:00
|
|
|
|
2022-01-05 23:18:48 -07:00
|
|
|
if (lockComp.UnlockSound != null)
|
2021-12-20 15:20:27 +01:00
|
|
|
{
|
|
|
|
|
SoundSystem.Play(Filter.Pvs(lockComp.Owner), lockComp.UnlockSound.GetSound(), lockComp.Owner, AudioParams.Default.WithVolume(-5));
|
|
|
|
|
}
|
2021-09-15 16:58:15 +02:00
|
|
|
|
2021-12-08 13:00:43 +01:00
|
|
|
if (EntityManager.TryGetComponent(lockComp.Owner, out AppearanceComponent? appearanceComp))
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
|
|
|
|
appearanceComp.SetData(StorageVisuals.Locked, false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 15:53:09 +01:00
|
|
|
RaiseLocalEvent(lockComp.Owner, new LockToggledEvent(false));
|
2022-01-05 23:18:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryUnlock(EntityUid uid, EntityUid user, LockComponent? lockComp = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref lockComp))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!CanToggleLock(uid, user, quiet: false))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!HasUserAccess(uid, user, quiet: false))
|
|
|
|
|
return false;
|
2021-08-22 19:32:24 +03:00
|
|
|
|
2022-01-05 23:18:48 -07:00
|
|
|
Unlock(uid, user, lockComp);
|
2021-10-02 12:00:02 +02:00
|
|
|
return true;
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-29 00:29:19 +13:00
|
|
|
/// <summary>
|
|
|
|
|
/// Before locking the entity, check whether it's a locker. If is, prevent it from being locked from the inside or while it is open.
|
|
|
|
|
/// </summary>
|
2021-12-05 18:09:01 +01:00
|
|
|
public bool CanToggleLock(EntityUid uid, EntityUid user, EntityStorageComponent? storage = null, bool quiet = true)
|
2021-10-05 14:29:03 +11:00
|
|
|
{
|
2021-10-29 00:29:19 +13:00
|
|
|
if (!Resolve(uid, ref storage, logMissing: false))
|
2021-10-05 14:29:03 +11:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// Cannot lock if the entity is currently opened.
|
|
|
|
|
if (storage.Open)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Cannot (un)lock from the inside. Maybe a bad idea? Security jocks could trap nerds in lockers?
|
|
|
|
|
if (storage.Contents.Contains(user))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 17:07:28 +13:00
|
|
|
private bool HasUserAccess(EntityUid uid, EntityUid user, AccessReaderComponent? reader = null, bool quiet = true)
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-10-02 12:00:02 +02:00
|
|
|
// Not having an AccessComponent means you get free access. woo!
|
|
|
|
|
if (!Resolve(uid, ref reader))
|
|
|
|
|
return true;
|
|
|
|
|
|
2021-12-03 15:53:09 +01:00
|
|
|
if (!_accessReader.IsAllowed(reader, user))
|
2021-07-30 19:22:06 +02:00
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!quiet)
|
|
|
|
|
reader.Owner.PopupMessage(user, Loc.GetString("lock-comp-has-user-access-fail"));
|
2021-10-02 12:00:02 +02:00
|
|
|
return false;
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-10-05 14:29:03 +11:00
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
private void AddToggleLockVerb(EntityUid uid, LockComponent component, GetVerbsEvent<AlternativeVerb> args)
|
2021-10-05 14:29:03 +11:00
|
|
|
{
|
|
|
|
|
if (!args.CanAccess || !args.CanInteract || !CanToggleLock(uid, args.User))
|
|
|
|
|
return;
|
|
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
AlternativeVerb verb = new();
|
2021-10-05 14:29:03 +11:00
|
|
|
verb.Act = component.Locked ?
|
|
|
|
|
() => TryUnlock(uid, args.User, component) :
|
|
|
|
|
() => TryLock(uid, args.User, component);
|
|
|
|
|
verb.Text = Loc.GetString(component.Locked ? "toggle-lock-verb-unlock" : "toggle-lock-verb-lock");
|
|
|
|
|
// TODO VERB ICONS need padlock open/close icons.
|
|
|
|
|
args.Verbs.Add(verb);
|
|
|
|
|
}
|
2022-02-17 21:43:24 -05:00
|
|
|
|
|
|
|
|
private void OnEmagged(EntityUid uid, LockComponent component, GotEmaggedEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (component.Locked == true)
|
|
|
|
|
{
|
|
|
|
|
if (component.UnlockSound != null)
|
|
|
|
|
{
|
|
|
|
|
SoundSystem.Play(Filter.Pvs(component.Owner), component.UnlockSound.GetSound(), component.Owner, AudioParams.Default.WithVolume(-5));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (EntityManager.TryGetComponent(component.Owner, out AppearanceComponent? appearanceComp))
|
|
|
|
|
{
|
|
|
|
|
appearanceComp.SetData(StorageVisuals.Locked, false);
|
|
|
|
|
}
|
|
|
|
|
EntityManager.RemoveComponent<LockComponent>(uid); //Literally destroys the lock as a tell it was emagged
|
|
|
|
|
args.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-30 19:22:06 +02:00
|
|
|
}
|
|
|
|
|
}
|