Skip to content

Commit f93d98a

Browse files
committed
speeding up some bits with id generation
1 parent 26b8fe2 commit f93d98a

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ public EntityPath(List<ShortGuid> _path)
682682
{
683683
path = _path;
684684

685+
//TODO: this whole system of modifying the path is dumb, should really come up with a better solution
685686
if (path.Count == 0 || path[path.Count - 1] != ShortGuid.Invalid)
686687
path.Add(ShortGuid.Invalid);
687688
}
@@ -770,15 +771,13 @@ public Entity GetPointedEntity(Commands commands, Composite startComposite, out
770771
/* Get the ID of the entity that this path points to */
771772
public ShortGuid GetPointedEntityID()
772773
{
773-
path.Reverse();
774774
ShortGuid id = ShortGuid.Invalid;
775-
for (int i = 0; i < path.Count; i++)
775+
for (int i = path.Count - 1; i >= 0; i--)
776776
{
777777
if (path[i] == ShortGuid.Invalid) continue;
778778
id = path[i];
779779
break;
780780
}
781-
path.Reverse();
782781
return id;
783782
}
784783

@@ -807,31 +806,15 @@ public ShortGuid GeneratePathHash()
807806
}
808807

809808
/* Generate the instance ID used to identify the instanced composite we're executed in */
810-
public ShortGuid GenerateInstance()
809+
public ShortGuid GenerateCompositeInstanceID(bool hasInternalEntityID = true) //Set this to false the final value in the path is not an entity ID within the composite
811810
{
812-
//TODO: This hijacks the usual use for this class, need to tidy it up
813-
ShortGuid entityID = GetPointedEntityID();
814-
path.Insert(0, ShortGuid.InitialiserBase);
815-
path.Remove(entityID);
816-
path.Reverse();
817-
ShortGuid instanceGenerated = path[0];
818-
for (int i = 0; i < path.Count; i++)
819-
{
820-
if (i == path.Count - 1) break;
821-
instanceGenerated = path[i + 1].Combine(instanceGenerated);
822-
}
823-
path.Reverse();
824-
path.RemoveAt(0);
825-
path.RemoveAll(o => o == ShortGuid.Invalid);
826-
path.Add(entityID);
827-
path.Add(ShortGuid.Invalid);
828-
return instanceGenerated;
811+
return path.GenerateCompositeInstanceID(hasInternalEntityID);
829812
}
830813

831814
/* Generate a zone ID (use this when the EntityHandle points to a Zone entity) */
832815
public ShortGuid GenerateZoneID()
833816
{
834-
return new ShortGuid(0 + GenerateInstance().ToUInt32() + GetPointedEntityID().ToUInt32() + 1);
817+
return new ShortGuid(0 + GenerateCompositeInstanceID().ToUInt32() + GetPointedEntityID().ToUInt32() + 1);
835818
}
836819

837820
/* Updates this path to have the path to another entity prepended to it */
@@ -843,6 +826,30 @@ public void PrependPath(EntityPath otherPath)
843826
}
844827
}
845828

829+
public static class PathUtils
830+
{
831+
/* Generate the instance ID used to identify the instanced composite we're executed in */
832+
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
833+
{
834+
bool hasTrailingInvalid = (path.Count > 0 && path[path.Count - 1] == ShortGuid.Invalid);
835+
ShortGuid[] values = new ShortGuid[hasInternalEntityID ? (hasTrailingInvalid ? path.Count - 1 : path.Count) : (hasTrailingInvalid ? path.Count : path.Count + 1)];
836+
values[values.Length - 1] = ShortGuid.InitialiserBase;
837+
int x = 0;
838+
for (int i = values.Length - 2; i >= 0; i--)
839+
{
840+
values[i] = path[x];
841+
x++;
842+
}
843+
ShortGuid instanceGenerated = values[0];
844+
for (int i = 0; i < values.Length; i++)
845+
{
846+
if (i == values.Length - 1) break;
847+
instanceGenerated = values[i + 1].Combine(instanceGenerated);
848+
}
849+
return instanceGenerated;
850+
}
851+
}
852+
846853
#if DEBUG
847854
public class EntityPathConverter : JsonConverter
848855
{

CathodeLib/Scripts/Utilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public EntityHandle() { }
474474
public EntityHandle(EntityPath hierarchy)
475475
{
476476
entity_id = hierarchy.GetPointedEntityID();
477-
composite_instance_id = hierarchy.GenerateInstance();
477+
composite_instance_id = hierarchy.GenerateCompositeInstanceID();
478478
}
479479

480480
public override bool Equals(object obj)

0 commit comments

Comments
 (0)