fish-station/Content.Shared/Lock/LockSystem.cs
VigersRay 6c158e6f0b Merge remote-tracking branch 'space-wizards/master'
# Conflicts:
#	.github/CODEOWNERS
#	.github/ISSUE_TEMPLATE/config.yml
#	.github/ISSUE_TEMPLATE/issue_report.md
#	Content.Client/Lobby/UI/HumanoidProfileEditor.xaml.cs
#	Content.Client/Medical/CrewMonitoring/CrewMonitoringWindow.xaml
#	Content.Client/UserInterface/Systems/Ghost/Controls/Roles/MakeGhostRoleWindow.xaml
#	Content.Client/UserInterface/Systems/Ghost/Controls/Roles/MakeGhostRoleWindow.xaml.cs
#	Content.Client/Weapons/Ranged/Systems/GunSystem.cs
#	Content.IntegrationTests/Tests/Destructible/DestructibleDamageGroupTest.cs
#	Content.IntegrationTests/Tests/Destructible/DestructibleDamageTypeTest.cs
#	Content.Server/Administration/Systems/AdminVerbSystem.Smites.cs
#	Content.Server/Arcade/SpaceVillainGame/SpaceVillainArcadeComponent.cs
#	Content.Server/Atmos/EntitySystems/FlammableSystem.cs
#	Content.Server/Chat/Systems/ChatSystem.cs
#	Content.Server/Destructible/Thresholds/DamageThreshold.cs
#	Content.Server/Entry/EntryPoint.cs
#	Content.Server/Fluids/EntitySystems/PuddleSystem.cs
#	Content.Server/GuideGenerator/ReagentEntry.cs
#	Content.Server/IoC/ServerContentIoC.cs
#	Content.Server/Medical/HealthAnalyzerSystem.cs
#	Content.Server/Medical/VomitSystem.cs
#	Content.Server/Radio/EntitySystems/HeadsetSystem.cs
#	Content.Server/Revenant/EntitySystems/RevenantSystem.cs
#	Content.Server/Weapons/Ranged/Systems/GunSystem.cs
#	Content.Shared/Armor/SharedArmorSystem.cs
#	Content.Shared/Bed/Sleep/SleepingSystem.cs
#	Content.Shared/Cabinet/ItemCabinetSystem.cs
#	Content.Shared/Chat/SharedChatSystem.Emote.cs
#	Content.Shared/Chat/SharedChatSystem.cs
#	Content.Shared/Clothing/EntitySystems/MaskSystem.cs
#	Content.Shared/Clothing/EntitySystems/SharedChameleonClothingSystem.cs
#	Content.Shared/Damage/Systems/DamageableSystem.cs
#	Content.Shared/EntityEffects/Effects/StatusEffects/GenericStatusEffect.cs
#	Content.Shared/EntityEffects/Effects/Transform/ExplosionEntityEffect.cs
#	Content.Shared/Humanoid/HumanoidCharacterAppearance.cs
#	Content.Shared/IoC/SharedContentIoC.cs
#	Content.Shared/Lock/LockSystem.cs
#	Content.Shared/Ninja/Components/NinjaSuitComponent.cs
#	Content.Shared/Preferences/HumanoidCharacterProfile.cs
#	Content.Shared/Revenant/Components/RevenantComponent.cs
#	Content.Shared/Silicons/StationAi/SharedStationAiSystem.Airlock.cs
#	Content.Shared/Trigger/Systems/TriggerSystem.Condition.cs
#	Content.Shared/VendingMachines/SharedVendingMachineSystem.cs
#	Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs
#	Content.Shared/Weapons/Ranged/HitscanPrototype.cs
#	Content.Shared/Xenoarchaeology/Artifact/XAT/BaseQueryUpdateXATSystem.cs
#	README.md
#	Resources/Audio/Weapons/attributions.yml
#	Resources/Locale/en-US/_strings/ghost/make-ghost-gui.ftl
#	Resources/Locale/en-US/_strings/guidebook/chemistry/conditions.ftl
#	Resources/Locale/en-US/_strings/guidebook/chemistry/effects.ftl
#	Resources/Locale/en-US/_strings/guidebook/chemistry/healthchange.ftl
#	Resources/Locale/en-US/_strings/guidebook/chemistry/statuseffects.ftl
#	Resources/Locale/en-US/_strings/job/job-description.ftl
#	Resources/Locale/en-US/_strings/medical/components/crew-monitoring-component.ftl
#	Resources/Locale/en-US/_strings/npc/hugbot.ftl
#	Resources/Locale/en-US/_strings/npc/npc-debug.ftl
#	Resources/Locale/en-US/_strings/speech/speech-triggers.ftl
#	Resources/Locale/en-US/_strings/ui/stat-values.ftl
#	Resources/Locale/en-US/guidebook/chemistry/conditions.ftl
#	Resources/Locale/en-US/guidebook/chemistry/effects.ftl
#	Resources/Locale/en-US/guidebook/chemistry/healthchange.ftl
#	Resources/Locale/en-US/guidebook/entity-effects/conditions.ftl
#	Resources/Locale/en-US/guidebook/entity-effects/effects.ftl
#	Resources/Locale/en-US/guidebook/entity-effects/healthchange.ftl
#	Resources/Maps/Shuttles/cargo_relic.yml
#	Resources/Maps/_Sunrise/Station/bagel.yml
#	Resources/Maps/_Sunrise/Station/barratry.yml
#	Resources/Maps/_Sunrise/Station/fland.yml
#	Resources/Maps/_Sunrise/Station/marathon.yml
#	Resources/Maps/_Sunrise/Station/oasis.yml
#	Resources/Maps/_Sunrise/Station/packed.yml
#	Resources/Maps/_Sunrise/Station/plasma.yml
#	Resources/Maps/_Sunrise/Station/relic.yml
#	Resources/Maps/_Sunrise/event/clon_mine.yml
#	Resources/Maps/_Sunrise/event/void.yml
#	Resources/Prototypes/Body/Prototypes/vox.yml
#	Resources/Prototypes/Catalog/Fills/Boxes/emergency.yml
#	Resources/Prototypes/Catalog/Fills/Crates/fun.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/heads.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/security.yml
#	Resources/Prototypes/Entities/Clothing/Belt/belts.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/suits.yml
#	Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml
#	Resources/Prototypes/Entities/Mobs/Species/skeleton.yml
#	Resources/Prototypes/Entities/Objects/Devices/Electronics/base_electronics.yml
#	Resources/Prototypes/Entities/Objects/Misc/identification_cards.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Cartridges/pistol.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Magazines/pistol.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Battery/battery_guns.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/hitscan.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml
#	Resources/Prototypes/Entities/Structures/Doors/Airlocks/access.yml
#	Resources/Prototypes/Entities/Structures/Doors/Airlocks/airlocks.yml
#	Resources/Prototypes/Entities/Structures/Doors/MaterialDoors/material_doors.yml
#	Resources/Prototypes/Entities/Structures/Machines/lathe.yml
#	Resources/Prototypes/Entities/Structures/Specific/Atmospherics/sensor.yml
#	Resources/Prototypes/Loadouts/Jobs/Engineering/station_engineer.yml
#	Resources/Prototypes/Loadouts/Jobs/Science/scientist.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/security_officer.yml
#	Resources/Prototypes/Loadouts/Miscellaneous/glasses.yml
#	Resources/Prototypes/Loadouts/loadout_groups.yml
#	Resources/Prototypes/Objectives/objectiveGroups.yml
#	Resources/Prototypes/Reagents/Consumable/Drink/alcohol.yml
#	Resources/Prototypes/Reagents/medicine.yml
#	Resources/Prototypes/Reagents/narcotics.yml
#	Resources/Prototypes/Reagents/toxins.yml
#	Resources/Prototypes/Recipes/Lathes/Packs/biogen.yml
#	Resources/Prototypes/Recipes/Lathes/Packs/security.yml
#	Resources/Prototypes/Recipes/Lathes/biogen.yml
#	Resources/Prototypes/Roles/Antags/nukeops.yml
#	Resources/Prototypes/Roles/Antags/revolutionary.yml
#	Resources/Prototypes/Roles/Antags/thief.yml
#	Resources/Prototypes/Roles/Antags/traitor.yml
#	Resources/Prototypes/Roles/Antags/wizard.yml
#	Resources/Prototypes/Roles/Antags/zombie.yml
#	Resources/Prototypes/Roles/Jobs/Cargo/salvage_specialist.yml
#	Resources/Prototypes/Roles/Jobs/Civilian/bartender.yml
#	Resources/Prototypes/Roles/Jobs/Civilian/chef.yml
#	Resources/Prototypes/Roles/Jobs/Civilian/mime.yml
#	Resources/Prototypes/Roles/Jobs/Engineering/atmospheric_technician.yml
#	Resources/Prototypes/Roles/Jobs/Engineering/station_engineer.yml
#	Resources/Prototypes/Roles/Jobs/Engineering/technical_assistant.yml
#	Resources/Prototypes/Roles/Jobs/Medical/chemist.yml
#	Resources/Prototypes/Roles/Jobs/Medical/medical_doctor.yml
#	Resources/Prototypes/Roles/Jobs/Medical/medical_intern.yml
#	Resources/Prototypes/Roles/Jobs/Medical/paramedic.yml
#	Resources/Prototypes/Roles/Jobs/Science/borg.yml
#	Resources/Prototypes/Roles/Jobs/Science/research_assistant.yml
#	Resources/Prototypes/Roles/Jobs/Science/scientist.yml
#	Resources/Prototypes/Roles/Jobs/Security/security_cadet.yml
#	Resources/Prototypes/Roles/Jobs/Security/security_officer.yml
#	Resources/Prototypes/Roles/Jobs/Security/warden.yml
#	Resources/Prototypes/Stacks/Materials/crystals.yml
#	Resources/Prototypes/Stacks/floor_tile_stacks.yml
#	Resources/Prototypes/tags.yml
#	Resources/ServerInfo/Guidebook/Science/ArtifactReports.xml
#	Resources/ServerInfo/Guidebook/Science/Xenoarchaeology.xml
#	Resources/Textures/Clothing/Hands/Gloves/Color/black.rsi/equipped-HAND.png
#	Resources/Textures/Clothing/Hands/Gloves/Color/black.rsi/meta.json
#	Resources/Textures/Clothing/Head/Hardsuits/mime.rsi/meta.json
#	Resources/Textures/Effects/speech.rsi/moth3.png
#	Resources/Textures/Interface/Misc/job_icons.rsi/meta.json
#	Resources/Textures/Structures/Machines/microwave.rsi/meta.json
#	Resources/Textures/Structures/Machines/microwave.rsi/mw.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mw0.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mw_running_unlit.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mw_unlit.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mwb.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mwbloody.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mwbloody0.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mwbloody1.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mwbloodyo.png
#	Resources/Textures/Structures/Machines/microwave.rsi/mwo.png
#	Resources/Textures/Structures/Machines/microwave_syndie.rsi/meta.json
#	Resources/Textures/Structures/Machines/microwave_syndie.rsi/mw.png
#	Resources/Textures/Structures/Machines/microwave_syndie.rsi/mw0.png
#	Resources/Textures/Structures/Machines/microwave_syndie.rsi/mw_running_unlit.png
#	Resources/Textures/Structures/Machines/microwave_syndie.rsi/mw_unlit.png
#	Resources/Textures/Structures/Machines/microwave_syndie.rsi/mwb.png
#	Resources/Textures/Structures/Machines/microwave_syndie.rsi/mwo.png
#	Resources/migration.yml
#	RobustToolbox
2025-10-19 17:38:04 +02:00

