diff --git a/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs b/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs index 93d04a203..a70577ebd 100644 --- a/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs +++ b/Robust.Client/GameObjects/Components/Renderable/SpriteComponent.cs @@ -2150,11 +2150,6 @@ namespace Robust.Client.GameObjects return null!; } - public IComponent GetComponent(uint netID) - { - return null!; - } - public bool TryGetComponent([NotNullWhen(true)] out T? component) where T : class { component = null; @@ -2181,17 +2176,6 @@ namespace Robust.Client.GameObjects return null; } - public bool TryGetComponent(uint netId, [NotNullWhen(true)] out IComponent? component) - { - component = null; - return false; - } - - public IComponent? GetComponentOrNull(uint netId) - { - return null; - } - public void Delete() { } diff --git a/Robust.Shared/GameObjects/ComponentManager.cs b/Robust.Shared/GameObjects/ComponentManager.cs index 33704d785..7e029d0f2 100644 --- a/Robust.Shared/GameObjects/ComponentManager.cs +++ b/Robust.Shared/GameObjects/ComponentManager.cs @@ -23,9 +23,11 @@ namespace Robust.Shared.GameObjects private const int TypeCapacity = 32; private const int ComponentCollectionCapacity = 1024; + private const int EntityCapacity = 1024; + private const int NetComponentCapacity = 8; - private readonly Dictionary> _entNetIdDict - = new(); + private readonly Dictionary> _netComponents + = new(EntityCapacity); private readonly Dictionary> _entTraitDict = new(); @@ -60,7 +62,7 @@ namespace Robust.Shared.GameObjects /// public void Clear() { - _entNetIdDict.Clear(); + _netComponents.Clear(); _entTraitDict.Clear(); _entCompIndex.Clear(); _deleteSet.Clear(); @@ -76,10 +78,6 @@ namespace Robust.Shared.GameObjects private void OnComponentAdded(IComponentRegistration obj) { _entTraitDict.Add(obj.Type, new Dictionary()); - - var netID = obj.NetID; - if (netID.HasValue) - _entNetIdDict.Add(netID.Value, new Dictionary()); } private void OnComponentReferenceAdded((IComponentRegistration, Type) obj) @@ -148,7 +146,13 @@ namespace Robust.Shared.GameObjects { // the main comp grid keeps this in sync var netId = component.NetID.Value; - _entNetIdDict[netId].Add(uid, component); + + if (!_netComponents.TryGetValue(uid, out var netSet)) + { + netSet = new Dictionary(NetComponentCapacity); + _netComponents.Add(uid, netSet); + } + netSet.Add(netId, component); // mark the component as dirty for networking component.Dirty(); @@ -325,15 +329,19 @@ namespace Robust.Shared.GameObjects _entTraitDict[refType].Remove(entityUid); } - if (component.NetID == null) return; + // ReSharper disable once InvertIf + if (component.NetID != null) + { + var netSet = _netComponents[entityUid]; + if (netSet.Count == 1) + _netComponents.Remove(entityUid); + else + netSet.Remove(component.NetID.Value); + + component.Owner.Dirty(); + } - var netId = component.NetID.Value; - _entNetIdDict[netId].Remove(entityUid); _entCompIndex.Remove(entityUid, component); - - // mark the owning entity as dirty for networking - component.Owner.Dirty(); - ComponentDeleted?.Invoke(this, new DeletedComponentEventArgs(component, entityUid)); } @@ -356,8 +364,8 @@ namespace Robust.Shared.GameObjects [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool HasComponent(EntityUid uid, uint netId) { - var dict = _entNetIdDict[netId]; - return dict.TryGetValue(uid, out var comp) && !comp.Deleted; + return _netComponents.TryGetValue(uid, out var netSet) + && netSet.ContainsKey(netId); } /// @@ -386,17 +394,7 @@ namespace Robust.Shared.GameObjects /// public IComponent GetComponent(EntityUid uid, uint netId) { - // ReSharper disable once InvertIf - var dict = _entNetIdDict[netId]; - if (dict.TryGetValue(uid, out var comp)) - { - if (!comp.Deleted) - { - return comp; - } - } - - throw new KeyNotFoundException($"Entity {uid} does not have a component of NetID {netId}"); + return _netComponents[uid][netId]; } /// @@ -433,19 +431,16 @@ namespace Robust.Shared.GameObjects } /// - public bool TryGetComponent(EntityUid uid, uint netId, [NotNullWhen(true)] out IComponent? component) + public bool TryGetComponent(EntityUid uid, uint netId, [MaybeNullWhen(false)] out IComponent component) { - var dict = _entNetIdDict[netId]; - if (dict.TryGetValue(uid, out var comp)) + if (_netComponents.TryGetValue(uid, out var netSet) + && netSet.TryGetValue(netId, out var comp)) { - if (!comp.Deleted) - { - component = comp; - return true; - } + component = comp; + return true; } - component = null; + component = default; return false; } @@ -476,13 +471,7 @@ namespace Robust.Shared.GameObjects /// public IEnumerable GetNetComponents(EntityUid uid) { - var comps = _entCompIndex[uid]; - foreach (var comp in comps) - { - if (comp.Deleted || comp.NetID == null) continue; - - yield return comp; - } + return _netComponents[uid].Values; } #region Join Functions @@ -601,11 +590,6 @@ namespace Robust.Shared.GameObjects { _entTraitDict.Add(refType, new Dictionary()); } - - foreach (var netId in _componentFactory.GetAllNetIds()) - { - _entNetIdDict.Add(netId, new Dictionary()); - } } } } diff --git a/Robust.Shared/GameObjects/Entity.cs b/Robust.Shared/GameObjects/Entity.cs index c7872aa8a..804cbe8f3 100644 --- a/Robust.Shared/GameObjects/Entity.cs +++ b/Robust.Shared/GameObjects/Entity.cs @@ -269,14 +269,6 @@ namespace Robust.Shared.GameObjects return EntityManager.ComponentManager.GetComponent(Uid, type); } - /// - public IComponent GetComponent(uint netId) - { - DebugTools.Assert(!Deleted, "Tried to get component on a deleted entity."); - - return EntityManager.ComponentManager.GetComponent(Uid, netId); - } - /// public bool TryGetComponent([NotNullWhen(true)] out T? component) where T : class { @@ -303,19 +295,6 @@ namespace Robust.Shared.GameObjects return TryGetComponent(type, out var component) ? component : null; } - /// - public bool TryGetComponent(uint netId, [NotNullWhen(true)] out IComponent? component) - { - DebugTools.Assert(!Deleted, "Tried to get component on a deleted entity."); - - return EntityManager.ComponentManager.TryGetComponent(Uid, netId, out component); - } - - public IComponent? GetComponentOrNull(uint netId) - { - return TryGetComponent(netId, out var component) ? component : null; - } - /// public void Delete() { diff --git a/Robust.Shared/GameObjects/IComponentManager.cs b/Robust.Shared/GameObjects/IComponentManager.cs index 35fffd1b7..bd2f42abf 100644 --- a/Robust.Shared/GameObjects/IComponentManager.cs +++ b/Robust.Shared/GameObjects/IComponentManager.cs @@ -112,7 +112,8 @@ namespace Robust.Shared.GameObjects bool HasComponent(EntityUid uid, Type type); /// - /// Checks if the entity has a component with a given network ID. + /// Checks if the entity has a component with a given network ID. This does not check + /// if the component is deleted. /// /// Entity UID to check. /// Network ID to check for. @@ -136,7 +137,8 @@ namespace Robust.Shared.GameObjects IComponent GetComponent(EntityUid uid, Type type); /// - /// Returns the component with a specific network ID. + /// Returns the component with a specific network ID. This does not check + /// if the component is deleted. /// /// Entity UID to look on. /// Network ID of the component to retrieve. @@ -162,7 +164,8 @@ namespace Robust.Shared.GameObjects bool TryGetComponent(EntityUid uid, Type type, [NotNullWhen(true)] out IComponent? component); /// - /// Returns the component with a specified network ID. + /// Returns the component with a specified network ID. This does not check + /// if the component is deleted. /// /// Entity UID to check. /// Component Network ID to check for. diff --git a/Robust.Shared/GameObjects/IEntity.cs b/Robust.Shared/GameObjects/IEntity.cs index b9cca5442..39fe2c461 100644 --- a/Robust.Shared/GameObjects/IEntity.cs +++ b/Robust.Shared/GameObjects/IEntity.cs @@ -127,17 +127,6 @@ namespace Robust.Shared.GameObjects /// IComponent GetComponent(Type type); - /// - /// Retrieves the component with the specified network ID. - /// - /// The net ID of the component to retrieve. - /// The component with the provided net ID. - /// - /// - /// Thrown if there is no component with the specified net ID. - /// - IComponent GetComponent(uint netID); - /// /// Attempt to retrieve the component with specified type, /// writing it to the out parameter if it was found. @@ -172,23 +161,6 @@ namespace Robust.Shared.GameObjects /// The component, if it was found. Null otherwise. IComponent? GetComponentOrNull(Type type); - /// - /// Attempt to retrieve the component with specified network ID, - /// writing it to the out parameter if it was found. - /// - /// The component net ID to attempt to fetch. - /// The component, if it was found. Null otherwise. - /// True if a component with specified net ID was found. - bool TryGetComponent(uint netId, [NotNullWhen(true)] out IComponent? component); - - /// - /// Attempt to retrieve the component with specified network ID, - /// returning it if it was found. - /// - /// The component net ID to attempt to fetch. - /// The component, if it was found. Null otherwise. - IComponent? GetComponentOrNull(uint netId); - /// /// Deletes this entity. /// @@ -222,6 +194,9 @@ namespace Robust.Shared.GameObjects /// Message to send. void SendNetworkMessage(IComponent owner, ComponentMessage message, INetChannel? channel = null); + /// + /// Marks this entity as dirty so that it will be updated over the network. + /// void Dirty(); } }