Sound Specifiers. (#4239)

* Adds SoundSpecifier and a type serializer.

* DiceComponent makes use of SoundSpecifier.

* When rider autorefactoring decices to add field:

* Remove SoundEmptySpecifier, use nullable everywhere
This commit is contained in:
Vera Aguilera Puerto
2021-06-28 16:20:57 +02:00
committed by GitHub
parent 9bedfe79be
commit 77ae49fd9c
5 changed files with 135 additions and 20 deletions

View File

@@ -0,0 +1,67 @@
using System;
using Content.Shared.Audio;
using Robust.Shared;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Utility;
namespace Content.Shared.Sound
{
[DataDefinition]
public abstract class SoundSpecifier
{
public abstract string GetSound();
}
[DataDefinition]
public sealed class SoundPathSpecifier : SoundSpecifier
{
public const string Node = "path";
[DataField(Node, customTypeSerializer:typeof(ResourcePathSerializer), required:true)]
public ResourcePath? Path { get; }
public SoundPathSpecifier()
{
}
public SoundPathSpecifier(string path)
{
Path = new ResourcePath(path);
}
public SoundPathSpecifier(ResourcePath path)
{
Path = path;
}
public override string GetSound()
{
return Path == null ? string.Empty : Path.ToString();
}
}
[DataDefinition]
public sealed class SoundCollectionSpecifier : SoundSpecifier
{
public const string Node = "collection";
[DataField(Node, customTypeSerializer:typeof(PrototypeIdSerializer<SoundCollectionPrototype>), required:true)]
public string? Collection { get; }
public SoundCollectionSpecifier()
{
}
public SoundCollectionSpecifier(string collection)
{
Collection = collection;
}
public override string GetSound()
{
return Collection == null ? string.Empty : AudioHelpers.GetRandomFileFromSoundCollection(Collection);
}
}
}

View File

@@ -0,0 +1,48 @@
using System;
using Robust.Shared.IoC;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.Manager.Result;
using Robust.Shared.Serialization.Markdown.Mapping;
using Robust.Shared.Serialization.Markdown.Validation;
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
namespace Content.Shared.Sound
{
[TypeSerializer]
public class SoundSpecifierTypeSerializer : ITypeReader<SoundSpecifier, MappingDataNode>
{
private Type GetType(MappingDataNode node)
{
var hasPath = node.Has(SoundPathSpecifier.Node);
var hasCollection = node.Has(SoundCollectionSpecifier.Node);
if (hasPath || !(hasPath ^ hasCollection))
return typeof(SoundPathSpecifier);
if (hasCollection)
return typeof(SoundCollectionSpecifier);
return typeof(SoundPathSpecifier);
}
public DeserializationResult Read(ISerializationManager serializationManager, MappingDataNode node,
IDependencyCollection dependencies, bool skipHook, ISerializationContext? context = null)
{
var type = GetType(node);
return serializationManager.Read(type, node, context, skipHook);
}
public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node,
IDependencyCollection dependencies, ISerializationContext? context = null)
{
if (node.Has(SoundPathSpecifier.Node) && node.Has(SoundCollectionSpecifier.Node))
return new ErrorNode(node, "You can only specify either a sound path or a sound collection!");
if (!node.Has(SoundPathSpecifier.Node) && !node.Has(SoundCollectionSpecifier.Node))
return new ErrorNode(node, "You need to specify either a sound path or a sound collection!");
return serializationManager.ValidateNode(GetType(node), node, context);
}
}
}