From 86ce2aeb220116a821c205b5298ce9f2c004f4d2 Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Mon, 22 Nov 2021 20:36:03 +0100 Subject: [PATCH] Add EntityUid AdminLogConverter --- .../Logs/Converters/EntityConverter.cs | 17 +++++++++ .../Logs/Converters/EntityJsonConverter.cs | 27 -------------- .../Logs/Converters/EntityUidConverter.cs | 36 +++++++++++++++++++ 3 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 Content.Server/Administration/Logs/Converters/EntityConverter.cs delete mode 100644 Content.Server/Administration/Logs/Converters/EntityJsonConverter.cs create mode 100644 Content.Server/Administration/Logs/Converters/EntityUidConverter.cs diff --git a/Content.Server/Administration/Logs/Converters/EntityConverter.cs b/Content.Server/Administration/Logs/Converters/EntityConverter.cs new file mode 100644 index 0000000000..413aa2e270 --- /dev/null +++ b/Content.Server/Administration/Logs/Converters/EntityConverter.cs @@ -0,0 +1,17 @@ +using System.Text.Json; +using Robust.Server.GameObjects; +using Robust.Shared.GameObjects; +using Robust.Shared.IoC; + +namespace Content.Server.Administration.Logs.Converters; + +[AdminLogConverter] +public class EntityConverter : AdminLogConverter +{ + [Dependency] private readonly IEntityManager _entities = default!; + + public override void Write(Utf8JsonWriter writer, Entity value, JsonSerializerOptions options) + { + EntityUidConverter.Write(writer, value.Uid, options, _entities); + } +} diff --git a/Content.Server/Administration/Logs/Converters/EntityJsonConverter.cs b/Content.Server/Administration/Logs/Converters/EntityJsonConverter.cs deleted file mode 100644 index 5ad98db2e1..0000000000 --- a/Content.Server/Administration/Logs/Converters/EntityJsonConverter.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Text.Json; -using Robust.Server.GameObjects; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; - -namespace Content.Server.Administration.Logs.Converters; - -[AdminLogConverter] -public class EntityJsonConverter : AdminLogConverter -{ - [Dependency] private readonly IEntityManager _entities = default!; - - public override void Write(Utf8JsonWriter writer, Entity value, JsonSerializerOptions options) - { - writer.WriteStartObject(); - - writer.WriteNumber("id", (int) value.Uid); - writer.WriteString("name", value.Name); - - if (_entities.TryGetComponent(value.Uid, out ActorComponent? actor)) - { - writer.WriteString("player", actor.PlayerSession.UserId.UserId); - } - - writer.WriteEndObject(); - } -} diff --git a/Content.Server/Administration/Logs/Converters/EntityUidConverter.cs b/Content.Server/Administration/Logs/Converters/EntityUidConverter.cs new file mode 100644 index 0000000000..1cd5809180 --- /dev/null +++ b/Content.Server/Administration/Logs/Converters/EntityUidConverter.cs @@ -0,0 +1,36 @@ +using System.Text.Json; +using Robust.Server.GameObjects; +using Robust.Shared.GameObjects; +using Robust.Shared.IoC; + +namespace Content.Server.Administration.Logs.Converters; + +[AdminLogConverter] +public class EntityUidConverter : AdminLogConverter +{ + [Dependency] private readonly IEntityManager _entities = default!; + + public static void Write(Utf8JsonWriter writer, EntityUid value, JsonSerializerOptions options, IEntityManager entities) + { + writer.WriteStartObject(); + + writer.WriteNumber("id", (int) value); + + if (entities.TryGetEntity(value, out var entity)) + { + writer.WriteString("name", entity.Name); + } + + if (entities.TryGetComponent(value, out ActorComponent? actor)) + { + writer.WriteString("player", actor.PlayerSession.UserId.UserId); + } + + writer.WriteEndObject(); + } + + public override void Write(Utf8JsonWriter writer, EntityUid value, JsonSerializerOptions options) + { + Write(writer, value, options, _entities); + } +}