@@ -1029,47 +1029,56 @@ 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
- if ( ! HasComponent < T > ( source ) )
1039
+ if ( ! TryGetComponent < T > ( source , out var sourceComp ) )
1040
1040
return false ;
1041
1041
1042
- var sourceComp = GetComponent < T > ( source ) ;
1043
1042
var compReg = ComponentFactory . GetRegistration ( typeof ( T ) ) ;
1044
1043
component = ( T ) ComponentFactory . GetComponent ( compReg ) ;
1045
1044
1046
1045
_serManager . CopyTo ( sourceComp , ref component , notNullableOverride : true ) ;
1047
1046
1048
- AddComponentInternal ( target , component , compReg , true , false , metadataTarget ) ;
1047
+ AddComponentInternal ( target , component , compReg , true , false , meta ) ;
1049
1048
return true ;
1050
1049
}
1051
1050
1052
1051
/// <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 )
1054
1053
{
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 ( ) ;
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 ) genericMethod . Invoke ( this , [ source , target , type , meta ] ) ! ;
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