Skip to content

Commit

Permalink
maybe works?
Browse files Browse the repository at this point in the history
  • Loading branch information
MilonPL committed Feb 10, 2025
1 parent dfb97d4 commit fdba05d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
37 changes: 23 additions & 14 deletions Robust.Shared/GameObjects/EntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,47 +1029,56 @@ public bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, ushort netId,
}

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

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

if (!HasComponent<T>(source))
if (!TryGetComponent<T>(source, out var sourceComp))
return false;

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

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

AddComponentInternal(target, component, compReg, true, false, metadataTarget);
AddComponentInternal(target, component, compReg, true, false, meta);
return true;
}

/// <inheritdoc/>
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null)
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? meta = null)
{
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type), $"Type {type} is not a component");
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type) && type != typeof(IComponent), $"Invalid component type: {type}");

var method = GetType()
.GetMethod(nameof(CopyComponent), [typeof(EntityUid), typeof(EntityUid), type, typeof(MetaDataComponent)
]);

var genericMethod = method!.MakeGenericMethod();

var success = CopyComponent<IComponent>(source, target, out var baseComponent, metadataTarget);
component = baseComponent;
if (method == null)
throw new InvalidOperationException($"Could not create generic method for type {type}");

var parameters = new object?[] { source, target, null, meta };
var success = (bool)genericMethod.Invoke(this, [source, target, type, meta])!;
component = (IComponent?)parameters[2];
return success;
}

/// <inheritdoc/>
public bool CopyComponents(EntityUid source, EntityUid target, MetaDataComponent? metadataTarget = null, params Type[] types)
public bool CopyComponents(EntityUid source, EntityUid target, MetaDataComponent? meta = null, params Type[] types)
{
if (!MetaQuery.Resolve(target, ref metadataTarget, false))
throw new ArgumentException($"Entity {target} is not valid.", nameof(target));
if (!MetaQuery.Resolve(target, ref meta))
return false;

var allSuccessful = true;

foreach (var type in types)
{
if (!CopyComponent(source, target, type, out _, metadataTarget))
if (!CopyComponent(source, target, type, out _, meta))
allSuccessful = false;
}

Expand Down
12 changes: 6 additions & 6 deletions Robust.Shared/GameObjects/IEntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ public partial interface IEntityManager
/// <param name="source">The source entity to copy from</param>
/// <param name="target">The target entity to copy to</param>
/// <param name="component">The copied component if successful</param>
/// <param name="metadataTarget">Optional metadata of the target entity</param>
/// <param name="meta">Optional metadata of the target entity</param>
/// <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;
bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? meta = null) where T : IComponent;

/// <summary>
/// Copy a single component from source to target entity and return a reference to the copied component.
Expand All @@ -360,19 +360,19 @@ public partial interface IEntityManager
/// <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>
/// <param name="meta">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);
bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? meta = null);

/// <summary>
/// Copy multiple components from source to target entity.
/// </summary>
/// <param name="source">The source entity to copy from</param>
/// <param name="target">The target entity to copy to</param>
/// <param name="metadataTarget">Optional metadata of the target entity</param>
/// <param name="meta">Optional metadata of the target entity</param>
/// <param name="types">Array of component types to copy</param>
/// <returns>Whether all components were successfully copied</returns>
bool CopyComponents(EntityUid source, EntityUid target, MetaDataComponent? metadataTarget = null, params Type[] types);
bool CopyComponents(EntityUid source, EntityUid target, MetaDataComponent? meta = null, params Type[] types);

/// <summary>
/// Returns a cached struct enumerator with the specified component.
Expand Down

0 comments on commit fdba05d

Please sign in to comment.