using Content.Shared.Access.Systems;
using Content.Shared.Administration.Logs;
using Content.Shared.Clothing;
using Content.Shared.CriminalRecords.Systems;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.Hands;
using Content.Shared.Humanoid;
using Content.Shared.IdentityManagement.Components;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Preferences;
using Content.Shared.Verbs;
using Content.Shared.VoiceMask;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects.Components.Localization;
using Robust.Shared.Timing;
namespace Content.Shared.IdentityManagement;
///
/// Responsible for updating the identity of an entity on init or clothing equip/unequip.
///
public sealed partial class IdentitySystem : EntitySystem
{
[Dependency] private GrammarSystem _grammarSystem = default!;
[Dependency] private IGameTiming _timing = default!;
[Dependency] private ISharedAdminLogManager _adminLog = default!;
[Dependency] private MetaDataSystem _metaData = default!;
[Dependency] private SharedContainerSystem _container = default!;
[Dependency] private SharedCriminalRecordsConsoleSystem _criminalRecordsConsole = default!;
[Dependency] private HumanoidProfileSystem _humanoidProfile = default!;
[Dependency] private SharedIdCardSystem _idCard = default!;
[Dependency] private ExamineSystemShared _examine = default!;
// The name of the container holding the identity entity
private const string SlotName = "identity";
// Recycled hashset for tracking identities each tick that need to update
private readonly HashSet _queuedIdentityUpdates = new();
///
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnSeeIdentity);
SubscribeLocalEvent>(OnRelaySeeIdentity);
SubscribeLocalEvent(OnMaskToggled);
SubscribeLocalEvent(OnMapInit);
SubscribeLocalEvent(OnComponentInit);
SubscribeLocalEvent((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent>(OnDetailedExamine);
}
///
/// Iterates through all identities that need to be updated.
///
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var ent in _queuedIdentityUpdates)
{
if (!TryComp(ent, out var identity))
continue;
UpdateIdentityInfo((ent, identity));
}
_queuedIdentityUpdates.Clear();
}
#region Event Handlers
// Creates an identity entity, and store it in the identity container
private void OnMapInit(Entity ent, ref MapInitEvent args)
{
if (ent.Comp.IdentityEntitySlot is not { } slot)
{
Log.Error($"Uninitialized IdentityEntitySlot for {ToPrettyString(ent.Owner)}.");
return;
}
var ident = Spawn(null, Transform(ent).Coordinates);
_metaData.SetEntityName(ident, "identity");
QueueIdentityUpdate(ent);
_container.Insert(ident, slot);
}
private void OnComponentInit(Entity ent, ref ComponentInit args)
{
ent.Comp.IdentityEntitySlot = _container.EnsureContainer(ent, SlotName);
}
// Adds an identity blocker's coverage, and cancels the event if coverage is complete.
private void OnSeeIdentity(Entity ent, ref SeeIdentityAttemptEvent args)
{
if (ent.Comp.Enabled)
{
args.TotalCoverage |= ent.Comp.Coverage;
if (args.TotalCoverage == IdentityBlockerCoverage.FULL)
args.Cancel();
}
}
private void OnRelaySeeIdentity(Entity ent, ref InventoryRelayedEvent args)
{
OnSeeIdentity(ent, ref args.Args);
}
// Toggles if a mask is hiding the identity.
private void OnMaskToggled(Entity ent, ref ItemMaskToggledEvent args)
{
ent.Comp.Enabled = !args.Mask.Comp.IsToggled;
Dirty(ent);
}
private void OnDetailedExamine(EntityUid ent, IdentityBlockerComponent component, ref GetVerbsEvent args)
{
var coverage = component.Coverage;
string coverageText;
string iconTexture;
switch (coverage)
{
case IdentityBlockerCoverage.MOUTH:
coverageText = "identity-block-coverage-text-mouth";
iconTexture = "/Textures/Interface/VerbIcons/human-head-mouth.svg.192dpi.png";
break;
case IdentityBlockerCoverage.EYES:
coverageText = "identity-block-coverage-text-eyes";
iconTexture = "/Textures/Interface/VerbIcons/human-head-eyes.svg.192dpi.png";
break;
case IdentityBlockerCoverage.FULL:
coverageText = "identity-block-coverage-text-full";
iconTexture = "/Textures/Interface/VerbIcons/human-head-mask.svg.192dpi.png";
break;
default:
// technically the coverage can be NONE, so we return just in case
return;
}
_examine.AddHoverExamineVerb(args,
component,
Loc.GetString("identity-block-examinable-verb-text"),
Loc.GetString(coverageText),
iconTexture
);
}
#endregion
///
/// Queues an identity update to the start of the next tick.
///
public void QueueIdentityUpdate(EntityUid uid)
{
if (_timing.ApplyingState)
return;
_queuedIdentityUpdates.Add(uid);
}
#region Private API
///
/// Updates the metadata name for the id(entity) from the current state of the character.
///
private void UpdateIdentityInfo(Entity ent)
{
if (ent.Comp.IdentityEntitySlot?.ContainedEntity is not { } ident)
return;
var representation = GetIdentityRepresentation(ent.Owner);
var name = GetIdentityName(ent, representation);
// Clone the old entity's grammar to the identity entity, for loc purposes.
if (TryComp(ent, out var grammar))
{
var identityGrammar = EnsureComp(ident);
identityGrammar.Attributes.Clear();
foreach (var (k, v) in grammar.Attributes)
{
identityGrammar.Attributes.Add(k, v);
}
// If presumed name is null and we're using that, we set proper noun to be false ("the old woman")
if (name != representation.TrueName && representation.PresumedName == null)
_grammarSystem.SetProperNoun((ident, identityGrammar), false);
Dirty(ident, identityGrammar);
}
if (name == Name(ident))
return;
_metaData.SetEntityName(ident, name);
_adminLog.Add(LogType.Identity, LogImpact.Medium, $"{ToPrettyString(ent)} changed identity to {name}");
var identityChangedEvent = new IdentityChangedEvent(ent, ident);
RaiseLocalEvent(ent, ref identityChangedEvent);
SetIdentityCriminalIcon(ent);
}
///
/// When the identity of a person is changed, searches the criminal records to see if the name of the new identity
/// has a record. If the new name has a criminal status attached to it, the person will get the criminal status
/// until they change identity again.
///
private void SetIdentityCriminalIcon(EntityUid uid)
{
_criminalRecordsConsole.CheckNewIdentity(uid);
}
///
/// Attempts to get an entity's name. Cancelled if the entity has full coverage from .
///
/// The entity being targeted.
/// The data structure containing an entity's identities.
///
/// An entity's real name if isn't cancelled,
/// or a hidden identity such as a fake ID or fully hidden identity like "middle-aged man".
///
private string GetIdentityName(EntityUid target, IdentityRepresentation representation)
{
var ev = new SeeIdentityAttemptEvent();
RaiseLocalEvent(target, ev);
return representation.ToStringKnown(!ev.Cancelled, ev.NameOverride);
}
///
/// Gets an 'identity representation' of an entity, with their true name being the entity name
/// and their 'presumed name' and 'presumed job' being the name/job on their ID card, if they have one.
///
private IdentityRepresentation GetIdentityRepresentation(Entity target)
{
var age = 18;
var gender = Gender.Epicene;
var species = HumanoidCharacterProfile.DefaultSpecies;
// Always use their actual age and gender, since that can't really be changed by an ID.
if (Resolve(target, ref target.Comp2, false))
{
gender = target.Comp2.Gender;
age = target.Comp2.Age;
species = target.Comp2.Species;
}
var ageString = _humanoidProfile.GetAgeRepresentation(species, age);
var trueName = Name(target);
if (!Resolve(target, ref target.Comp1, false))
return new(trueName, gender, ageString, string.Empty);
string? presumedJob = null;
string? presumedName = null;
// Get their name and job from their ID for their presumed name.
if (_idCard.TryFindIdCard(target, out var id))
{
presumedName = string.IsNullOrWhiteSpace(id.Comp.FullName) ? null : id.Comp.FullName;
presumedJob = id.Comp.LocalizedJobTitle?.ToLowerInvariant();
}
// If it didn't find a job, that's fine.
return new(trueName, gender, ageString, presumedName, presumedJob);
}
#endregion
///
/// Attempts to get a display name and ID title for the given entity.
/// For example "Urist McHands (Captain)" for players or "Robby (SI-123)" for silicons.
///
/// The entity to find the name for.
/// The entity being used to request the target's name.
/// For special IDs that don't leave behind a log trail; It compares toIdCardComponent.BypassLogging. // TODO: This should not be here.
/// A string of the name and ID or null if no valid identity or no ID card was found.
[PublicAPI]
public string? GetIdentityShortInfo(EntityUid target, EntityUid? whileInteractingWith = null, bool forLogging = false)
{
var tryGetIdentityShortInfoEvent = new TryGetIdentityShortInfoEvent(target, whileInteractingWith, forLogging);
RaiseLocalEvent(target, tryGetIdentityShortInfoEvent, true);
return tryGetIdentityShortInfoEvent.Title;
}
}
///
/// Gets called whenever an entity changes their identity.
///
[ByRefEvent]
public record struct IdentityChangedEvent(EntityUid CharacterEntity, EntityUid IdentityEntity);