public void Parse(BinaryReaderExt r) { r.BigEndian = true; var start = r.Position; var sectionCount = r.ReadInt32(); var sectionHeaderLength = r.ReadInt32(); PlaySpeed = r.ReadSingle(); EndTime = r.ReadSingle(); for (int j = 0; j < sectionCount; j++) { MOT_JOINT joint = new MOT_JOINT(); Joints.Add(joint); joint.Flag1 = r.ReadByte(); joint.Flag2 = r.ReadByte(); joint.TrackFlag = (MOT_FLAGS)r.ReadUInt16(); joint.BoneID = r.ReadInt16(); var floatCount = r.ReadInt16(); joint.MaxTime = r.ReadSingle(); joint.Unknown = r.ReadInt32(); var offset1 = r.ReadUInt32() + start; var offset2 = r.ReadUInt32() + start; var offset3 = r.ReadUInt32() + start; var offset4 = r.ReadUInt32() + start; if (offset3 != start) { throw new NotSupportedException("Section 3 detected"); } if (offset4 != start) { throw new NotSupportedException("Section 4 detected"); } var temp = r.Position; for (uint k = 0; k < floatCount; k++) { MOT_KEY key = new MOT_KEY(); r.Seek(offset1 + 4 * k); key.Time = r.ReadSingle(); if (offset2 != start) { r.Seek(offset2 + 8 * k); key.X = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); key.Y = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); key.Z = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); key.W = BitConverter.ToSingle(BitConverter.GetBytes(((r.ReadByte() & 0xFF) << 24) | ((r.ReadByte() & 0xFF) << 16)), 0); } joint.Keys.Add(key); } r.Seek(temp); } }
private void ParseXML(XmlReader r) { const int STATE_ENTER = 0; const int STATE_JOINTS = 1; const int STATE_KEYS = 2; int state = STATE_ENTER; float time = Single.NegativeInfinity; while (r.Read()) { switch (r.NodeType) { case XmlNodeType.Element: { if (state == STATE_ENTER) { if (r.Name != "MOT") { throw new NotSupportedException("Expected Element \"MOT\""); } string strPlaySpeed = r.GetAttribute("PlaySpeed"); string strEndTime = r.GetAttribute("EndTime"); if (!float.TryParse(strPlaySpeed, out PlaySpeed)) { throw new NotSupportedException("Could not parse attribute \"PlaySpeed\""); } if (!float.TryParse(strEndTime, out EndTime)) { throw new NotSupportedException("Could not parse attribute \"EndTime\""); } state = STATE_JOINTS; } else if (state == STATE_JOINTS) { if (r.Name == "Joint") { var j = new MOT_JOINT(); string strFlag1 = r.GetAttribute("Flag1"); string strFlag2 = r.GetAttribute("Flag2"); string strTrackFlag = r.GetAttribute("TrackFlag"); string strBoneID = r.GetAttribute("BoneID"); string strMaxTime = r.GetAttribute("MaxTime"); string strUnknown = r.GetAttribute("Unknown"); if (!byte.TryParse(strFlag1, out j.Flag1)) { throw new NotSupportedException("Could not parse attribute \"Flag1\""); } if (!byte.TryParse(strFlag2, out j.Flag2)) { throw new NotSupportedException("Could not parse attribute \"Flag2\""); } if (!Enum.TryParse <MOT_FLAGS>(strTrackFlag, out j.TrackFlag)) { throw new NotSupportedException("Could not parse attribute \"Flag2\""); } if (!short.TryParse(strBoneID, out j.BoneID)) { throw new NotSupportedException("Could not parse attribute \"BoneID\""); } if (!float.TryParse(strMaxTime, out j.MaxTime)) { throw new NotSupportedException("Could not parse attribute \"MaxTime\""); } if (!int.TryParse(strUnknown, out j.Unknown)) { throw new NotSupportedException("Could not parse attribute \"Unknown\""); } Joints.Add(j); } else if (r.Name == "Key") { state = STATE_KEYS; } } if (state == STATE_KEYS) { if (r.Name == "Key") { string strTime = r.GetAttribute("Time"); if (!float.TryParse(strTime, out time)) { throw new NotSupportedException("Could not parse attribute \"Time\""); } } else if (r.Name == "Joint") { if (Single.IsNegativeInfinity(time)) { throw new NotSupportedException("No Key element defined"); } int index; string indexStr; float X, Y, Z, W; string xStr, yStr, zStr, wStr; indexStr = r.GetAttribute("Index"); if (!int.TryParse(indexStr, out index)) { throw new NotSupportedException("Unable to parse Index attribute"); } xStr = r.GetAttribute("X"); if (!float.TryParse(xStr, out X)) { throw new NotSupportedException("Unable to parse X attribute"); } yStr = r.GetAttribute("Y"); if (!float.TryParse(yStr, out Y)) { throw new NotSupportedException("Unable to parse Y attribute"); } zStr = r.GetAttribute("Z"); if (!float.TryParse(zStr, out Z)) { throw new NotSupportedException("Unable to parse Z attribute"); } wStr = r.GetAttribute("W"); if (!float.TryParse(wStr, out W)) { throw new NotSupportedException("Unable to parse W attribute"); } var key = new MOT_KEY(); key.Time = time; key.X = X; key.Y = Y; key.Z = Z; key.W = W; Joints[index].Keys.Add(key); } } } break; } } }