Rejig LogStringHandler (#30706)

* Rejig LogStringHandler

* Fix session logs

* Fix properly

* comments

* IAsType support

* Fix mind logs

* Fix mind logging AGAIN

---------

Co-authored-by: PJB3005 <pieterjan.briers+git@gmail.com>
This commit is contained in:
Leon Friedrich
2025-09-06 00:22:49 +12:00
committed by GitHub
parent f45bf4590f
commit 828b1f2044
17 changed files with 413 additions and 198 deletions

View File

@@ -2,9 +2,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Content.Server.Administration.Logs.Converters;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Collections;
namespace Content.Server.Administration.Logs;
@@ -22,55 +20,25 @@ public sealed partial class AdminLogManager
PropertyNamingPolicy = NamingPolicy
};
var interfaces = new ValueList<IAdminLogConverter>();
foreach (var converter in _reflection.FindTypesWithAttribute<AdminLogConverterAttribute>())
{
var instance = _typeFactory.CreateInstance<JsonConverter>(converter);
(instance as IAdminLogConverter)?.Init(_dependencies);
if (instance is IAdminLogConverter converterInterface)
{
interfaces.Add(converterInterface);
converterInterface.Init(_dependencies);
}
_jsonOptions.Converters.Add(instance);
}
foreach (var @interface in interfaces)
{
@interface.Init2(_jsonOptions);
}
var converterNames = _jsonOptions.Converters.Select(converter => converter.GetType().Name);
_sawmill.Debug($"Admin log converters found: {string.Join(" ", converterNames)}");
}
private (JsonDocument Json, HashSet<Guid> Players) ToJson(
Dictionary<string, object?> properties)
{
var players = new HashSet<Guid>();
var parsed = new Dictionary<string, object?>();
foreach (var key in properties.Keys)
{
var value = properties[key];
value = value switch
{
ICommonSession player => new SerializablePlayer(player),
EntityCoordinates entityCoordinates => new SerializableEntityCoordinates(_entityManager, entityCoordinates),
_ => value
};
var parsedKey = NamingPolicy.ConvertName(key);
parsed.Add(parsedKey, value);
var entityId = properties[key] switch
{
EntityUid id => id,
EntityStringRepresentation rep => rep.Uid,
ICommonSession {AttachedEntity: {Valid: true}} session => session.AttachedEntity,
IComponent component => component.Owner,
_ => null
};
if (_entityManager.TryGetComponent(entityId, out ActorComponent? actor))
{
players.Add(actor.PlayerSession.UserId.UserId);
}
else if (value is SerializablePlayer player)
{
players.Add(player.Player.UserId.UserId);
}
}
return (JsonSerializer.SerializeToDocument(parsed, _jsonOptions), players);
}
}