mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Reverts component NetId storage in ComponentManager back to the way Acruid originally designed it.
Removes NetId methods from IEntity, content does not need to be messing with them. Fixes bug in DeleteComponent where the ComponentDeleted event was not being raised if a component did not have a NetId.
This commit is contained in:
@@ -2150,11 +2150,6 @@ namespace Robust.Client.GameObjects
|
||||
return null!;
|
||||
}
|
||||
|
||||
public IComponent GetComponent(uint netID)
|
||||
{
|
||||
return null!;
|
||||
}
|
||||
|
||||
public bool TryGetComponent<T>([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()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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<uint, Dictionary<EntityUid, Component>> _entNetIdDict
|
||||
= new();
|
||||
private readonly Dictionary<EntityUid, Dictionary<uint, Component>> _netComponents
|
||||
= new(EntityCapacity);
|
||||
|
||||
private readonly Dictionary<Type, Dictionary<EntityUid, Component>> _entTraitDict
|
||||
= new();
|
||||
@@ -60,7 +62,7 @@ namespace Robust.Shared.GameObjects
|
||||
/// <inheritdoc />
|
||||
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<EntityUid, Component>());
|
||||
|
||||
var netID = obj.NetID;
|
||||
if (netID.HasValue)
|
||||
_entNetIdDict.Add(netID.Value, new Dictionary<EntityUid, Component>());
|
||||
}
|
||||
|
||||
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<uint, Component>(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);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -386,17 +394,7 @@ namespace Robust.Shared.GameObjects
|
||||
/// <inheritdoc />
|
||||
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];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -433,19 +431,16 @@ namespace Robust.Shared.GameObjects
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<IComponent> 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<EntityUid, Component>());
|
||||
}
|
||||
|
||||
foreach (var netId in _componentFactory.GetAllNetIds())
|
||||
{
|
||||
_entNetIdDict.Add(netId, new Dictionary<EntityUid, Component>());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,14 +269,6 @@ namespace Robust.Shared.GameObjects
|
||||
return EntityManager.ComponentManager.GetComponent(Uid, type);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IComponent GetComponent(uint netId)
|
||||
{
|
||||
DebugTools.Assert(!Deleted, "Tried to get component on a deleted entity.");
|
||||
|
||||
return EntityManager.ComponentManager.GetComponent(Uid, netId);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGetComponent<T>([NotNullWhen(true)] out T? component) where T : class
|
||||
{
|
||||
@@ -303,19 +295,6 @@ namespace Robust.Shared.GameObjects
|
||||
return TryGetComponent(type, out var component) ? component : null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Delete()
|
||||
{
|
||||
|
||||
@@ -112,7 +112,8 @@ namespace Robust.Shared.GameObjects
|
||||
bool HasComponent(EntityUid uid, Type type);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="uid">Entity UID to check.</param>
|
||||
/// <param name="netId">Network ID to check for.</param>
|
||||
@@ -136,7 +137,8 @@ namespace Robust.Shared.GameObjects
|
||||
IComponent GetComponent(EntityUid uid, Type type);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="uid">Entity UID to look on.</param>
|
||||
/// <param name="netId">Network ID of the component to retrieve.</param>
|
||||
@@ -162,7 +164,8 @@ namespace Robust.Shared.GameObjects
|
||||
bool TryGetComponent(EntityUid uid, Type type, [NotNullWhen(true)] out IComponent? component);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="uid">Entity UID to check.</param>
|
||||
/// <param name="netId">Component Network ID to check for.</param>
|
||||
|
||||
@@ -127,17 +127,6 @@ namespace Robust.Shared.GameObjects
|
||||
/// </exception>
|
||||
IComponent GetComponent(Type type);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the component with the specified network ID.
|
||||
/// </summary>
|
||||
/// <param name="netID">The net ID of the component to retrieve.</param>
|
||||
/// <returns>The component with the provided net ID.</returns>
|
||||
/// <seealso cref="IComponent.NetID" />
|
||||
/// <exception cref="Shared.GameObjects.UnknownComponentException">
|
||||
/// Thrown if there is no component with the specified net ID.
|
||||
/// </exception>
|
||||
IComponent GetComponent(uint netID);
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to retrieve the component with specified type,
|
||||
/// writing it to the <paramref name="component" /> out parameter if it was found.
|
||||
@@ -172,23 +161,6 @@ namespace Robust.Shared.GameObjects
|
||||
/// <returns>The component, if it was found. Null otherwise.</returns>
|
||||
IComponent? GetComponentOrNull(Type type);
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to retrieve the component with specified network ID,
|
||||
/// writing it to the <paramref name="component" /> out parameter if it was found.
|
||||
/// </summary>
|
||||
/// <param name="netId">The component net ID to attempt to fetch.</param>
|
||||
/// <param name="component">The component, if it was found. Null otherwise.</param>
|
||||
/// <returns>True if a component with specified net ID was found.</returns>
|
||||
bool TryGetComponent(uint netId, [NotNullWhen(true)] out IComponent? component);
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to retrieve the component with specified network ID,
|
||||
/// returning it if it was found.
|
||||
/// </summary>
|
||||
/// <param name="netId">The component net ID to attempt to fetch.</param>
|
||||
/// <returns>The component, if it was found. Null otherwise.</returns>
|
||||
IComponent? GetComponentOrNull(uint netId);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes this entity.
|
||||
/// </summary>
|
||||
@@ -222,6 +194,9 @@ namespace Robust.Shared.GameObjects
|
||||
/// <param name="message">Message to send.</param>
|
||||
void SendNetworkMessage(IComponent owner, ComponentMessage message, INetChannel? channel = null);
|
||||
|
||||
/// <summary>
|
||||
/// Marks this entity as dirty so that it will be updated over the network.
|
||||
/// </summary>
|
||||
void Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user