Use dictionary and hashset for admin log entities and players respectively

Add test for duplicate player ids in an admin log not throwing
This commit is contained in:
DrSmugleaf
2021-12-26 00:50:10 +01:00
parent 82d9c38c96
commit 0ca8c705ab
5 changed files with 65 additions and 25 deletions

View File

@@ -336,4 +336,49 @@ public class AddTests : ContentIntegrationTest
Assert.Pass();
}
[Test]
public async Task DuplicatePlayerIdDoesNotThrowTest()
{
var (client, server) = await StartConnectedServerClientPair(serverOptions: new ServerContentIntegrationOption
{
CVarOverrides =
{
[CCVars.AdminLogsQueueSendDelay.Name] = "0"
},
});
await Task.WhenAll(client.WaitIdleAsync(), server.WaitIdleAsync());
var sPlayers = server.ResolveDependency<IPlayerManager>();
var sSystems = server.ResolveDependency<IEntitySystemManager>();
var sAdminLogSystem = sSystems.GetEntitySystem<AdminLogSystem>();
var guid = Guid.NewGuid();
await server.WaitPost(() =>
{
var player = sPlayers.ServerSessions.Single();
sAdminLogSystem.Add(LogType.Unknown, $"{player:first} {player:second} test log: {guid}");
});
await WaitUntil(server, async () =>
{
var logs = await sAdminLogSystem.CurrentRoundLogs(new LogFilter
{
Search = guid.ToString()
});
if (logs.Count == 0)
{
return false;
}
return true;
});
Assert.Pass();
}
}

View File

@@ -35,11 +35,11 @@ public partial class AdminLogSystem
_sawmill.Debug($"Admin log converters found: {string.Join(" ", converterNames)}");
}
private (JsonDocument json, List<Guid> players, List<(int id, string? name)> entities) ToJson(
private (JsonDocument json, HashSet<Guid> players, Dictionary<int, string?> entities) ToJson(
Dictionary<string, object?> properties)
{
var entities = new List<(int id, string? name)>();
var players = new List<Guid>();
var entities = new Dictionary<int, string?>();
var players = new HashSet<Guid>();
var parsed = new Dictionary<string, object?>();
foreach (var key in properties.Keys)
@@ -54,7 +54,7 @@ public partial class AdminLogSystem
var parsedKey = NamingPolicy.ConvertName(key);
parsed.Add(parsedKey, value);
EntityUid? entityId = properties[key] switch
var entityId = properties[key] switch
{
EntityUid id => id,
EntityStringRepresentation rep => rep.Uid,
@@ -72,9 +72,7 @@ public partial class AdminLogSystem
? metadata.EntityName
: null;
if (entities.Any(e => e.id == (int) uid)) continue;
entities.Add(((int) uid, entityName));
entities.TryAdd((int) uid, entityName);
if (_entityManager.TryGetComponent(uid, out ActorComponent? actor))
{

View File

@@ -243,7 +243,7 @@ public partial class AdminLogSystem : SharedAdminLogSystem
}
}
private async void Add(LogType type, LogImpact impact, string message, JsonDocument json, List<Guid> players, List<(int id, string? name)> entities)
private async void Add(LogType type, LogImpact impact, string message, JsonDocument json, HashSet<Guid> players, Dictionary<int, string?> entities)
{
var logId = NextLogId;
var date = DateTime.UtcNow;

View File

@@ -1,12 +1,11 @@
using System.Collections.Generic;
using Content.Server.Database;
using JetBrains.Annotations;
namespace Content.Server.Administration.Logs;
public readonly struct QueuedLog
{
public QueuedLog(AdminLog log, List<(int id, string? name)> entities)
public QueuedLog(AdminLog log, Dictionary<int, string?> entities)
{
Log = log;
Entities = entities;
@@ -14,9 +13,9 @@ public readonly struct QueuedLog
public AdminLog Log { get; }
public List<(int id, string? name)> Entities { get; }
public Dictionary<int, string?> Entities { get; }
public void Deconstruct(out AdminLog log, out List<(int id, string? name)> entities)
public void Deconstruct(out AdminLog log, out Dictionary<int, string?> entities)
{
log = Log;
entities = Entities;

View File

@@ -40,9 +40,8 @@ public ref struct LogStringHandler
format = argument[0] == '@' ? argument[1..] : argument;
}
if (!Values.TryAdd(format, value))
{
if (Values[format] == (object?) value)
if (Values.TryAdd(format, value) ||
Values[format] == (object?) value)
{
return;
}
@@ -57,7 +56,6 @@ public ref struct LogStringHandler
i++;
}
}
}
public void AppendLiteral(string value)
{