private static void ReadAnimationOptions(XAnimation animation, XofFileEnumData enumData) { byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { animation.OpenClosedOption = reader.ReadInt32(); animation.PositionQualityOption = reader.ReadInt32(); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } }
private static void ReadXSkinMeshHeader(XMesh mesh, XofFileEnumData enumData) { byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { mesh.MaxSkinWeightsPerVertex = reader.ReadInt16(); mesh.MaxSkinWeightsPerFace = reader.ReadInt16(); mesh.BonesCount = reader.ReadInt16(); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } }
private static void ConvertData(XofFileEnumData enumData, XofFileSaveData saveData) { int count = enumData.GetChildrenCount(); for (int i = 0; i < count; i++) { using (var child = enumData.GetChild(i)) { if (child.IsReference()) { saveData.AddDataReference(child); } else { using (var saveChild = saveData.AddData(child)) { ConvertData(child, saveChild); } } } } }
private static void ReadFrameMeshName(XFrame frame, XofFileEnumData enumData) { byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { int renderPass = reader.ReadInt32(); string fileName = ReadString(reader); frame.MeshesNames.Add(renderPass, fileName); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } }
private static void ReadHeader(XFile file, XofFileEnumData enumData) { byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { short majorVersion = reader.ReadInt16(); short minorVersion = reader.ReadInt16(); file.FileVersion = new Version(majorVersion, minorVersion); file.FileFlags = reader.ReadInt32(); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } }
private static string ReadTextureFilename(XofFileEnumData enumData) { string filename; byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { filename = ReadString(reader); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } return(filename); }
private static void ReadDeclData(XMesh mesh, XofFileEnumData enumData) { byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { int nElements = reader.ReadInt32(); mesh.VertexElements.Capacity = nElements; for (int index = 0; index < nElements; index++) { mesh.VertexElements.Add(ReadVertexElement(reader)); } int nDWords = reader.ReadInt32(); var elementsData = new uint[nDWords]; for (int i = 0; i < nDWords; i++) { elementsData[i] = reader.ReadUInt32(); } mesh.VertexElementsData = elementsData; if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } }
private static int ReadAnimTicksPerSecond(XofFileEnumData enumData) { int animTicksPerSecond; byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { animTicksPerSecond = reader.ReadInt32(); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } return(animTicksPerSecond); }
private static XAnimation ReadAnimation(XofFileEnumData enumData) { var animaton = new XAnimation { Name = enumData.GetName() }; byte[] data = enumData.GetData(); if (data.Length != 0) { throw new InvalidDataException(); } bool frameRead = false; bool animationOptionsRead = false; int childrenCount = enumData.GetChildrenCount(); for (int childIndex = 0; childIndex < childrenCount; childIndex++) { using (var child = enumData.GetChild(childIndex)) { Guid type = child.GetTemplateType(); if (type == XofFileDefaultTemplates.FrameId) { if (frameRead) { throw new InvalidDataException(); } if (!child.IsReference()) { throw new InvalidDataException(); } animaton.FrameReference = child.GetName(); frameRead = true; } else if (type == XofFileDefaultTemplates.AnimationKeyId) { animaton.Keys.Add(ReadAnimationKey(child)); } else if (type == XofFileDefaultTemplates.AnimationOptionsId) { if (animationOptionsRead) { throw new InvalidDataException(); } ReadAnimationOptions(animaton, child); animationOptionsRead = true; } else { throw new InvalidDataException(); } } } return(animaton); }
private static XFrame ReadFrame(XofFileEnumData enumData) { var frame = new XFrame { Name = enumData.GetName() }; byte[] data = enumData.GetData(); if (data.Length != 0) { throw new InvalidDataException(); } bool frameTransformMatrixRead = false; bool frameCameraRead = false; int childrenCount = enumData.GetChildrenCount(); for (int childIndex = 0; childIndex < childrenCount; childIndex++) { using (var child = enumData.GetChild(childIndex)) { Guid type = child.GetTemplateType(); if (type == XofFileDefaultTemplates.FrameTransformMatrixId) { if (frameTransformMatrixRead) { throw new InvalidDataException(); } ReadFrameTransformMatrix(frame, child); frameTransformMatrixRead = true; } else if (type == XofFileDefaultTemplates.FrameId) { frame.Frames.Add(ReadFrame(child)); } else if (type == XofFileDefaultTemplates.MeshId) { frame.Meshes.Add(ReadMesh(child)); } else if (type == XofFileDefaultTemplates.FrameMeshNameId) { ReadFrameMeshName(frame, child); } else if (type == XofFileDefaultTemplates.FrameCameraId) { if (frameCameraRead) { throw new InvalidDataException(); } ReadFrameCamera(frame, child); frameCameraRead = true; } else { throw new InvalidDataException(); } } } return(frame); }
private static XMaterial ReadMaterial(XofFileEnumData enumData) { var material = new XMaterial { Name = enumData.GetName() }; if (enumData.IsReference()) { material.IsReference = true; return(material); } byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { material.FaceColor = ReadColorRgba(reader); material.Power = reader.ReadSingle(); material.SpecularColor = ReadColorRgb(reader); material.EmissiveColor = ReadColorRgb(reader); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } bool textureFilenameRead = false; bool effectInstanceRead = false; int childrenCount = enumData.GetChildrenCount(); for (int childIndex = 0; childIndex < childrenCount; childIndex++) { using (var child = enumData.GetChild(childIndex)) { Guid type = child.GetTemplateType(); if (type == XofFileDefaultTemplates.TextureFilenameId) { if (textureFilenameRead) { throw new InvalidDataException(); } material.Filename = ReadTextureFilename(child); textureFilenameRead = true; } else if (type == XofFileDefaultTemplates.EffectInstanceId) { if (effectInstanceRead) { throw new InvalidDataException(); } material.EffectInstance = ReadEffectInstance(child); effectInstanceRead = true; } else { throw new InvalidDataException(); } } } return(material); }
private static XMesh ReadMesh(XofFileEnumData enumData) { var mesh = new XMesh { Name = enumData.GetName() }; byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { int nVertices = reader.ReadInt32(); mesh.Vertices.Capacity = nVertices; for (int index = 0; index < nVertices; index++) { mesh.Vertices.Add(ReadVector(reader)); } int nFaces = reader.ReadInt32(); mesh.FacesIndices.Capacity = nFaces; for (int index = 0; index < nFaces; index++) { int indicesCount = reader.ReadInt32(); var vertices = new List <int>(indicesCount); for (int i = 0; i < indicesCount; i++) { vertices.Add(reader.ReadInt32()); } mesh.FacesIndices.Add(vertices); } if (ms.Position != ms.Length) { throw new InvalidDataException(); } } bool meshNormalsRead = false; bool meshTextureCoordsRead = false; bool meshMaterialListRead = false; bool vertexDuplicationIndicesRead = false; bool fvfDataRead = false; bool meshVertexColorsIdRead = false; bool declDataRead = false; bool xSkinMeshHeaderRead = false; int childrenCount = enumData.GetChildrenCount(); for (int childIndex = 0; childIndex < childrenCount; childIndex++) { using (var child = enumData.GetChild(childIndex)) { Guid type = child.GetTemplateType(); if (type == XofFileDefaultTemplates.MeshNormalsId) { if (meshNormalsRead) { mesh.Normals.Clear(); mesh.FacesNormalsIndices.Clear(); //throw new InvalidDataException(); } ReadMeshNormals(mesh, child); meshNormalsRead = true; } else if (type == XofFileDefaultTemplates.MeshTextureCoordsId) { if (meshTextureCoordsRead) { throw new InvalidDataException(); } ReadMeshTextureCoords(mesh, child); meshTextureCoordsRead = true; } else if (type == XofFileDefaultTemplates.MeshMaterialListId) { if (meshMaterialListRead) { throw new InvalidDataException(); } ReadMeshMaterialList(mesh, child); meshMaterialListRead = true; } else if (type == XofFileDefaultTemplates.VertexDuplicationIndicesId) { if (vertexDuplicationIndicesRead) { throw new InvalidDataException(); } ReadVertexDuplicationIndices(mesh, child); vertexDuplicationIndicesRead = true; } else if (type == XofFileDefaultTemplates.FVFDataId) { if (fvfDataRead) { throw new InvalidDataException(); } ReadFVFData(mesh, child); fvfDataRead = true; } else if (type == XofFileDefaultTemplates.MeshVertexColorsId) { if (meshVertexColorsIdRead) { throw new InvalidDataException(); } ReadMeshVertexColors(mesh, child); meshVertexColorsIdRead = true; } else if (type == XofFileDefaultTemplates.DeclDataId) { if (declDataRead) { throw new InvalidDataException(); } ReadDeclData(mesh, child); declDataRead = true; } else if (type == XofFileDefaultTemplates.XSkinMeshHeaderId) { if (xSkinMeshHeaderRead) { throw new InvalidDataException(); } ReadXSkinMeshHeader(mesh, child); xSkinMeshHeaderRead = true; } else if (type == XofFileDefaultTemplates.SkinWeightsId) { mesh.SkinWeights.Add(ReadSkinWeights(child)); } else { throw new InvalidDataException(); } } } return(mesh); }