From 8b16928c524be7d056007384e1b873766aba8f51 Mon Sep 17 00:00:00 2001 From: MattFiler Date: Tue, 3 Jan 2023 19:19:41 +0000 Subject: [PATCH 1/6] store unknowns --- CathodeLib/Scripts/CATHODE/Commands.cs | 7 ++++--- .../CATHODE/CommandsPAK/Components/Composite.cs | 9 +++++++++ .../Scripts/CATHODE/CommandsPAK/Components/Entity.cs | 12 ++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CathodeLib/Scripts/CATHODE/Commands.cs b/CathodeLib/Scripts/CATHODE/Commands.cs index f014f3a..4446b8c 100644 --- a/CathodeLib/Scripts/CATHODE/Commands.cs +++ b/CathodeLib/Scripts/CATHODE/Commands.cs @@ -644,8 +644,8 @@ override public bool Save() } //Write function entity counts (TODO: these values still aren't correct!) - writer.Write(_composites[i].functions.Count); - writer.Write(_composites[i].functions.Count); + writer.Write(_composites[i].unk1 == -1 ? _composites[i].functions.Count : _composites[i].unk1); + writer.Write(_composites[i].unk2 == -1 ? _composites[i].functions.Count : _composites[i].unk2); } //Write out parameter offsets @@ -766,7 +766,8 @@ override protected bool Load() offsetPairs[x] = Utilities.Consume(reader); if (x == 0) composite.shortGUID = new ShortGuid(reader); } - reader.BaseStream.Position += 8; + composite.unk1 = reader.ReadInt32(); + composite.unk2 = reader.ReadInt32(); //Read script ID and string name reader.BaseStream.Position = (scriptStartOffset * 4) + 4; diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs index 5c6cf6f..82859ae 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs @@ -13,6 +13,12 @@ namespace CATHODE.Scripting [Serializable] public class Composite { + //TEMP + public override string ToString() + { + return "[" + unk1 + ", " + unk2 + "]"; + } + public Composite() { } public Composite(string name) { @@ -29,6 +35,9 @@ public Composite(string name) public List overrides = new List(); //Overrides of parameters in child composites public List proxies = new List(); //Instances of entities from other composites + public int unk1 = -1; + public int unk2 = -1; + /* If an entity exists in the composite, return it */ public Entity GetEntityByID(ShortGuid id) { diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs index 7eec4b2..e00441b 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs @@ -131,6 +131,12 @@ namespace CATHODE.Scripting [Serializable] public class VariableEntity : Entity { + //TEMP + public override string ToString() + { + return parameter.ToString(); + } + public VariableEntity(bool addDefaultParam = false) : base(EntityVariant.DATATYPE) { if (addDefaultParam) AddDefaultParam(); } public VariableEntity(ShortGuid shortGUID, bool addDefaultParam = false) : base(shortGUID, EntityVariant.DATATYPE) { if (addDefaultParam) AddDefaultParam(); } @@ -194,6 +200,12 @@ private void AddDefaultParam() [Serializable] public class FunctionEntity : Entity { + //TEMP + public override string ToString() + { + return function.ToString(); + } + public FunctionEntity() : base(EntityVariant.FUNCTION) { } public FunctionEntity(ShortGuid shortGUID) : base(shortGUID, EntityVariant.FUNCTION) { } From 34be173b6edb01c7b3828bc9334ba1362f7cd070 Mon Sep 17 00:00:00 2001 From: MattFiler Date: Tue, 3 Jan 2023 21:32:06 +0000 Subject: [PATCH 2/6] misc --- CathodeLib/Scripts/CATHODE/Commands.cs | 2 +- .../CommandsPAK/Components/Composite.cs | 17 +++++++------- .../CATHODE/CommandsPAK/Components/Entity.cs | 22 +++++++++---------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/CathodeLib/Scripts/CATHODE/Commands.cs b/CathodeLib/Scripts/CATHODE/Commands.cs index 4446b8c..869327e 100644 --- a/CathodeLib/Scripts/CATHODE/Commands.cs +++ b/CathodeLib/Scripts/CATHODE/Commands.cs @@ -1117,7 +1117,7 @@ override protected bool Load() public Composite AddComposite(string name, bool isRoot = false) { Composite comp = new Composite(name); - _composites.Add(comp); + _composites.Insert(0, comp); if (isRoot) SetRootComposite(comp); return comp; } diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs index 82859ae..e34f123 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs @@ -1,4 +1,4 @@ -using CATHODE.Scripting.Internal; +using CATHODE.Scripting.Internal; using System; using System.Collections.Generic; using System.Linq; @@ -13,12 +13,6 @@ namespace CATHODE.Scripting [Serializable] public class Composite { - //TEMP - public override string ToString() - { - return "[" + unk1 + ", " + unk2 + "]"; - } - public Composite() { } public Composite(string name) { @@ -81,9 +75,9 @@ public FunctionEntity AddFunction(string function, bool autopopulateParameters = functions.Add(func); return func; } - public FunctionEntity AddFunction(Composite function, bool autopopulateParameters = false) + public FunctionEntity AddFunction(Composite composite, bool autopopulateParameters = false) { - FunctionEntity func = new FunctionEntity(function.shortGUID, autopopulateParameters); + FunctionEntity func = new FunctionEntity(composite.shortGUID, autopopulateParameters); functions.Add(func); return func; } @@ -95,5 +89,10 @@ public VariableEntity AddVariable(string parameter, DataType type, bool addDefau variables.Add(vari); return vari; } + + public override string ToString() + { + return name; + } } } diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs index e00441b..53093a6 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs @@ -131,12 +131,6 @@ namespace CATHODE.Scripting [Serializable] public class VariableEntity : Entity { - //TEMP - public override string ToString() - { - return parameter.ToString(); - } - public VariableEntity(bool addDefaultParam = false) : base(EntityVariant.DATATYPE) { if (addDefaultParam) AddDefaultParam(); } public VariableEntity(ShortGuid shortGUID, bool addDefaultParam = false) : base(shortGUID, EntityVariant.DATATYPE) { if (addDefaultParam) AddDefaultParam(); } @@ -196,16 +190,15 @@ private void AddDefaultParam() public ShortGuid parameter; //Translates to string via ShortGuidUtils.FindString public DataType type = DataType.NONE; + + public override string ToString() + { + return parameter.ToString(); + } } [Serializable] public class FunctionEntity : Entity { - //TEMP - public override string ToString() - { - return function.ToString(); - } - public FunctionEntity() : base(EntityVariant.FUNCTION) { } public FunctionEntity(ShortGuid shortGUID) : base(shortGUID, EntityVariant.FUNCTION) { } @@ -271,6 +264,11 @@ public ResourceReference GetResource(ResourceType type) { return resources.FirstOrDefault(o => o.entryType == type); } + + public override string ToString() + { + return function.ToString(); + } } [Serializable] public class ProxyEntity : Entity From b3af29b47343245b8ec84e06c63ad95be4db5266 Mon Sep 17 00:00:00 2001 From: MattFiler Date: Tue, 3 Jan 2023 22:04:59 +0000 Subject: [PATCH 3/6] remove unk vals --- CathodeLib/Scripts/CATHODE/Commands.cs | 11 +++++------ .../CATHODE/CommandsPAK/Components/Composite.cs | 3 --- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/CathodeLib/Scripts/CATHODE/Commands.cs b/CathodeLib/Scripts/CATHODE/Commands.cs index 869327e..1be90f1 100644 --- a/CathodeLib/Scripts/CATHODE/Commands.cs +++ b/CathodeLib/Scripts/CATHODE/Commands.cs @@ -643,9 +643,9 @@ override public bool Save() if (x == 0) Utilities.Write(writer, _composites[i].shortGUID); } - //Write function entity counts (TODO: these values still aren't correct!) - writer.Write(_composites[i].unk1 == -1 ? _composites[i].functions.Count : _composites[i].unk1); - writer.Write(_composites[i].unk2 == -1 ? _composites[i].functions.Count : _composites[i].unk2); + //Write function count (TODO: sometimes this count excludes some entities in the vanilla paks - why?) + writer.Write(_composites[i].functions.FindAll(o => CommandsUtils.FunctionTypeExists(o.function)).Count); + writer.Write(_composites[i].functions.Count); } //Write out parameter offsets @@ -766,8 +766,7 @@ override protected bool Load() offsetPairs[x] = Utilities.Consume(reader); if (x == 0) composite.shortGUID = new ShortGuid(reader); } - composite.unk1 = reader.ReadInt32(); - composite.unk2 = reader.ReadInt32(); + reader.BaseStream.Position += 8; //Read script ID and string name reader.BaseStream.Position = (scriptStartOffset * 4) + 4; @@ -1117,7 +1116,7 @@ override protected bool Load() public Composite AddComposite(string name, bool isRoot = false) { Composite comp = new Composite(name); - _composites.Insert(0, comp); + _composites.Add(comp); if (isRoot) SetRootComposite(comp); return comp; } diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs index e34f123..f72a430 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs @@ -29,9 +29,6 @@ public Composite(string name) public List overrides = new List(); //Overrides of parameters in child composites public List proxies = new List(); //Instances of entities from other composites - public int unk1 = -1; - public int unk2 = -1; - /* If an entity exists in the composite, return it */ public Entity GetEntityByID(ShortGuid id) { From 06f805ccc71040979b9c141a9e86d179fa76c2b3 Mon Sep 17 00:00:00 2001 From: MattFiler Date: Tue, 3 Jan 2023 22:28:03 +0000 Subject: [PATCH 4/6] string hashcode --- .../Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs index 3f80fa3..b6e76c8 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs @@ -76,10 +76,7 @@ public override int GetHashCode() case DataType.INTEGER: return ((cInteger)this).value; case DataType.STRING: - cString x_s = (cString)this; - string num = ""; - for (int i = 0; i < x_s.value.Length; i++) num += ((int)x_s.value[i]).ToString(); - return Convert.ToInt32(num); + return ((cString)this).value.GetHashCode(); case DataType.BOOL: return ((cBool)this).value ? 1 : 0; case DataType.FLOAT: From beebe649b670474afa123233b3769673979f653f Mon Sep 17 00:00:00 2001 From: MattFiler Date: Tue, 3 Jan 2023 23:08:35 +0000 Subject: [PATCH 5/6] sorting improvements --- CathodeLib/Scripts/CATHODE/Commands.cs | 29 ++++++++-------- .../CommandsPAK/Components/Composite.cs | 8 ++--- .../CommandsPAK/Components/ParameterData.cs | 33 ++++++------------- 3 files changed, 27 insertions(+), 43 deletions(-) diff --git a/CathodeLib/Scripts/CATHODE/Commands.cs b/CathodeLib/Scripts/CATHODE/Commands.cs index 1be90f1..1834b78 100644 --- a/CathodeLib/Scripts/CATHODE/Commands.cs +++ b/CathodeLib/Scripts/CATHODE/Commands.cs @@ -88,6 +88,7 @@ override public bool Save() writer.Write(0); //Fix (& verify) entity-attached resource info + _composites = _composites.OrderBy(o => o.shortGUID.ToUInt32()).ToList(); for (int i = 0; i < _composites.Count; i++) { List animatedModels = new List(); @@ -184,10 +185,10 @@ override public bool Save() parameters = PruneParameterList(parameters); //Write out parameters & track offsets - int[] parameterOffsets = new int[parameters.Count]; + Dictionary parameterOffsets = new Dictionary(); for (int i = 0; i < parameters.Count; i++) { - parameterOffsets[i] = (int)writer.BaseStream.Position / 4; + parameterOffsets.Add(parameters[i], (int)writer.BaseStream.Position / 4); Utilities.Write(writer, CommandsUtils.GetDataTypeGUID(parameters[i].dataType)); switch (parameters[i].dataType) { @@ -343,21 +344,16 @@ override public bool Save() foreach (Entity entityWithParam in entitiesWithParams) { offsetPairs.Add(new OffsetPair(writer.BaseStream.Position, entityWithParam.parameters.Count)); + + Dictionary paramsWithOffsets = new Dictionary(); for (int y = 0; y < entityWithParam.parameters.Count; y++) + paramsWithOffsets.Add(entityWithParam.parameters[y].shortGUID, parameterOffsets[entityWithParam.parameters[y].content]); + paramsWithOffsets = paramsWithOffsets.OrderBy(o => o.Value).ToDictionary(o => o.Key, o => o.Value); + + foreach (KeyValuePair entry in paramsWithOffsets) { - Utilities.Write(writer, entityWithParam.parameters[y].shortGUID); - //TODO: this is super slow! Find a better way to lookup parameter content offsets (precalculate a nicer structure) - int paramOffset = -1; - for (int z = 0; z < parameters.Count; z++) - { - if (parameters[z] == entityWithParam.parameters[y].content) - { - paramOffset = parameterOffsets[z]; - break; - } - } - if (paramOffset == -1) throw new Exception("Error writing parameter offset. Could not find parameter content!"); - writer.Write(paramOffset); + Utilities.Write(writer, entry.Key); + writer.Write(entry.Value); } } @@ -650,7 +646,8 @@ override public bool Save() //Write out parameter offsets int parameterOffsetPos = (int)writer.BaseStream.Position; - Utilities.Write(writer, parameterOffsets); + foreach (KeyValuePair offset in parameterOffsets) + writer.Write(offset.Value); //Write out composite offsets int compositeOffsetPos = (int)writer.BaseStream.Position; diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs index f72a430..d548010 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs @@ -53,10 +53,10 @@ public List GetEntities() /* Sort all entity arrays */ public void SortEntities() { - variables.OrderBy(o => o.shortGUID.ToUInt32()); - functions.OrderBy(o => o.shortGUID.ToUInt32()); - overrides.OrderBy(o => o.shortGUID.ToUInt32()); - proxies.OrderBy(o => o.shortGUID.ToUInt32()); + variables.OrderBy(o => o.parameters.Count); + functions.OrderBy(o => o.parameters.Count); + overrides.OrderBy(o => o.parameters.Count); + proxies.OrderBy(o => o.parameters.Count); } /* Add a new function entity */ diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs index b6e76c8..222a440 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs @@ -70,40 +70,27 @@ public override int GetHashCode() { case DataType.TRANSFORM: cTransform x_t = (cTransform)this; - return Convert.ToInt32( - x_t.rotation.x.ToString() + x_t.rotation.y.ToString() + x_t.rotation.z.ToString() + - x_t.position.x.ToString() + x_t.position.y.ToString() + x_t.position.z.ToString()); + return x_t.position.GetHashCode() + x_t.rotation.GetHashCode(); case DataType.INTEGER: - return ((cInteger)this).value; + return ((cInteger)this).value.GetHashCode(); case DataType.STRING: return ((cString)this).value.GetHashCode(); case DataType.BOOL: - return ((cBool)this).value ? 1 : 0; + return ((cBool)this).value.GetHashCode(); case DataType.FLOAT: - return Convert.ToInt32(((cFloat)this).value.ToString().Replace(".", "")); + return ((cFloat)this).value.GetHashCode(); case DataType.RESOURCE: - string x_g_s = ((cString)this).value.ToString(); - string num2 = ""; - for (int i = 0; i < x_g_s.Length; i++) num2 += ((int)x_g_s[i]).ToString(); - return Convert.ToInt32(num2); + return ((cResource)this).resourceID.GetHashCode(); case DataType.VECTOR: - cVector3 x_v = (cVector3)this; - return Convert.ToInt32(x_v.value.x.ToString() + x_v.value.y.ToString() + x_v.value.z.ToString()); + return ((cVector3)this).value.GetHashCode(); case DataType.ENUM: cEnum x_e = (cEnum)this; - string x_e_s = x_e.enumID.ToByteString(); - string num3 = ""; - for (int i = 0; i < x_e_s.Length; i++) num3 += ((int)x_e_s[i]).ToString(); - return Convert.ToInt32(num3 + x_e.enumIndex.ToString()); + return x_e.enumID.ToByteString().GetHashCode() + x_e.enumIndex; case DataType.SPLINE: cSpline x_sd = (cSpline)this; - string x_sd_s = ""; - for (int i = 0; i < x_sd.splinePoints.Count; i++) x_sd_s += x_sd.splinePoints[i].position.GetHashCode().ToString(); - ShortGuid x_sd_g = ShortGuidUtils.Generate(x_sd_s); - string x_sd_g_s = x_sd_g.ToByteString(); - string num4 = ""; - for (int i = 0; i < x_sd_g_s.Length; i++) num4 += ((int)x_sd_g_s[i]).ToString(); - return Convert.ToInt32(num4); + int x_sd_i = 0; + for (int i = 0; i < x_sd.splinePoints.Count; i++) x_sd_i += x_sd.splinePoints[i].GetHashCode(); + return x_sd_i; default: return -1; } From 54a9fb5db5b4576cabfc245cb1f9e291cf1684c6 Mon Sep 17 00:00:00 2001 From: MattFiler Date: Tue, 3 Jan 2023 23:10:18 +0000 Subject: [PATCH 6/6] revert to old composite entity sorting --- .../Scripts/CATHODE/CommandsPAK/Components/Composite.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs index d548010..f72a430 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs @@ -53,10 +53,10 @@ public List GetEntities() /* Sort all entity arrays */ public void SortEntities() { - variables.OrderBy(o => o.parameters.Count); - functions.OrderBy(o => o.parameters.Count); - overrides.OrderBy(o => o.parameters.Count); - proxies.OrderBy(o => o.parameters.Count); + variables.OrderBy(o => o.shortGUID.ToUInt32()); + functions.OrderBy(o => o.shortGUID.ToUInt32()); + overrides.OrderBy(o => o.shortGUID.ToUInt32()); + proxies.OrderBy(o => o.shortGUID.ToUInt32()); } /* Add a new function entity */