Skip to content

Commit 901fae4

Browse files
committed
changing shortguid to handle as uint32
1 parent c0f70d0 commit 901fae4

5 files changed

Lines changed: 83 additions & 71 deletions

File tree

CathodeLib/Scripts/CATHODE/Commands.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ override protected bool SaveInternal()
503503
//Validate entry points and composite count
504504
if (Entries.Count == 0) return false;
505505
if (_entryPoints == null) _entryPoints = new ShortGuid[3];
506-
if (_entryPoints[0].val == null && _entryPoints[1].val == null && _entryPoints[2].val == null && Entries.Count == 0) return false;
506+
if (_entryPoints[0].IsInvalid || _entryPoints[1].IsInvalid || _entryPoints[2].IsInvalid || Entries.Count == 0) return false;
507507

508508
#region FIX_POTENTIAL_ERRORS
509509
//If we have composites but the entry points are broken, correct them first!
@@ -676,7 +676,7 @@ override protected bool SaveInternal()
676676
//Write entry points
677677
for (int i = 0; i < 3; i++)
678678
{
679-
if (_entryPoints[i].val == null || GetComposite(_entryPoints[i]) == null)
679+
if (_entryPoints[i].IsInvalid || GetComposite(_entryPoints[i]) == null)
680680
writer.Write(new byte[] { 0x00, 0x00, 0x00, 0x00 });
681681
else
682682
Utilities.Write<ShortGuid>(writer, _entryPoints[i]);
@@ -717,7 +717,7 @@ override protected bool SaveInternal()
717717
stringStartRaw[3] = 0x80;
718718
writer.Write(stringStartRaw);
719719
string str = ((cString)parameters[i]).value.Replace("\u0092", "'");
720-
writer.Write(ShortGuidUtils.Generate(str).val);
720+
writer.Write(ShortGuidUtils.Generate(str).ToUInt32());
721721
for (int x = 0; x < str.Length; x++) writer.Write(str[x]);
722722
writer.Write((char)0x00);
723723
Utilities.Align(writer, 4);
@@ -807,7 +807,7 @@ override protected bool SaveInternal()
807807
scriptPointerOffsetInfo[x] = new OffsetPair(writer.BaseStream.Position, linkedEntities[i].Count);
808808
for (int p = 0; p < linkedEntities[i].Count; p++)
809809
{
810-
writer.Write(linkedEntities[i][p].shortGUID.val);
810+
writer.Write(linkedEntities[i][p].shortGUID.ToUInt32());
811811
writer.Write(offsetPairs[p].GlobalOffset / 4);
812812
writer.Write(offsetPairs[p].EntryCount);
813813
}
@@ -831,7 +831,7 @@ override protected bool SaveInternal()
831831
scriptPointerOffsetInfo[x] = new OffsetPair(writer.BaseStream.Position, offsetPairs.Count);
832832
for (int p = 0; p < parameterisedEntities[i].Count; p++)
833833
{
834-
writer.Write(parameterisedEntities[i][p].shortGUID.val);
834+
writer.Write(parameterisedEntities[i][p].shortGUID.ToUInt32());
835835
writer.Write(offsetPairs[p].GlobalOffset / 4);
836836
writer.Write(offsetPairs[p].EntryCount);
837837
}
@@ -849,7 +849,7 @@ override protected bool SaveInternal()
849849
scriptPointerOffsetInfo[x] = new OffsetPair(writer.BaseStream.Position, reshuffledAliases[i].Count);
850850
for (int p = 0; p < reshuffledAliases[i].Count; p++)
851851
{
852-
writer.Write(reshuffledAliases[i][p].shortGUID.val);
852+
writer.Write(reshuffledAliases[i][p].shortGUID.ToUInt32());
853853
writer.Write(offsetPairs[p].GlobalOffset / 4);
854854
writer.Write(offsetPairs[p].EntryCount);
855855
}
@@ -860,8 +860,8 @@ override protected bool SaveInternal()
860860
scriptPointerOffsetInfo[x] = new OffsetPair(writer.BaseStream.Position, reshuffledAliasPathHashes[i].Count);
861861
for (int p = 0; p < reshuffledAliasPathHashes[i].Count; p++)
862862
{
863-
writer.Write(reshuffledAliasPathHashes[i][p].shortGUID.val);
864-
writer.Write(reshuffledAliasPathHashes[i][p].alias.GeneratePathHash().val);
863+
writer.Write(reshuffledAliasPathHashes[i][p].shortGUID.ToUInt32());
864+
writer.Write(reshuffledAliasPathHashes[i][p].alias.GeneratePathHash().ToUInt32());
865865
}
866866
break;
867867
}
@@ -870,9 +870,9 @@ override protected bool SaveInternal()
870870
scriptPointerOffsetInfo[x] = new OffsetPair(writer.BaseStream.Position, Entries[i].variables.Count);
871871
for (int p = 0; p < Entries[i].variables.Count; p++)
872872
{
873-
writer.Write(Entries[i].variables[p].shortGUID.val);
874-
writer.Write(CommandsUtils.GetDataTypeGUID(Entries[i].variables[p].type).val);
875-
writer.Write(Entries[i].variables[p].name.val);
873+
writer.Write(Entries[i].variables[p].shortGUID.ToUInt32());
874+
writer.Write(CommandsUtils.GetDataTypeGUID(Entries[i].variables[p].type).ToUInt32());
875+
writer.Write(Entries[i].variables[p].name.ToUInt32());
876876
}
877877
break;
878878
}
@@ -888,11 +888,11 @@ override protected bool SaveInternal()
888888
scriptPointerOffsetInfo[x] = new OffsetPair(writer.BaseStream.Position, offsetPairs.Count);
889889
for (int p = 0; p < Entries[i].proxies.Count; p++)
890890
{
891-
writer.Write(Entries[i].proxies[p].shortGUID.val);
891+
writer.Write(Entries[i].proxies[p].shortGUID.ToUInt32());
892892
writer.Write(offsetPairs[p].GlobalOffset / 4);
893893
writer.Write(offsetPairs[p].EntryCount);
894-
writer.Write(Entries[i].proxies[p].shortGUID.val);
895-
writer.Write(Entries[i].proxies[p].function.val);
894+
writer.Write(Entries[i].proxies[p].shortGUID.ToUInt32());
895+
writer.Write(Entries[i].proxies[p].function.ToUInt32());
896896
}
897897
break;
898898
}
@@ -901,8 +901,8 @@ override protected bool SaveInternal()
901901
scriptPointerOffsetInfo[x] = new OffsetPair(writer.BaseStream.Position, Entries[i].functions.Count);
902902
for (int p = 0; p < Entries[i].functions.Count; p++)
903903
{
904-
writer.Write(Entries[i].functions[p].shortGUID.val);
905-
writer.Write(Entries[i].functions[p].function.val);
904+
writer.Write(Entries[i].functions[p].shortGUID.ToUInt32());
905+
writer.Write(Entries[i].functions[p].function.ToUInt32());
906906
}
907907
break;
908908
}
@@ -926,8 +926,8 @@ override protected bool SaveInternal()
926926
writer.Write(resourceReferences[i][p].rotation.Y);
927927
writer.Write(resourceReferences[i][p].rotation.Z);
928928
#endif
929-
writer.Write(resourceReferences[i][p].resourceID.val); //Sometimes this is the entity ID that uses the resource, other times it's the "resource" parameter ID link
930-
writer.Write(CommandsUtils.GetResourceEntryTypeGUID(resourceReferences[i][p].entryType).val);
929+
writer.Write(resourceReferences[i][p].resourceID.ToUInt32()); //Sometimes this is the entity ID that uses the resource, other times it's the "resource" parameter ID link
930+
writer.Write(CommandsUtils.GetResourceEntryTypeGUID(resourceReferences[i][p].entryType).ToUInt32());
931931
switch (resourceReferences[i][p].entryType)
932932
{
933933
case ResourceType.RENDERABLE_INSTANCE:
@@ -936,7 +936,7 @@ override protected bool SaveInternal()
936936
break;
937937
case ResourceType.COLLISION_MAPPING:
938938
writer.Write(resourceReferences[i][p].index);
939-
writer.Write(resourceReferences[i][p].collisionID.val);
939+
writer.Write(resourceReferences[i][p].collisionID.ToUInt32());
940940
break;
941941
case ResourceType.ANIMATED_MODEL:
942942
case ResourceType.DYNAMIC_PHYSICS_SYSTEM:
@@ -1055,7 +1055,7 @@ override protected bool SaveInternal()
10551055
Utilities.Write<int>(writer, internalOffsets);
10561056

10571057
globalOffsets.Add((int)writer.BaseStream.Position);
1058-
writer.Write(cageAnimationEntities[i][p].shortGUID.val);
1058+
writer.Write(cageAnimationEntities[i][p].shortGUID.ToUInt32());
10591059
writer.Write(headerOffset / 4);
10601060
writer.Write(cageAnimationEntities[i][p].connections.Count);
10611061
writer.Write(animationOffset / 4);
@@ -1094,13 +1094,13 @@ override protected bool SaveInternal()
10941094
int eventOffset = (int)writer.BaseStream.Position;
10951095
for (int pp = 0; pp < triggerSequenceEntities[i][p].events.Count; pp++)
10961096
{
1097-
writer.Write(triggerSequenceEntities[i][p].events[pp].start.val);
1098-
writer.Write(triggerSequenceEntities[i][p].events[pp].shortGUID.val);
1099-
writer.Write(triggerSequenceEntities[i][p].events[pp].end.val);
1097+
writer.Write(triggerSequenceEntities[i][p].events[pp].start.ToUInt32());
1098+
writer.Write(triggerSequenceEntities[i][p].events[pp].shortGUID.ToUInt32());
1099+
writer.Write(triggerSequenceEntities[i][p].events[pp].end.ToUInt32());
11001100
}
11011101

11021102
globalOffsets.Add((int)writer.BaseStream.Position);
1103-
writer.Write(triggerSequenceEntities[i][p].shortGUID.val);
1103+
writer.Write(triggerSequenceEntities[i][p].shortGUID.ToUInt32());
11041104
writer.Write(triggerOffset / 4);
11051105
writer.Write(triggerSequenceEntities[i][p].entities.Count);
11061106
writer.Write(eventOffset / 4);
@@ -1189,7 +1189,6 @@ public Composite GetComposite(string name)
11891189
}
11901190
public Composite GetComposite(ShortGuid id)
11911191
{
1192-
if (id.val == null) return null;
11931192
return Entries.FirstOrDefault(o => o != null && o.shortGUID == id);
11941193
}
11951194

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ public Entity(ShortGuid shortGUID, EntityVariant variant)
4545
/* Implements IComparable for searching */
4646
public int CompareTo(Entity other)
4747
{
48-
int TotalThis = shortGUID.val[0] + shortGUID.val[1] + shortGUID.val[2] + shortGUID.val[3];
49-
int TotalOther = other.shortGUID.val[0] + other.shortGUID.val[1] + other.shortGUID.val[2] + other.shortGUID.val[3];
50-
if (TotalThis > TotalOther) return 1;
51-
else if (TotalThis == TotalOther) return 0;
52-
return -1;
48+
if (other == null) return 1;
49+
50+
if (this.shortGUID.ToUInt32() > other.shortGUID.ToUInt32())
51+
return 1;
52+
else if (this.shortGUID.ToUInt32() < other.shortGUID.ToUInt32())
53+
return -1;
54+
55+
return 0;
5356
}
5457

