2021-11-22 23:15:22 +01:00
|
|
|
|
using System.Text.Json;
|
2023-10-28 09:59:53 +11:00
|
|
|
|
using Robust.Shared.Player;
|
2021-11-22 23:15:22 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Administration.Logs.Converters;
|
|
|
|
|
|
|
|
|
|
|
|
[AdminLogConverter]
|
2022-02-16 00:23:23 -07:00
|
|
|
|
public sealed class PlayerSessionConverter : AdminLogConverter<SerializablePlayer>
|
2021-11-22 23:15:22 +01:00
|
|
|
|
{
|
2022-12-19 03:09:50 +01:00
|
|
|
|
// System.Text.Json actually keeps hold of your JsonSerializerOption instances in a cache on .NET 7.
|
|
|
|
|
|
// Use a weak reference to avoid holding server instances live too long in integration tests.
|
|
|
|
|
|
private WeakReference<IEntityManager> _entityManager = default!;
|
|
|
|
|
|
|
|
|
|
|
|
public override void Init(IDependencyCollection dependencies)
|
|
|
|
|
|
{
|
|
|
|
|
|
_entityManager = new WeakReference<IEntityManager>(dependencies.Resolve<IEntityManager>());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-22 23:15:22 +01:00
|
|
|
|
public override void Write(Utf8JsonWriter writer, SerializablePlayer value, JsonSerializerOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
writer.WriteStartObject();
|
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
|
if (value.Player.AttachedEntity is {Valid: true} playerEntity)
|
2021-11-22 23:15:22 +01:00
|
|
|
|
{
|
2022-12-19 03:09:50 +01:00
|
|
|
|
if (!_entityManager.TryGetTarget(out var entityManager))
|
|
|
|
|
|
throw new InvalidOperationException("EntityManager got garbage collected!");
|
2021-12-05 18:09:01 +01:00
|
|
|
|
|
|
|
|
|
|
writer.WriteNumber("id", (int) value.Player.AttachedEntity);
|
|
|
|
|
|
writer.WriteString("name", entityManager.GetComponent<MetaDataComponent>(playerEntity).EntityName);
|
2021-11-22 23:15:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writer.WriteString("player", value.Player.UserId.UserId);
|
|
|
|
|
|
|
|
|
|
|
|
writer.WriteEndObject();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public readonly struct SerializablePlayer
|
|
|
|
|
|
{
|
2023-10-28 09:59:53 +11:00
|
|
|
|
public readonly ICommonSession Player;
|
2021-11-22 23:15:22 +01:00
|
|
|
|
|
2023-10-28 09:59:53 +11:00
|
|
|
|
public SerializablePlayer(ICommonSession player)
|
2021-11-22 23:15:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
Player = player;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|