1
0

Identity.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Content.Shared.Ghost;
  2. using Content.Shared.IdentityManagement.Components;
  3. namespace Content.Shared.IdentityManagement;
  4. /// <summary>
  5. /// Static content API for getting the identity entities/names for a given entity.
  6. /// This should almost always be used in favor of metadata name, if the entity in question is a human player that
  7. /// can have identity.
  8. /// </summary>
  9. public static class Identity
  10. {
  11. /// <summary>
  12. /// Returns the name that should be used for this entity for identity purposes.
  13. /// </summary>
  14. public static string Name(EntityUid uid, IEntityManager ent, EntityUid? viewer=null)
  15. {
  16. if (!uid.IsValid())
  17. return string.Empty;
  18. var meta = ent.GetComponent<MetaDataComponent>(uid);
  19. if (meta.EntityLifeStage <= EntityLifeStage.Initializing)
  20. return meta.EntityName; // Identity component and such will not yet have initialized and may throw NREs
  21. var uidName = meta.EntityName;
  22. if (!ent.TryGetComponent<IdentityComponent>(uid, out var identity))
  23. return uidName;
  24. var ident = identity.IdentityEntitySlot.ContainedEntity;
  25. if (ident is null)
  26. return uidName;
  27. var identName = ent.GetComponent<MetaDataComponent>(ident.Value).EntityName;
  28. if (viewer == null || !CanSeeThroughIdentity(uid, viewer.Value, ent))
  29. {
  30. return identName;
  31. }
  32. if (uidName == identName)
  33. {
  34. return uidName;
  35. }
  36. return $"{uidName} ({identName})";
  37. }
  38. /// <summary>
  39. /// Returns the entity that should be used for identity purposes, for example to pass into localization.
  40. /// This is an extension method because of its simplicity, and if it was any harder to call it might not
  41. /// be used enough for loc.
  42. /// </summary>
  43. /// <param name="viewer">
  44. /// If this entity can see through identities, this method will always return the actual target entity.
  45. /// </param>
  46. public static EntityUid Entity(EntityUid uid, IEntityManager ent, EntityUid? viewer = null)
  47. {
  48. if (!ent.TryGetComponent<IdentityComponent>(uid, out var identity))
  49. return uid;
  50. if (viewer != null && CanSeeThroughIdentity(uid, viewer.Value, ent))
  51. return uid;
  52. return identity.IdentityEntitySlot.ContainedEntity ?? uid;
  53. }
  54. public static bool CanSeeThroughIdentity(EntityUid uid, EntityUid viewer, IEntityManager ent)
  55. {
  56. // Would check for uid == viewer here but I think it's better for you to see yourself
  57. // how everyone else will see you, otherwise people will probably get confused and think they aren't disguised
  58. return ent.HasComponent<GhostComponent>(viewer);
  59. }
  60. }