Better ToPrettyString with EntityStringRepresentation (#2301)

This commit is contained in:
Vera Aguilera Puerto
2021-12-03 10:09:49 +01:00
committed by GitHub
parent 6e2f18d0d8
commit 6515b08b41
4 changed files with 43 additions and 6 deletions

View File

@@ -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;

View File

@@ -473,15 +473,15 @@ namespace Robust.Shared.GameObjects
}
/// <inheritdoc />
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

View File

@@ -0,0 +1,30 @@
using Robust.Shared.Players;
namespace Robust.Shared.GameObjects;
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="Uid">The unique identifier of the entity.</param>
/// <param name="Deleted">Whether the entity has been deleted or not. Also true if the entity does not exist.</param>
/// <param name="Name">The name of the entity.</param>
/// <param name="Prototype">The prototype identifier of the entity, if any.</param>
/// <param name="Session">The session attached to the entity, if any.</param>
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();
}

View File

@@ -126,9 +126,9 @@ namespace Robust.Shared.GameObjects
bool EntityExists(EntityUid uid);
/// <summary>
/// Returns a string with various information regarding an entity.
/// Returns a string representation of an entity with various information regarding it.
/// </summary>
string ToPrettyString(EntityUid uid);
EntityStringRepresentation ToPrettyString(EntityUid uid);
#endregion Entity Management
}