5558
/* Get parameter by string name or ShortGuid */

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

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,60 +19,66 @@ public struct ShortGuid : IComparable<ShortGuid>
1919
public static readonly ShortGuid Invalid = new ShortGuid(0);
2020
public static readonly ShortGuid InitialiserBase = new ShortGuid("FE-5B-F0-4A");
2121

22-
public ShortGuid(float num)
22+
public bool IsInvalid => val == Invalid.val;
23+
24+
private UInt32 val;
25+
26+
public ShortGuid(BinaryReader reader)
2327
{
24-
val = BitConverter.GetBytes(num);
28+
val = reader.ReadUInt32();
2529
}
26-
public ShortGuid(int num)
30+
public ShortGuid(float num)
2731
{
28-
val = BitConverter.GetBytes(num);
32+
val = Convert.ToUInt32(num);
2933
}
30-
public ShortGuid(byte[] id)
34+
public ShortGuid(uint num)
3135
{
32-
val = id;
36+
val = num;
3337
}
34-
public ShortGuid(BinaryReader reader)
38+
39+
[Obsolete("For performance reasons, it is recommended to initialse ShortGuid as an unsigned integer.")]
40+
public ShortGuid(byte[] id)
3541
{
36-
val = reader.ReadBytes(4);
42+
val = BitConverter.ToUInt32(id, 0);
3743
}
44+
45+
[Obsolete("For performance reasons, it is recommended to initialse ShortGuid as an unsigned integer.")]
3846
public ShortGuid(string id)
3947
{
4048
System.String[] arr = id.Split('-');
4149
if (arr.Length != 4) throw new Exception("Tried to initialise ShortGuid without 4-byte ID string.");
4250
byte[] array = new byte[arr.Length];
4351
for (int i = 0; i < arr.Length; i++) array[i] = Convert.ToByte(arr[i], 16);
44-
val = array;
52+
val = BitConverter.ToUInt32(array, 0);
4553
}
4654

