@@ -466,13 +466,13 @@ public class ProxyEntity : Entity
466466 public ProxyEntity ( ) : base ( EntityVariant . PROXY ) { }
467467 public ProxyEntity ( ShortGuid shortGUID ) : base ( shortGUID , EntityVariant . PROXY ) { }
468468
469- public ProxyEntity ( List < ShortGuid > hierarchy = null , ShortGuid targetType = new ShortGuid ( ) , bool autoGenerateParameters = false ) : base ( EntityVariant . PROXY )
469+ public ProxyEntity ( ShortGuid [ ] hierarchy = null , ShortGuid targetType = new ShortGuid ( ) , bool autoGenerateParameters = false ) : base ( EntityVariant . PROXY )
470470 {
471471 this . function = targetType ;
472472 if ( hierarchy != null ) this . proxy . path = hierarchy ;
473473 if ( autoGenerateParameters ) EntityUtils . ApplyDefaults ( this ) ;
474474 }
475- public ProxyEntity ( ShortGuid shortGUID , List < ShortGuid > hierarchy = null , ShortGuid targetType = new ShortGuid ( ) , bool autoGenerateParameters = false ) : base ( shortGUID , EntityVariant . PROXY )
475+ public ProxyEntity ( ShortGuid shortGUID , ShortGuid [ ] hierarchy = null , ShortGuid targetType = new ShortGuid ( ) , bool autoGenerateParameters = false ) : base ( shortGUID , EntityVariant . PROXY )
476476 {
477477 this . shortGUID = shortGUID ;
478478 this . function = targetType ;
@@ -489,11 +489,11 @@ public class AliasEntity : Entity
489489 public AliasEntity ( ) : base ( EntityVariant . ALIAS ) { }
490490 public AliasEntity ( ShortGuid shortGUID ) : base ( shortGUID , EntityVariant . ALIAS ) { }
491491
492- public AliasEntity ( List < ShortGuid > hierarchy = null ) : base ( EntityVariant . ALIAS )
492+ public AliasEntity ( ShortGuid [ ] hierarchy = null ) : base ( EntityVariant . ALIAS )
493493 {
494494 if ( hierarchy != null ) this . alias . path = hierarchy ;
495495 }
496- public AliasEntity ( ShortGuid shortGUID , List < ShortGuid > hierarchy = null ) : base ( shortGUID , EntityVariant . ALIAS )
496+ public AliasEntity ( ShortGuid shortGUID , ShortGuid [ ] hierarchy = null ) : base ( shortGUID , EntityVariant . ALIAS )
497497 {
498498 this . shortGUID = shortGUID ;
499499 if ( hierarchy != null ) this . alias . path = hierarchy ;
@@ -678,21 +678,19 @@ public override int GetHashCode()
678678 public class EntityPath
679679 {
680680 public EntityPath ( ) { }
681- public EntityPath ( List < ShortGuid > _path )
681+ public EntityPath ( ShortGuid [ ] _path )
682682 {
683- path = _path ;
684-
685- if ( path . Count == 0 || path [ path . Count - 1 ] != ShortGuid . Invalid )
686- path . Add ( ShortGuid . Invalid ) ;
683+ path = ( ShortGuid [ ] ) _path . Clone ( ) ;
684+ EnsureFinalIsEmpty ( ) ;
687685 }
688- public List < ShortGuid > path = new List < ShortGuid > ( ) ;
686+ public ShortGuid [ ] path = new ShortGuid [ 0 ] ;
689687
690688 public static bool operator == ( EntityPath x , EntityPath y )
691689 {
692690 if ( ReferenceEquals ( x , null ) ) return ReferenceEquals ( y , null ) ;
693691 if ( ReferenceEquals ( y , null ) ) return ReferenceEquals ( x , null ) ;
694- if ( x . path . Count != y . path . Count ) return false ;
695- for ( int i = 0 ; i < x . path . Count ; i ++ )
692+ if ( x . path . Length != y . path . Length ) return false ;
693+ for ( int i = 0 ; i < x . path . Length ; i ++ )
696694 {
697695 if ( x . path [ i ] . ToByteString ( ) != y . path [ i ] . ToByteString ( ) )
698696 return false ;
@@ -706,23 +704,32 @@ public EntityPath(List<ShortGuid> _path)
706704
707705 public override bool Equals ( object obj )
708706 {
709- return obj is EntityPath hierarchy &&
710- EqualityComparer < List < ShortGuid > > . Default . Equals ( this . path , hierarchy . path ) ;
707+ return obj is EntityPath path &&
708+ EqualityComparer < ShortGuid [ ] > . Default . Equals ( this . path , path . path ) ;
711709 }
712710
713711 public override int GetHashCode ( )
714712 {
715- return 218564712 + EqualityComparer < List < ShortGuid > > . Default . GetHashCode ( path ) ;
713+ return - 1757656154 + EqualityComparer < ShortGuid [ ] > . Default . GetHashCode ( path ) ;
714+ }
715+
716+ private void EnsureFinalIsEmpty ( )
717+ {
718+ if ( path . Length == 0 || path [ path . Length - 1 ] != ShortGuid . Invalid )
719+ {
720+ Array . Resize ( ref path , path . Length + 1 ) ;
721+ path [ path . Length - 1 ] = ShortGuid . Invalid ;
722+ }
716723 }
717724
718725 /* Get this path as a string */
719726 public string GetAsString ( )
720727 {
721728 string val = "" ;
722- for ( int i = 0 ; i < path . Count ; i ++ )
729+ for ( int i = 0 ; i < path . Length ; i ++ )
723730 {
724731 val += path [ i ] . ToByteString ( ) ;
725- if ( i != path . Count - 1 ) val += " -> " ;
732+ if ( i != path . Length - 1 ) val += " -> " ;
726733 }
727734 return val ;
728735 }
@@ -735,7 +742,7 @@ public string GetAsString(Commands commands, Composite composite, bool withIDs =
735742 public UInt32 ToUInt32 ( )
736743 {
737744 UInt32 val = 0 ;
738- for ( int i = 0 ; i < path . Count ; i ++ ) val += path [ i ] . ToUInt32 ( ) ;
745+ for ( int i = 0 ; i < path . Length ; i ++ ) val += path [ i ] . ToUInt32 ( ) ;
739746 return val ;
740747 }
741748
@@ -771,7 +778,7 @@ public Entity GetPointedEntity(Commands commands, Composite startComposite, out
771778 public ShortGuid GetPointedEntityID ( )
772779 {
773780 ShortGuid id = ShortGuid . Invalid ;
774- for ( int i = path . Count - 1 ; i >= 0 ; i -- )
781+ for ( int i = path . Length - 1 ; i >= 0 ; i -- )
775782 {
776783 if ( path [ i ] == ShortGuid . Invalid ) continue ;
777784 id = path [ i ] ;
@@ -789,15 +796,16 @@ public bool IsPathValid(Commands commands, Composite composite)
789796 /* Generate the checksum used identify the path */
790797 public ShortGuid GeneratePathHash ( )
791798 {
792- if ( path . Count == 0 ) return ShortGuid . Invalid ;
793- if ( path [ path . Count - 1 ] != ShortGuid . Invalid ) path . Add ( ShortGuid . Invalid ) ;
799+ if ( path . Length == 0 ) return ShortGuid . Invalid ;
800+ EnsureFinalIsEmpty ( ) ;
794801
802+ //TODO: invert loop rather than array
795803 path . Reverse ( ) ;
796804 ShortGuid checksumGenerated = path [ 0 ] ;
797- for ( int i = 0 ; i < path . Count ; i ++ )
805+ for ( int i = 0 ; i < path . Length ; i ++ )
798806 {
799807 checksumGenerated = checksumGenerated . Combine ( path [ i + 1 ] ) ;
800- if ( i == path . Count - 2 ) break ;
808+ if ( i == path . Length - 2 ) break ;
801809 }
802810 path . Reverse ( ) ;
803811
@@ -815,19 +823,78 @@ public ShortGuid GenerateZoneID()
815823 {
816824 return new ShortGuid ( 0 + GenerateCompositeInstanceID ( ) . ToUInt32 ( ) + GetPointedEntityID ( ) . ToUInt32 ( ) + 1 ) ;
817825 }
818-
819- /* Updates this path to have the path to another entity prepended to it */
820- public void PrependPath ( EntityPath otherPath )
826+
827+ /* Add the next entity GUID along the path */
828+ public void AddNextStep ( Entity entity )
821829 {
822- int length = otherPath . path [ otherPath . path . Count - 1 ] == ShortGuid . Invalid ? otherPath . path . Count - 2 : otherPath . path . Count - 1 ;
823- for ( int i = 0 ; i < length ; i ++ )
824- path . Insert ( i , otherPath . path [ i ] ) ;
830+ AddNextStep ( entity . shortGUID ) ;
831+ }
832+ public void AddNextStep ( ShortGuid guid )
833+ {
834+ if ( path . Length > 0 && path [ path . Length - 1 ] == ShortGuid . Invalid )
835+ {
836+ path [ path . Length - 1 ] = guid ;
837+ }
838+ else
839+ {
840+ Array . Resize ( ref path , path . Length + 1 ) ;
841+ path [ path . Length - 1 ] = guid ;
842+ }
843+ EnsureFinalIsEmpty ( ) ;
844+ }
845+
846+ /* Remove the last entity GUID along the path */
847+ public void GoBackOneStep ( )
848+ {
849+ if ( path . Length > 0 && path [ path . Length - 1 ] == ShortGuid . Invalid )
850+ {
851+ if ( path . Length > 1 )
852+ {
853+ path [ path . Length - 2 ] = ShortGuid . Invalid ;
854+ Array . Resize ( ref path , path . Length - 1 ) ;
855+ }
856+ }
857+ else if ( path . Length > 0 )
858+ {
859+ path [ path . Length - 1 ] = ShortGuid . Invalid ;
860+ }
861+ else
862+ {
863+ EnsureFinalIsEmpty ( ) ;
864+ }
825865 }
866+
867+ /* Updates this path to have the path to another entity prepended to it */
868+ //public void PrependPath(EntityPath otherPath)
869+ //{
870+ // int length = otherPath.path[otherPath.path.Count - 1] == ShortGuid.Invalid ? otherPath.path.Count - 2 : otherPath.path.Count - 1;
871+ // for (int i = 0; i < length; i++)
872+ // path.Insert(i, otherPath.path[i]);
873+ //}
826874 }
827875
828876 public static class PathUtils
829877 {
830878 /* Generate the instance ID used to identify the instanced composite we're executed in */
879+ public static ShortGuid GenerateCompositeInstanceID ( this ShortGuid [ ] path , bool hasInternalEntityID = true ) //Set this to false the final value in the path is not an entity ID within the composite
880+ {
881+ bool hasTrailingInvalid = ( path . Length > 0 && path [ path . Length - 1 ] == ShortGuid . Invalid ) ;
882+ ShortGuid [ ] values = new ShortGuid [ hasInternalEntityID ? ( hasTrailingInvalid ? path . Length - 1 : path . Length ) : ( hasTrailingInvalid ? path . Length : path . Length + 1 ) ] ;
883+ values [ values . Length - 1 ] = ShortGuid . InitialiserBase ;
884+ int x = 0 ;
885+ for ( int i = values . Length - 2 ; i >= 0 ; i -- )
886+ {
887+ values [ i ] = path [ x ] ;
888+ x ++ ;
889+ }
890+ ShortGuid instanceGenerated = values [ 0 ] ;
891+ for ( int i = 0 ; i < values . Length ; i ++ )
892+ {
893+ if ( i == values . Length - 1 ) break ;
894+ instanceGenerated = values [ i + 1 ] . Combine ( instanceGenerated ) ;
895+ }
896+ return instanceGenerated ;
897+ }
831898 public static ShortGuid GenerateCompositeInstanceID ( this List < ShortGuid > path , bool hasInternalEntityID = true ) //Set this to false the final value in the path is not an entity ID within the composite
832899 {
833900 bool hasTrailingInvalid = ( path . Count > 0 && path [ path . Count - 1 ] == ShortGuid . Invalid ) ;
@@ -861,7 +928,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
861928 {
862929 EntityPath e = new EntityPath ( ) ;
863930 List < string > vals = reader . Value . ToString ( ) . Split ( new [ ] { " -> " } , StringSplitOptions . None ) . ToList ( ) ;
864- for ( int i = 0 ; i < vals . Count ; i ++ ) e . path . Add ( new ShortGuid ( vals [ i ] ) ) ;
931+ e . path = new ShortGuid [ vals . Count ] ;
932+ for ( int i = 0 ; i < vals . Count ; i ++ ) e . path [ i ] = new ShortGuid ( vals [ i ] ) ;
865933 return e ;
866934 }
867935
0 commit comments