@@ -1029,12 +1029,12 @@ public bool TryGetComponent([NotNullWhen(true)] EntityUid? uid, ushort netId,
1029
1029
}
1030
1030
1031
1031
/// <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
1033
1033
{
1034
1034
component = default ;
1035
1035
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 ;
1038
1038
1039
1039
if ( ! HasComponent < T > ( source ) )
1040
1040
return false ;
@@ -1045,31 +1045,40 @@ public bool CopyComponent<T>(EntityUid source, EntityUid target, [NotNullWhen(tr
1045
1045
1046
1046
_serManager . CopyTo ( sourceComp , ref component , notNullableOverride : true ) ;
1047
1047
1048
- AddComponentInternal ( target , component , compReg , true , false , metadataTarget ) ;
1048
+ AddComponentInternal ( target , component , compReg , true , false , meta ) ;
1049
1049
return true ;
1050
1050
}
1051
1051
1052
1052
/// <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 )
1054
1054
{
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 ) ;
1056
1061
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 ] ;
1059
1068
return success ;
1060
1069
}
1061
1070
1062
1071
/// <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 )
1064
1073
{
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 ;
1067
1076
1068
1077
var allSuccessful = true ;
1069
1078
1070
1079
foreach ( var type in types )
1071
1080
{
1072
- if ( ! CopyComponent ( source , target , type , out _ , metadataTarget ) )
1081
+ if ( ! CopyComponent ( source , target , type , out _ , meta ) )
1073
1082
allSuccessful = false ;
1074
1083
}
1075
1084
0 commit comments