private static XAnimationSet ReadAnimationSet(XofFileEnumData enumData) { var animationSet = new XAnimationSet { Name = enumData.GetName() }; byte[] data = enumData.GetData(); if (data.Length != 0) { throw new InvalidDataException(); } int childrenCount = enumData.GetChildrenCount(); for (int childIndex = 0; childIndex < childrenCount; childIndex++) { using (var child = enumData.GetChild(childIndex)) { Guid type = child.GetTemplateType(); if (type == XofFileDefaultTemplates.AnimationId) { animationSet.Animations.Add(ReadAnimation(child)); } else { throw new InvalidDataException(); } } } return(animationSet); }
private static void ReadMeshMaterialList(XMesh mesh, XofFileEnumData enumData) { byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { int nMaterials = reader.ReadInt32(); mesh.Materials.Capacity = nMaterials; int nFaceIndexes = reader.ReadInt32(); mesh.MaterialsFacesIndices.Capacity = nFaceIndexes; for (int index = 0; index < nFaceIndexes; index++) { mesh.MaterialsFacesIndices.Add(reader.ReadInt32()); } if (ms.Position != ms.Length) { throw new InvalidDataException(); } } int materialsCount = mesh.Materials.Capacity; int childrenCount = enumData.GetChildrenCount(); for (int childIndex = 0; childIndex < childrenCount; childIndex++) { using (var child = enumData.GetChild(childIndex)) { Guid type = child.GetTemplateType(); if (type == XofFileDefaultTemplates.MaterialId) { mesh.Materials.Add(ReadMaterial(child)); } else { throw new InvalidDataException(); } } } if (mesh.Materials.Count != materialsCount) { throw new InvalidDataException(); } }
private static XEffectInstance ReadEffectInstance(XofFileEnumData enumData) { var effect = new XEffectInstance { Name = enumData.GetName() }; byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { effect.EffectFilename = ReadString(reader); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } int childrenCount = enumData.GetChildrenCount(); for (int childIndex = 0; childIndex < childrenCount; childIndex++) { using (var child = enumData.GetChild(childIndex)) { Guid type = child.GetTemplateType(); if (type == XofFileDefaultTemplates.EffectParamDWordId) { effect.IntegerParameters.Add(ReadEffectParamDWord(child)); } else if (type == XofFileDefaultTemplates.EffectParamFloatsId) { effect.FloatParameters.Add(ReadEffectParamFloats(child)); } else if (type == XofFileDefaultTemplates.EffectParamStringId) { effect.StringParameters.Add(ReadEffectParamString(child)); } else { throw new InvalidDataException(); } } } return(effect); }
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 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); }