2022-09-06 00:28:23 +10:00
|
|
|
using Robust.Shared.Reflection;
|
2022-12-20 23:25:34 +01:00
|
|
|
using Robust.Shared.Serialization;
|
2022-09-06 00:28:23 +10:00
|
|
|
using Robust.Shared.Serialization.Manager;
|
|
|
|
|
using Robust.Shared.Serialization.Markdown;
|
|
|
|
|
using Robust.Shared.Serialization.Markdown.Mapping;
|
|
|
|
|
using Robust.Shared.Serialization.Markdown.Validation;
|
|
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
|
|
|
|
|
using Robust.Shared.Utility;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.NPC;
|
|
|
|
|
|
2022-12-15 15:30:28 +11:00
|
|
|
public sealed class NPCBlackboardSerializer : ITypeReader<NPCBlackboard, MappingDataNode>, ITypeCopier<NPCBlackboard>
|
2022-09-06 00:28:23 +10:00
|
|
|
{
|
|
|
|
|
public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node,
|
|
|
|
|
IDependencyCollection dependencies, ISerializationContext? context = null)
|
|
|
|
|
{
|
|
|
|
|
var validated = new List<ValidationNode>();
|
|
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
if (node.Count <= 0)
|
|
|
|
|
return new ValidatedSequenceNode(validated);
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
var reflection = dependencies.Resolve<IReflectionManager>();
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
foreach (var data in node)
|
|
|
|
|
{
|
|
|
|
|
var key = data.Key.ToYamlNode().AsString();
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
if (data.Value.Tag == null)
|
|
|
|
|
{
|
|
|
|
|
validated.Add(new ErrorNode(data.Key, $"Unable to validate {key}'s type"));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
var typeString = data.Value.Tag[6..];
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
if (!reflection.TryLooseGetType(typeString, out var type))
|
|
|
|
|
{
|
|
|
|
|
validated.Add(new ErrorNode(data.Key, $"Unable to find type for {typeString}"));
|
|
|
|
|
continue;
|
2022-09-06 00:28:23 +10:00
|
|
|
}
|
2023-08-02 10:48:56 +10:00
|
|
|
|
|
|
|
|
var validatedNode = serializationManager.ValidateNode(type, data.Value, context);
|
|
|
|
|
validated.Add(validatedNode);
|
2022-09-06 00:28:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ValidatedSequenceNode(validated);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 19:25:55 +01:00
|
|
|
public NPCBlackboard Read(ISerializationManager serializationManager, MappingDataNode node,
|
|
|
|
|
IDependencyCollection dependencies,
|
2022-12-20 23:25:34 +01:00
|
|
|
SerializationHookContext hookCtx, ISerializationContext? context = null,
|
2022-11-27 19:25:55 +01:00
|
|
|
ISerializationManager.InstantiationDelegate<NPCBlackboard>? instanceProvider = null)
|
2022-09-06 00:28:23 +10:00
|
|
|
{
|
2022-11-27 19:25:55 +01:00
|
|
|
var value = instanceProvider != null ? instanceProvider() : new NPCBlackboard();
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
if (node.Count <= 0)
|
|
|
|
|
return value;
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
var reflection = dependencies.Resolve<IReflectionManager>();
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
foreach (var data in node)
|
|
|
|
|
{
|
|
|
|
|
var key = data.Key.ToYamlNode().AsString();
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
if (data.Value.Tag == null)
|
|
|
|
|
throw new NullReferenceException($"Found null tag for {key}");
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
var typeString = data.Value.Tag[6..];
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
if (!reflection.TryLooseGetType(typeString, out var type))
|
|
|
|
|
throw new NullReferenceException($"Found null type for {key}");
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
var bbData = serializationManager.Read(type, data.Value, hookCtx, context);
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
if (bbData == null)
|
|
|
|
|
throw new NullReferenceException($"Found null data for {key}, expected {type}");
|
|
|
|
|
|
|
|
|
|
value.SetValue(key, bbData);
|
2022-09-06 00:28:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2022-12-15 15:30:28 +11:00
|
|
|
|
2022-12-20 23:25:34 +01:00
|
|
|
public void CopyTo(
|
|
|
|
|
ISerializationManager serializationManager,
|
|
|
|
|
NPCBlackboard source,
|
|
|
|
|
ref NPCBlackboard target,
|
2023-05-01 14:49:25 +10:00
|
|
|
IDependencyCollection dependencies,
|
2022-12-20 23:25:34 +01:00
|
|
|
SerializationHookContext hookCtx,
|
2022-12-15 15:30:28 +11:00
|
|
|
ISerializationContext? context = null)
|
|
|
|
|
{
|
|
|
|
|
target.Clear();
|
|
|
|
|
using var enumerator = source.GetEnumerator();
|
|
|
|
|
|
|
|
|
|
while (enumerator.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
var current = enumerator.Current;
|
|
|
|
|
target.SetValue(current.Key, current.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-06 00:28:23 +10:00
|
|
|
}
|