removes beforeserialization hook (#12319)

This commit is contained in:
Paul Ritter
2022-11-03 02:41:12 +01:00
committed by GitHub
parent 6eca66a637
commit c5e5729bd4
14 changed files with 199 additions and 229 deletions

View File

@@ -1,5 +1,4 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.Alert
{
@@ -8,59 +7,55 @@ namespace Content.Shared.Alert
/// </summary>
[Prototype("alertOrder")]
[DataDefinition]
public sealed class AlertOrderPrototype : IPrototype, IComparer<AlertPrototype>, ISerializationHooks
public sealed class AlertOrderPrototype : IPrototype, IComparer<AlertPrototype>
{
[ViewVariables]
[IdDataFieldAttribute]
public string ID { get; } = default!;
[DataField("order")] private readonly List<(string type, string alert)> _order = new();
private readonly Dictionary<AlertType, int> _typeToIdx = new();
private readonly Dictionary<AlertCategory, int> _categoryToIdx = new();
void ISerializationHooks.BeforeSerialization()
[DataField("order")]
private List<(string type, string alert)> Order
{
_order.Clear();
var orderArray = new KeyValuePair<string, string>[_typeToIdx.Count + _categoryToIdx.Count];
foreach (var (type, id) in _typeToIdx)
get
{
orderArray[id] = new KeyValuePair<string, string>("alertType", type.ToString());
}
var res = new List<(string, string)>(_typeToIdx.Count + _categoryToIdx.Count);
foreach (var (category, id) in _categoryToIdx)
{
orderArray[id] = new KeyValuePair<string, string>("category", category.ToString());
}
foreach (var (type, alert) in orderArray)
{
_order.Add((type, alert));
}
}
void ISerializationHooks.AfterDeserialization()
{
var i = 0;
foreach (var (type, alert) in _order)
{
switch (type)
foreach (var (type, id) in _typeToIdx)
{
case "alertType":
_typeToIdx[Enum.Parse<AlertType>(alert)] = i++;
break;
case "category":
_categoryToIdx[Enum.Parse<AlertCategory>(alert)] = i++;
break;
default:
throw new ArgumentException();
res.Insert(id, ("alertType", type.ToString()));
}
foreach (var (category, id) in _categoryToIdx)
{
res.Insert(id, ("category", category.ToString()));
}
return res;
}
set
{
var i = 0;
foreach (var (type, alert) in value)
{
switch (type)
{
case "alertType":
_typeToIdx[Enum.Parse<AlertType>(alert)] = i++;
break;
case "category":
_categoryToIdx[Enum.Parse<AlertCategory>(alert)] = i++;
break;
default:
throw new ArgumentException();
}
}
}
}
private readonly Dictionary<AlertType, int> _typeToIdx = new();
private readonly Dictionary<AlertCategory, int> _categoryToIdx = new();
private int GetOrderIndex(AlertPrototype alert)
{
if (_typeToIdx.TryGetValue(alert.AlertType, out var idx))