Skip to content

Commit ffa29e6

Browse files
committed
handle paths as fixed size arrays
1 parent dee3954 commit ffa29e6

4 files changed

Lines changed: 114 additions & 83 deletions

File tree

CathodeLib/Scripts/CATHODE/Commands.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ override protected bool LoadInternal()
189189
reader_parallel.BaseStream.Position = (offsetPairs[x].GlobalOffset * 4) + (y * 12);
190190
AliasEntity overrider = new AliasEntity(new ShortGuid(reader_parallel));
191191
int NumberOfParams = JumpToOffset(reader_parallel);
192-
overrider.alias.path.AddRange(Utilities.ConsumeArray<ShortGuid>(reader_parallel, NumberOfParams));
192+
overrider.alias.path = Utilities.ConsumeArray<ShortGuid>(reader_parallel, NumberOfParams);
193193
composite.aliases.Add(overrider);
194194
break;
195195
}
@@ -214,7 +214,7 @@ override protected bool LoadInternal()
214214
ProxyEntity thisProxy = new ProxyEntity(new ShortGuid(reader_parallel));
215215
int resetPos = (int)reader_parallel.BaseStream.Position + 8; //TODO: This is a HACK - I need to rework JumpToOffset to make a temp stream
216216
int NumberOfParams = JumpToOffset(reader_parallel);
217-
thisProxy.proxy.path.AddRange(Utilities.ConsumeArray<ShortGuid>(reader_parallel, NumberOfParams)); //Last is always 0x00, 0x00, 0x00, 0x00
217+
thisProxy.proxy.path = Utilities.ConsumeArray<ShortGuid>(reader_parallel, NumberOfParams); //Last is always 0x00, 0x00, 0x00, 0x00
218218
reader_parallel.BaseStream.Position = resetPos;
219219
ShortGuid idCheck = new ShortGuid(reader_parallel);
220220
if (idCheck != thisProxy.shortGUID) throw new Exception("Proxy ID mismatch!");
@@ -315,7 +315,7 @@ override protected bool LoadInternal()
315315
header.parameterSubID = new ShortGuid(reader_parallel);
316316

317317
int hierarchyCount = JumpToOffset(reader_parallel);
318-
header.connectedEntity.path = Utilities.ConsumeArray<ShortGuid>(reader_parallel, hierarchyCount).ToList<ShortGuid>();
318+
header.connectedEntity.path = Utilities.ConsumeArray<ShortGuid>(reader_parallel, hierarchyCount);
319319
animEntity.connections.Add(header);
320320
}
321321

@@ -397,7 +397,7 @@ override protected bool LoadInternal()
397397
TriggerSequence.Entity thisTrigger = new TriggerSequence.Entity();
398398
thisTrigger.timing = reader_parallel.ReadSingle();
399399
reader_parallel.BaseStream.Position = hierarchyOffset;
400-
thisTrigger.connectedEntity.path = Utilities.ConsumeArray<ShortGuid>(reader_parallel, hierarchyCount).ToList<ShortGuid>();
400+
thisTrigger.connectedEntity.path = Utilities.ConsumeArray<ShortGuid>(reader_parallel, hierarchyCount);
401401
trigEntity.entities.Add(thisTrigger);
402402
}
403403

@@ -849,7 +849,7 @@ override protected bool SaveInternal()
849849
List<OffsetPair> offsetPairs = new List<OffsetPair>(reshuffledAliases[i].Count);
850850
for (int p = 0; p < reshuffledAliases[i].Count; p++)
851851
{
852-
offsetPairs.Add(new OffsetPair(writer.BaseStream.Position, reshuffledAliases[i][p].alias.path.Count));
852+
offsetPairs.Add(new OffsetPair(writer.BaseStream.Position, reshuffledAliases[i][p].alias.path.Length));
853853
Utilities.Write<ShortGuid>(writer, reshuffledAliases[i][p].alias.path);
854854
}
855855

@@ -888,7 +888,7 @@ override protected bool SaveInternal()
888888
List<OffsetPair> offsetPairs = new List<OffsetPair>();
889889
for (int p = 0; p < Entries[i].proxies.Count; p++)
890890
{
891-
offsetPairs.Add(new OffsetPair(writer.BaseStream.Position, Entries[i].proxies[p].proxy.path.Count));
891+
offsetPairs.Add(new OffsetPair(writer.BaseStream.Position, Entries[i].proxies[p].proxy.path.Length));
892892
Utilities.Write<ShortGuid>(writer, Entries[i].proxies[p].proxy.path);
893893
}
894894

@@ -983,7 +983,7 @@ override protected bool SaveInternal()
983983
Utilities.Write(writer, CommandsUtils.GetDataTypeGUID(header.parameterDataType));
984984
Utilities.Write(writer, header.parameterSubID);
985985
writer.Write(hierarchyOffsets[pp] / 4);
986-
writer.Write(header.connectedEntity.path.Count);
986+
writer.Write(header.connectedEntity.path.Length);
987987
}
988988

989989
List<int> internalOffsets = new List<int>(cageAnimationEntities[i][p].animations.Count);
@@ -1094,7 +1094,7 @@ override protected bool SaveInternal()
10941094
for (int pp = 0; pp < triggerSequenceEntities[i][p].entities.Count; pp++)
10951095
{
10961096
writer.Write(hierarchyOffsets[pp] / 4);
1097-
writer.Write(triggerSequenceEntities[i][p].entities[pp].connectedEntity.path.Count);
1097+
writer.Write(triggerSequenceEntities[i][p].entities[pp].connectedEntity.path.Length);
10981098
writer.Write(triggerSequenceEntities[i][p].entities[pp].timing);
10991099
}
11001100

CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public VariableEntity AddVariable(string parameter, DataType type, bool addDefau
105105
}
106106

107107
/* Add a new proxy entity */
108-
public ProxyEntity AddProxy(Commands commands, List<ShortGuid> hierarchy, bool addDefaultParam = false)
108+
public ProxyEntity AddProxy(Commands commands, ShortGuid[] hierarchy, bool addDefaultParam = false)
109109
{
110110
CommandsUtils.ResolveHierarchy(commands, this, hierarchy, out Composite targetComposite, out string str);
111-
Entity ent = targetComposite.GetEntityByID(hierarchy[hierarchy.Count - 2]);
111+
Entity ent = targetComposite.GetEntityByID(hierarchy[hierarchy.Length - 2]);
112112
if (ent.variant != EntityVariant.FUNCTION) return null;
113113

114114
ProxyEntity proxy = new ProxyEntity(hierarchy, ((FunctionEntity)ent).function, addDefaultParam);
@@ -117,7 +117,7 @@ public ProxyEntity AddProxy(Commands commands, List<ShortGuid> hierarchy, bool a
117117
}
118118

119119
/* Add a new alias entity */
120-
public AliasEntity AddAlias(List<ShortGuid> hierarchy)
120+
public AliasEntity AddAlias(ShortGuid[] hierarchy)
121121
{
122122
AliasEntity alias = new AliasEntity(hierarchy);
123123
aliases.Add(alias);

CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs

Lines changed: 98 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)