using System; using Robust.Shared.IoC; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; namespace Robust.Shared.GameObjects { /// /// Serialized state of a . /// [Serializable, NetSerializable] public class MetaDataComponentState : ComponentState { /// /// The in-game name of this entity. /// public string? Name { get; } /// /// The in-game description of this entity. /// public string? Description { get; } /// /// The prototype this entity was created from, if any. /// public string? PrototypeId { get; } public override uint NetID => NetIDs.META_DATA; /// /// Constructs a new instance of . /// /// The in-game name of this entity. /// The in-game description of this entity. /// The prototype this entity was created from, if any. public MetaDataComponentState(string? name, string? description, string? prototypeId) { Name = name; Description = description; PrototypeId = prototypeId; } } /// /// Contains meta data about this entity that isn't component specific. /// public interface IMetaDataComponent { /// /// The in-game name of this entity. /// string EntityName { get; set; } /// /// The in-game description of this entity. /// string EntityDescription { get; set; } /// /// The prototype this entity was created from, if any. /// EntityPrototype? EntityPrototype { get; set; } } /// internal class MetaDataComponent : Component, IMetaDataComponent { [Dependency] private readonly IPrototypeManager _prototypes = default!; private string? _entityName; private string? _entityDescription; private EntityPrototype? _entityPrototype; /// public override string Name => "MetaData"; /// public override uint? NetID => NetIDs.META_DATA; /// [ViewVariables(VVAccess.ReadWrite)] public string EntityName { get { if (_entityName == null) return _entityPrototype != null ? _entityPrototype.Name : string.Empty; return _entityName; } set { string? newValue = value; if (_entityPrototype != null && _entityPrototype.Name == newValue) newValue = null; if (_entityName == newValue) return; _entityName = newValue; Dirty(); } } /// [ViewVariables(VVAccess.ReadWrite)] public string EntityDescription { get { if (_entityDescription == null) return _entityPrototype != null ? _entityPrototype.Description : string.Empty; return _entityDescription; } set { string? newValue = value; if (_entityPrototype != null && _entityPrototype.Description == newValue) newValue = null; if(_entityDescription == newValue) return; _entityDescription = newValue; Dirty(); } } /// [ViewVariables] public EntityPrototype? EntityPrototype { get => _entityPrototype; set { _entityPrototype = value; Dirty(); } } /// public override ComponentState GetComponentState() { return new MetaDataComponentState(_entityName, _entityDescription, EntityPrototype?.ID); } /// public override void HandleComponentState(ComponentState? curState, ComponentState? nextState) { base.HandleComponentState(curState, nextState); if (!(curState is MetaDataComponentState state)) return; _entityName = state.Name; _entityDescription = state.Description; if(state.PrototypeId != null) _entityPrototype = _prototypes.Index(state.PrototypeId); } /// public override void ExposeData(ObjectSerializer serializer) { base.ExposeData(serializer); serializer.DataField(ref _entityName, "name", null); serializer.DataField(ref _entityDescription, "desc", null); //serializer.DataField(ref _entityPrototype, "proto", null, // s => _prototypes.Index(s), // p => p.ID); } internal override void ClearTicks() { // Do not clear modified ticks. // MetaDataComponent is used in the game state system to carry initial data like prototype ID. // So it ALWAYS has to be sent. // (Creation can still be cleared though) ClearCreationTick(); } } }