public AssetEntry(EndianAwareBinaryReader reader, Layer layer) { Layer = layer; Size = reader.ReadInt32(); RelativeDataOffset = reader.ReadInt32(); DataSize = reader.ReadInt32(); Unknown1 = reader.ReadInt32(); AssetID = reader.ReadUInt64(); // this might be a hash of the filepath? AssetType = (AssetType)reader.ReadUInt32(); Unknown2 = reader.ReadInt16(); Unknown3 = reader.ReadInt16(); }
/// <summary> /// Reads the method header from the instruction stream. /// </summary> /// <param name="reader">The reader used to decode the instruction stream.</param> /// <returns></returns> private MethodHeader ReadMethodHeader(EndianAwareBinaryReader reader) { MethodHeader header = new MethodHeader(); // Read first byte header.Flags = (MethodFlags)reader.ReadByte(); // Check least significant 2 bits switch (header.Flags & MethodFlags.HeaderMask) { case MethodFlags.TinyFormat: header.CodeSize = ((uint)(header.Flags & MethodFlags.TinyCodeSizeMask) >> 2); header.Flags &= MethodFlags.HeaderMask; break; case MethodFlags.FatFormat: // Read second byte of flags header.Flags = (MethodFlags)(reader.ReadByte() << 8 | (byte)header.Flags); if (MethodFlags.ValidHeader != (header.Flags & MethodFlags.HeaderSizeMask)) throw new InvalidDataException(@"Invalid method header."); header.MaxStack = reader.ReadUInt16(); header.CodeSize = reader.ReadUInt32(); header.LocalsSignature = new Token(reader.ReadUInt32()); // ReadStandAloneSigRow break; default: throw new InvalidDataException(@"Invalid method header while trying to decode " + this.methodCompiler.Method.ToString() + ". (Flags = " + header.Flags.ToString("X") + ", Rva = " + this.methodCompiler.Method.Rva + ")"); } // Are there sections following the code? if (MethodFlags.MoreSections != (header.Flags & MethodFlags.MoreSections)) return header; // Yes, seek to them and process those sections long codepos = reader.BaseStream.Position; // Seek to the end of the code... long dataSectPos = codepos + header.CodeSize; if (0 != (dataSectPos & 3)) dataSectPos += (4 - (dataSectPos % 4)); reader.BaseStream.Position = dataSectPos; // Read all headers, so the IL decoder knows how to handle these... byte flags; do { flags = reader.ReadByte(); bool isFat = (0x40 == (flags & 0x40)); int length; int blocks; if (isFat) { byte a = reader.ReadByte(); byte b = reader.ReadByte(); byte c = reader.ReadByte(); length = (c << 24) | (b << 16) | a; blocks = (length - 4) / 24; } else { length = reader.ReadByte(); blocks = (length - 4) / 12; /* Read & skip the padding. */ reader.ReadInt16(); } Debug.Assert(0x01 == (flags & 0x3F), @"Unsupported method data section."); // Read the clause for (int i = 0; i < blocks; i++) { ExceptionHandlingClause clause = new ExceptionHandlingClause(); clause.Read(reader, isFat); methodCompiler.ExceptionClauseHeader.AddClause(clause); } } while (0x80 == (flags & 0x80)); reader.BaseStream.Position = codepos; return header; }
public Vector3 Scale; //!< Scaling information #endregion Fields #region Methods /// <summary> /// Load a mesh from a stream /// </summary> /// <param name="filename">Filename and path of the file containing the reference mesh</param> public virtual void LoadMesh(string filename) { using(FileStream meshStream = new FileStream(filename, FileMode.Open, FileAccess.Read)) using (EndianAwareBinaryReader reader = new EndianAwareBinaryReader(meshStream)) { Header = TrimAt0(reader.ReadString(24)); if (!String.Equals(Header, MeshHeader)) throw new FileLoadException("Unrecognized mesh format"); // Populate base mesh parameters HasWeights = (reader.ReadByte() != 0); HasDetailTexCoords = (reader.ReadByte() != 0); Position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); RotationAngles = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); RotationOrder = reader.ReadByte(); Scale = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); NumFaces = reader.ReadUInt16(); Faces = new Face[NumFaces]; for (int i = 0; i < NumFaces; i++) Faces[i].Indices = new[] { reader.ReadInt16(), reader.ReadInt16(), reader.ReadInt16() }; } }
/// <summary> /// Load the mesh from a stream /// </summary> /// <param name="filename">The filename and path of the file containing the mesh data</param> public virtual void LoadMesh(string filename) { using(FileStream meshData = new FileStream(filename, FileMode.Open, FileAccess.Read)) using (EndianAwareBinaryReader reader = new EndianAwareBinaryReader(meshData)) { Header = TrimAt0(reader.ReadString(24)); if (!String.Equals(Header, MeshHeader)) throw new FileLoadException("Unrecognized mesh format"); // Populate base mesh parameters HasWeights = (reader.ReadByte() != 0); HasDetailTexCoords = (reader.ReadByte() != 0); Position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); RotationAngles = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); /* RotationOrder = */ reader.ReadByte(); Scale = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); // Populate the vertex array NumVertices = reader.ReadUInt16(); Vertices = new Vertex[NumVertices]; for (int i = 0; i < NumVertices; i++) Vertices[i].Coord = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); for (int i = 0; i < NumVertices; i++) Vertices[i].Normal = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); for (int i = 0; i < NumVertices; i++) Vertices[i].BiNormal = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); for (int i = 0; i < NumVertices; i++) Vertices[i].TexCoord = new Vector2(reader.ReadSingle(), reader.ReadSingle()); if (HasDetailTexCoords) { for (int i = 0; i < NumVertices; i++) Vertices[i].DetailTexCoord = new Vector2(reader.ReadSingle(), reader.ReadSingle()); } if (HasWeights) { for (int i = 0; i < NumVertices; i++) Vertices[i].Weight = reader.ReadSingle(); } NumFaces = reader.ReadUInt16(); Faces = new Face[NumFaces]; for (int i = 0; i < NumFaces; i++) Faces[i].Indices = new[] { reader.ReadInt16(), reader.ReadInt16(), reader.ReadInt16() }; if (HasWeights) { NumSkinJoints = reader.ReadUInt16(); SkinJoints = new string[NumSkinJoints]; for (int i = 0; i < NumSkinJoints; i++) { SkinJoints[i] = TrimAt0(reader.ReadString(64)); } } else { NumSkinJoints = 0; SkinJoints = new string[0]; } // Grab morphs List<Morph> morphs = new List<Morph>(); string morphName = TrimAt0(reader.ReadString(64)); while (morphName != MorphFooter) { if (reader.BaseStream.Position + 48 >= reader.BaseStream.Length) throw new FileLoadException("Encountered end of file while parsing morphs"); Morph morph = new Morph(); morph.Name = morphName; morph.NumVertices = reader.ReadInt32(); morph.Vertices = new MorphVertex[morph.NumVertices]; for (int i = 0; i < morph.NumVertices; i++) { morph.Vertices[i].VertexIndex = reader.ReadUInt32(); morph.Vertices[i].Coord = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); morph.Vertices[i].Normal = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); morph.Vertices[i].BiNormal = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); morph.Vertices[i].TexCoord = new Vector2(reader.ReadSingle(), reader.ReadSingle()); } morphs.Add(morph); // Grab the next name morphName = TrimAt0(reader.ReadString(64)); } Morphs = morphs.ToArray(); // Check if there are remaps or if we're at the end of the file if (reader.BaseStream.Position < reader.BaseStream.Length - 1) { NumRemaps = reader.ReadInt32(); VertexRemaps = new VertexRemap[NumRemaps]; for (int i = 0; i < NumRemaps; i++) { VertexRemaps[i].RemapSource = reader.ReadInt32(); VertexRemaps[i].RemapDestination = reader.ReadInt32(); } } else { NumRemaps = 0; VertexRemaps = new VertexRemap[0]; } } // uncompress the skin weights if (Skeleton != null) { // some meshes aren't weighted, which doesn't make much sense. // we check for left and right eyeballs, and assign them a 100% // to their respective bone List<string> expandedJointList = Skeleton.BuildExpandedJointList(SkinJoints); if (expandedJointList.Count == 0) { if (Name == "eyeBallLeftMesh") { expandedJointList.AddRange(new[] { "mEyeLeft", "mSkull" }); } else if (Name == "eyeBallRightMesh") { expandedJointList.AddRange(new[] { "mEyeRight", "mSkull" }); } } if (expandedJointList.Count > 0) ExpandCompressedSkinWeights(expandedJointList); } }
public object Deserialize(EndianAwareBinaryReader reader, SerializedType serializedType, int?length = null) { int?effectiveLength = null; var typeParent = TypeNode.Parent as TypeNode; if (length != null) { effectiveLength = length.Value; } else if (TypeNode.FieldLengthBinding != null) { object lengthValue = TypeNode.FieldLengthBinding.GetValue(this); effectiveLength = Convert.ToInt32(lengthValue); } else if (typeParent != null && typeParent.ItemLengthBinding != null) { object lengthValue = typeParent.ItemLengthBinding.GetValue((ValueNode)Parent); effectiveLength = Convert.ToInt32(lengthValue); } else if (TypeNode.FieldCountBinding != null) { object countValue = TypeNode.FieldCountBinding.GetValue(this); effectiveLength = Convert.ToInt32(countValue); } else if (serializedType == SerializedType.ByteArray || serializedType == SerializedType.SizedString) { checked { effectiveLength = (int)_remainder; } } object value; switch (serializedType) { case SerializedType.Int1: value = reader.ReadSByte(); break; case SerializedType.UInt1: value = reader.ReadByte(GetBitSize()); break; case SerializedType.Int2: value = reader.ReadInt16(); break; case SerializedType.UInt2: value = reader.ReadUInt16(); break; case SerializedType.Int4: value = reader.ReadInt32(); break; case SerializedType.UInt4: value = reader.ReadUInt32(); break; case SerializedType.Int8: value = reader.ReadInt64(); break; case SerializedType.UInt8: value = reader.ReadUInt64(); break; case SerializedType.Float4: value = reader.ReadSingle(); break; case SerializedType.Float8: value = reader.ReadDouble(); break; case SerializedType.ByteArray: { Debug.Assert(effectiveLength != null, "effectiveLength != null"); value = reader.ReadBytes(effectiveLength.Value); break; } case SerializedType.NullTerminatedString: { byte[] data = ReadNullTerminatedString(reader).ToArray(); value = Encoding.GetString(data, 0, data.Length); break; } case SerializedType.SizedString: { Debug.Assert(effectiveLength != null, "effectiveLength != null"); byte[] data = reader.ReadBytes(effectiveLength.Value); value = Encoding.GetString(data, 0, data.Length).TrimEnd('\0'); break; } case SerializedType.LengthPrefixedString: { value = reader.ReadString(); break; } default: throw new NotSupportedException(); } return(value); }
/// <summary> /// Reads the method header from the instruction stream. /// </summary> /// <param name="reader">The reader used to decode the instruction stream.</param> /// <returns></returns> public MethodHeader(EndianAwareBinaryReader reader) { Clauses = new List <ExceptionHandlingClause>(); // Read first byte Flags = (MethodFlags)reader.ReadByte(); // Check least significant 2 bits switch (Flags & MethodFlags.HeaderMask) { case MethodFlags.TinyFormat: CodeSize = ((int)(Flags & MethodFlags.TinyCodeSizeMask) >> 2); Flags &= MethodFlags.HeaderMask; break; case MethodFlags.FatFormat: // Read second byte of flags Flags = (MethodFlags)(reader.ReadByte() << 8 | (byte)Flags); if (MethodFlags.ValidHeader != (Flags & MethodFlags.HeaderSizeMask)) { throw new CompilerException("Invalid method "); } MaxStack = reader.ReadUInt16(); CodeSize = reader.ReadInt32(); LocalVarSigTok = new Token(reader.ReadUInt32()); // ReadStandAloneSigRow break; default: throw new CompilerException("Invalid method header"); } // Are there sections following the code? if (MethodFlags.MoreSections != (Flags & MethodFlags.MoreSections)) { return; } // Yes, seek to them and process those sections long codepos = reader.BaseStream.Position; // Seek to the end of the code... long dataSectPos = codepos + CodeSize; if (0 != (dataSectPos & 3)) { dataSectPos += (4 - (dataSectPos % 4)); } reader.BaseStream.Position = dataSectPos; // Read all headers, so the IL decoder knows how to handle these... byte flags; do { flags = reader.ReadByte(); bool isFat = (0x40 == (flags & 0x40)); int length; int blocks; if (isFat) { byte a = reader.ReadByte(); byte b = reader.ReadByte(); byte c = reader.ReadByte(); length = (c << 24) | (b << 16) | a; blocks = (length - 4) / 24; } else { length = reader.ReadByte(); blocks = (length - 4) / 12; /* Read & skip the padding. */ reader.ReadInt16(); } Debug.Assert(0x01 == (flags & 0x3F), "Unsupported method data section."); // Read the clause for (int i = 0; i < blocks; i++) { ExceptionHandlingClause clause = new ExceptionHandlingClause(); clause.Read(reader, isFat); Clauses.Add(clause); } }while (0x80 == (flags & 0x80)); reader.BaseStream.Position = codepos; }