Audio System Rejig (#9635)

This commit is contained in:
Leon Friedrich
2022-07-29 14:13:12 +12:00
committed by GitHub
parent 05ee746efb
commit c7ad6b709e
191 changed files with 211 additions and 540 deletions

View File

@@ -1,75 +0,0 @@
using Content.Shared.Audio;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Utility;
namespace Content.Shared.Sound
{
[ImplicitDataDefinitionForInheritors, Serializable, NetSerializable]
public abstract class SoundSpecifier
{
[ViewVariables(VVAccess.ReadWrite), DataField("params")]
public AudioParams Params = AudioParams.Default;
// TODO: remove most uses of this function, and just make the audio-system take in a SoundSpecifier
public abstract string GetSound(IRobustRandom? rand = null, IPrototypeManager? proto = null);
}
[Serializable, NetSerializable]
public sealed class SoundPathSpecifier : SoundSpecifier
{
public const string Node = "path";
[DataField(Node, customTypeSerializer: typeof(ResourcePathSerializer), required: true)]
public ResourcePath? Path { get; }
[UsedImplicitly]
public SoundPathSpecifier()
{
}
public SoundPathSpecifier(string path)
{
Path = new ResourcePath(path);
}
public SoundPathSpecifier(ResourcePath path)
{
Path = path;
}
public override string GetSound(IRobustRandom? rand = null, IPrototypeManager? proto = null)
{
return Path == null ? string.Empty : Path.ToString();
}
}
[Serializable, NetSerializable]
public sealed class SoundCollectionSpecifier : SoundSpecifier
{
public const string Node = "collection";
[DataField(Node, customTypeSerializer: typeof(PrototypeIdSerializer<SoundCollectionPrototype>), required: true)]
public string? Collection { get; }
[UsedImplicitly]
public SoundCollectionSpecifier()
{
}
public SoundCollectionSpecifier(string collection)
{
Collection = collection;
}
public override string GetSound(IRobustRandom? rand = null, IPrototypeManager? proto = null)
{
return Collection == null ? string.Empty : AudioHelpers.GetRandomFileFromSoundCollection(Collection, rand, proto);
}
}
}

View File

@@ -1,63 +0,0 @@
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Markdown.Mapping;
using Robust.Shared.Serialization.Markdown.Validation;
using Robust.Shared.Serialization.Markdown.Value;
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
using Robust.Shared.Utility;
namespace Content.Shared.Sound
{
[TypeSerializer]
public sealed class SoundSpecifierTypeSerializer :
ITypeReader<SoundSpecifier, MappingDataNode>,
ITypeReader<SoundSpecifier, ValueDataNode>
{
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 SoundSpecifier Read(ISerializationManager serializationManager, MappingDataNode node,
IDependencyCollection dependencies, bool skipHook, ISerializationContext? context = null, SoundSpecifier? _ = null)
{
var type = GetType(node);
return (SoundSpecifier) serializationManager.Read(type, node, context, skipHook)!;
}
public SoundSpecifier Read(ISerializationManager serializationManager, ValueDataNode node,
IDependencyCollection dependencies, bool skipHook, ISerializationContext? context = null, SoundSpecifier? _ = null)
{
return new SoundPathSpecifier(node.Value);
}
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);
}
public ValidationNode Validate(ISerializationManager serializationManager, ValueDataNode node,
IDependencyCollection dependencies, ISerializationContext? context = null)
{
if (serializationManager.ValidateNode<ResourcePath>(node, context) is not ErrorNode)
return new ValidatedValueNode(node);
return new ErrorNode(node, "SoundSpecifier value is not a valid resource path!");
}
}
}