mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Reduce uses of IComponentFactory.GetIndex(Type) (#5242)
* Update `RaiseComponentEvent` & component lifestatge methods * Fix ComponentNetworkGenerator * a * A
This commit is contained in:
@@ -35,7 +35,8 @@ END TEMPLATE-->
|
||||
|
||||
### Breaking changes
|
||||
|
||||
*None yet*
|
||||
* `IEventBus.RaiseComponentEvent` now requires an EntityUid argument.
|
||||
* The `AddedComponentEventArgs` and `RemovedComponentEventArgs` constructors are now internal
|
||||
|
||||
### New features
|
||||
|
||||
|
||||
@@ -1379,7 +1379,7 @@ namespace Robust.Client.GameObjects
|
||||
|
||||
// TODO whenever sprite comp gets ECS'd , just make this a direct method call.
|
||||
var ev = new QueueSpriteTreeUpdateEvent(entities.GetComponent<TransformComponent>(Owner));
|
||||
entities.EventBus.RaiseComponentEvent(this, ref ev);
|
||||
entities.EventBus.RaiseComponentEvent(Owner, this, ref ev);
|
||||
}
|
||||
|
||||
private void QueueUpdateIsInert()
|
||||
@@ -1389,7 +1389,7 @@ namespace Robust.Client.GameObjects
|
||||
|
||||
// TODO whenever sprite comp gets ECS'd , just make this a direct method call.
|
||||
var ev = new SpriteUpdateInertEvent();
|
||||
entities.EventBus.RaiseComponentEvent(this, ref ev);
|
||||
entities.EventBus.RaiseComponentEvent(Owner, this, ref ev);
|
||||
}
|
||||
|
||||
[Obsolete("Use SpriteSystem instead.")]
|
||||
|
||||
@@ -603,7 +603,7 @@ namespace Robust.Client.GameStates
|
||||
if (compState != null)
|
||||
{
|
||||
var handleState = new ComponentHandleState(compState, null);
|
||||
_entities.EventBus.RaiseComponentEvent(comp, ref handleState);
|
||||
_entities.EventBus.RaiseComponentEvent(entity, comp, ref handleState);
|
||||
}
|
||||
|
||||
comp.LastModifiedTick = _timing.LastRealTick;
|
||||
@@ -640,7 +640,7 @@ namespace Robust.Client.GameStates
|
||||
if (state != null)
|
||||
{
|
||||
var stateEv = new ComponentHandleState(state, null);
|
||||
_entities.EventBus.RaiseComponentEvent(comp, ref stateEv);
|
||||
_entities.EventBus.RaiseComponentEvent(entity, comp, ref stateEv);
|
||||
}
|
||||
|
||||
comp.ClearCreationTick(); // don't undo the re-adding.
|
||||
@@ -1361,7 +1361,7 @@ namespace Robust.Client.GameStates
|
||||
continue;
|
||||
|
||||
var handleState = new ComponentHandleState(cur, next);
|
||||
bus.RaiseComponentEvent(comp, ref handleState);
|
||||
bus.RaiseComponentEvent(uid, comp, ref handleState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1516,7 +1516,7 @@ namespace Robust.Client.GameStates
|
||||
continue;
|
||||
|
||||
var handleState = new ComponentHandleState(state, null);
|
||||
_entityManager.EventBus.RaiseComponentEvent(comp, ref handleState);
|
||||
_entityManager.EventBus.RaiseComponentEvent(uid, comp, ref handleState);
|
||||
}
|
||||
|
||||
// ensure we don't have any extra components
|
||||
|
||||
@@ -275,7 +275,7 @@ namespace Robust.Shared.CompNetworkGenerator
|
||||
{
|
||||
eventRaise = @"
|
||||
var ev = new AfterAutoHandleStateEvent(args.Current);
|
||||
EntityManager.EventBus.RaiseComponentEvent(component, ref ev);";
|
||||
EntityManager.EventBus.RaiseComponentEvent(uid, component, ref ev);";
|
||||
}
|
||||
|
||||
return $@"// <auto-generated />
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace Robust.Shared.GameObjects
|
||||
public readonly ComponentEventArgs BaseArgs;
|
||||
public readonly ComponentRegistration ComponentType;
|
||||
|
||||
public AddedComponentEventArgs(ComponentEventArgs baseArgs, ComponentRegistration componentType)
|
||||
internal AddedComponentEventArgs(ComponentEventArgs baseArgs, ComponentRegistration componentType)
|
||||
{
|
||||
BaseArgs = baseArgs;
|
||||
ComponentType = componentType;
|
||||
@@ -48,11 +48,14 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
public readonly MetaDataComponent Meta;
|
||||
|
||||
public RemovedComponentEventArgs(ComponentEventArgs baseArgs, bool terminating, MetaDataComponent meta)
|
||||
public readonly CompIdx Idx;
|
||||
|
||||
internal RemovedComponentEventArgs(ComponentEventArgs baseArgs, bool terminating, MetaDataComponent meta, CompIdx idx)
|
||||
{
|
||||
BaseArgs = baseArgs;
|
||||
Terminating = terminating;
|
||||
Meta = meta;
|
||||
Idx = idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,39 +69,29 @@ namespace Robust.Shared.GameObjects
|
||||
/// DO NOT USE THIS IN CONTENT UNLESS YOU KNOW WHAT YOU'RE DOING, the only reason it's not internal
|
||||
/// is because of the component network source generator.
|
||||
/// </remarks>
|
||||
/// <typeparam name="TEvent">Event to dispatch.</typeparam>
|
||||
/// <param name="component">Component receiving the event.</param>
|
||||
/// <param name="args">Event arguments for the event.</param>
|
||||
public void RaiseComponentEvent<TEvent>(IComponent component, TEvent args)
|
||||
public void RaiseComponentEvent<TEvent, TComponent>(EntityUid uid, TComponent component, TEvent args)
|
||||
where TEvent : notnull
|
||||
where TComponent : IComponent;
|
||||
|
||||
/// <inheritdoc cref="RaiseComponentEvent{TEvent,TComponent}(Robust.Shared.GameObjects.EntityUid,TComponent,TEvent)"/>
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, TEvent args)
|
||||
where TEvent : notnull;
|
||||
|
||||
/// <summary>
|
||||
/// Dispatches an event directly to a specific component.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This has a very specific purpose, and has massive potential to be abused.
|
||||
/// DO NOT USE THIS IN CONTENT UNLESS YOU KNOW WHAT YOU'RE DOING, the only reason it's not internal
|
||||
/// is because of the component network source generator.
|
||||
/// </remarks>
|
||||
/// <typeparam name="TEvent">Event to dispatch.</typeparam>
|
||||
/// <param name="component">Component receiving the event.</param>
|
||||
/// <param name="idx">Type of the component, for faster lookups.</param>
|
||||
/// <param name="args">Event arguments for the event.</param>
|
||||
public void RaiseComponentEvent<TEvent>(IComponent component, CompIdx idx, TEvent args)
|
||||
/// <inheritdoc cref="RaiseComponentEvent{TEvent,TComponent}(Robust.Shared.GameObjects.EntityUid,TComponent,TEvent)"/>
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, CompIdx idx, TEvent args)
|
||||
where TEvent : notnull;
|
||||
|
||||
/// <summary>
|
||||
/// Dispatches an event directly to a specific component, by-ref.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This has a very specific purpose, and has massive potential to be abused.
|
||||
/// DO NOT USE THIS IN CONTENT UNLESS YOU KNOW WHAT YOU'RE DOING, the only reason it's not internal
|
||||
/// is because of the component network source generator.
|
||||
/// </remarks>
|
||||
/// <typeparam name="TEvent">Event to dispatch.</typeparam>
|
||||
/// <param name="component">Component receiving the event.</param>
|
||||
/// <param name="args">Event arguments for the event.</param>
|
||||
public void RaiseComponentEvent<TEvent>(IComponent component, ref TEvent args)
|
||||
/// <inheritdoc cref="RaiseComponentEvent{TEvent,TComponent}(Robust.Shared.GameObjects.EntityUid,TComponent,TEvent)"/>
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, ref TEvent args)
|
||||
where TEvent : notnull;
|
||||
|
||||
/// <inheritdoc cref="RaiseComponentEvent{TEvent,TComponent}(Robust.Shared.GameObjects.EntityUid,TComponent,TEvent)"/>
|
||||
public void RaiseComponentEvent<TEvent, TComponent>(EntityUid uid, TComponent component, ref TEvent args)
|
||||
where TEvent : notnull
|
||||
where TComponent : IComponent;
|
||||
|
||||
/// <inheritdoc cref="RaiseComponentEvent{TEvent,TComponent}(Robust.Shared.GameObjects.EntityUid,TComponent,TEvent)"/>
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, CompIdx idx, ref TEvent args)
|
||||
where TEvent : notnull;
|
||||
|
||||
public void OnlyCallOnRobustUnitTestISwearToGodPleaseSomebodyKillThisNightmare();
|
||||
@@ -140,37 +130,58 @@ namespace Robust.Shared.GameObjects
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
void IDirectedEventBus.RaiseComponentEvent<TEvent>(IComponent component, TEvent args)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, TEvent args)
|
||||
where TEvent : notnull
|
||||
{
|
||||
ref var unitRef = ref Unsafe.As<TEvent, Unit>(ref args);
|
||||
|
||||
DispatchComponent<TEvent>(
|
||||
component.Owner,
|
||||
component,
|
||||
_comFac.GetIndex(component.GetType()),
|
||||
ref unitRef);
|
||||
}
|
||||
|
||||
void IDirectedEventBus.RaiseComponentEvent<TEvent>(IComponent component, CompIdx type, TEvent args)
|
||||
{
|
||||
ref var unitRef = ref Unsafe.As<TEvent, Unit>(ref args);
|
||||
|
||||
DispatchComponent<TEvent>(
|
||||
component.Owner,
|
||||
component,
|
||||
type,
|
||||
ref unitRef);
|
||||
RaiseComponentEvent(uid, component, _comFac.GetIndex(component.GetType()), ref args);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
void IDirectedEventBus.RaiseComponentEvent<TEvent>(IComponent component, ref TEvent args)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RaiseComponentEvent<TEvent, TComponent>(EntityUid uid, TComponent component, TEvent args)
|
||||
where TEvent : notnull
|
||||
where TComponent : IComponent
|
||||
{
|
||||
RaiseComponentEvent(uid, component, CompIdx.Index<TComponent>(), ref args);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, CompIdx type, TEvent args)
|
||||
where TEvent : notnull
|
||||
{
|
||||
RaiseComponentEvent(uid, component, type, ref args);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, ref TEvent args)
|
||||
where TEvent : notnull
|
||||
{
|
||||
RaiseComponentEvent(uid, component, _comFac.GetIndex(component.GetType()), ref args);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RaiseComponentEvent<TEvent, TComponent>(EntityUid uid, TComponent component, ref TEvent args)
|
||||
where TEvent : notnull
|
||||
where TComponent : IComponent
|
||||
{
|
||||
RaiseComponentEvent(uid, component, CompIdx.Index<TComponent>(), ref args);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RaiseComponentEvent<TEvent>(EntityUid uid, IComponent component, CompIdx type, ref TEvent args)
|
||||
where TEvent : notnull
|
||||
{
|
||||
ref var unitRef = ref Unsafe.As<TEvent, Unit>(ref args);
|
||||
|
||||
DispatchComponent<TEvent>(
|
||||
component.Owner,
|
||||
uid,
|
||||
component,
|
||||
_comFac.GetIndex(component.GetType()),
|
||||
type,
|
||||
ref unitRef);
|
||||
}
|
||||
|
||||
@@ -399,7 +410,7 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
public void OnComponentRemoved(in RemovedComponentEventArgs e)
|
||||
{
|
||||
EntRemoveComponent(e.BaseArgs.Owner, _comFac.GetIndex(e.BaseArgs.Component.GetType()));
|
||||
EntRemoveComponent(e.BaseArgs.Owner, e.Idx);
|
||||
}
|
||||
|
||||
private void EntAddSubscription(
|
||||
|
||||
@@ -125,8 +125,8 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
foreach (var comp in comps)
|
||||
{
|
||||
if (comp is { LifeStage: ComponentLifeStage.Added })
|
||||
LifeInitialize(comp, _componentFactory.GetIndex(comp.GetType()));
|
||||
if (comp is {LifeStage: ComponentLifeStage.Added})
|
||||
LifeInitialize(uid, comp, _componentFactory.GetIndex(comp.GetType()));
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
@@ -159,19 +159,19 @@ namespace Robust.Shared.GameObjects
|
||||
// Init transform first, we always have it.
|
||||
var transform = TransformQuery.GetComponent(uid);
|
||||
if (transform.LifeStage == ComponentLifeStage.Initialized)
|
||||
LifeStartup(transform);
|
||||
LifeStartup(uid, transform, CompIdx.Index<TransformComponent>());
|
||||
|
||||
// Init physics second if it exists.
|
||||
if (_physicsQuery.TryComp(uid, out var phys) && phys.LifeStage == ComponentLifeStage.Initialized)
|
||||
{
|
||||
LifeStartup(phys);
|
||||
LifeStartup(uid, phys, CompIdx.Index<PhysicsComponent>());
|
||||
}
|
||||
|
||||
// Do rest of components.
|
||||
foreach (var comp in comps)
|
||||
{
|
||||
if (comp is { LifeStage: ComponentLifeStage.Initialized })
|
||||
LifeStartup(comp);
|
||||
LifeStartup(uid, comp, _componentFactory.GetIndex(comp.GetType()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,10 +267,10 @@ namespace Robust.Shared.GameObjects
|
||||
return;
|
||||
|
||||
if (!Comp.Initialized)
|
||||
((EntityManager) _entMan).LifeInitialize(Comp, CompType);
|
||||
((EntityManager) _entMan).LifeInitialize(_owner, Comp, CompType);
|
||||
|
||||
if (metadata.EntityInitialized && !Comp.Running)
|
||||
((EntityManager) _entMan).LifeStartup(Comp);
|
||||
((EntityManager) _entMan).LifeStartup(_owner, Comp, CompType);
|
||||
}
|
||||
|
||||
public static implicit operator T(CompInitializeHandle<T> handle)
|
||||
@@ -355,7 +355,7 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
// This will invalidate the comp ref as it removes the key from the dictionary.
|
||||
// This is inefficient, but component overriding rarely ever happens.
|
||||
RemoveComponentImmediate(comp!, uid, false, metadata);
|
||||
RemoveComponentImmediate(uid, comp!, type, false, metadata);
|
||||
dict.Add(uid, component);
|
||||
}
|
||||
else
|
||||
@@ -382,7 +382,7 @@ namespace Robust.Shared.GameObjects
|
||||
ComponentAdded?.Invoke(eventArgs);
|
||||
_eventBus.OnComponentAdded(eventArgs);
|
||||
|
||||
LifeAddToEntity(component, reg.Idx);
|
||||
LifeAddToEntity(uid, component, reg.Idx);
|
||||
|
||||
if (skipInit)
|
||||
return;
|
||||
@@ -395,20 +395,24 @@ namespace Robust.Shared.GameObjects
|
||||
if (component.Networked)
|
||||
DirtyEntity(uid, metadata);
|
||||
|
||||
LifeInitialize(component, reg.Idx);
|
||||
LifeInitialize(uid, component, reg.Idx);
|
||||
|
||||
if (metadata.EntityInitialized)
|
||||
LifeStartup(component);
|
||||
LifeStartup(uid, component, reg.Idx);
|
||||
|
||||
if (metadata.EntityLifeStage >= EntityLifeStage.MapInitialized)
|
||||
EventBus.RaiseComponentEvent(component, MapInitEventInstance);
|
||||
EventBus.RaiseComponentEvent(uid, component, reg.Idx, MapInitEventInstance);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool RemoveComponent<T>(EntityUid uid, MetaDataComponent? meta = null)
|
||||
public bool RemoveComponent<T>(EntityUid uid, MetaDataComponent? meta = null) where T : IComponent
|
||||
{
|
||||
return RemoveComponent(uid, typeof(T), meta);
|
||||
if (!TryGetComponent(uid, out T? comp))
|
||||
return false;
|
||||
|
||||
RemoveComponentImmediate(uid, comp, CompIdx.Index<T>(), false, meta);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -418,7 +422,7 @@ namespace Robust.Shared.GameObjects
|
||||
if (!TryGetComponent(uid, type, out var comp))
|
||||
return false;
|
||||
|
||||
RemoveComponentImmediate(comp, uid, false, meta);
|
||||
RemoveComponentImmediate(uid, comp, _componentFactory.GetIndex(type), false, meta);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -432,7 +436,8 @@ namespace Robust.Shared.GameObjects
|
||||
if (!TryGetComponent(uid, netId, out var comp, meta))
|
||||
return false;
|
||||
|
||||
RemoveComponentImmediate(comp, uid, false, meta);
|
||||
var idx = _componentFactory.GetIndex(comp.GetType());
|
||||
RemoveComponentImmediate(uid, comp, idx, false, meta);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -440,7 +445,8 @@ namespace Robust.Shared.GameObjects
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void RemoveComponent(EntityUid uid, IComponent component, MetaDataComponent? meta = null)
|
||||
{
|
||||
RemoveComponentImmediate(component, uid, false, meta);
|
||||
var idx = _componentFactory.GetIndex(component.GetType());
|
||||
RemoveComponentImmediate(uid, component, idx, false, meta);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -510,7 +516,8 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
foreach (var comp in InSafeOrder(_entCompIndex[uid]))
|
||||
{
|
||||
RemoveComponentImmediate(comp, uid, false, meta);
|
||||
var idx = _componentFactory.GetIndex(comp.GetType());
|
||||
RemoveComponentImmediate(uid, comp, idx, false, meta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -524,7 +531,8 @@ namespace Robust.Shared.GameObjects
|
||||
{
|
||||
try
|
||||
{
|
||||
RemoveComponentImmediate(comp, uid, true, meta);
|
||||
var idx = _componentFactory.GetIndex(comp.GetType());
|
||||
RemoveComponentImmediate(uid, comp, idx, true, meta);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -537,14 +545,16 @@ namespace Robust.Shared.GameObjects
|
||||
|
||||
private void RemoveComponentDeferred(IComponent component, EntityUid uid, bool terminating)
|
||||
{
|
||||
if (component == null) throw new ArgumentNullException(nameof(component));
|
||||
if (component == null)
|
||||
throw new ArgumentNullException(nameof(component));
|
||||
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
if (component.Owner != uid)
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
throw new InvalidOperationException("Component is not owned by entity.");
|
||||
|
||||
if (component.Deleted) return;
|
||||
if (component.Deleted)
|
||||
return;
|
||||
|
||||
#if EXCEPTION_TOLERANCE
|
||||
try
|
||||
@@ -564,7 +574,7 @@ namespace Robust.Shared.GameObjects
|
||||
}
|
||||
|
||||
if (component.LifeStage >= ComponentLifeStage.Initialized && component.LifeStage <= ComponentLifeStage.Running)
|
||||
LifeShutdown(component);
|
||||
LifeShutdown(uid, component, _componentFactory.GetIndex(component.GetType()));
|
||||
#if EXCEPTION_TOLERANCE
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -575,7 +585,11 @@ namespace Robust.Shared.GameObjects
|
||||
#endif
|
||||
}
|
||||
|
||||
private void RemoveComponentImmediate(IComponent component, EntityUid uid, bool terminating,
|
||||
private void RemoveComponentImmediate(
|
||||
EntityUid uid,
|
||||
IComponent component,
|
||||
CompIdx idx,
|
||||
bool terminating,
|
||||
MetaDataComponent? meta)
|
||||
{
|
||||
if (component.Deleted)
|
||||
@@ -596,10 +610,10 @@ namespace Robust.Shared.GameObjects
|
||||
}
|
||||
|
||||
if (component.Running)
|
||||
LifeShutdown(component);
|
||||
LifeShutdown(uid, component, idx);
|
||||
|
||||
if (component.LifeStage != ComponentLifeStage.PreAdd)
|
||||
LifeRemoveFromEntity(component); // Sets delete
|
||||
LifeRemoveFromEntity(uid, component, idx); // Sets delete
|
||||
|
||||
#if EXCEPTION_TOLERANCE
|
||||
}
|
||||
@@ -609,7 +623,7 @@ namespace Robust.Shared.GameObjects
|
||||
_runtimeLog.LogException(e, nameof(RemoveComponentImmediate));
|
||||
}
|
||||
#endif
|
||||
DeleteComponent(uid, component, terminating, meta);
|
||||
DeleteComponent(uid, component, idx, terminating, meta);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -620,6 +634,7 @@ namespace Robust.Shared.GameObjects
|
||||
if (component.Deleted)
|
||||
continue;
|
||||
var uid = component.Owner;
|
||||
var idx = _componentFactory.GetIndex(component.GetType());
|
||||
|
||||
#if EXCEPTION_TOLERANCE
|
||||
try
|
||||
@@ -630,11 +645,11 @@ namespace Robust.Shared.GameObjects
|
||||
{
|
||||
// TODO add options to cancel deferred deletion?
|
||||
_sawmill.Warning($"Found a running component while culling deferred deletions, owner={ToPrettyString(uid)}, type={component.GetType()}");
|
||||
LifeShutdown(component);
|
||||
LifeShutdown(uid, component, idx);
|
||||
}
|
||||
|
||||
if (component.LifeStage != ComponentLifeStage.PreAdd)
|
||||
LifeRemoveFromEntity(component);
|
||||
LifeRemoveFromEntity(uid, component, idx);
|
||||
|
||||
#if EXCEPTION_TOLERANCE
|
||||
}
|
||||
@@ -645,43 +660,49 @@ namespace Robust.Shared.GameObjects
|
||||
}
|
||||
#endif
|
||||
var meta = MetaQuery.GetComponent(uid);
|
||||
DeleteComponent(uid, component, false, meta);
|
||||
DeleteComponent(uid, component, idx, false, meta);
|
||||
}
|
||||
|
||||
_deleteSet.Clear();
|
||||
}
|
||||
|
||||
private void DeleteComponent(EntityUid entityUid, IComponent component, bool terminating, MetaDataComponent? metadata)
|
||||
private void DeleteComponent(
|
||||
EntityUid entityUid,
|
||||
IComponent component,
|
||||
CompIdx idx,
|
||||
bool terminating,
|
||||
MetaDataComponent? metadata)
|
||||
{
|
||||
if (!MetaQuery.ResolveInternal(entityUid, ref metadata))
|
||||
return;
|
||||
|
||||
var eventArgs = new RemovedComponentEventArgs(new ComponentEventArgs(component, entityUid), false, metadata);
|
||||
var eventArgs = new RemovedComponentEventArgs(new ComponentEventArgs(component, entityUid), false, metadata, idx);
|
||||
ComponentRemoved?.Invoke(eventArgs);
|
||||
_eventBus.OnComponentRemoved(eventArgs);
|
||||
|
||||
var reg = _componentFactory.GetRegistration(component);
|
||||
DebugTools.Assert(component.Networked == (reg.NetID != null));
|
||||
|
||||
if (!terminating && reg.NetID != null)
|
||||
if (!terminating)
|
||||
{
|
||||
if (!metadata.NetComponents.Remove(reg.NetID.Value))
|
||||
_sawmill.Error($"Entity {ToPrettyString(entityUid, metadata)} did not have {component.GetType().Name} in its networked component dictionary during component deletion.");
|
||||
|
||||
if (component.NetSyncEnabled)
|
||||
var reg = _componentFactory.GetRegistration(component);
|
||||
DebugTools.Assert(component.Networked == (reg.NetID != null));
|
||||
if (reg.NetID != null)
|
||||
{
|
||||
DirtyEntity(entityUid, metadata);
|
||||
metadata.LastComponentRemoved = _gameTiming.CurTick;
|
||||
if (!metadata.NetComponents.Remove(reg.NetID.Value))
|
||||
_sawmill.Error($"Entity {ToPrettyString(entityUid, metadata)} did not have {component.GetType().Name} in its networked component dictionary during component deletion.");
|
||||
|
||||
if (component.NetSyncEnabled)
|
||||
{
|
||||
DirtyEntity(entityUid, metadata);
|
||||
metadata.LastComponentRemoved = _gameTiming.CurTick;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_entTraitArray[reg.Idx.Value].Remove(entityUid);
|
||||
_entTraitArray[idx.Value].Remove(entityUid);
|
||||
|
||||
// TODO if terminating the entity, maybe defer this?
|
||||
// _entCompIndex.Remove(uid) gets called later on anyways.
|
||||
_entCompIndex.Remove(entityUid, component);
|
||||
|
||||
|
||||
DebugTools.Assert(_netMan.IsClient // Client side prediction can set LastComponentRemoved to some future tick,
|
||||
|| metadata.EntityLastModifiedTick >= metadata.LastComponentRemoved);
|
||||
}
|
||||
@@ -1407,7 +1428,7 @@ namespace Robust.Shared.GameObjects
|
||||
{
|
||||
DebugTools.Assert(component.NetSyncEnabled, $"Attempting to get component state for an un-synced component: {component.GetType()}");
|
||||
var getState = new ComponentGetState(session, fromTick);
|
||||
eventBus.RaiseComponentEvent(component, ref getState);
|
||||
eventBus.RaiseComponentEvent(component.Owner, component, ref getState);
|
||||
|
||||
return getState.State;
|
||||
}
|
||||
@@ -1415,7 +1436,7 @@ namespace Robust.Shared.GameObjects
|
||||
public bool CanGetComponentState(IEventBus eventBus, IComponent component, ICommonSession player)
|
||||
{
|
||||
var attempt = new ComponentGetStateAttemptEvent(player);
|
||||
eventBus.RaiseComponentEvent(component, ref attempt);
|
||||
eventBus.RaiseComponentEvent(component.Owner, component, ref attempt);
|
||||
return !attempt.Cancelled;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ public partial class EntityManager
|
||||
/// Increases the life stage from <see cref="ComponentLifeStage.PreAdd" /> to <see cref="ComponentLifeStage.Added" />,
|
||||
/// after raising a <see cref="ComponentAdd"/> event.
|
||||
/// </summary>
|
||||
internal void LifeAddToEntity(IComponent component, CompIdx type)
|
||||
internal void LifeAddToEntity(EntityUid uid, IComponent component, CompIdx idx)
|
||||
{
|
||||
DebugTools.Assert(component.LifeStage == ComponentLifeStage.PreAdd);
|
||||
|
||||
@@ -23,7 +23,7 @@ public partial class EntityManager
|
||||
component.CreationTick = CurrentTick;
|
||||
// networked components are assumed to be dirty when added to entities. See also: ClearTicks()
|
||||
component.LastModifiedTick = CurrentTick;
|
||||
EventBus.RaiseComponentEvent(component, type, CompAddInstance);
|
||||
EventBus.RaiseComponentEvent(uid, component, idx, CompAddInstance);
|
||||
component.LifeStage = ComponentLifeStage.Added;
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
}
|
||||
@@ -32,12 +32,12 @@ public partial class EntityManager
|
||||
/// Increases the life stage from <see cref="ComponentLifeStage.Added" /> to <see cref="ComponentLifeStage.Initialized" />,
|
||||
/// calling <see cref="Initialize" />.
|
||||
/// </summary>
|
||||
internal void LifeInitialize(IComponent component, CompIdx type)
|
||||
internal void LifeInitialize(EntityUid uid, IComponent component, CompIdx idx)
|
||||
{
|
||||
DebugTools.Assert(component.LifeStage == ComponentLifeStage.Added);
|
||||
|
||||
component.LifeStage = ComponentLifeStage.Initializing;
|
||||
EventBus.RaiseComponentEvent(component, type, CompInitInstance);
|
||||
EventBus.RaiseComponentEvent(uid, component, idx, CompInitInstance);
|
||||
component.LifeStage = ComponentLifeStage.Initialized;
|
||||
}
|
||||
|
||||
@@ -45,12 +45,12 @@ public partial class EntityManager
|
||||
/// Increases the life stage from <see cref="ComponentLifeStage.Initialized" /> to
|
||||
/// <see cref="ComponentLifeStage.Running" />, calling <see cref="Startup" />.
|
||||
/// </summary>
|
||||
internal void LifeStartup(IComponent component)
|
||||
internal void LifeStartup(EntityUid uid, IComponent component, CompIdx idx)
|
||||
{
|
||||
DebugTools.Assert(component.LifeStage == ComponentLifeStage.Initialized);
|
||||
|
||||
component.LifeStage = ComponentLifeStage.Starting;
|
||||
EventBus.RaiseComponentEvent(component, CompStartupInstance);
|
||||
EventBus.RaiseComponentEvent(uid, component, idx, CompStartupInstance);
|
||||
component.LifeStage = ComponentLifeStage.Running;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ public partial class EntityManager
|
||||
/// <remarks>
|
||||
/// Components are allowed to remove themselves in their own Startup function.
|
||||
/// </remarks>
|
||||
internal void LifeShutdown(IComponent component)
|
||||
internal void LifeShutdown(EntityUid uid, IComponent component, CompIdx idx)
|
||||
{
|
||||
DebugTools.Assert(component.LifeStage is >= ComponentLifeStage.Initializing and < ComponentLifeStage.Stopping);
|
||||
|
||||
@@ -73,7 +73,7 @@ public partial class EntityManager
|
||||
}
|
||||
|
||||
component.LifeStage = ComponentLifeStage.Stopping;
|
||||
EventBus.RaiseComponentEvent(component, CompShutdownInstance);
|
||||
EventBus.RaiseComponentEvent(uid, component, idx, CompShutdownInstance);
|
||||
component.LifeStage = ComponentLifeStage.Stopped;
|
||||
}
|
||||
|
||||
@@ -81,13 +81,13 @@ public partial class EntityManager
|
||||
/// Increases the life stage from <see cref="ComponentLifeStage.Stopped" /> to <see cref="ComponentLifeStage.Deleted" />,
|
||||
/// calling <see cref="Component.OnRemove" />.
|
||||
/// </summary>
|
||||
internal void LifeRemoveFromEntity(IComponent component)
|
||||
internal void LifeRemoveFromEntity(EntityUid uid, IComponent component, CompIdx idx)
|
||||
{
|
||||
// can be called at any time after PreAdd, including inside other life stage events.
|
||||
DebugTools.Assert(component.LifeStage != ComponentLifeStage.PreAdd);
|
||||
|
||||
component.LifeStage = ComponentLifeStage.Removing;
|
||||
EventBus.RaiseComponentEvent(component, CompRemoveInstance);
|
||||
EventBus.RaiseComponentEvent(uid, component, idx, CompRemoveInstance);
|
||||
component.LifeStage = ComponentLifeStage.Deleted;
|
||||
}
|
||||
|
||||
|
||||
@@ -586,7 +586,7 @@ namespace Robust.Shared.GameObjects
|
||||
{
|
||||
try
|
||||
{
|
||||
LifeShutdown(component);
|
||||
LifeShutdown(uid, component, _componentFactory.GetIndex(component.GetType()));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -100,7 +100,7 @@ namespace Robust.Shared.GameObjects
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The component reference type to remove.</typeparam>
|
||||
/// <param name="uid">Entity UID to modify.</param>
|
||||
bool RemoveComponent<T>(EntityUid uid, MetaDataComponent? meta = null);
|
||||
bool RemoveComponent<T>(EntityUid uid, MetaDataComponent? meta = null) where T : IComponent;
|
||||
|
||||
/// <summary>
|
||||
/// Removes the component with a specified type.
|
||||
@@ -294,7 +294,7 @@ namespace Robust.Shared.GameObjects
|
||||
/// <param name="uid">Entity UID to check.</param>
|
||||
/// <param name="component">Component of the specified type (if exists).</param>
|
||||
/// <returns>If the component existed in the entity.</returns>
|
||||
bool TryGetComponent<T>(EntityUid uid, [NotNullWhen(true)] out T? component) where T : IComponent?;
|
||||
bool TryGetComponent<T>(EntityUid uid, [NotNullWhen(true)] out T? component) where T : IComponent?;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the component of a specific type.
|
||||
|
||||
@@ -267,7 +267,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components
|
||||
|
||||
var containerMan = entManager.GetComponent<ContainerManagerComponent>(entity);
|
||||
var getState = new ComponentGetState();
|
||||
entManager.EventBus.RaiseComponentEvent(containerMan, ref getState);
|
||||
entManager.EventBus.RaiseComponentEvent(entity, containerMan, ref getState);
|
||||
var state = (ContainerManagerComponent.ContainerManagerComponentState)getState.State!;
|
||||
|
||||
Assert.That(state.Containers, Has.Count.EqualTo(1));
|
||||
|
||||
@@ -169,7 +169,7 @@ namespace Robust.UnitTesting.Shared.GameObjects
|
||||
entManMock.Raise(m => m.ComponentAdded += null, new AddedComponentEventArgs(new ComponentEventArgs(compInstance, entUid), reg));
|
||||
|
||||
// Raise
|
||||
((IEventBus)bus).RaiseComponentEvent(compInstance, new ComponentInit());
|
||||
((IEventBus)bus).RaiseComponentEvent(entUid, compInstance, new ComponentInit());
|
||||
|
||||
// Assert
|
||||
Assert.That(calledCount, Is.EqualTo(1));
|
||||
@@ -310,7 +310,7 @@ namespace Robust.UnitTesting.Shared.GameObjects
|
||||
handlerACount++;
|
||||
|
||||
// add and then remove component B
|
||||
bus.OnComponentRemoved(new RemovedComponentEventArgs(new ComponentEventArgs(instB, entUid), false, default!));
|
||||
bus.OnComponentRemoved(new RemovedComponentEventArgs(new ComponentEventArgs(instB, entUid), false, default!, CompIdx.Index<OrderBComponent>()));
|
||||
bus.OnComponentAdded(new AddedComponentEventArgs(new ComponentEventArgs(instB, entUid), regB));
|
||||
}
|
||||
|
||||
@@ -321,7 +321,7 @@ namespace Robust.UnitTesting.Shared.GameObjects
|
||||
handlerBCount++;
|
||||
|
||||
// add and then remove component A
|
||||
bus.OnComponentRemoved(new RemovedComponentEventArgs(new ComponentEventArgs(instA, entUid), false, default!));
|
||||
bus.OnComponentRemoved(new RemovedComponentEventArgs(new ComponentEventArgs(instA, entUid), false, default!, CompIdx.Index<OrderAComponent>()));
|
||||
bus.OnComponentAdded(new AddedComponentEventArgs(new ComponentEventArgs(instA, entUid), regA));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user