47-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
48-
public byte[] val;
49-
5055
public override bool Equals(object obj)
5156
{
5257
if (!(obj is ShortGuid)) return false;
53-
if (((ShortGuid)obj).val == null) return this.val == null;
54-
if (this.val == null) return ((ShortGuid)obj).val == null;
55-
return ((ShortGuid)obj).val.SequenceEqual(this.val);
58+
return ((ShortGuid)obj).val == this.val;
5659
}
5760
public static bool operator ==(ShortGuid x, ShortGuid y)
5861
{
5962
if (ReferenceEquals(x, null)) return ReferenceEquals(y, null);
60-
if (x.val == null) return y.val == null;
61-
if (y.val == null) return x.val == null;
62-
return x.val.SequenceEqual(y.val);
63+
return x.val == y.val;
6364
}
6465
public static bool operator !=(ShortGuid x, ShortGuid y)
6566
{
66-
return !x.val.SequenceEqual(y.val);
67+
return !(x.val == y.val);
6768
}
69+
70+
[Obsolete("For performance reasons, it is recommended to compare as ShortGuid or integer.")]
6871
public static bool operator ==(ShortGuid x, string y)
6972
{
7073
return x.ToByteString() == y;
7174
}
75+
76+
[Obsolete("For performance reasons, it is recommended to compare as ShortGuid or integer.")]
7277
public static bool operator !=(ShortGuid x, string y)
7378
{
7479
return x.ToByteString() != y;
7580
}
81+
7682
public static bool operator ==(ShortGuid x, uint y)
7783
{
7884
return x.ToUInt32() == y;
@@ -81,40 +87,43 @@ public override bool Equals(object obj)
8187
{
8288
return x.ToUInt32() != y;
8389
}
90+
8491
public override int GetHashCode()
8592
{
86-
if (val == null) return 0;
87-
return BitConverter.ToInt32(val, 0);
93+
return 1835847388 + val.GetHashCode();
8894
}
8995

9096
public int CompareTo(ShortGuid x)
9197
{
92-
if (x == null) return 0;
93-
if (x.val == null && val != null) return 0;
94-
if (x.val != null && val == null) return 0;
95-
if (x.val.Length != val.Length) return 0;
98+
if (x == null) return 1;
9699

97-
int comp = 0;
98-
for (int i = 0; i < x.val.Length; i++)
99-
{
100-
comp += x.val[i].CompareTo(val[i]);
101-
}
102-
comp /= x.val.Length;
100+
if (this.val > x.val)
101+
return 1;
102+
else if (this.val < x.val)
103+
return -1;
103104

104-
return comp;
105+
return 0;
105106
}
106107

107108
public override string ToString()
108109
{
109110
return ShortGuidUtils.FindString(this);
110111
}
112+
public uint ToUInt32()
113+
{
114+
return val;
115+
}
116+
117+
[Obsolete("For performance reasons, it is recommended to use ToUInt32.")]
111118
public string ToByteString()
112119
{
113-
return BitConverter.ToString(val);
120+
return BitConverter.ToString(BitConverter.GetBytes(val));
114121
}
115-
public uint ToUInt32()
122+
123+
[Obsolete("For performance reasons, it is recommended to use ToUInt32.")]
124+
public byte[] ToBytes()
116125
{
117-
return BitConverter.ToUInt32(val, 0);
126+
return BitConverter.GetBytes(val);
118127
}
119128
}
120129

