From 6515b08b41b7bbd28f51f4d1a2c25f927f8a5560 Mon Sep 17 00:00:00 2001
From: Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
Date: Fri, 3 Dec 2021 10:09:49 +0100
Subject: [PATCH] Better ToPrettyString with EntityStringRepresentation (#2301)
---
.../GameObjects/ServerEntityManager.cs | 7 +++++
Robust.Shared/GameObjects/EntityManager.cs | 8 ++---
.../GameObjects/EntityStringRepresentation.cs | 30 +++++++++++++++++++
Robust.Shared/GameObjects/IEntityManager.cs | 4 +--
4 files changed, 43 insertions(+), 6 deletions(-)
create mode 100644 Robust.Shared/GameObjects/EntityStringRepresentation.cs
diff --git a/Robust.Server/GameObjects/ServerEntityManager.cs b/Robust.Server/GameObjects/ServerEntityManager.cs
index 1ab615fb8..db3a64e36 100644
--- a/Robust.Server/GameObjects/ServerEntityManager.cs
+++ b/Robust.Server/GameObjects/ServerEntityManager.cs
@@ -87,6 +87,13 @@ namespace Robust.Server.GameObjects
return entity;
}
+ public override EntityStringRepresentation ToPrettyString(EntityUid uid)
+ {
+ TryGetComponent(uid, out ActorComponent? actor);
+
+ return base.ToPrettyString(uid) with { Session = actor?.PlayerSession };
+ }
+
#region IEntityNetworkManager impl
public override IEntityNetworkManager EntityNetManager => this;
diff --git a/Robust.Shared/GameObjects/EntityManager.cs b/Robust.Shared/GameObjects/EntityManager.cs
index b7baa717f..9b4fc8d78 100644
--- a/Robust.Shared/GameObjects/EntityManager.cs
+++ b/Robust.Shared/GameObjects/EntityManager.cs
@@ -473,15 +473,15 @@ namespace Robust.Shared.GameObjects
}
///
- public virtual string ToPrettyString(EntityUid uid)
+ public virtual EntityStringRepresentation ToPrettyString(EntityUid uid)
{
// We want to retrieve the MetaData component even if it is deleted.
if (!_entTraitDict[typeof(MetaDataComponent)].TryGetValue(uid, out var component))
- return $"{uid}D";
+ return new EntityStringRepresentation(uid, true);
- var metaData = (MetaDataComponent) component;
+ var metadata = (MetaDataComponent) component;
- return $"{metaData.EntityName} ({uid}, {metaData.EntityPrototype?.ID}){(metaData.EntityDeleted ? "D" : "")}";
+ return new EntityStringRepresentation(uid, metadata.EntityDeleted, metadata.EntityName, metadata.EntityPrototype?.ID);
}
#endregion Entity Management
diff --git a/Robust.Shared/GameObjects/EntityStringRepresentation.cs b/Robust.Shared/GameObjects/EntityStringRepresentation.cs
new file mode 100644
index 000000000..e75a42f24
--- /dev/null
+++ b/Robust.Shared/GameObjects/EntityStringRepresentation.cs
@@ -0,0 +1,30 @@
+using Robust.Shared.Players;
+
+namespace Robust.Shared.GameObjects;
+
+///
+/// A type that represents an entity, and allows you to get a string containing human-readable information about it.
+/// This type converts implicitly to string, for convenience purposes.
+///
+///
+/// This can be used to pretty-print information about entities and also to log various information regarding an
+/// entity, if you're using string interpolation handlers.
+///
+/// The unique identifier of the entity.
+/// Whether the entity has been deleted or not. Also true if the entity does not exist.
+/// The name of the entity.
+/// The prototype identifier of the entity, if any.
+/// The session attached to the entity, if any.
+public readonly record struct EntityStringRepresentation
+ (EntityUid Uid, bool Deleted, string? Name = null, string? Prototype = null, ICommonSession? Session = null)
+{
+ public override string ToString()
+ {
+ if (Deleted && Name == null)
+ return $"{Uid}D";
+
+ return $"{Name} ({Uid}{(Prototype != null ? $", {Prototype}" : "")}{(Session != null ? $", {Session.Name}" : "")}){(Deleted ? "D" : "")}";
+ }
+
+ public static implicit operator string(EntityStringRepresentation rep) => rep.ToString();
+}
diff --git a/Robust.Shared/GameObjects/IEntityManager.cs b/Robust.Shared/GameObjects/IEntityManager.cs
index 998849885..c2de1babb 100644
--- a/Robust.Shared/GameObjects/IEntityManager.cs
+++ b/Robust.Shared/GameObjects/IEntityManager.cs
@@ -126,9 +126,9 @@ namespace Robust.Shared.GameObjects
bool EntityExists(EntityUid uid);
///
- /// Returns a string with various information regarding an entity.
+ /// Returns a string representation of an entity with various information regarding it.
///
- string ToPrettyString(EntityUid uid);
+ EntityStringRepresentation ToPrettyString(EntityUid uid);
#endregion Entity Management
}