Skip to content

Commit

Permalink
Add new EnsureEntity variants (space-wizards#4586)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr authored Nov 20, 2023
1 parent 96cb52e commit 202182e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static string GenerateSource(in GeneratorExecutionContext context, IName
getStateInit.Append($@"
{name} = GetNetEntitySet(component.{name}),");
handleStateSetters.Append($@"
component.{name} = EnsureEntitySet<{componentName}>(state.{name}, uid);");
EnsureEntitySet<{componentName}>(state.{name}, uid, component.{name});");

break;
case GlobalEntityUidListName:
Expand All @@ -177,7 +177,7 @@ private static string GenerateSource(in GeneratorExecutionContext context, IName
getStateInit.Append($@"
{name} = GetNetEntityList(component.{name}),");
handleStateSetters.Append($@"
component.{name} = EnsureEntityList<{componentName}>(state.{name}, uid);");
EnsureEntityList<{componentName}>(state.{name}, uid, component.{name});");

break;
default:
Expand Down
23 changes: 21 additions & 2 deletions Robust.Shared/GameObjects/EntityManager.Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ public virtual EntityCoordinates EnsureCoordinates<T>(NetCoordinates netCoordina
/// <inheritdoc />
public HashSet<EntityUid> GetEntitySet(HashSet<NetEntity> netEntities)
{
var entities = new HashSet<EntityUid>();
entities.EnsureCapacity(netEntities.Count);
var entities = new HashSet<EntityUid>(netEntities.Count);

foreach (var netEntity in netEntities)
{
Expand Down Expand Up @@ -292,6 +291,16 @@ public HashSet<EntityUid> EnsureEntitySet<T>(HashSet<NetEntity> netEntities, Ent
return entities;
}

public void EnsureEntitySet<T>(HashSet<NetEntity> netEntities, EntityUid callerEntity, HashSet<EntityUid> entities)
{
entities.Clear();
entities.EnsureCapacity(netEntities.Count);
foreach (var netEntity in netEntities)
{
entities.Add(EnsureEntity<T>(netEntity, callerEntity));
}
}

/// <inheritdoc />
public List<EntityUid> EnsureEntityList<T>(List<NetEntity> netEntities, EntityUid callerEntity)
{
Expand All @@ -305,6 +314,16 @@ public List<EntityUid> EnsureEntityList<T>(List<NetEntity> netEntities, EntityUi
return entities;
}

public void EnsureEntityList<T>(List<NetEntity> netEntities, EntityUid callerEntity, List<EntityUid> entities)
{
entities.Clear();
entities.EnsureCapacity(netEntities.Count);
foreach (var netEntity in netEntities)
{
entities.Add(EnsureEntity<T>(netEntity, callerEntity));
}
}

/// <inheritdoc />
public List<EntityUid> GetEntityList(ICollection<NetEntity> netEntities)
{
Expand Down
12 changes: 12 additions & 0 deletions Robust.Shared/GameObjects/EntitySystem.Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,24 @@ protected HashSet<EntityUid> EnsureEntitySet<T>(HashSet<NetEntity> netEntities,
return EntityManager.EnsureEntitySet<T>(netEntities, callerEntity);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void EnsureEntitySet<T>(HashSet<NetEntity> netEntities, EntityUid callerEntity, HashSet<EntityUid> entities)
{
EntityManager.EnsureEntitySet<T>(netEntities, callerEntity, entities);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected List<EntityUid> EnsureEntityList<T>(List<NetEntity> netEntities, EntityUid callerEntity)
{
return EntityManager.EnsureEntityList<T>(netEntities, callerEntity);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void EnsureEntityList<T>(List<NetEntity> netEntities, EntityUid callerEntity, List<EntityUid> entities)
{
EntityManager.EnsureEntityList<T>(netEntities, callerEntity, entities);
}

/// <summary>
/// Returns the <see cref="EntityUid"/> of a <see cref="NetEntity"/>. Returns <see cref="EntityUid.Invalid"/> if it doesn't exist.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions Robust.Shared/Physics/Systems/SharedJointSystem.Relay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ private void OnRelayHandleState(EntityUid uid, JointRelayTargetComponent compone
if (args.Current is not JointRelayComponentState state)
return;

component.Relayed.Clear();
component.Relayed.UnionWith(EnsureEntitySet<JointRelayTargetComponent>(state.Entities, uid));
EnsureEntitySet<JointRelayTargetComponent>(state.Entities, uid, component.Relayed);
}

private void OnRelayShutdown(EntityUid uid, JointRelayTargetComponent component, ComponentShutdown args)
Expand Down

0 comments on commit 202182e

Please sign in to comment.