@@ -129,9 +138,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
129138

130139
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
131140
{
132-
ShortGuid user = new ShortGuid();
133-
user.val = (byte[])reader.Value;
134-
return user;
141+
return new ShortGuid((byte[])reader.Value);
135142
}
136143

137144
public override bool CanConvert(Type objectType)

CathodeLib/Scripts/CATHODE/CommandsPAK/Helpers/CommandsUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static Entity ResolveHierarchy(Commands commands, Composite composite, Li
171171

172172
List<ShortGuid> hierarchyCopy = new List<ShortGuid>();
173173
for (int x = 0; x < hierarchy.Count; x++)
174-
hierarchyCopy.Add(new ShortGuid((byte[])hierarchy[x].val.Clone()));
174+
hierarchyCopy.Add(new ShortGuid(hierarchy[x].ToUInt32()));
175175

176176
Composite currentFlowgraphToSearch = composite;
177177
if (currentFlowgraphToSearch == null || currentFlowgraphToSearch.GetEntityByID(hierarchyCopy[0]) == null)

CathodeLib/Scripts/CATHODE/CommandsPAK/Helpers/ShortGuidUtils.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ public static ShortGuid Combine(this ShortGuid guid1, ShortGuid guid2)
7777
{
7878
if (guid1.ToUInt32() != 0)
7979
{
80-
ulong hash = BitConverter.ToUInt64(new byte[8] { guid1.val[0], guid1.val[1], guid1.val[2], guid1.val[3], guid2.val[0], guid2.val[1], guid2.val[2], guid2.val[3] }, 0);
80+
byte[] guid1_b = guid1.ToBytes();
81+
byte[] guid2_b = guid2.ToBytes();
82+
83+
ulong hash = BitConverter.ToUInt64(new byte[8] { guid1_b[0], guid1_b[1], guid1_b[2], guid1_b[3], guid2_b[0], guid2_b[1], guid2_b[2], guid2_b[3] }, 0);
8184
hash = ~hash + hash * 262144; hash = (hash ^ (hash >> 31)) * 21; hash = (hash ^ (hash >> 11)) * 65;
82-
return new ShortGuid(BitConverter.GetBytes((uint)(hash >> 22) ^ (uint)hash));
85+
return new ShortGuid(((uint)(hash >> 22) ^ (uint)hash));
8386
}
8487
return guid2;
8588
}

0 commit comments

Comments
 (0)