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); }
public void AddDataReference(XofFileEnumData child) { if (child == null) { throw new ArgumentNullException(nameof(child)); } AddDataReference(child.GetName(), child.GetId()); }
public XofFileSaveData AddData(XofFileEnumData child) { if (child == null) { throw new ArgumentNullException(nameof(child)); } return(AddData(child.GetTemplateType(), child.GetName(), child.GetId(), child.GetData())); }
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 XAnimationKey ReadAnimationKey(XofFileEnumData enumData) { var animationKey = new XAnimationKey { Name = enumData.GetName() }; byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { animationKey.KeyType = (XAnimationKeyType)reader.ReadInt32(); int nKeys = reader.ReadInt32(); animationKey.Keys.Capacity = nKeys; for (int index = 0; index < nKeys; index++) { int time = reader.ReadInt32(); int valuesCount = reader.ReadInt32(); var values = new float[valuesCount]; for (int i = 0; i < valuesCount; i++) { values[i] = reader.ReadSingle(); } animationKey.Keys.Add(Tuple.Create(time, values)); } if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } return(animationKey); }
private static XSkinWeights ReadSkinWeights(XofFileEnumData enumData) { var skin = new XSkinWeights { Name = enumData.GetName() }; byte[] data = enumData.GetData(); using (var ms = new MemoryStream(data, false)) using (var reader = new BinaryReader(ms)) { skin.TransformNodeName = ReadString(reader); int nWeights = reader.ReadInt32(); skin.VertexIndices.Capacity = nWeights; skin.Weights.Capacity = nWeights; for (int i = 0; i < nWeights; i++) { skin.VertexIndices.Add(reader.ReadInt32()); } for (int i = 0; i < nWeights; i++) { skin.Weights.Add(reader.ReadSingle()); } skin.MatrixOffset = ReadMatrix4x4(reader); if (ms.Position != ms.Length) { throw new InvalidDataException(); } } if (enumData.GetChildrenCount() != 0) { throw new InvalidDataException(); } return(skin); }
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); }