Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
MilonPL committed Feb 8, 2025
1 parent 9124d01 commit dfb97d4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
29 changes: 12 additions & 17 deletions Robust.Shared/GameObjects/EntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,22 +1029,19 @@ public bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, ushort netId,
}

/// <inheritdoc/>
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null)
public bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? metadataTarget = null) where T : IComponent
{
component = null;
component = default;

if (!MetaQuery.Resolve(target, ref metadataTarget, false))
throw new ArgumentException($"Entity {target} is not valid.", nameof(target));

if (!typeof(IComponent).IsAssignableFrom(type))
if (!HasComponent<T>(source))
return false;

var compReg = ComponentFactory.GetRegistration(type);
if (!HasComponent(source, type))
return false;

var sourceComp = GetComponent(source, type);
component = ComponentFactory.GetComponent(compReg);
var sourceComp = GetComponent<T>(source);
var compReg = ComponentFactory.GetRegistration(typeof(T));
component = (T)ComponentFactory.GetComponent(compReg);

_serManager.CopyTo(sourceComp, ref component, notNullableOverride: true);

Expand All @@ -1053,15 +1050,13 @@ public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNul
}

/// <inheritdoc/>
public bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? metadataTarget = null) where T : IComponent
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null)
{
component = default;
if (CopyComponent(source, target, typeof(T), out var baseComponent, metadataTarget))
{
component = (T)baseComponent;
return true;
}
return false;
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type), $"Type {type} is not a component");

var success = CopyComponent<IComponent>(source, target, out var baseComponent, metadataTarget);
component = baseComponent;
return success;
}

/// <inheritdoc/>
Expand Down
12 changes: 6 additions & 6 deletions Robust.Shared/GameObjects/EntitySystem.Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,18 +567,18 @@ protected bool TryGetEntityData(NetEntity nuid, [NotNullWhen(true)] out EntityUi

#region Component Copy

/// <inheritdoc cref="IEntityManager.CopyComponent"/>
/// <inheritdoc cref="IEntityManager.CopyComponent{T}"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected bool CopyComp(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null)
protected bool CopyComp<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? metadataTarget = null) where T : IComponent
{
return EntityManager.CopyComponent(source, target, type, out component, metadataTarget);
return EntityManager.CopyComponent(source, target, out component, metadataTarget);
}

/// <inheritdoc cref="IEntityManager.CopyComponent{T}"/>
/// <inheritdoc cref="IEntityManager.CopyComponent"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected bool CopyComp<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? metadataTarget = null) where T : IComponent
protected bool CopyComp(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null)
{
return EntityManager.CopyComponent(source, target, out component, metadataTarget);
return EntityManager.CopyComponent(source, target, type, out component, metadataTarget);
}

/// <inheritdoc cref="IEntityManager.CopyComponents"/>
Expand Down
21 changes: 11 additions & 10 deletions Robust.Shared/GameObjects/IEntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,6 @@ public partial interface IEntityManager
/// <returns>If the component existed in the entity.</returns>
bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, ushort netId, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? meta = null);

/// <summary>
/// Copy a single component from source to target entity and return a reference to the copied component.
/// </summary>
/// <param name="source">The source entity to copy the component from</param>
/// <param name="target">The target entity to copy the component to</param>
/// <param name="type">The type of component to copy</param>
/// <param name="component">The copied component if successful</param>
/// <param name="metadataTarget">Optional metadata of the target entity</param>
/// <returns>Whether the component was successfully copied</returns>
bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null);

/// <summary>
/// Copy a typed component from source to target entity and return a reference to the copied component.
Expand All @@ -363,6 +353,17 @@ public partial interface IEntityManager
/// <returns>Whether the component was successfully copied</returns>
bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? metadataTarget = null) where T : IComponent;

/// <summary>
/// Copy a single component from source to target entity and return a reference to the copied component.
/// </summary>
/// <param name="source">The source entity to copy the component from</param>
/// <param name="target">The target entity to copy the component to</param>
/// <param name="type">The type of component to copy</param>
/// <param name="component">The copied component if successful</param>
/// <param name="metadataTarget">Optional metadata of the target entity</param>
/// <returns>Whether the component was successfully copied</returns>
bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null);

/// <summary>
/// Copy multiple components from source to target entity.
/// </summary>
Expand Down

0 comments on commit dfb97d4

Please sign in to comment.