diff --git a/CathodeLib/Scripts/CATHODE/Commands.cs b/CathodeLib/Scripts/CATHODE/Commands.cs index f014f3a..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); } } @@ -643,14 +639,15 @@ override public bool Save() if (x == 0) Utilities.Write(writer, _composites[i].shortGUID); } - //Write function entity counts (TODO: these values still aren't correct!) + //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); - writer.Write(_composites[i].functions.Count); } //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 5c6cf6f..f72a430 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; @@ -72,9 +72,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; } @@ -86,5 +86,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 7eec4b2..53093a6 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs @@ -190,6 +190,11 @@ 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 @@ -259,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 diff --git a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs index 3f80fa3..222a440 100644 --- a/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs +++ b/CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs @@ -70,43 +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: - 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; + 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; }