Files
tbd-station-14/Content.Server/IdentityManagement/IdentitySystem.cs
Kara 2d5ec7f85c Id[entity] 2.0 (real) (#9612)
* starter API

* network ID cards

* Port more stuff from old identity

* Re-implement identity representation + name updating

* move

* proper name returning for `IdentityName`

* move everything important to server, give in to  temptation

* shared / server / client split sadly. move ensure to shared and spawn to server

* identity update queueing + identityblocker

* fixes

* and just like that it's usable for admins

* huge identity pass

* pass dos

* jesus christ

* figs :D

* fuck u

* fix bad merge.

Co-authored-by: Moony <moonheart08@users.noreply.github.com>
2022-07-10 20:36:53 -05:00

164 lines
5.5 KiB
C#

using Content.Server.Access.Systems;
using Content.Server.Administration.Logs;
using Content.Shared.CharacterAppearance.Components;
using Content.Shared.Database;
using Content.Shared.Hands;
using Content.Shared.IdentityManagement;
using Content.Shared.IdentityManagement.Components;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Preferences;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects.Components.Localization;
namespace Content.Server.IdentityManagement;
/// <summary>
/// Responsible for updating the identity of an entity on init or clothing equip/unequip.
/// </summary>
public class IdentitySystem : SharedIdentitySystem
{
[Dependency] private readonly IdCardSystem _idCard = default!;
[Dependency] private readonly IAdminLogManager _adminLog = default!;
private Queue<EntityUid> _queuedIdentityUpdates = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<IdentityComponent, DidEquipEvent>((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent<IdentityComponent, DidEquipHandEvent>((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent<IdentityComponent, DidUnequipEvent>((uid, _, _) => QueueIdentityUpdate(uid));
SubscribeLocalEvent<IdentityComponent, DidUnequipHandEvent>((uid, _, _) => QueueIdentityUpdate(uid));
}
public override void Update(float frameTime)
{
base.Update(frameTime);
while (_queuedIdentityUpdates.TryDequeue(out var ent))
{
if (!TryComp<IdentityComponent>(ent, out var identity))
continue;
UpdateIdentityInfo(ent, identity);
}
}
// This is where the magic happens
protected override void OnComponentInit(EntityUid uid, IdentityComponent component, ComponentInit args)
{
base.OnComponentInit(uid, component, args);
var ident = Spawn(null, Transform(uid).Coordinates);
QueueIdentityUpdate(uid);
component.IdentityEntitySlot.Insert(ident);
}
/// <summary>
/// Queues an identity update to the start of the next tick.
/// </summary>
public void QueueIdentityUpdate(EntityUid uid)
{
_queuedIdentityUpdates.Enqueue(uid);
}
#region Private API
/// <summary>
/// Updates the metadata name for the id(entity) from the current state of the character.
/// </summary>
private void UpdateIdentityInfo(EntityUid uid, IdentityComponent identity)
{
if (identity.IdentityEntitySlot.ContainedEntity is not { } ident)
return;
var representation = GetIdentityRepresentation(uid);
var name = GetIdentityName(uid, representation);
// Clone the old entity's grammar to the identity entity, for loc purposes.
if (TryComp<GrammarComponent>(uid, out var grammar))
{
var identityGrammar = EnsureComp<GrammarComponent>(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)
identityGrammar.ProperNoun = false;
}
if (name == Name(ident))
return;
MetaData(ident).EntityName = name;
_adminLog.Add(LogType.Identity, LogImpact.Medium, $"{ToPrettyString(uid)} changed identity to {name}");
RaiseLocalEvent(new IdentityChangedEvent(uid, ident));
}
private string GetIdentityName(EntityUid target, IdentityRepresentation representation)
{
var ev = new SeeIdentityAttemptEvent();
RaiseLocalEvent(target, ev);
return representation.ToStringKnown(!ev.Cancelled);
}
/// <summary>
/// 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.
/// </summary>
private IdentityRepresentation GetIdentityRepresentation(EntityUid target,
InventoryComponent? inventory=null,
HumanoidAppearanceComponent? appearance=null)
{
int age = HumanoidCharacterProfile.MinimumAge;
Gender gender = Gender.Neuter;
// Always use their actual age and gender, since that can't really be changed by an ID.
if (Resolve(target, ref appearance, false))
{
gender = appearance.Gender;
age = appearance.Age;
}
var trueName = Name(target);
if (!Resolve(target, ref inventory, false))
return new(trueName, age, gender, 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 = id.FullName;
presumedJob = id.JobTitle?.ToLowerInvariant();
}
// If it didn't find a job, that's fine.
return new(trueName, age, gender, presumedName, presumedJob);
}
#endregion
}
public sealed class IdentityChangedEvent : EntityEventArgs
{
public EntityUid CharacterEntity;
public EntityUid IdentityEntity;
public IdentityChangedEvent(EntityUid characterEntity, EntityUid identityEntity)
{
CharacterEntity = characterEntity;
IdentityEntity = identityEntity;
}
}