572 lines
20 KiB
C#

using Content.Shared.ActionBlocker;
using Content.Shared.Construction.Components;
using Content.Shared.DoAfter;
using Content.Shared.Emag.Systems;
using Content.Shared.Examine;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Mindshield.Components;
using Content.Shared.Popups;
using Content.Shared.Storage;
using Content.Shared.Storage.Components;
using Content.Shared.UserInterface;
using Content.Shared.Verbs;
using Content.Shared.Wires;
using Content.Shared.Item.ItemToggle.Components;
using JetBrains.Annotations;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Shared.Lock;
/// <summary>
/// Handles (un)locking and examining of Lock components
/// </summary>
[UsedImplicitly]
public sealed class LockSystem : EntitySystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly EmagSystem _emag = default!;
[Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedPopupSystem _sharedPopupSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedUserInterfaceSystem _ui = default!;
private readonly LocId _defaultDenyReason = "lock-comp-has-user-access-fail";
/// <inheritdoc />
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LockComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<LockComponent, ActivateInWorldEvent>(OnActivated, before: [typeof(ActivatableUISystem)]);
SubscribeLocalEvent<LockComponent, UseInHandEvent>(OnUseInHand, before: [typeof(ActivatableUISystem)]);
SubscribeLocalEvent<LockComponent, StorageOpenAttemptEvent>(OnStorageOpenAttempt);
SubscribeLocalEvent<LockComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<LockComponent, GetVerbsEvent<AlternativeVerb>>(AddToggleLockVerb);
SubscribeLocalEvent<LockComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<LockComponent, LockDoAfter>(OnDoAfterLock);
SubscribeLocalEvent<LockComponent, UnlockDoAfter>(OnDoAfterUnlock);
SubscribeLocalEvent<LockedWiresPanelComponent, LockToggleAttemptEvent>(OnLockToggleAttempt);
SubscribeLocalEvent<LockedWiresPanelComponent, AttemptChangePanelEvent>(OnAttemptChangePanel);
SubscribeLocalEvent<LockedAnchorableComponent, UnanchorAttemptEvent>(OnUnanchorAttempt);
SubscribeLocalEvent<LockedStorageComponent, StorageInteractAttemptEvent>(OnStorageInteractAttempt);
SubscribeLocalEvent<UIRequiresLockComponent, ActivatableUIOpenAttemptEvent>(OnUIOpenAttempt);
SubscribeLocalEvent<UIRequiresLockComponent, LockToggledEvent>(LockToggled);
SubscribeLocalEvent<ItemToggleRequiresLockComponent, ItemToggleActivateAttemptEvent>(OnActivateAttempt);
}
private void OnStartup(EntityUid uid, LockComponent lockComp, ComponentStartup args)
{
_appearanceSystem.SetData(uid, LockVisuals.Locked, lockComp.Locked);
}
private void OnActivated(EntityUid uid, LockComponent lockComp, ActivateInWorldEvent args)
{
if (args.Handled || !args.Complex)
return;
// Only attempt an unlock by default on Activate
if (lockComp.Locked && lockComp.UnlockOnClick)
{
args.Handled = true;
TryUnlock(uid, args.User, lockComp);
}
else if (!lockComp.Locked && lockComp.LockOnClick)
{
args.Handled = true;
TryLock(uid, args.User, lockComp);
}
}
private void OnUseInHand(EntityUid uid, LockComponent lockComp, UseInHandEvent args)
{
if (args.Handled)
return;
if (lockComp.Locked && lockComp.UnlockInHand)
{
args.Handled = true;
TryUnlock(uid, args.User, lockComp);
}
else if (!lockComp.Locked && lockComp.LockInHand)
{
args.Handled = true;
TryLock(uid, args.User, lockComp);
}
}
private void OnStorageOpenAttempt(EntityUid uid, LockComponent component, ref StorageOpenAttemptEvent args)
{
if (!component.Locked)
return;
if (!args.Silent)
_sharedPopupSystem.PopupClient(Loc.GetString("entity-storage-component-locked-message"), uid, args.User);
args.Cancelled = true;
}
private void OnExamined(EntityUid uid, LockComponent lockComp, ExaminedEvent args)
{
if (!lockComp.ShowExamine)
return;
args.PushText(Loc.GetString(lockComp.Locked
? "lock-comp-on-examined-is-locked"
: "lock-comp-on-examined-is-unlocked",
("entityName", Identity.Name(uid, EntityManager))));
}
/// <summary>
/// Attmempts to lock a given entity
/// </summary>
/// <remarks>
/// If the lock is set to require a do-after, a true return value only indicates that the do-after started.
/// </remarks>
/// <param name="uid">The entity with the lock</param>
/// <param name="user">The person trying to lock it</param>
/// <param name="lockComp"></param>
/// <param name="skipDoAfter">If true, skip the required do-after if one is configured.</param>
/// <returns>If locking was successful</returns>
public bool TryLock(EntityUid uid, EntityUid user, LockComponent? lockComp = null, bool skipDoAfter = false)
{
if (!Resolve(uid, ref lockComp))
return false;
if (!CanToggleLock(uid, user, quiet: false))
return false;
if (lockComp.MindShieldLock && !HasMindshield(uid, user, quiet: false))
return false;
if (lockComp.UseAccess && !HasUserAccess(uid, user, false))
return false;
if (!skipDoAfter && lockComp.LockTime != TimeSpan.Zero)
{
return _doAfter.TryStartDoAfter(
new DoAfterArgs(EntityManager, user, lockComp.LockTime, new LockDoAfter(), uid, uid)
{
BreakOnDamage = true,
BreakOnMove = true,
NeedHand = true,
BreakOnDropItem = false,
});
}
Lock(uid, user, lockComp);
return true;
}
/// <summary>
/// Forces a given entity to be locked, does not activate a do-after.
/// </summary>
public void Lock(EntityUid uid, EntityUid? user, LockComponent? lockComp = null)
{
if (!Resolve(uid, ref lockComp))
return;
if (lockComp.Locked)
return;
if (user is { Valid: true })
{
_sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-do-lock-success",
("entityName", Identity.Name(uid, EntityManager))), uid, user);
}
_audio.PlayPredicted(lockComp.LockSound, uid, user);
lockComp.Locked = true;
_appearanceSystem.SetData(uid, LockVisuals.Locked, true);
Dirty(uid, lockComp);
var ev = new LockToggledEvent(true);
RaiseLocalEvent(uid, ref ev, true);
}
/// <summary>
/// Forces a given entity to be unlocked
/// </summary>
/// <remarks>
/// This does not process do-after times.
/// </remarks>
/// <param name="uid">The entity with the lock</param>
/// <param name="user">The person unlocking it. Can be null</param>
/// <param name="lockComp"></param>
public void Unlock(EntityUid uid, EntityUid? user, LockComponent? lockComp = null)
{
if (!Resolve(uid, ref lockComp))
return;
if (!lockComp.Locked)
return;
if (user is { Valid: true })
{
_sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-do-unlock-success",
("entityName", Identity.Name(uid, EntityManager))), uid, user.Value);
}
_audio.PlayPredicted(lockComp.UnlockSound, uid, user);
lockComp.Locked = false;
_appearanceSystem.SetData(uid, LockVisuals.Locked, false);
Dirty(uid, lockComp);
var ev = new LockToggledEvent(false);
RaiseLocalEvent(uid, ref ev, true);
}
/// <summary>
/// Attmempts to unlock a given entity
/// </summary>
/// <remarks>
/// If the lock is set to require a do-after, a true return value only indicates that the do-after started.
/// </remarks>
/// <param name="uid">The entity with the lock</param>
/// <param name="user">The person trying to unlock it</param>
/// <param name="lockComp"></param>
/// <param name="skipDoAfter">If true, skip the required do-after if one is configured.</param>
/// <returns>If locking was successful</returns>
public bool TryUnlock(EntityUid uid, EntityUid user, LockComponent? lockComp = null, bool skipDoAfter = false)
{
if (!Resolve(uid, ref lockComp))
return false;
if (!CanToggleLock(uid, user, quiet: false))
return false;
if (lockComp.MindShieldLock && !HasMindshield(uid, user, quiet: false))
return false;
if (lockComp.UseAccess && !HasUserAccess(uid, user, false))
return false;
if (!skipDoAfter && lockComp.UnlockTime != TimeSpan.Zero)
{
return _doAfter.TryStartDoAfter(
new DoAfterArgs(EntityManager, user, lockComp.LockTime, new UnlockDoAfter(), uid, uid)
{
BreakOnDamage = true,
BreakOnMove = true,
NeedHand = true,
BreakOnDropItem = false,
});
}
Unlock(uid, user, lockComp);
return true;
}
/// <summary>
/// Toggle the lock to locked if unlocked, and unlocked if locked.
/// </summary>
/// <param name="uid">Entity to toggle the lock state of.</param>
/// <param name="user">The person trying to toggle the lock</param>
/// <param name="lockComp">Entities lock comp (will be resolved)</param>
public void ToggleLock(EntityUid uid, EntityUid? user, LockComponent? lockComp = null)
{
if (IsLocked((uid, lockComp)))
Unlock(uid, user, lockComp);
else
Lock(uid, user, lockComp);
}
/// <summary>
/// Returns true if the entity is locked.
/// Entities with no lock component are considered unlocked.
/// </summary>
public bool IsLocked(Entity<LockComponent?> ent)
{
if (!Resolve(ent, ref ent.Comp, false))
return false;
return ent.Comp.Locked;
}
/// <summary>
/// Raises an event for other components to check whether or not
/// the entity can be locked in its current state.
/// </summary>
public bool CanToggleLock(Entity<LockComponent?> ent, EntityUid user, bool quiet = true)
{
if (!Resolve(ent, ref ent.Comp))
return false;
if (!_actionBlocker.CanComplexInteract(user))
return false;
if (!ent.Comp.Locked && !ent.Comp.AllowRepeatedLocking)
return false;
var ev = new LockToggleAttemptEvent(user, quiet);
RaiseLocalEvent(ent, ref ev, true);
if (ev.Cancelled)
return false;
var userEv = new UserLockToggleAttemptEvent(ent, quiet);
RaiseLocalEvent(user, ref userEv, true);
return !userEv.Cancelled;
}
/// <summary>
/// Checks whether the user has access to locks on an entity.
/// </summary>
/// <param name="ent">The entity we check for locks.</param>
/// <param name="user">The user we check for access.</param>
/// <param name="quiet">Whether to display a popup if user has no access.</param>
/// <returns>True if the user has access, otherwise False.</returns>
[PublicAPI]
public bool HasUserAccess(Entity<LockComponent?> ent, EntityUid user, bool quiet = true)
{
// Entity literally has no lock. Congratulations.
if (!Resolve(ent, ref ent.Comp, false))
return true;
var checkedReaders = LockTypes.None;
if (ent.Comp.CheckedLocks is null)
{
var lockEv = new FindAvailableLocksEvent(user);
RaiseLocalEvent(ent, ref lockEv);
checkedReaders = lockEv.FoundReaders;
}
// If no locks are found, you have access. Woo!
if (checkedReaders == LockTypes.None)
return true;
var accessEv = new CheckUserHasLockAccessEvent(user, checkedReaders);
RaiseLocalEvent(ent, ref accessEv);
// If we check for any, as long as user has access to any of the locks we grant access.
if (accessEv.HasAccess != LockTypes.None && ent.Comp.CheckForAnyReaders)
return true;
if (accessEv.HasAccess == checkedReaders)
return true;
if (!quiet)
_sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-has-user-access-fail"), uid, user);
return false;
}
private bool HasMindshield(EntityUid uid, EntityUid user, bool quiet = true)
{
if (HasComp<MindShieldComponent>(user))
return true;
if (!quiet)
{
var denyReason = accessEv.DenyReason ?? _defaultDenyReason;
_sharedPopupSystem.PopupClient(denyReason, ent, user);
}
return false;
}
private void AddToggleLockVerb(EntityUid uid, LockComponent component, GetVerbsEvent<AlternativeVerb> args)
{
if (!args.CanAccess || !args.CanInteract || !args.CanComplexInteract || !component.ShowLockVerbs)
return;
AlternativeVerb verb = new()
{
Disabled = !CanToggleLock(uid, args.User),
Act = component.Locked
? () => TryUnlock(uid, args.User, component)
: () => TryLock(uid, args.User, component),
Text = Loc.GetString(component.Locked ? "toggle-lock-verb-unlock" : "toggle-lock-verb-lock"),
Icon = !component.Locked
? new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/lock.svg.192dpi.png"))
: new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/unlock.svg.192dpi.png")),
};
args.Verbs.Add(verb);
}
private void OnEmagged(EntityUid uid, LockComponent component, ref GotEmaggedEvent args)
{
if (!_emag.CompareFlag(args.Type, EmagType.Access))
return;
if (!component.Locked || !component.BreakOnAccessBreaker)
return;
_audio.PlayPredicted(component.UnlockSound, uid, args.UserUid);
component.Locked = false;
_appearanceSystem.SetData(uid, LockVisuals.Locked, false);
Dirty(uid, component);
var ev = new LockToggledEvent(false);
RaiseLocalEvent(uid, ref ev, true);
args.Repeatable = true;
args.Handled = true;
}
private void OnDoAfterLock(EntityUid uid, LockComponent component, LockDoAfter args)
{
if (args.Cancelled)
return;
TryLock(uid, args.User, skipDoAfter: true);
}
private void OnDoAfterUnlock(EntityUid uid, LockComponent component, UnlockDoAfter args)
{
if (args.Cancelled)
return;
TryUnlock(uid, args.User, skipDoAfter: true);
}
private void OnStorageInteractAttempt(Entity<LockedStorageComponent> ent, ref StorageInteractAttemptEvent args)
{
if (IsLocked(ent.Owner))
args.Cancelled = true;
}
private void OnLockToggleAttempt(Entity<LockedWiresPanelComponent> ent, ref LockToggleAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<WiresPanelComponent>(ent, out var panel) || !panel.Open)
return;
if (!args.Silent)
{
_sharedPopupSystem.PopupClient(Loc.GetString("construction-step-condition-wire-panel-close"),
ent,
args.User);
}
args.Cancelled = true;
}
private void OnAttemptChangePanel(Entity<LockedWiresPanelComponent> ent, ref AttemptChangePanelEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<LockComponent>(ent, out var lockComp) || !lockComp.Locked)
return;
_sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-generic-fail",
("target", Identity.Entity(ent, EntityManager))),
ent,
args.User);
args.Cancelled = true;
}
private void OnUnanchorAttempt(Entity<LockedAnchorableComponent> ent, ref UnanchorAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<LockComponent>(ent, out var lockComp) || !lockComp.Locked)
return;
_sharedPopupSystem.PopupClient(Loc.GetString("lock-comp-generic-fail",
("target", Identity.Entity(ent, EntityManager))),
ent,
args.User);
args.Cancel();
}
private void OnUIOpenAttempt(EntityUid uid, UIRequiresLockComponent component, ActivatableUIOpenAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<LockComponent>(uid, out var lockComp) || lockComp.Locked == component.RequireLocked)
return;
args.Cancel();
if (lockComp.Locked && component.Popup != null)
{
_sharedPopupSystem.PopupClient(Loc.GetString(component.Popup), uid, args.User);
}
_audio.PlayPredicted(component.AccessDeniedSound, uid, args.User);
}
private void LockToggled(EntityUid uid, UIRequiresLockComponent component, LockToggledEvent args)
{
if (!TryComp<LockComponent>(uid, out var lockComp) || lockComp.Locked == component.RequireLocked)
return;
if (component.UserInterfaceKeys == null)
{
_ui.CloseUis(uid);
return;
}
foreach (var key in component.UserInterfaceKeys)
{
_ui.CloseUi(uid, key);
}
}
private void OnActivateAttempt(EntityUid uid, ItemToggleRequiresLockComponent component, ref ItemToggleActivateAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<LockComponent>(uid, out var lockComp) || lockComp.Locked == component.RequireLocked)
return;
args.Cancelled = true;
if (lockComp.Locked && component.LockedPopup != null)
{
_sharedPopupSystem.PopupClient(Loc.GetString(component.LockedPopup,
("target", Identity.Entity(uid, EntityManager))),
uid,
args.User);
}
}
}
/// <summary>
/// Raised on an entity to check whether it has any readers that can prevent it from being opened.
/// </summary>
/// <param name="User">The person attempting to open the entity.</param>
/// <param name="FoundReaders">What readers were found. This should not be set when raising the event.</param>
[ByRefEvent]
public record struct FindAvailableLocksEvent(EntityUid User, LockTypes FoundReaders = LockTypes.None);
/// <summary>
/// Raised on an entity to check if the user has access (ID, Fingerprint, etc) to said entity.
/// </summary>
/// <param name="User">The user we are checking.</param>
/// <param name="FoundReaders">What readers we are attempting to verify access for.</param>
/// <param name="HasAccess">Which readers the user has access to. This should not be set when raising the event.</param>
[ByRefEvent]
public record struct CheckUserHasLockAccessEvent(EntityUid User, LockTypes FoundReaders = LockTypes.None, LockTypes HasAccess = LockTypes.None, string? DenyReason = null);
/// <summary>
/// Enum of all readers a lock can be "locked" by.
/// Used to determine what you need in order to access the lock.
/// For example, an entity with <see cref="AccessReaderComponent"/> will have the Access type, which is gathered by an event and handled by the respective system.
/// </summary>
[Flags]
[Serializable, NetSerializable]
public enum LockTypes : byte
{
None, // Default state, means the lock is not restricted.
Access, // Means there is an AccessReader currently present.
Fingerprint, // Means there is a FingerprintReader currently present.
All = Access | Fingerprint,
}