@@ -250,6 +250,35 @@ public void Copy<T>(int index, in T cmp)
250250        Unsafe . Add ( ref  item ,  index )  =  cmp; 
251251    } 
252252
253+     [ Pure ] 
254+     internal  bool  TryIndex < T > ( out  int  i ) 
255+     { 
256+         var  id  =  Component < T > . ComponentType . Id ; 
257+         return  TryIndex ( id ,  out  i ) ; 
258+     } 
259+ 
260+     [ Pure ] 
261+     internal  bool  TryIndex ( int  id ,  out  int  i ) 
262+     { 
263+         Debug . Assert ( id  !=  - 1 ,  $ "Supplied component index is invalid") ; 
264+ 
265+         if  ( id  >=  ComponentIdToArrayIndex . Length ) 
266+         { 
267+             i  =  - 1 ; 
268+             return  false ; 
269+         } 
270+ 
271+         i  =  ComponentIdToArrayIndex . DangerousGetReferenceAt ( id ) ; 
272+         return  i  !=  - 1 ; 
273+     } 
274+ 
275+     [ Pure ] 
276+     public  bool  Has ( int  id ) 
277+     { 
278+         var  idToArrayIndex  =  ComponentIdToArrayIndex ; 
279+         return  id  <  idToArrayIndex . Length  &&  idToArrayIndex . DangerousGetReferenceAt ( id )  !=  - 1 ; 
280+     } 
281+ 
253282    /// <summary> 
254283    ///     Checks if a component is included in this <see cref="Chunk"/>. 
255284    /// </summary> 
@@ -259,8 +288,7 @@ public void Copy<T>(int index, in T cmp)
259288    public  bool  Has < T > ( ) 
260289    { 
261290        var  id  =  Component < T > . ComponentType . Id ; 
262-         var  idToArrayIndex  =  ComponentIdToArrayIndex ; 
263-         return  id  <  idToArrayIndex . Length  &&  idToArrayIndex . DangerousGetReferenceAt ( id )  !=  - 1 ; 
291+         return  Has ( id ) ; 
264292    } 
265293
266294    /// <summary> 
@@ -451,12 +479,7 @@ public void Copy(int index, object cmp)
451479    public  bool  Has ( ComponentType  t ) 
452480    { 
453481        var  id  =  t . Id ; 
454-         if  ( id  >=  ComponentIdToArrayIndex . Length ) 
455-         { 
456-             return  false ; 
457-         } 
458- 
459-         return  ComponentIdToArrayIndex . DangerousGetReferenceAt ( id )  !=  - 1 ; 
482+         return  Has ( id ) ; 
460483    } 
461484
462485    /// <summary> 
@@ -550,45 +573,37 @@ internal static void Fill<T>(ref Chunk chunk, int destinationIndex, int length,
550573    /// </summary> 
551574    /// <param name="source">The source <see cref="Chunk"/>.</param> 
552575    /// <param name="index">The start index in the source <see cref="Chunk"/>.</param> 
576+     /// <param name="sourceSignature">The <see cref="Signature"/> from the source archetype.</param> 
553577    /// <param name="destination">The destination <see cref="Chunk"/>.</param> 
554578    /// <param name="destinationIndex">The start index in the destination <see cref="Chunk"/>.</param> 
555579    /// <param name="length">The length indicating the amount of <see cref="Entity"/>s being copied.</param> 
556-     [ Pure ] 
557-     internal  static void  Copy ( ref  Chunk  source ,  int  index ,  ref  Chunk  destination ,  int  destinationIndex ,  int  length ) 
580+     internal  static void  Copy ( 
581+         ref  Chunk  source ,  int  index ,  ref  Signature  sourceSignature , 
582+         ref  Chunk  destination ,  int  destinationIndex , 
583+         int  length ) 
558584    { 
559585        // Arrays 
560586        var  entities  =  source . Entities ; 
561-         var  sourceComponents  =  source . Components ; 
562587
563588        // Copy entities array 
564589        Array . Copy ( entities ,  index ,  destination . Entities ,  destinationIndex ,  length ) ; 
565590
566-         // Copy component arrays 
567-         for  ( var  i  =  0 ;  i  <  sourceComponents . Length ;  i ++ ) 
568-         { 
569-             var  sourceArray  =  sourceComponents [ i ] ; 
570-             var  sourceType  =  ( ComponentType )  sourceArray . GetType ( ) . GetElementType ( ) ! ; 
571- 
572-             if  ( ! destination . Has ( sourceType ) ) 
573-             { 
574-                 continue ; 
575-             } 
576- 
577-             var  destinationArray  =  destination . GetArray ( sourceType ) ; 
578-             Array . Copy ( sourceArray ,  index ,  destinationArray ,  destinationIndex ,  length ) ; 
579-         } 
591+         CopyComponents ( ref  source ,  index ,  ref  sourceSignature ,  ref  destination ,  destinationIndex ,  length ) ; 
580592    } 
581593
582594    /// <summary> 
583595    ///     Copies an <see cref="Arch.Core.Entity"/> components at one index to another <see cref="Chunk"/>-index. 
584596    /// </summary> 
585597    /// <param name="source">The source <see cref="Chunk"/>.</param> 
586598    /// <param name="index">The start index in the source <see cref="Chunk"/>.</param> 
599+     /// <param name="sourceSignature">The <see cref="Signature"/> from the source archetype.</param> 
587600    /// <param name="destination">The destination <see cref="Chunk"/>.</param> 
588601    /// <param name="destinationIndex">The start index in the destination <see cref="Chunk"/>.</param> 
589602    /// <param name="length">The length indicating the amount of <see cref="Entity"/>s being copied.</param> 
590-     [ Pure ] 
591-     internal  static void  CopyComponents ( ref  Chunk  source ,  int  index ,  ref  Chunk  destination ,  int  destinationIndex ,  int  length ) 
603+     internal  static void  CopyComponents ( 
604+         ref  Chunk  source ,  int  index ,  ref  Signature  sourceSignature , 
605+         ref  Chunk  destination ,  int  destinationIndex , 
606+         int  length ) 
592607    { 
593608        // Arrays 
594609        var  sourceComponents  =  source . Components ; 
@@ -597,15 +612,15 @@ internal static void CopyComponents(ref Chunk source, int index, ref Chunk desti
597612        for  ( var  i  =  0 ;  i  <  sourceComponents . Length ;  i ++ ) 
598613        { 
599614            var  sourceArray  =  sourceComponents [ i ] ; 
600-             var  sourceType  =  sourceArray . GetType ( ) . GetElementType ( ) ; 
601-             var  compType  =  ( ComponentType )  sourceType ! ; 
615+             var  sourceType  =  sourceSignature . Components [ i ] ; 
602616
603-             if  ( ! destination . Has ( compType ) ) 
617+             // Doesn't have component in destination array. 
618+             if  ( ! destination . TryIndex ( sourceType . Id ,  out  var  arrayIndex ) ) 
604619            { 
605620                continue ; 
606621            } 
607622
608-             var  destinationArray  =  destination . GetArray ( compType ) ; 
623+             var  destinationArray  =  destination . Components . DangerousGetReferenceAt ( arrayIndex ) ; 
609624            Array . Copy ( sourceArray ,  index ,  destinationArray ,  destinationIndex ,  length ) ; 
610625        } 
611626    } 
0 commit comments