From 48f50cf7aec809481e127560cf918ea6a4506047 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Wed, 13 Nov 2024 15:17:23 +0100 Subject: [PATCH] Animations changes rebase --- Libraries/SAModel/Animation.cs | 1274 +++++++++++++++++--------------- 1 file changed, 662 insertions(+), 612 deletions(-) diff --git a/Libraries/SAModel/Animation.cs b/Libraries/SAModel/Animation.cs index 7ed4b7cc..b9d24748 100644 --- a/Libraries/SAModel/Animation.cs +++ b/Libraries/SAModel/Animation.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Numerics; using System.Text; namespace SAModel @@ -9,8 +10,8 @@ namespace SAModel public class NJS_ACTION { public string Name { get; set; } - public NJS_OBJECT Model { get; private set; } - public NJS_MOTION Animation { get; private set; } + public NJS_OBJECT Model { get; } + public NJS_MOTION Animation { get; } public NJS_ACTION(byte[] file, int address, uint imageBase, ModelFormat format, Dictionary attaches) : this(file, address, imageBase, format, new Dictionary(), attaches) @@ -25,7 +26,7 @@ public NJS_ACTION(byte[] file, int address, uint imageBase, ModelFormat format, } else { - Name = "action_" + address.ToString("X8"); + Name = $"action_{address:X8}"; } if (address > file.Length - 4) @@ -36,7 +37,7 @@ public NJS_ACTION(byte[] file, int address, uint imageBase, ModelFormat format, } else { - int objaddr = (int)(ByteConverter.ToUInt32(file, address) - imageBase); + var objaddr = (int)(ByteConverter.ToUInt32(file, address) - imageBase); if (objaddr > file.Length - 4) { Model = new NJS_OBJECT(); @@ -62,17 +63,17 @@ public NJS_ACTION(byte[] file, int address, uint imageBase, ModelFormat format, public NJS_ACTION(NJS_OBJECT model, NJS_MOTION animation) { - Name = "action_" + animation.Name; + Name = $"action_{animation.Name}"; Model = model; Animation = animation; } public byte[] GetBytes(uint imageBase, bool DX, Dictionary labels, out uint address) { - List result = new List(); - result.AddRange(Model.GetBytes(imageBase, DX, labels, new List(), out uint modeladdr)); - uint tmp = (uint)result.Count; - result.AddRange(Animation.GetBytes(imageBase + tmp, labels, out uint head2)); + var result = new List(); + result.AddRange(Model.GetBytes(imageBase, DX, labels, new List(), out var modeladdr)); + var tmp = (uint)result.Count; + result.AddRange(Animation.GetBytes(imageBase + tmp, labels, out var head2)); address = (uint)result.Count; result.AddRange(ByteConverter.GetBytes(modeladdr + imageBase)); result.AddRange(ByteConverter.GetBytes(head2 + tmp + imageBase)); @@ -86,16 +87,16 @@ public byte[] GetBytes(uint imageBase, bool DX, out uint address) public byte[] GetBytes(uint imageBase, bool DX) { - return GetBytes(imageBase, DX, out uint address); + return GetBytes(imageBase, DX, out var address); } } public class NJS_MOTION { - public const ulong SAANIM = 0x4D494E414153u; - public const ulong FormatMask = 0xFFFFFFFFFFFFu; - public const ulong CurrentVersion = 2; - public const ulong SAANIMVer = SAANIM | (CurrentVersion << 56); + private const ulong SAANIM = 0x4D494E414153u; + private const ulong FormatMask = 0xFFFFFFFFFFFFu; + private const ulong CurrentVersion = 2; + private const ulong SAANIMVer = SAANIM | (CurrentVersion << 56); public int Frames { get; set; } public string Name { get; set; } @@ -107,129 +108,68 @@ public class NJS_MOTION public string ActionName { get; set; } public string ObjectName { get; set; } - public Dictionary Models = new Dictionary(); + public readonly Dictionary Models = new(); - static bool optimizeMotions = false; // Set to false to preserve duplicate data + private static bool _optimizeMotions; // Set to false to preserve duplicate data public NJS_MOTION() { - Name = "animation_" + Extensions.GenerateIdentifier(); - MdataName = Name + "_mdat"; + Name = $"animation_{Extensions.GenerateIdentifier()}"; + MdataName = $"{Name}_mdat"; } - public int CalculateModelParts(byte[] file, int address, uint imageBase) + private static int CalculateModelParts(byte[] file, int address, uint imageBase) { - int mdatap = ByteConverter.ToInt32(file, address); - AnimFlags animtype = (AnimFlags)ByteConverter.ToUInt16(file, address + 8); - if (animtype == 0) - { - return 0; - } - - int mdata = 0; - if (animtype.HasFlag(AnimFlags.Position)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Rotation)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Scale)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Vector)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Vertex)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Normal)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Color)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Intensity)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Target)) - { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Spot)) - { - mdata++; - } + var modelDataPointer = ByteConverter.ToInt32(file, address); + var animType = (AnimFlags)ByteConverter.ToUInt16(file, address + 8); - if (animtype.HasFlag(AnimFlags.Point)) + if (animType == 0) { - mdata++; - } - - if (animtype.HasFlag(AnimFlags.Roll)) - { - mdata++; + return 0; } - if (animtype.HasFlag(AnimFlags.Quaternion)) - { - mdata++; - } + // Count the number of AnimFlags set + var modelData = BitOperations.PopCount((uint)animType); + var lost = false; + var modelDataSize = 0; - int mdatasize = 0; - bool lost = false; - switch (mdata) + switch (modelData) { case 1: case 2: - mdatasize = 16; + modelDataSize = 16; break; case 3: - mdatasize = 24; + modelDataSize = 24; break; case 4: - mdatasize = 32; + modelDataSize = 32; break; case 5: - mdatasize = 40; + modelDataSize = 40; break; default: lost = true; break; } + if (lost) { return 0; } // Check MKEY pointers - int mdatas = 0; - for (int u = 0; u < 255; u++) + var modelDataCount = 0; + for (var u = 0; u < 255; u++) { - for (int m = 0; m < mdata; m++) + for (var m = 0; m < modelData; m++) { if (lost) { continue; } - uint pointer = ByteConverter.ToUInt32(file, mdatap - (int)imageBase + mdatasize * u + 4 * m); + var pointer = ByteConverter.ToUInt32(file, modelDataPointer - (int)imageBase + modelDataSize * u + 4 * m); if (pointer != 0 && (pointer < imageBase || pointer - (int)imageBase >= file.Length - 36)) { lost = true; @@ -237,19 +177,21 @@ public int CalculateModelParts(byte[] file, int address, uint imageBase) if (!lost) { - int framecount = ByteConverter.ToInt32(file, mdatap - (int)imageBase + mdatasize * u + 4 * mdata + 4 * m); + var framecount = ByteConverter.ToInt32(file, modelDataPointer - (int)imageBase + modelDataSize * u + 4 * modelData + 4 * m); if (framecount < 0 || framecount > 100 || (pointer == 0 && framecount != 0)) { lost = true; } } } + if (!lost) { - mdatas++; + modelDataCount++; } } - return mdatas; + + return modelDataCount; } public bool IsShapeMotion() @@ -261,12 +203,13 @@ public bool IsShapeMotion() return true; } } + return false; } - public bool OptimizeShape() + public static bool OptimizeShape() { - return optimizeMotions = true; + return _optimizeMotions = true; } public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dictionary labels = null, bool shortrot = false, int[] numverts = null, string actionName = null, string objectName = null, bool shortcheck = true) @@ -278,17 +221,18 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti ActionName = actionName; ObjectName = objectName; - if (labels != null && labels.ContainsKey(address)) + + if (labels != null && labels.TryGetValue(address, out var name)) { - Name = labels[address]; - if (int.TryParse(Name, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out int num) == true) + Name = name; + if (int.TryParse(Name, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out _)) { - Name = "animation_" + address.ToString("X8"); + Name = $"animation_{address:X8}"; } } else { - Name = "animation_" + address.ToString("X8"); + Name = $"animation_{address:X8}"; } if (address > file.Length - 12) @@ -297,43 +241,40 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } Frames = ByteConverter.ToInt32(file, address + 4); - AnimFlags animtype = (AnimFlags)ByteConverter.ToUInt16(file, address + 8); - ushort tmp = ByteConverter.ToUInt16(file, address + 10); - switch ((StructEnums.NJD_MTYPE_FN)tmp & StructEnums.NJD_MTYPE_FN.NJD_MTYPE_MASK) + var animationType = (AnimFlags)ByteConverter.ToUInt16(file, address + 8); + var tmp = ByteConverter.ToUInt16(file, address + 10); + + InterpolationMode = ((StructEnums.NJD_MTYPE_FN)tmp & StructEnums.NJD_MTYPE_FN.NJD_MTYPE_MASK) switch { - case StructEnums.NJD_MTYPE_FN.NJD_MTYPE_LINER: - InterpolationMode = InterpolationMode.Linear; - break; - case StructEnums.NJD_MTYPE_FN.NJD_MTYPE_SPLINE: - InterpolationMode = InterpolationMode.Spline; - break; - case StructEnums.NJD_MTYPE_FN.NJD_MTYPE_USER: - InterpolationMode = InterpolationMode.User; - break; - } + StructEnums.NJD_MTYPE_FN.NJD_MTYPE_LINER => InterpolationMode.Linear, + StructEnums.NJD_MTYPE_FN.NJD_MTYPE_SPLINE => InterpolationMode.Spline, + StructEnums.NJD_MTYPE_FN.NJD_MTYPE_USER => InterpolationMode.User, + _ => InterpolationMode + }; + ShortRot = shortrot; - int framesize = (tmp & 0xF) * 8; address = (int)(ByteConverter.ToUInt32(file, address) - imageBase); - if (labels != null && labels.ContainsKey(address)) + + if (labels != null && labels.TryGetValue(address, out var mdataName)) { - MdataName = labels[address]; + MdataName = mdataName; } else { - MdataName = Name + "_mdat_" + address.ToString("X8"); + MdataName = $"{Name}_mdat_{address:X8}"; } - for (int i = 0; i < nummodels; i++) + for (var i = 0; i < nummodels; i++) { - AnimModelData data = new AnimModelData(); - bool hasdata = false; + var data = new AnimModelData(); + var hasdata = false; uint posoff = 0; if (address > file.Length - 4) { continue; } - if (animtype.HasFlag(AnimFlags.Position)) + if (animationType.HasFlag(AnimFlags.Position)) { posoff = ByteConverter.ToUInt32(file, address); if (posoff > 0) @@ -344,7 +285,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint rotoff = 0; - if (animtype.HasFlag(AnimFlags.Rotation)) + if (animationType.HasFlag(AnimFlags.Rotation)) { rotoff = ByteConverter.ToUInt32(file, address); if (rotoff > 0) @@ -355,7 +296,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint scloff = 0; - if (animtype.HasFlag(AnimFlags.Scale)) + if (animationType.HasFlag(AnimFlags.Scale)) { scloff = ByteConverter.ToUInt32(file, address); if (scloff > 0) @@ -366,7 +307,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint vecoff = 0; - if (animtype.HasFlag(AnimFlags.Vector)) + if (animationType.HasFlag(AnimFlags.Vector)) { vecoff = ByteConverter.ToUInt32(file, address); if (vecoff > 0) @@ -377,7 +318,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint vertoff = 0; - if (animtype.HasFlag(AnimFlags.Vertex)) + if (animationType.HasFlag(AnimFlags.Vertex)) { vertoff = ByteConverter.ToUInt32(file, address); if (vertoff > 0) @@ -388,7 +329,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint normoff = 0; - if (animtype.HasFlag(AnimFlags.Normal)) + if (animationType.HasFlag(AnimFlags.Normal)) { normoff = ByteConverter.ToUInt32(file, address); if (normoff > 0) @@ -399,7 +340,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint targoff = 0; - if (animtype.HasFlag(AnimFlags.Target)) + if (animationType.HasFlag(AnimFlags.Target)) { targoff = ByteConverter.ToUInt32(file, address); if (targoff > 0) @@ -410,7 +351,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint rolloff = 0; - if (animtype.HasFlag(AnimFlags.Roll)) + if (animationType.HasFlag(AnimFlags.Roll)) { rolloff = ByteConverter.ToUInt32(file, address); if (rolloff > 0) @@ -421,7 +362,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint angoff = 0; - if (animtype.HasFlag(AnimFlags.Angle)) + if (animationType.HasFlag(AnimFlags.Angle)) { angoff = ByteConverter.ToUInt32(file, address); if (angoff > 0) @@ -432,7 +373,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint coloff = 0; - if (animtype.HasFlag(AnimFlags.Color)) + if (animationType.HasFlag(AnimFlags.Color)) { coloff = ByteConverter.ToUInt32(file, address); if (coloff > 0) @@ -443,7 +384,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint intoff = 0; - if (animtype.HasFlag(AnimFlags.Intensity)) + if (animationType.HasFlag(AnimFlags.Intensity)) { intoff = ByteConverter.ToUInt32(file, address); if (intoff > 0) @@ -454,7 +395,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint spotoff = 0; - if (animtype.HasFlag(AnimFlags.Spot)) + if (animationType.HasFlag(AnimFlags.Spot)) { spotoff = ByteConverter.ToUInt32(file, address); if (spotoff > 0) @@ -465,7 +406,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint pntoff = 0; - if (animtype.HasFlag(AnimFlags.Point)) + if (animationType.HasFlag(AnimFlags.Point)) { pntoff = ByteConverter.ToUInt32(file, address); if (pntoff > 0) @@ -476,7 +417,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } uint quatoff = 0; - if (animtype.HasFlag(AnimFlags.Quaternion)) + if (animationType.HasFlag(AnimFlags.Quaternion)) { quatoff = ByteConverter.ToUInt32(file, address); if (quatoff > 0) @@ -487,23 +428,23 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti address += 4; } int tmpaddr; - if (animtype.HasFlag(AnimFlags.Position)) + if (animationType.HasFlag(AnimFlags.Position)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (posoff != 0 && frames > 0) { hasdata = true; tmpaddr = (int)posoff; - if (labels != null && labels.ContainsKey(tmpaddr)) + if (labels != null && labels.TryGetValue(tmpaddr, out var positionName)) { - data.PositionName = labels[tmpaddr]; + data.PositionName = positionName; } else { - data.PositionName = Name + "_mkey_" + i.ToString() + "_pos_" + tmpaddr.ToString("X8"); + data.PositionName = $"{Name}_mkey_{i}_pos_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Position.Add(ByteConverter.ToInt32(file, tmpaddr), new Vertex(file, tmpaddr + 4)); tmpaddr += 16; @@ -511,26 +452,26 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Rotation)) + if (animationType.HasFlag(AnimFlags.Rotation)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (rotoff != 0 && frames > 0) { hasdata = true; tmpaddr = (int)rotoff; - if (labels != null && labels.ContainsKey(tmpaddr)) + if (labels != null && labels.TryGetValue(tmpaddr, out var rotationName)) { - data.RotationName = labels[tmpaddr]; + data.RotationName = rotationName; } else { - data.RotationName = Name + "_mkey_" + i.ToString() + "_rot_" + tmpaddr.ToString("X8"); + data.RotationName = $"{Name}_mkey_{i}_rot_{tmpaddr:X8}"; } if (shortcheck) { // Check if the animation uses short rotation or not - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { // If any of the rotation frames go outside the file, assume it uses shorts if (tmpaddr + 4 + 12 > file.Length) @@ -539,7 +480,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti break; } // If any of the rotation frames isn't in the range from -65535 to 65535, assume it uses shorts - Rotation rot = new Rotation(file, tmpaddr + 4); + var rot = new Rotation(file, tmpaddr + 4); if (rot.X > 65535 || rot.X < -65535 || rot.Y > 65535 || rot.Y < -65535 || rot.Z > 65535 || rot.Z < -65535) @@ -550,7 +491,7 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } } // Read rotation values - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { if (ShortRot) { @@ -574,23 +515,23 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Scale)) + if (animationType.HasFlag(AnimFlags.Scale)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (scloff != 0 && frames > 0) { hasdata = true; tmpaddr = (int)scloff; - if (labels != null && labels.ContainsKey(tmpaddr)) + if (labels != null && labels.TryGetValue(tmpaddr, out var scaleName)) { - data.ScaleName = labels[tmpaddr]; + data.ScaleName = scaleName; } else { - data.ScaleName = Name + "_mkey_" + i.ToString() + "_scl_" + tmpaddr.ToString("X8"); + data.ScaleName = $"{Name}_mkey_{i}_scl_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Scale.Add(ByteConverter.ToInt32(file, tmpaddr), new Vertex(file, tmpaddr + 4)); tmpaddr += 16; @@ -598,23 +539,23 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Vector)) + if (animationType.HasFlag(AnimFlags.Vector)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (vecoff != 0 && frames > 0) { hasdata = true; tmpaddr = (int)vecoff; - if (labels != null && labels.ContainsKey(tmpaddr)) + if (labels != null && labels.TryGetValue(tmpaddr, out var vectorName)) { - data.VectorName = labels[tmpaddr]; + data.VectorName = vectorName; } else { - data.VectorName = Name + "_mkey_" + i.ToString() + "_vec_" + tmpaddr.ToString("X8"); + data.VectorName = $"{Name}_mkey_{i}_vec_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Vector.Add(ByteConverter.ToInt32(file, tmpaddr), new Vertex(file, tmpaddr + 4)); tmpaddr += 16; @@ -622,36 +563,36 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - int vtxcount = -1; - if (animtype.HasFlag(AnimFlags.Vertex)) + var vtxcount = -1; + if (animationType.HasFlag(AnimFlags.Vertex)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (vertoff != 0 && frames > 0) { hasdata = true; tmpaddr = (int)vertoff; - if (labels != null && labels.ContainsKey(tmpaddr)) + if (labels != null && labels.TryGetValue(tmpaddr, out var vertexName)) { - data.VertexName = labels[tmpaddr]; + data.VertexName = vertexName; } else { - data.VertexName = Name + "_mkey_" + i.ToString() + "_vert_" + tmpaddr.ToString("X8"); + data.VertexName = $"{Name}_mkey_{i}_vert_{tmpaddr:X8}"; } - List ptrs = new List(); + var ptrs = new List(); data.VertexItemName = new string[frames]; - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { ptrs.AddUnique((int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase)); - int itemaddr = (int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase); + var itemaddr = (int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase); if (labels != null && labels.ContainsKey(itemaddr)) { data.VertexItemName[j] = labels[itemaddr]; } else { - data.VertexItemName[j] = Name + "_" + i.ToString() + "_vtx_" + j.ToString() + "_" + itemaddr.ToString("X8"); + data.VertexItemName[j] = $"{Name}_{i}_vtx_{j}_{itemaddr:X8}"; } tmpaddr += 8; @@ -674,11 +615,11 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } } tmpaddr = (int)vertoff; - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { - Vertex[] verts = new Vertex[vtxcount]; - int newaddr = (int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase); - for (int k = 0; k < verts.Length; k++) + var verts = new Vertex[vtxcount]; + var newaddr = (int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase); + for (var k = 0; k < verts.Length; k++) { verts[k] = new Vertex(file, newaddr); newaddr += Vertex.Size; @@ -693,9 +634,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Normal)) + if (animationType.HasFlag(AnimFlags.Normal)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (normoff != 0 && frames > 0) { hasdata = true; @@ -708,8 +649,8 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti else if (vtxcount < 0) { tmpaddr = (int)normoff; - List ptrs = new List(); - for (int j = 0; j < frames; j++) + var ptrs = new List(); + for (var j = 0; j < frames; j++) { ptrs.AddUnique((int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase)); tmpaddr += 8; @@ -731,23 +672,23 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.NormalName = Name + "_mkey_" + i.ToString() + "_norm_" + tmpaddr.ToString("X8"); + data.NormalName = $"{Name}_mkey_{i}_norm_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { - Vertex[] verts = new Vertex[vtxcount]; - int newaddr = (int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase); + var verts = new Vertex[vtxcount]; + var newaddr = (int)(ByteConverter.ToUInt32(file, tmpaddr + 4) - imageBase); if (labels != null && labels.ContainsKey(newaddr)) { data.NormalItemName[j] = labels[newaddr]; } else { - data.NormalItemName[j] = Name + "_" + i.ToString() + "_nrm_" + j.ToString() + "_" + newaddr.ToString("X8"); + data.NormalItemName[j] = $"{Name}_{i}_nrm_{j}_{newaddr:X8}"; } - for (int k = 0; k < verts.Length; k++) + for (var k = 0; k < verts.Length; k++) { verts[k] = new Vertex(file, newaddr); newaddr += Vertex.Size; @@ -762,9 +703,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Target)) + if (animationType.HasFlag(AnimFlags.Target)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (targoff != 0 && frames > 0) { hasdata = true; @@ -775,10 +716,10 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.TargetName = Name + "_mkey_" + i.ToString() + "_target_" + tmpaddr.ToString("X8"); + data.TargetName = $"{Name}_mkey_{i}_target_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Target.Add(ByteConverter.ToInt32(file, tmpaddr), new Vertex(file, tmpaddr + 4)); tmpaddr += 16; @@ -786,9 +727,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Roll)) + if (animationType.HasFlag(AnimFlags.Roll)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (rolloff != 0 && frames > 0) { hasdata = true; @@ -799,10 +740,10 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.RollName = Name + "_mkey_" + i.ToString() + "_roll_" + tmpaddr.ToString("X8"); + data.RollName = $"{Name}_mkey_{i}_roll_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Roll.Add(ByteConverter.ToInt32(file, tmpaddr), ByteConverter.ToInt32(file, tmpaddr + 4)); tmpaddr += 8; @@ -810,9 +751,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Angle)) + if (animationType.HasFlag(AnimFlags.Angle)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (angoff != 0 && frames > 0) { hasdata = true; @@ -823,10 +764,10 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.AngleName = Name + "_mkey_" + i.ToString() + "_ang_" + tmpaddr.ToString("X8"); + data.AngleName = $"{Name}_mkey_{i}_ang_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Angle.Add(ByteConverter.ToInt32(file, tmpaddr), ByteConverter.ToInt32(file, tmpaddr + 4)); tmpaddr += 8; @@ -834,9 +775,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Color)) + if (animationType.HasFlag(AnimFlags.Color)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (coloff != 0 && frames > 0) { hasdata = true; @@ -847,10 +788,10 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.ColorName = Name + "_mkey_" + i.ToString() + "_col_" + tmpaddr.ToString("X8"); + data.ColorName = $"{Name}_mkey_{i}_col_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Color.Add(ByteConverter.ToInt32(file, tmpaddr), ByteConverter.ToUInt32(file, tmpaddr + 4)); tmpaddr += 8; @@ -858,9 +799,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Intensity)) + if (animationType.HasFlag(AnimFlags.Intensity)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (intoff != 0 && frames > 0) { hasdata = true; @@ -871,10 +812,10 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.IntensityName = Name + "_mkey_" + i.ToString() + "_int_" + tmpaddr.ToString("X8"); + data.IntensityName = $"{Name}_mkey_{i}_int_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Intensity.Add(ByteConverter.ToInt32(file, tmpaddr), ByteConverter.ToSingle(file, tmpaddr + 4)); tmpaddr += 8; @@ -882,9 +823,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Spot)) + if (animationType.HasFlag(AnimFlags.Spot)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (spotoff != 0 && frames > 0) { hasdata = true; @@ -895,10 +836,10 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.SpotName = Name + "_mkey_" + i.ToString() + "_spot_" + tmpaddr.ToString("X8"); + data.SpotName = $"{Name}_mkey_{i}_spot_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Spot.Add(ByteConverter.ToInt32(file, tmpaddr), new Spotlight(file, tmpaddr + 4)); tmpaddr += 20; @@ -906,9 +847,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Point)) + if (animationType.HasFlag(AnimFlags.Point)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (pntoff != 0 && frames > 0) { hasdata = true; @@ -919,10 +860,10 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.PointName = Name + "_mkey_" + i.ToString() + "_point_" + tmpaddr.ToString("X8"); + data.PointName = $"{Name}_mkey_{i}_point_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { data.Point.Add(ByteConverter.ToInt32(file, tmpaddr), new float[] { ByteConverter.ToSingle(file, tmpaddr + 4), ByteConverter.ToSingle(file, tmpaddr + 8) }); tmpaddr += 12; @@ -930,9 +871,9 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } address += 4; } - if (animtype.HasFlag(AnimFlags.Quaternion)) + if (animationType.HasFlag(AnimFlags.Quaternion)) { - int frames = ByteConverter.ToInt32(file, address); + var frames = ByteConverter.ToInt32(file, address); if (quatoff != 0 && frames > 0) { hasdata = true; @@ -943,13 +884,13 @@ public NJS_MOTION(byte[] file, int address, uint imageBase, int nummodels, Dicti } else { - data.QuaternionName = Name + "_mkey_" + i.ToString() + "_quat_" + tmpaddr.ToString("X8"); + data.QuaternionName = $"{Name}_mkey_{i}_quat_{tmpaddr:X8}"; } - for (int j = 0; j < frames; j++) + for (var j = 0; j < frames; j++) { - //WXYZ order - data.Quaternion.Add(ByteConverter.ToInt32(file, tmpaddr), new float[] { ByteConverter.ToSingle(file, tmpaddr + 4), ByteConverter.ToSingle(file, tmpaddr + 8), ByteConverter.ToSingle(file, tmpaddr + 12), ByteConverter.ToSingle(file, tmpaddr + 16) }); + // WXYZ order + data.Quaternion.Add(ByteConverter.ToInt32(file, tmpaddr), [ByteConverter.ToSingle(file, tmpaddr + 4), ByteConverter.ToSingle(file, tmpaddr + 8), ByteConverter.ToSingle(file, tmpaddr + 12), ByteConverter.ToSingle(file, tmpaddr + 16)]); tmpaddr += 20; } } @@ -972,7 +913,7 @@ public static NJS_MOTION ReadHeader(byte[] file, int address, uint imageBase, Mo public static NJS_MOTION ReadHeader(byte[] file, int address, uint imageBase, ModelFormat format, Dictionary labels, Dictionary attaches) { - NJS_OBJECT Model = new NJS_OBJECT(file, (int)(ByteConverter.ToUInt32(file, address) - imageBase), imageBase, format, attaches); + var Model = new NJS_OBJECT(file, (int)(ByteConverter.ToUInt32(file, address) - imageBase), imageBase, format, attaches); return new NJS_MOTION(file, (int)(ByteConverter.ToUInt32(file, address + 4) - imageBase), imageBase, Model.CountAnimated(), labels); } @@ -990,13 +931,13 @@ public static NJS_MOTION ReadDirect(byte[] file, int count, int motionaddress, u public static NJS_MOTION Load(string filename, int nummodels = -1) { - bool be = ByteConverter.BigEndian; + var be = ByteConverter.BigEndian; ByteConverter.BigEndian = false; - byte[] file = File.ReadAllBytes(filename); - ulong magic = ByteConverter.ToUInt64(file, 0) & FormatMask; + var file = File.ReadAllBytes(filename); + var magic = ByteConverter.ToUInt64(file, 0) & FormatMask; if (magic == SAANIM) { - byte version = file[7]; + var version = file[7]; if (version > CurrentVersion) { ByteConverter.BigEndian = be; @@ -1005,23 +946,23 @@ public static NJS_MOTION Load(string filename, int nummodels = -1) string description = null; string actionName = null; string objectName = null; - int aniaddr = ByteConverter.ToInt32(file, 8); - Dictionary labels = new Dictionary(); - int tmpaddr = BitConverter.ToInt32(file, 0xC); + var aniaddr = ByteConverter.ToInt32(file, 8); + var labels = new Dictionary(); + var tmpaddr = BitConverter.ToInt32(file, 0xC); if (version >= 2) { if (tmpaddr != 0) { - bool finished = false; + var finished = false; while (!finished) { - ChunkTypes type = (ChunkTypes)ByteConverter.ToUInt32(file, tmpaddr); - int chunksz = ByteConverter.ToInt32(file, tmpaddr + 4); - int nextchunk = tmpaddr + 8 + chunksz; + var type = (ChunkTypes)ByteConverter.ToUInt32(file, tmpaddr); + var chunksz = ByteConverter.ToInt32(file, tmpaddr + 4); + var nextchunk = tmpaddr + 8 + chunksz; tmpaddr += 8; - byte[] chunk = new byte[chunksz]; + var chunk = new byte[chunksz]; Array.Copy(file, tmpaddr, chunk, 0, chunksz); - int chunkaddr = 0; + var chunkaddr = 0; switch (type) { case ChunkTypes.Label: @@ -1065,7 +1006,7 @@ public static NJS_MOTION Load(string filename, int nummodels = -1) ByteConverter.BigEndian = be; throw new NotImplementedException("Cannot open version 0 animations without a model!"); } - NJS_MOTION anim = new NJS_MOTION(file, aniaddr, 0, nummodels & int.MaxValue, labels, nummodels < 0, shortcheck: false) { Description = description, ActionName = actionName, ObjectName = objectName }; + var anim = new NJS_MOTION(file, aniaddr, 0, nummodels & int.MaxValue, labels, nummodels < 0, shortcheck: false) { Description = description, ActionName = actionName, ObjectName = objectName }; ByteConverter.BigEndian = be; return anim; } @@ -1075,10 +1016,10 @@ public static NJS_MOTION Load(string filename, int nummodels = -1) public static bool CheckAnimationFile(string filename) { - bool be = ByteConverter.BigEndian; + var be = ByteConverter.BigEndian; ByteConverter.BigEndian = false; - byte[] file = File.ReadAllBytes(filename); - ulong magic = ByteConverter.ToUInt64(file, 0) & FormatMask; + var file = File.ReadAllBytes(filename); + var magic = ByteConverter.ToUInt64(file, 0) & FormatMask; ByteConverter.BigEndian = be; if (magic == SAANIM) { @@ -1090,55 +1031,55 @@ public static bool CheckAnimationFile(string filename) public byte[] GetBytes(uint imageBase, Dictionary labels, out uint address, bool useNMDM = false) { - List result = new List(); - List parameterData = new List(); - List pofOffsets = new List(); - uint[] posoffs = new uint[ModelParts]; - int[] posframes = new int[ModelParts]; - bool hasPos = false; - uint[] rotoffs = new uint[ModelParts]; - int[] rotframes = new int[ModelParts]; - bool hasRot = false; - uint[] scloffs = new uint[ModelParts]; - int[] sclframes = new int[ModelParts]; - bool hasScl = false; - uint[] vecoffs = new uint[ModelParts]; - int[] vecframes = new int[ModelParts]; - bool hasVec = false; - uint[] vertoffs = new uint[ModelParts]; - int[] vertframes = new int[ModelParts]; - bool hasVert = false; - uint[] normoffs = new uint[ModelParts]; - int[] normframes = new int[ModelParts]; - bool hasNorm = false; - uint[] targoffs = new uint[ModelParts]; - int[] targframes = new int[ModelParts]; - bool hasTarg = false; - uint[] rolloffs = new uint[ModelParts]; - int[] rollframes = new int[ModelParts]; - bool hasRoll = false; - uint[] angoffs = new uint[ModelParts]; - int[] angframes = new int[ModelParts]; - bool hasAng = false; - uint[] coloffs = new uint[ModelParts]; - int[] colframes = new int[ModelParts]; - bool hasCol = false; - uint[] intoffs = new uint[ModelParts]; - int[] intframes = new int[ModelParts]; - bool hasInt = false; - uint[] spotoffs = new uint[ModelParts]; - int[] spotframes = new int[ModelParts]; - bool hasSpot = false; - uint[] pntoffs = new uint[ModelParts]; - int[] pntframes = new int[ModelParts]; - bool hasPnt = false; - uint[] quatoffs = new uint[ModelParts]; - int[] quatframes = new int[ModelParts]; - bool hasQuat = false; + var result = new List(); + var parameterData = new List(); + var pofOffsets = new List(); + var posoffs = new uint[ModelParts]; + var posframes = new int[ModelParts]; + var hasPos = false; + var rotoffs = new uint[ModelParts]; + var rotframes = new int[ModelParts]; + var hasRot = false; + var scloffs = new uint[ModelParts]; + var sclframes = new int[ModelParts]; + var hasScl = false; + var vecoffs = new uint[ModelParts]; + var vecframes = new int[ModelParts]; + var hasVec = false; + var vertoffs = new uint[ModelParts]; + var vertframes = new int[ModelParts]; + var hasVert = false; + var normoffs = new uint[ModelParts]; + var normframes = new int[ModelParts]; + var hasNorm = false; + var targoffs = new uint[ModelParts]; + var targframes = new int[ModelParts]; + var hasTarg = false; + var rolloffs = new uint[ModelParts]; + var rollframes = new int[ModelParts]; + var hasRoll = false; + var angoffs = new uint[ModelParts]; + var angframes = new int[ModelParts]; + var hasAng = false; + var coloffs = new uint[ModelParts]; + var colframes = new int[ModelParts]; + var hasCol = false; + var intoffs = new uint[ModelParts]; + var intframes = new int[ModelParts]; + var hasInt = false; + var spotoffs = new uint[ModelParts]; + var spotframes = new int[ModelParts]; + var hasSpot = false; + var pntoffs = new uint[ModelParts]; + var pntframes = new int[ModelParts]; + var hasPnt = false; + var quatoffs = new uint[ModelParts]; + var quatframes = new int[ModelParts]; + var hasQuat = false; pofOffsets.Add(0); // First offset in the motion - foreach (KeyValuePair model in Models) + foreach (var model in Models) { if (model.Value.Position.Count > 0) { @@ -1154,7 +1095,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.PositionName; + var newname = model.Value.PositionName; do { newname += "_dup"; @@ -1163,7 +1104,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } posframes[model.Key] = model.Value.Position.Count; - foreach (KeyValuePair item in model.Value.Position) + foreach (var item in model.Value.Position) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(item.Value.GetBytes()); @@ -1183,7 +1124,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.RotationName; + var newname = model.Value.RotationName; do { newname += "_dup"; @@ -1192,7 +1133,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } rotframes[model.Key] = model.Value.Rotation.Count; - foreach (KeyValuePair item in model.Value.Rotation) + foreach (var item in model.Value.Rotation) { if (ShortRot) { @@ -1222,7 +1163,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.ScaleName; + var newname = model.Value.ScaleName; do { newname += "_dup"; @@ -1231,7 +1172,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } sclframes[model.Key] = model.Value.Scale.Count; - foreach (KeyValuePair item in model.Value.Scale) + foreach (var item in model.Value.Scale) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(item.Value.GetBytes()); @@ -1251,7 +1192,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.VectorName; + var newname = model.Value.VectorName; do { newname += "_dup"; @@ -1260,7 +1201,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } vecframes[model.Key] = model.Value.Vector.Count; - foreach (KeyValuePair item in model.Value.Vector) + foreach (var item in model.Value.Vector) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(item.Value.GetBytes()); @@ -1270,32 +1211,37 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint { hasVert = true; result.Align(4); - List offs = new List(); - List<(Vertex[] vlist, uint off)> voffs = new List<(Vertex[] vlist, uint off)>(); - foreach (KeyValuePair item in model.Value.Vertex) + var offs = new List(); + var voffs = new List<(Vertex[] vlist, uint off)>(); + foreach (var item in model.Value.Vertex) { - bool found = false; + var found = false; foreach (var (vlist, off) in voffs) + { if (item.Value.SequenceEqual(vlist)) { offs.Add(off); found = true; break; } - if (optimizeMotions && found) + } + + if (_optimizeMotions && found) { continue; } result.Align(4); offs.Add(imageBase + (uint)result.Count); - if (optimizeMotions) + if (_optimizeMotions) { voffs.Add((item.Value, imageBase + (uint)result.Count)); } - foreach (Vertex v in item.Value) + foreach (var v in item.Value) + { result.AddRange(v.GetBytes()); + } } vertoffs[model.Key] = imageBase + (uint)result.Count; @@ -1307,7 +1253,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.VertexName; + var newname = model.Value.VertexName; do { newname += "_dup"; @@ -1316,13 +1262,13 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } vertframes[model.Key] = model.Value.Vertex.Count; - int i = 0; - foreach (KeyValuePair item in model.Value.Vertex) + var i = 0; + foreach (var item in model.Value.Vertex) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(offs[i++])); } - for (int u = 0; u < model.Value.Vertex.Count; u++) + for (var u = 0; u < model.Value.Vertex.Count; u++) { if (!labels.ContainsValue(offs[u]) && model.Value.VertexItemName[u] != null) { @@ -1332,7 +1278,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.VertexItemName[u]; + var newname = model.Value.VertexItemName[u]; do { newname += "_dup"; @@ -1346,32 +1292,37 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint { hasNorm = true; result.Align(4); - List offs = new List(); - List<(Vertex[] vlist, uint off)> voffs = new List<(Vertex[] vlist, uint off)>(); - foreach (KeyValuePair item in model.Value.Normal) + var offs = new List(); + var voffs = new List<(Vertex[] vlist, uint off)>(); + foreach (var item in model.Value.Normal) { - bool found = false; + var found = false; foreach (var (vlist, off) in voffs) + { if (item.Value.SequenceEqual(vlist)) { offs.Add(off); found = true; break; } - if (optimizeMotions && found) + } + + if (_optimizeMotions && found) { continue; } result.Align(4); offs.Add(imageBase + (uint)result.Count); - if (optimizeMotions) + if (_optimizeMotions) { voffs.Add((item.Value, imageBase + (uint)result.Count)); } - foreach (Vertex v in item.Value) + foreach (var v in item.Value) + { result.AddRange(v.GetBytes()); + } } normoffs[model.Key] = imageBase + (uint)result.Count; @@ -1383,7 +1334,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.NormalName; + var newname = model.Value.NormalName; do { newname += "_dup"; @@ -1392,13 +1343,13 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } normframes[model.Key] = model.Value.Normal.Count; - int i = 0; - foreach (KeyValuePair item in model.Value.Normal) + var i = 0; + foreach (var item in model.Value.Normal) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(offs[i++])); } - for (int u = 0; u < model.Value.Normal.Count; u++) + for (var u = 0; u < model.Value.Normal.Count; u++) { if (!labels.ContainsValue(offs[u]) && model.Value.NormalItemName[u] != null) { @@ -1408,7 +1359,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.NormalItemName[u]; + var newname = model.Value.NormalItemName[u]; do { newname += "_dup"; @@ -1432,7 +1383,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.TargetName; + var newname = model.Value.TargetName; do { newname += "_dup"; @@ -1441,7 +1392,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } targframes[model.Key] = model.Value.Target.Count; - foreach (KeyValuePair item in model.Value.Target) + foreach (var item in model.Value.Target) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(item.Value.GetBytes()); @@ -1461,7 +1412,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.RollName; + var newname = model.Value.RollName; do { newname += "_dup"; @@ -1470,7 +1421,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } rollframes[model.Key] = model.Value.Roll.Count; - foreach (KeyValuePair item in model.Value.Roll) + foreach (var item in model.Value.Roll) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(item.Value)); @@ -1490,7 +1441,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.AngleName; + var newname = model.Value.AngleName; do { newname += "_dup"; @@ -1499,7 +1450,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } angframes[model.Key] = model.Value.Angle.Count; - foreach (KeyValuePair item in model.Value.Angle) + foreach (var item in model.Value.Angle) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(item.Value)); @@ -1519,7 +1470,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.ColorName; + var newname = model.Value.ColorName; do { newname += "_dup"; @@ -1528,7 +1479,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } colframes[model.Key] = model.Value.Color.Count; - foreach (KeyValuePair item in model.Value.Color) + foreach (var item in model.Value.Color) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(item.Value)); @@ -1548,7 +1499,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.IntensityName; + var newname = model.Value.IntensityName; do { newname += "_dup"; @@ -1557,7 +1508,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } intframes[model.Key] = model.Value.Intensity.Count; - foreach (KeyValuePair item in model.Value.Intensity) + foreach (var item in model.Value.Intensity) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(item.Value)); @@ -1577,7 +1528,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.SpotName; + var newname = model.Value.SpotName; do { newname += "_dup"; @@ -1586,7 +1537,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } spotframes[model.Key] = model.Value.Spot.Count; - foreach (KeyValuePair item in model.Value.Spot) + foreach (var item in model.Value.Spot) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(item.Value.GetBytes()); @@ -1606,7 +1557,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.PointName; + var newname = model.Value.PointName; do { newname += "_dup"; @@ -1615,7 +1566,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } pntframes[model.Key] = model.Value.Point.Count; - foreach (KeyValuePair item in model.Value.Point) + foreach (var item in model.Value.Point) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(item.Value[0])); @@ -1636,7 +1587,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = model.Value.QuaternionName; + var newname = model.Value.QuaternionName; do { newname += "_dup"; @@ -1645,7 +1596,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } } quatframes[model.Key] = model.Value.Quaternion.Count; - foreach (KeyValuePair item in model.Value.Quaternion) + foreach (var item in model.Value.Quaternion) { result.AddRange(ByteConverter.GetBytes(item.Key)); result.AddRange(ByteConverter.GetBytes(item.Value[0])); @@ -1750,8 +1701,8 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint numpairs = 2; break; } - uint modeldata = imageBase + (uint)result.Count; - //Dealing with uninitialized data. + var modeldata = imageBase + (uint)result.Count; + //Dealing with uninitialized data. //This is to avoid MDATA and MOTIONS sharing the same address, which interferes with labels. if (result.Count == 0 && numpairs == 0 && Models.Count == 0 && flags == 0) { @@ -1760,18 +1711,14 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint posoffs[0] = imageBase + (uint)result.Count; posframes[0] = 1; result.AddRange(ByteConverter.GetBytes(0)); - Vertex temp = new Vertex(0.0f, 0.0f, 0.0f); + var temp = new Vertex(0.0f, 0.0f, 0.0f); result.AddRange(temp.GetBytes()); } if (!labels.ContainsValue(modeldata) && MdataName != null) { - if (!labels.ContainsKey(MdataName)) + if (!labels.TryAdd(MdataName, modeldata)) { - labels.Add(MdataName, modeldata); - } - else - { - string newname = MdataName; + var newname = MdataName; do { newname += "_dup"; @@ -1779,7 +1726,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint labels.Add(newname, modeldata); } } - for (int i = 0; i < ModelParts; i++) + for (var i = 0; i < ModelParts; i++) { //Offsets if (hasPos) @@ -1951,7 +1898,7 @@ public byte[] GetBytes(uint imageBase, Dictionary labels, out uint } else { - string newname = Name; + var newname = Name; do { newname += "_dup"; @@ -1987,27 +1934,27 @@ public byte[] GetBytes(uint imageBase) public void ToStructVariables(TextWriter writer, List labels = null) { - bool hasPos = false; - bool hasRot = false; - bool hasScl = false; - bool hasVec = false; - bool hasVert = false; - bool hasNorm = false; - bool hasTarg = false; - bool hasRoll = false; - bool hasAng = false; - bool hasCol = false; - bool hasInt = false; - bool hasSpot = false; - bool hasPnt = false; - bool hasQuat = false; - string id = Name.MakeIdentifier(); + var hasPos = false; + var hasRot = false; + var hasScl = false; + var hasVec = false; + var hasVert = false; + var hasNorm = false; + var hasTarg = false; + var hasRoll = false; + var hasAng = false; + var hasCol = false; + var hasInt = false; + var hasSpot = false; + var hasPnt = false; + var hasQuat = false; + var id = Name.MakeIdentifier(); if (labels == null) { labels = new List(); } - foreach (KeyValuePair model in Models) + foreach (var model in Models) { if (model.Value.Position.Count > 0 && !labels.Contains(model.Value.PositionName)) { @@ -2015,10 +1962,14 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_F "); writer.Write(model.Value.PositionName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Position.Count); - foreach (KeyValuePair item in model.Value.Position) - lines.Add("\t{ " + item.Key + ", " + item.Value.X.ToC() + ", " + item.Value.Y.ToC() + ", " + item.Value.Z.ToC() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Position.Count); + foreach (var item in model.Value.Position) + { + lines.Add( + $"\t{{ {item.Key}, {item.Value.X.ToC()}, {item.Value.Y.ToC()}, {item.Value.Z.ToC()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.PositionName); @@ -2037,19 +1988,21 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write(model.Value.RotationName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Rotation.Count); - foreach (KeyValuePair item in model.Value.Rotation) + var lines = new List(model.Value.Rotation.Count); + foreach (var item in model.Value.Rotation) { if (ShortRot) { - lines.Add("\t{ " + item.Key + ", " + ((short)item.Value.X).ToCHex() + ", " + ((short)item.Value.Y).ToCHex() + ", " + ((short)item.Value.Z).ToCHex() + " }"); + lines.Add( + $"\t{{ {item.Key}, {((short)item.Value.X).ToCHex()}, {((short)item.Value.Y).ToCHex()}, {((short)item.Value.Z).ToCHex()} }}"); } else { - lines.Add("\t{ " + item.Key + ", " + item.Value.X.ToCHex() + ", " + item.Value.Y.ToCHex() + ", " + item.Value.Z.ToCHex() + " }"); + lines.Add( + $"\t{{ {item.Key}, {item.Value.X.ToCHex()}, {item.Value.Y.ToCHex()}, {item.Value.Z.ToCHex()} }}"); } } - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.RotationName); @@ -2060,10 +2013,14 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_F "); writer.Write(model.Value.ScaleName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Scale.Count); - foreach (KeyValuePair item in model.Value.Scale) - lines.Add("\t{ " + item.Key + ", " + item.Value.X.ToC() + ", " + item.Value.Y.ToC() + ", " + item.Value.Z.ToC() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Scale.Count); + foreach (var item in model.Value.Scale) + { + lines.Add( + $"\t{{ {item.Key}, {item.Value.X.ToC()}, {item.Value.Y.ToC()}, {item.Value.Z.ToC()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.ScaleName); @@ -2074,10 +2031,14 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_F "); writer.Write(model.Value.VectorName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Vector.Count); - foreach (KeyValuePair item in model.Value.Vector) - lines.Add("\t{ " + item.Key + ", " + item.Value.X.ToC() + ", " + item.Value.Y.ToC() + ", " + item.Value.Z.ToC() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Vector.Count); + foreach (var item in model.Value.Vector) + { + lines.Add( + $"\t{{ {item.Key}, {item.Value.X.ToC()}, {item.Value.Y.ToC()}, {item.Value.Z.ToC()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.VectorName); @@ -2085,32 +2046,38 @@ public void ToStructVariables(TextWriter writer, List labels = null) if (model.Value.Vertex.Count > 0 && !labels.Contains(model.Value.VertexName)) { hasVert = true; - int z = 0; - foreach (KeyValuePair item in model.Value.Vertex) + var z = 0; + foreach (var item in model.Value.Vertex) { if (!labels.Contains(model.Value.VertexItemName[z])) { writer.Write("NJS_VECTOR "); writer.Write(model.Value.VertexItemName[z].MakeIdentifier()); writer.WriteLine("[] = {"); - List l2 = new List(item.Value.Length); - foreach (Vertex v in item.Value) - l2.Add("\t" + v.ToStruct()); - writer.WriteLine(string.Join("," + Environment.NewLine, l2.ToArray())); + var l2 = new List(item.Value.Length); + foreach (var v in item.Value) + { + l2.Add($"\t{v.ToStruct()}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", l2.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.VertexItemName[z]); } - z++; + z++; } writer.Write("NJS_MKEY_P "); writer.Write(model.Value.VertexName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Vertex.Count); - int v_c = 0; - foreach (KeyValuePair item in model.Value.Vertex) - lines.Add("\t{ " + item.Key + ", " + model.Value.VertexItemName[v_c++].MakeIdentifier() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Vertex.Count); + var v_c = 0; + foreach (var item in model.Value.Vertex) + { + lines.Add($"\t{{ {item.Key}, {model.Value.VertexItemName[v_c++].MakeIdentifier()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.VertexName); @@ -2118,32 +2085,38 @@ public void ToStructVariables(TextWriter writer, List labels = null) if (model.Value.Normal.Count > 0 && !labels.Contains(model.Value.NormalName)) { hasNorm = true; - int z = 0; - foreach (KeyValuePair item in model.Value.Normal) + var z = 0; + foreach (var item in model.Value.Normal) { if (!labels.Contains(model.Value.NormalItemName[z])) { writer.Write("NJS_VECTOR "); writer.Write(model.Value.NormalItemName[z].MakeIdentifier()); writer.WriteLine("[] = {"); - List l2 = new List(item.Value.Length); - foreach (Vertex v in item.Value) - l2.Add("\t" + v.ToStruct()); - writer.WriteLine(string.Join("," + Environment.NewLine, l2.ToArray())); + var l2 = new List(item.Value.Length); + foreach (var v in item.Value) + { + l2.Add($"\t{v.ToStruct()}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", l2.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.NormalItemName[z]); } - z++; + z++; } writer.Write("NJS_MKEY_P "); writer.Write(model.Value.NormalName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Vertex.Count); - int v_c = 0; - foreach (KeyValuePair item in model.Value.Vertex) - lines.Add("\t{ " + item.Key + ", " + model.Value.NormalItemName[v_c++].MakeIdentifier() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Vertex.Count); + var v_c = 0; + foreach (var item in model.Value.Vertex) + { + lines.Add($"\t{{ {item.Key}, {model.Value.NormalItemName[v_c++].MakeIdentifier()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.NormalName); @@ -2154,10 +2127,14 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_F "); writer.Write(model.Value.TargetName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Target.Count); - foreach (KeyValuePair item in model.Value.Target) - lines.Add("\t{ " + item.Key + ", " + item.Value.X.ToC() + ", " + item.Value.Y.ToC() + ", " + item.Value.Z.ToC() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Target.Count); + foreach (var item in model.Value.Target) + { + lines.Add( + $"\t{{ {item.Key}, {item.Value.X.ToC()}, {item.Value.Y.ToC()}, {item.Value.Z.ToC()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.TargetName); @@ -2168,10 +2145,13 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_A1 "); writer.Write(model.Value.RollName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Roll.Count); - foreach (KeyValuePair item in model.Value.Roll) - lines.Add("\t{ " + item.Key + ", " + item.Value.ToCHex() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Roll.Count); + foreach (var item in model.Value.Roll) + { + lines.Add($"\t{{ {item.Key}, {item.Value.ToCHex()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.RollName); @@ -2182,10 +2162,13 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_A1 "); writer.Write(model.Value.AngleName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Angle.Count); - foreach (KeyValuePair item in model.Value.Angle) - lines.Add("\t{ " + item.Key + ", " + item.Value.ToCHex() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Angle.Count); + foreach (var item in model.Value.Angle) + { + lines.Add($"\t{{ {item.Key}, {item.Value.ToCHex()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.AngleName); @@ -2196,10 +2179,13 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_UI32 "); writer.Write(model.Value.ColorName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Color.Count); - foreach (KeyValuePair item in model.Value.Color) - lines.Add("\t{ " + item.Key + ", " + item.Value.ToCHex() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Color.Count); + foreach (var item in model.Value.Color) + { + lines.Add($"\t{{ {item.Key}, {item.Value.ToCHex()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.ColorName); @@ -2210,10 +2196,13 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_F1 "); writer.Write(model.Value.IntensityName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Intensity.Count); - foreach (KeyValuePair item in model.Value.Intensity) - lines.Add("\t{ " + item.Key + ", " + item.Value.ToC() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Intensity.Count); + foreach (var item in model.Value.Intensity) + { + lines.Add($"\t{{ {item.Key}, {item.Value.ToC()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.IntensityName); @@ -2224,10 +2213,14 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_SPOT "); writer.Write(model.Value.SpotName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Spot.Count); - foreach (KeyValuePair item in model.Value.Spot) - lines.Add("\t{ " + item.Key + ", " + item.Value.Near.ToC() + ", " + item.Value.Far.ToC() + ", " + item.Value.InsideAngle.ToCHex() + ", " + item.Value.OutsideAngle.ToCHex() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Spot.Count); + foreach (var item in model.Value.Spot) + { + lines.Add( + $"\t{{ {item.Key}, {item.Value.Near.ToC()}, {item.Value.Far.ToC()}, {item.Value.InsideAngle.ToCHex()}, {item.Value.OutsideAngle.ToCHex()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.SpotName); @@ -2238,10 +2231,13 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_F2 "); writer.Write(model.Value.PointName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Point.Count); - foreach (KeyValuePair item in model.Value.Point) - lines.Add("\t{ " + item.Key + ", " + item.Value[0].ToC() + ", " + item.Value[1].ToC() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Point.Count); + foreach (var item in model.Value.Point) + { + lines.Add($"\t{{ {item.Key}, {item.Value[0].ToC()}, {item.Value[1].ToC()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.PointName); @@ -2252,10 +2248,14 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write("NJS_MKEY_QUAT "); writer.Write(model.Value.QuaternionName.MakeIdentifier()); writer.WriteLine("[] = {"); - List lines = new List(model.Value.Quaternion.Count); - foreach (KeyValuePair item in model.Value.Quaternion) - lines.Add("\t{ " + item.Key + ", " + item.Value[0].ToC() + ", " + item.Value[1].ToC() + item.Value[2].ToC() + item.Value[3].ToC() + " }"); - writer.WriteLine(string.Join("," + Environment.NewLine, lines.ToArray())); + var lines = new List(model.Value.Quaternion.Count); + foreach (var item in model.Value.Quaternion) + { + lines.Add( + $"\t{{ {item.Key}, {item.Value[0].ToC()}, {item.Value[1].ToC()}{item.Value[2].ToC()}{item.Value[3].ToC()} }}"); + } + + writer.WriteLine(string.Join($",{Environment.NewLine}", lines.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(model.Value.QuaternionName); @@ -2370,10 +2370,10 @@ public void ToStructVariables(TextWriter writer, List labels = null) writer.Write(" "); writer.Write(MdataName.MakeIdentifier()); writer.WriteLine("[] = {"); - List mdats = new List(ModelParts); - for (int i = 0; i < ModelParts; i++) + var mdats = new List(ModelParts); + for (var i = 0; i < ModelParts; i++) { - List elems = new List(numpairs * 2); + var elems = new List(numpairs * 2); if (hasPos) { if (Models.ContainsKey(i) && Models[i].Position.Count > 0) @@ -2682,9 +2682,9 @@ public void ToStructVariables(TextWriter writer, List labels = null) elems.Add("0"); } } - mdats.Add("\t{ " + string.Join(", ", elems.ToArray()) + " }"); + mdats.Add($"\t{{ {string.Join(", ", elems.ToArray())} }}"); } - writer.WriteLine(string.Join("," + Environment.NewLine, mdats.ToArray())); + writer.WriteLine(string.Join($",{Environment.NewLine}", mdats.ToArray())); writer.WriteLine("};"); writer.WriteLine(); labels.Add(MdataName); @@ -2731,7 +2731,7 @@ public void ToStructVariables(TextWriter writer, List labels = null) public string ToStructVariables(List labels = null) { - using (StringWriter sw = new StringWriter()) + using (var sw = new StringWriter()) { ToStructVariables(sw, labels); return sw.ToString(); @@ -2740,21 +2740,21 @@ public string ToStructVariables(List labels = null) public void ToNJA(TextWriter writer, List labels = null, bool isDum = false) { - bool hasPos = false; - bool hasRot = false; - bool hasScl = false; - bool hasVec = false; - bool hasVert = false; - bool hasNorm = false; - bool hasTarg = false; - bool hasRoll = false; - bool hasAng = false; - bool hasCol = false; - bool hasInt = false; - bool hasSpot = false; - bool hasPnt = false; - bool hasQuat = false; - string id = Name.MakeIdentifier(); + var hasPos = false; + var hasRot = false; + var hasScl = false; + var hasVec = false; + var hasVert = false; + var hasNorm = false; + var hasTarg = false; + var hasRoll = false; + var hasAng = false; + var hasCol = false; + var hasInt = false; + var hasSpot = false; + var hasPnt = false; + var hasQuat = false; + var id = Name.MakeIdentifier(); if (labels == null) { labels = new List(); @@ -2764,7 +2764,7 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa if (!isDum) { writer.WriteLine(); - foreach (KeyValuePair model in Models) + foreach (var model in Models) { // Not implemented: Target, Roll, Angle, Color, Intensity, Spot, Point if (model.Value.Position.Count > 0 && !labels.Contains(model.Value.PositionName)) @@ -2772,8 +2772,12 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa hasPos = true; writer.WriteLine("POSITION {0}[]", model.Value.PositionName.MakeIdentifier()); writer.WriteLine("START"); - foreach (KeyValuePair item in model.Value.Position) - writer.WriteLine(" MKEYF( " + item.Key + ", " + item.Value.X.ToNJA() + ", " + item.Value.Y.ToNJA() + ", " + item.Value.Z.ToNJA() + " ),"); + foreach (var item in model.Value.Position) + { + writer.WriteLine( + $" MKEYF( {item.Key}, {item.Value.X.ToNJA()}, {item.Value.Y.ToNJA()}, {item.Value.Z.ToNJA()} ),"); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.PositionName); @@ -2793,16 +2797,16 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa writer.Write(model.Value.RotationName.MakeIdentifier()); writer.WriteLine("[]"); writer.WriteLine("START"); - foreach (KeyValuePair item in model.Value.Rotation) + foreach (var item in model.Value.Rotation) { if (ShortRot) { - writer.WriteLine(" MKEYSA( " + item.Key + ", " + (((short)item.Value.X) / 182.044f).ToNJA() + ", " + (((short)item.Value.Y) / 182.044f).ToNJA() + ", " + (((short)item.Value.Z) / 182.044f).ToNJA() + " ),"); + writer.WriteLine($" MKEYSA( {item.Key}, {(((short)item.Value.X) / 182.044f).ToNJA()}, {(((short)item.Value.Y) / 182.044f).ToNJA()}, {(((short)item.Value.Z) / 182.044f).ToNJA()} ),"); } else { - writer.WriteLine(" MKEYA( " + item.Key + ", " + (item.Value.X / 182.044f).ToNJA() + ", " + (item.Value.Y / 182.044f).ToNJA() + ", " + (item.Value.Z / 182.044f).ToNJA() + " ),"); + writer.WriteLine($" MKEYA( {item.Key}, {(item.Value.X / 182.044f).ToNJA()}, {(item.Value.Y / 182.044f).ToNJA()}, {(item.Value.Z / 182.044f).ToNJA()} ),"); } } writer.WriteLine("END"); @@ -2814,8 +2818,11 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa hasScl = true; writer.WriteLine("SCALE {0}[]", model.Value.ScaleName.MakeIdentifier()); writer.WriteLine("START"); - foreach (KeyValuePair item in model.Value.Scale) - writer.WriteLine(" MKEYF( " + item.Key + ", " + item.Value.X.ToNJA() + ", " + item.Value.Y.ToNJA() + ", " + item.Value.Z.ToNJA() + " ),"); + foreach (var item in model.Value.Scale) + { + writer.WriteLine($" MKEYF( {item.Key}, {item.Value.X.ToNJA()}, {item.Value.Y.ToNJA()}, {item.Value.Z.ToNJA()} ),"); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.ScaleName); @@ -2825,8 +2832,11 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa hasVec = true; writer.WriteLine("VECTOR {0}[]", model.Value.VectorName.MakeIdentifier()); writer.WriteLine("START"); - foreach (KeyValuePair item in model.Value.Vector) - writer.WriteLine(" MKEYF( " + item.Key + ", " + item.Value.X.ToNJA() + ", " + item.Value.Y.ToNJA() + ", " + item.Value.Z.ToNJA() + " ),"); + foreach (var item in model.Value.Vector) + { + writer.WriteLine($" MKEYF( {item.Key}, {item.Value.X.ToNJA()}, {item.Value.Y.ToNJA()}, {item.Value.Z.ToNJA()} ),"); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.VectorName); @@ -2834,16 +2844,19 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa if (model.Value.Vertex.Count > 0 && !labels.Contains(model.Value.VertexName)) { hasVert = true; - int z = 0; - foreach (KeyValuePair item in model.Value.Vertex) + var z = 0; + foreach (var item in model.Value.Vertex) { if (!labels.Contains(model.Value.VertexItemName[z])) { writer.WriteLine("POINT {0}[]", model.Value.VertexItemName[z].MakeIdentifier()); writer.WriteLine("START"); - List l2 = new List(item.Value.Length); - foreach (Vertex v in item.Value) + var l2 = new List(item.Value.Length); + foreach (var v in item.Value) + { writer.WriteLine(" VERT{0},", v.ToNJA()); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.VertexItemName[z]); @@ -2853,10 +2866,13 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa writer.WriteLine(); writer.WriteLine("POINTER {0}[]", model.Value.VertexName.MakeIdentifier()); writer.WriteLine("START"); - List lines = new List(model.Value.Vertex.Count); - int v_c = 0; - foreach (KeyValuePair item in model.Value.Vertex) - writer.WriteLine(" MKEYP( " + item.Key + ", " + model.Value.VertexItemName[v_c++].MakeIdentifier() + "),"); + var lines = new List(model.Value.Vertex.Count); + var v_c = 0; + foreach (var item in model.Value.Vertex) + { + writer.WriteLine($" MKEYP( {item.Key}, {model.Value.VertexItemName[v_c++].MakeIdentifier()}),"); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.VertexName); @@ -2864,15 +2880,18 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa if (model.Value.Normal.Count > 0 && !labels.Contains(model.Value.NormalName)) { hasNorm = true; - int z = 0; - foreach (KeyValuePair item in model.Value.Normal) + var z = 0; + foreach (var item in model.Value.Normal) { if (!labels.Contains(model.Value.NormalItemName[z])) { writer.WriteLine("NORMAL {0}[]", model.Value.NormalItemName[z].MakeIdentifier()); writer.WriteLine("START"); - foreach (Vertex v in item.Value) + foreach (var v in item.Value) + { writer.WriteLine(" NORM{0},", v.ToNJA()); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.NormalItemName[z]); @@ -2882,9 +2901,12 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa writer.WriteLine(); writer.WriteLine("POINTER {0}[]", model.Value.NormalName.MakeIdentifier()); writer.WriteLine("START"); - int v_c = 0; - foreach (KeyValuePair item in model.Value.Normal) - writer.WriteLine(" MKEYP( " + item.Key + ", " + model.Value.NormalItemName[v_c++].MakeIdentifier() + "),"); + var v_c = 0; + foreach (var item in model.Value.Normal) + { + writer.WriteLine($" MKEYP( {item.Key}, {model.Value.NormalItemName[v_c++].MakeIdentifier()}),"); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.NormalName); @@ -2894,8 +2916,11 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa hasQuat = true; writer.WriteLine("QROTATION {0}[]", model.Value.QuaternionName.MakeIdentifier()); writer.WriteLine("START"); - foreach (KeyValuePair item in model.Value.Quaternion) - writer.WriteLine(" MKEYQ( " + item.Key + ", " + item.Value[0].ToNJA() + ", " + item.Value[1].ToNJA() + ", " + item.Value[2].ToNJA() + ", " + item.Value[3].ToNJA() + " ),"); + foreach (var item in model.Value.Quaternion) + { + writer.WriteLine($" MKEYQ( {item.Key}, {item.Value[0].ToNJA()}, {item.Value[1].ToNJA()}, {item.Value[2].ToNJA()}, {item.Value[3].ToNJA()} ),"); + } + writer.WriteLine("END"); writer.WriteLine(); labels.Add(model.Value.QuaternionName); @@ -3011,10 +3036,10 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa writer.Write(MdataName.MakeIdentifier()); writer.WriteLine("[]"); writer.WriteLine("START"); - List mdats = new List(ModelParts); - for (int i = 0; i < ModelParts; i++) + var mdats = new List(ModelParts); + for (var i = 0; i < ModelParts; i++) { - List elems = new List(numpairs * 2); + var elems = new List(numpairs * 2); if (hasPos) { if (Models.ContainsKey(i) && Models[i].Position.Count > 0) @@ -3325,14 +3350,14 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa } if (elems.Count > 0) { - mdats.Add(" " + string.Join(", ", elems.ToArray())); + mdats.Add($" {string.Join(", ", elems.ToArray())}"); } else { mdats.Add(" NULL, NULL, NULL, 0, 0, 0"); } } - writer.WriteLine(string.Join("," + Environment.NewLine, mdats.ToArray()) + ","); + writer.WriteLine($"{string.Join($",{Environment.NewLine}", mdats.ToArray())},"); writer.WriteLine("END"); writer.WriteLine(); labels.Add(MdataName); @@ -3346,7 +3371,7 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa writer.WriteLine("MdataArray {0}, ", MdataName.MakeIdentifier()); writer.WriteLine("MFrameNum {0},", Frames); writer.WriteLine("MotionBit 0x{0},", ((int)flags).ToString("X")); - int interpol = numpairs > 0 ? numpairs : 2; + var interpol = numpairs > 0 ? numpairs : 2; switch (InterpolationMode) { case InterpolationMode.Spline: @@ -3371,20 +3396,20 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa writer.WriteLine("ACTION {0}[]", ActionName.MakeIdentifier()); writer.WriteLine("START"); writer.WriteLine("ObjectHead {0},", ObjectName.MakeIdentifier()); - writer.WriteLine("Motion " + Name.MakeIdentifier()); + writer.WriteLine($"Motion {Name.MakeIdentifier()}"); writer.WriteLine("END"); } writer.WriteLine(IsShapeMotion() ? "SHAPE_MOTION_END" : "MOTION_END"); writer.WriteLine(); writer.WriteLine("DEFAULT_START"); writer.WriteLine(); - writer.WriteLine("#ifndef DEFAULT_" + (IsShapeMotion() ? "SHAPE" : "MOTION") + "_NAME"); - writer.WriteLine("#define DEFAULT_" + (IsShapeMotion() ? "SHAPE" : "MOTION") + "_NAME " + Name.MakeIdentifier()); + writer.WriteLine($"#ifndef DEFAULT_{(IsShapeMotion() ? "SHAPE" : "MOTION")}_NAME"); + writer.WriteLine($"#define DEFAULT_{(IsShapeMotion() ? "SHAPE" : "MOTION")}_NAME {Name.MakeIdentifier()}"); writer.WriteLine("#endif"); if (!string.IsNullOrEmpty(ActionName)) { writer.WriteLine("#ifndef DEFAULT_ACTION_NAME"); - writer.WriteLine("#define DEFAULT_ACTION_NAME " + ActionName.MakeIdentifier()); + writer.WriteLine($"#define DEFAULT_ACTION_NAME {ActionName.MakeIdentifier()}"); writer.WriteLine("#endif"); } writer.WriteLine(); @@ -3393,8 +3418,8 @@ public void ToNJA(TextWriter writer, List labels = null, bool isDum = fa public byte[] WriteHeader(uint imageBase, uint modeladdr, Dictionary labels, out uint address) { - List result = new List(); - result.AddRange(GetBytes(imageBase, labels, out uint head2)); + var result = new List(); + result.AddRange(GetBytes(imageBase, labels, out var head2)); address = (uint)result.Count; result.AddRange(ByteConverter.GetBytes(modeladdr)); result.AddRange(ByteConverter.GetBytes(head2 + imageBase)); @@ -3413,12 +3438,12 @@ public byte[] WriteHeader(uint imageBase, uint modeladdr) public void Save(string filename, bool nometa = false) { - bool be = ByteConverter.BigEndian; + var be = ByteConverter.BigEndian; ByteConverter.BigEndian = false; - List file = new List(); + var file = new List(); file.AddRange(ByteConverter.GetBytes(SAANIMVer)); - Dictionary labels = new Dictionary(); - byte[] anim = GetBytes(0x14, labels, out uint addr); + var labels = new Dictionary(); + var anim = GetBytes(0x14, labels, out var addr); file.AddRange(ByteConverter.GetBytes(addr + 0x14)); file.Align(0x10); file.AddRange(ByteConverter.GetBytes(ModelParts | (ShortRot ? int.MinValue : 0))); @@ -3429,10 +3454,10 @@ public void Save(string filename, bool nometa = false) file.InsertRange(0xC, ByteConverter.GetBytes(file.Count + 4)); if (labels.Count > 0 && !nometa) { - List chunk = new List((labels.Count * 8) + 8); - int straddr = (labels.Count * 8) + 8; - List strbytes = new List(); - foreach (KeyValuePair label in labels) + var chunk = new List((labels.Count * 8) + 8); + var straddr = (labels.Count * 8) + 8; + var strbytes = new List(); + foreach (var label in labels) { chunk.AddRange(ByteConverter.GetBytes(label.Value)); chunk.AddRange(ByteConverter.GetBytes(straddr + strbytes.Count)); @@ -3447,7 +3472,7 @@ public void Save(string filename, bool nometa = false) file.AddRange(chunk); if (!string.IsNullOrEmpty(Description)) { - List chunkd = new List(Description.Length + 1); + var chunkd = new List(Description.Length + 1); chunkd.AddRange(Encoding.UTF8.GetBytes(Description)); chunkd.Add(0); chunkd.Align(4); @@ -3457,7 +3482,7 @@ public void Save(string filename, bool nometa = false) } if (!string.IsNullOrEmpty(ActionName)) { - List chunkd = new List(ActionName.Length + 1); + var chunkd = new List(ActionName.Length + 1); chunkd.AddRange(Encoding.UTF8.GetBytes(ActionName)); chunkd.Add(0); chunkd.Align(4); @@ -3467,7 +3492,7 @@ public void Save(string filename, bool nometa = false) } if (!string.IsNullOrEmpty(ObjectName)) { - List chunkd = new List(ObjectName.Length + 1); + var chunkd = new List(ObjectName.Length + 1); chunkd.AddRange(Encoding.UTF8.GetBytes(ObjectName)); chunkd.Add(0); chunkd.Align(4); @@ -3485,20 +3510,20 @@ public void Save(string filename, bool nometa = false) public class AnimModelData { - public Dictionary Position = new Dictionary(); - public Dictionary Rotation = new Dictionary(); - public Dictionary Scale = new Dictionary(); - public Dictionary Vector = new Dictionary(); - public Dictionary Vertex = new Dictionary(); - public Dictionary Normal = new Dictionary(); - public Dictionary Target = new Dictionary(); - public Dictionary Roll = new Dictionary(); - public Dictionary Angle = new Dictionary(); - public Dictionary Color = new Dictionary(); - public Dictionary Intensity = new Dictionary(); - public Dictionary Spot = new Dictionary(); - public Dictionary Point = new Dictionary(); - public Dictionary Quaternion = new Dictionary(); + public Dictionary Position = new(); + public Dictionary Rotation = new(); + public Dictionary Scale = new(); + public Dictionary Vector = new(); + public Dictionary Vertex = new(); + public Dictionary Normal = new(); + public Dictionary Target = new(); + public Dictionary Roll = new(); + public Dictionary Angle = new(); + public Dictionary Color = new(); + public Dictionary Intensity = new(); + public Dictionary Spot = new(); + public Dictionary Point = new(); + public Dictionary Quaternion = new(); public string PositionName; public string RotationName; public string ScaleName; @@ -3516,9 +3541,6 @@ public class AnimModelData public string PointName; public string QuaternionName; public int NbKeyframes; - public AnimModelData() - { - } public Vertex GetPosition(float frame) { @@ -3527,28 +3549,31 @@ public Vertex GetPosition(float frame) return Position[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Position.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Position.Keys) + { keys.Add(k); - for (int i = 0; i < Position.Count; i++) + } + + for (var i = 0; i < Position.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Position.Count - 1; i >= 0; i--) + for (var i = Position.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; - Vertex val = new Vertex() + var val = new Vertex { X = ((Position[f2z].X - Position[f1].X) / diff * (frame - f1)) + Position[f1].X, Y = ((Position[f2z].Y - Position[f1].Y) / diff * (frame - f1)) + Position[f1].Y, @@ -3564,28 +3589,31 @@ public Rotation GetRotation(float frame) return Rotation[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Rotation.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Rotation.Keys) + { keys.Add(k); - for (int i = 0; i < Rotation.Count; i++) + } + + for (var i = 0; i < Rotation.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Rotation.Count - 1; i >= 0; i--) + for (var i = Rotation.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; - Rotation val = new Rotation() + var val = new Rotation() { X = (int)Math.Round(((Rotation[f2z].X - Rotation[f1].X) / (double)diff * (frame - f1)) + Rotation[f1].X, MidpointRounding.AwayFromZero), Y = (int)Math.Round(((Rotation[f2z].Y - Rotation[f1].Y) / (double)diff * (frame - f1)) + Rotation[f1].Y, MidpointRounding.AwayFromZero), @@ -3601,28 +3629,31 @@ public Vertex GetScale(float frame) return Scale[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Scale.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Scale.Keys) + { keys.Add(k); - for (int i = 0; i < Scale.Count; i++) + } + + for (var i = 0; i < Scale.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Scale.Count - 1; i >= 0; i--) + for (var i = Scale.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; - Vertex val = new Vertex() + var val = new Vertex() { X = ((Scale[f2z].X - Scale[f1].X) / diff * (frame - f1)) + Scale[f1].X, Y = ((Scale[f2z].Y - Scale[f1].Y) / diff * (frame - f1)) + Scale[f1].Y, @@ -3638,28 +3669,31 @@ public Vertex GetVector(float frame) return Vector[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Vector.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Vector.Keys) + { keys.Add(k); - for (int i = 0; i < Vector.Count; i++) + } + + for (var i = 0; i < Vector.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Vector.Count - 1; i >= 0; i--) + for (var i = Vector.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; - Vertex val = new Vertex() + var val = new Vertex() { X = ((Vector[f2z].X - Vector[f1].X) / diff * (frame - f1)) + Vector[f1].X, Y = ((Vector[f2z].Y - Vector[f1].Y) / diff * (frame - f1)) + Vector[f1].Y, @@ -3675,29 +3709,32 @@ public Vertex[] GetVertex(float frame) return Vertex[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Vertex.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Vertex.Keys) + { keys.Add(k); - for (int i = 0; i < Vertex.Count; i++) + } + + for (var i = 0; i < Vertex.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Vertex.Count - 1; i >= 0; i--) + for (var i = Vertex.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; - Vertex[] result = new Vertex[Vertex[f1].Length]; - for (int i = 0; i < Vertex[f1].Length; i++) + var result = new Vertex[Vertex[f1].Length]; + for (var i = 0; i < Vertex[f1].Length; i++) result[i] = new Vertex() { X = ((Vertex[f2z][i].X - Vertex[f1][i].X) / diff * (frame - f1)) + Vertex[f1][i].X, @@ -3714,29 +3751,32 @@ public Vertex[] GetNormal(float frame) return Normal[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Normal.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Normal.Keys) + { keys.Add(k); - for (int i = 0; i < Normal.Count; i++) + } + + for (var i = 0; i < Normal.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Normal.Count - 1; i >= 0; i--) + for (var i = Normal.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; - Vertex[] result = new Vertex[Normal[f1].Length]; - for (int i = 0; i < Normal[f1].Length; i++) + var result = new Vertex[Normal[f1].Length]; + for (var i = 0; i < Normal[f1].Length; i++) result[i] = new Vertex() { X = ((Normal[f2z][i].X - Normal[f1][i].X) / diff * (frame - f1)) + Normal[f1][i].X, @@ -3753,28 +3793,31 @@ public Vertex GetTarget(float frame) return Target[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Target.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Target.Keys) + { keys.Add(k); - for (int i = 0; i < Target.Count; i++) + } + + for (var i = 0; i < Target.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Target.Count - 1; i >= 0; i--) + for (var i = Target.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; - Vertex val = new Vertex() + var val = new Vertex() { X = ((Target[f2z].X - Target[f1].X) / diff * (frame - f1)) + Target[f1].X, Y = ((Target[f2z].Y - Target[f1].Y) / diff * (frame - f1)) + Target[f1].Y, @@ -3790,26 +3833,29 @@ public int GetRoll(float frame) return Roll[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Roll.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Roll.Keys) + { keys.Add(k); - for (int i = 0; i < Roll.Count; i++) + } + + for (var i = 0; i < Roll.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Roll.Count - 1; i >= 0; i--) + for (var i = Roll.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; return (int)Math.Round((((Roll[f2z] - Roll[f1]) / (double)diff) * (frame - f1)) + Roll[f1], MidpointRounding.AwayFromZero); } @@ -3821,26 +3867,29 @@ public int GetAngle(float frame) return Angle[(int)Math.Floor(frame)]; } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Angle.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Angle.Keys) + { keys.Add(k); - for (int i = 0; i < Angle.Count; i++) + } + + for (var i = 0; i < Angle.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Angle.Count - 1; i >= 0; i--) + for (var i = Angle.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; return (int)Math.Round((((Angle[f2z] - Angle[f1]) / (double)diff) * (frame - f1)) + Angle[f1], MidpointRounding.AwayFromZero); } @@ -3851,37 +3900,40 @@ public Rotation GetQuaternion(float frame) { return RotFromQuat(FloatsAsQuat(Quaternion[(int)Math.Floor(frame)])); } - int f1 = 0; - int f2 = 0; - List keys = new List(); - foreach (int k in Quaternion.Keys) + var f1 = 0; + var f2 = 0; + var keys = new List(); + foreach (var k in Quaternion.Keys) + { keys.Add(k); - for (int i = 0; i < Quaternion.Count; i++) + } + + for (var i = 0; i < Quaternion.Count; i++) { if (keys[i] < frame) { f1 = keys[i]; } } - for (int i = Quaternion.Count - 1; i >= 0; i--) + for (var i = Quaternion.Count - 1; i >= 0; i--) { if (keys[i] > frame) { f2 = keys[i]; } } - int diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; + var diff = f2 != 0 ? (f2 - f1) : NbKeyframes - f1 + keys[0]; int f2z = f2 != 0 ? f2 : keys[0]; return RotFromQuat(System.Numerics.Quaternion.Slerp(FloatsAsQuat(Quaternion[f1]), FloatsAsQuat(Quaternion[f2z]), diff * (frame - f1))); } - public System.Numerics.Quaternion FloatsAsQuat(float[] ninjaQuats) + public Quaternion FloatsAsQuat(float[] ninjaQuats) { - return new System.Numerics.Quaternion(ninjaQuats[1], ninjaQuats[2], ninjaQuats[3], ninjaQuats[0]); + return new Quaternion(ninjaQuats[1], ninjaQuats[2], ninjaQuats[3], ninjaQuats[0]); } - public Rotation RotFromQuat(System.Numerics.Quaternion quat) + public Rotation RotFromQuat(Quaternion quat) { float X; float Y; @@ -3912,7 +3964,7 @@ public Rotation RotFromQuat(System.Numerics.Quaternion quat) } public static double CopySign(double valMain, double valSign) { - double final = Math.Abs(valMain); + var final = Math.Abs(valMain); if (valSign >= 0) { return final; @@ -3931,8 +3983,6 @@ public class Spotlight : IEquatable public int InsideAngle { get; set; } public int OutsideAngle { get; set; } - public Spotlight() { } - public Spotlight(byte[] file, int address) { Near = ByteConverter.ToSingle(file, address); @@ -3943,7 +3993,7 @@ public Spotlight(byte[] file, int address) public byte[] GetBytes() { - List result = new List(16); + var result = new List(16); result.AddRange(ByteConverter.GetBytes(Near)); result.AddRange(ByteConverter.GetBytes(Far)); result.AddRange(ByteConverter.GetBytes(InsideAngle)); @@ -3965,4 +4015,4 @@ public enum ChunkTypes : uint ObjectName = 0x4F424A4E, End = 0x444E45 } -} \ No newline at end of file +}