Skip to content

Commit fdba05d

Browse files
committed
maybe works?
1 parent dfb97d4 commit fdba05d

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

Robust.Shared/GameObjects/EntityManager.Components.cs

+23-14
Original file line numberDiff line numberDiff line change
@@ -1029,47 +1029,56 @@ 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

1039-
if (!HasComponent<T>(source))
1039+
if (!TryGetComponent<T>(source, out var sourceComp))
10401040
return false;
10411041

1042-
var sourceComp = GetComponent<T>(source);
10431042
var compReg = ComponentFactory.GetRegistration(typeof(T));
10441043
component = (T)ComponentFactory.GetComponent(compReg);
10451044

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

1048-
AddComponentInternal(target, component, compReg, true, false, metadataTarget);
1047+
AddComponentInternal(target, component, compReg, true, false, meta);
10491048
return true;
10501049
}
10511050

10521051
/// <inheritdoc/>
1053-
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? metadataTarget = null)
1052+
public bool CopyComponent(EntityUid source, EntityUid target, Type type, [NotNullWhen(true)] out IComponent? component, MetaDataComponent? meta = null)
10541053
{
1055-
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type), $"Type {type} is not a component");
1054+
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(type) && type != typeof(IComponent), $"Invalid component type: {type}");
1055+
1056+
var method = GetType()
1057+
.GetMethod(nameof(CopyComponent), [typeof(EntityUid), typeof(EntityUid), type, typeof(MetaDataComponent)
1058+
]);
1059+
1060+
var genericMethod = method!.MakeGenericMethod();
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)genericMethod.Invoke(this, [source, target, type, meta])!;
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)