#nullable enable using System; using System.Collections.Generic; using Content.Server.Interfaces.Chemistry; using Content.Shared.Interfaces; using Robust.Shared.Interfaces.Serialization; using Robust.Shared.IoC; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using YamlDotNet.RepresentationModel; namespace Content.Shared.Chemistry { /// /// Prototype for chemical reaction definitions /// [Prototype("reaction")] public class ReactionPrototype : IPrototype, IIndexedPrototype { private string _id = default!; private string _name = default!; private Dictionary _reactants = default!; private Dictionary _products = default!; private IReactionEffect[] _effects = default!; public string ID => _id; public string Name => _name; /// /// Reactants required for the reaction to occur. /// public IReadOnlyDictionary Reactants => _reactants; /// /// Reagents created when the reaction occurs. /// public IReadOnlyDictionary Products => _products; /// /// Effects to be triggered when the reaction occurs. /// public IReadOnlyList Effects => _effects; public string? Sound { get; private set; } [Dependency] private readonly IModuleManager _moduleManager = default!; public void LoadFrom(YamlMappingNode mapping) { var serializer = YamlObjectSerializer.NewReader(mapping); serializer.DataField(ref _id, "id", string.Empty); serializer.DataField(ref _name, "name", string.Empty); serializer.DataField(ref _reactants, "reactants", new Dictionary()); serializer.DataField(ref _products, "products", new Dictionary()); serializer.DataField(this, x => x.Sound, "sound", "/Audio/Effects/Chemistry/bubbles.ogg"); if (_moduleManager.IsServerModule) { //TODO: Don't have a check for if this is the server //Some implementations of IReactionEffect can't currently be moved to shared, so this is here to prevent the client from breaking when reading server-only IReactionEffects. serializer.DataField(ref _effects, "effects", Array.Empty()); } else { _effects = Array.Empty(); //To ensure _effects isn't null since it is only serializable on the server right snow } } } /// /// Prototype for chemical reaction reactants. /// public class ReactantPrototype : IExposeData { private ReagentUnit _amount; private bool _catalyst; /// /// Minimum amount of the reactant needed for the reaction to occur. /// public ReagentUnit Amount => _amount; /// /// Whether or not the reactant is a catalyst. Catalysts aren't removed when a reaction occurs. /// public bool Catalyst => _catalyst; public void ExposeData(ObjectSerializer serializer) { serializer.DataField(ref _amount, "amount", ReagentUnit.New(1)); serializer.DataField(ref _catalyst, "catalyst", false); } } }