Skip to content

Commit a7433ee

Browse files
committed
maybe works?
1 parent dfb97d4 commit a7433ee

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

Robust.Shared/GameObjects/EntityManager.Components.cs

+21-12
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,12 @@ public bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, ushort netId,
10291029
}
10301030

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

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

10391039
if (!HasComponent<T>(source))
10401040
return false;
@@ -1045,31 +1045,40 @@ public bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(tr
10451045

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

1048-
AddComponentInternal(target, component, compReg, true, false, metadataTarget);
1048+
AddComponentInternal(target, component, compReg, true, false, meta);
10491049
return true;
10501050
}
10511051

10521052
/// <inheritdoc/>
1053-
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null)
1053+
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? meta = null)
10541054
{
1055-
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type), $"Type {type} is not a component");
1055+
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type) && type != typeof(IComponent), $"Invalid component type: {type}");
1056+
1057+
var method = GetType()
1058+
.GetMethod(nameof(CopyComponent), [typeof(EntityUid), typeof(EntityUid), type.MakeByRefType(), typeof(MetaDataComponent)
1059+
])?
1060+
.MakeGenericMethod(type);
10561061

1057-
var success = CopyComponent<IComponent>(source, target, out var baseComponent, metadataTarget);
1058-
component = baseComponent;
1062+
if (method == null)
1063+
throw new InvalidOperationException($"Could not create generic method for type {type}");
1064+
1065+
var parameters = new object?[] { source, target, null, meta };
1066+
var success = (bool)method.Invoke(this, parameters)!;
1067+
component = (IComponent?)parameters[2];
10591068
return success;
10601069
}
10611070

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

10681077
var allSuccessful = true;
10691078

10701079
foreach (var type in types)
10711080
{
1072-
if (!CopyComponent(source, target, type, out _, metadataTarget))
1081+
if (!CopyComponent(source, target, type, out _, meta))
10731082
allSuccessful = false;
10741083
}
10751084

Robust.Shared/GameObjects/IEntityManager.Components.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ public partial interface IEntityManager
349349
/// <param name="source">The source entity to copy from</param>
350350
/// <param name="target">The target entity to copy to</param>
351351
/// <param name="component">The copied component if successful</param>
352-
/// <param name="metadataTarget">Optional metadata of the target entity</param>
352+
/// <param name="meta">Optional metadata of the target entity</param>
353353
/// <returns>Whether the component was successfully copied</returns>
354-
bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? metadataTarget = null) where T : IComponent;
354+
bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(true)] out T? component, MetaDataComponent? meta = null) where T : IComponent;
355355

356356
/// <summary>
357357
/// Copy a single component from source to target entity and return a reference to the copied component.
@@ -360,19 +360,19 @@ public partial interface IEntityManager
360360
/// <param name="target">The target entity to copy the component to</param>
361361
/// <param name="type">The type of component to copy</param>
362362
/// <param name="component">The copied component if successful</param>
363-
/// <param name="metadataTarget">Optional metadata of the target entity</param>
363+
/// <param name="meta">Optional metadata of the target entity</param>
364364
/// <returns>Whether the component was successfully copied</returns>
365-
bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null);
365+
bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? meta = null);
366366

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

377377
/// <summary>
378378
/// Returns a cached struct enumerator with the specified component.

0 commit comments

Comments
 (0)