Example #1
0
        public int Unknown1; // only used by padding section with 0 size?

        public AssetSection(EndianAwareBinaryReader reader)
        {
            Type        = (AssetSectionType)reader.ReadInt32();
            StartOffset = reader.ReadInt32();
            Size        = reader.ReadInt32();
            Unknown1    = reader.ReadInt32();
        }
Example #2
0
        public static TypeHeader Deserialize(EndianAwareBinaryReader reader)
        {
            var typeId   = reader.ReadInt32();
            var typeName = Util.ReadLengthPrefixedString(reader);
            var containsExtraDataByte = reader.ReadByte();
            var instanceCount         = reader.ReadInt32();
            var referentArray         = Util.ReadReferentArray(reader, instanceCount);
            var extraData             = (containsExtraDataByte != 0) ? reader.ReadBytes(instanceCount) : null;

            return(new TypeHeader(typeName, typeId, referentArray, extraData));
        }
Example #3
0
        public AssetLayer(EndianAwareBinaryReader reader, Layer layer)
        {
            Layer = layer;

            Entries      = new List <AssetEntry>();
            Size         = reader.ReadInt32();
            SectionCount = reader.ReadInt32();
            Unknown1     = reader.ReadInt32();
            Sections     = Utils.CreateArray <AssetSection>(SectionCount, reader);

            ReadEntries(reader);
        }
Example #4
0
        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();
        }
Example #5
0
        public void ReadEntries(EndianAwareBinaryReader reader)
        {
            var offset = Layer.PageOffset << 11;

            reader.Seek(offset, SeekOrigin.Begin);

            // sizes for each entry
            var entrySizes = new int[AmountOfEntries];

            for (var i = 0; i < AmountOfEntries; i++)
            {
                entrySizes[i] = reader.ReadInt32();
            }

            // skip padding
            reader.Seek(offset + Size, SeekOrigin.Begin);

            // read directories
            Entries.EnsureCapacity(AmountOfEntries);
            for (var i = 0; i < AmountOfEntries; i++)
            {
                var entry = new DirectoryEntry(reader, entrySizes[i]);
                Entries.Add(entry.AssetID, entry);
            }
        }
Example #6
0
        public static string ReadLengthPrefixedString(EndianAwareBinaryReader reader)
        {
            var stringLength = reader.ReadInt32();
            var stringBytes  = reader.ReadBytes(stringLength);

            return(RobloxEncoding.GetString(stringBytes));
        }
Example #7
0
        public Section(EndianAwareBinaryReader reader)
        {
            Offset          = reader.BaseStream.Position;
            AssetLayers     = new List <AssetLayer>();
            DirectoryLayers = new List <DirectoryLayer>();

            reader.AssertTag(BlockType.SECT);
            LayerCount         = reader.ReadInt32();
            Unknown1           = reader.ReadInt32();
            SizeOfLayerData    = reader.ReadInt32();
            SizeOfName         = reader.ReadInt32();
            SizeOfChunk        = reader.ReadInt32();
            SizeOfSubLayerData = reader.ReadInt32();
            Unknown2           = reader.ReadInt32();
            Layers             = Utils.CreateArray <Layer>(LayerCount, reader);
            SectionName        = reader.ReadCString();

            // skip padding
            reader.Seek(Offset + SizeOfChunk, SeekOrigin.Begin);

            // read sub layers
            for (var i = 0; i < LayerCount; i++)
            {
                ReadLayer(reader, Layers[i]);
            }

            // append internal names to asset entries
            UpdateAssetEntries();
        }
Example #8
0
        public readonly int Unknown7; // always 0

        public MAST(EndianAwareBinaryReader reader)
        {
            PageSize          = reader.ReadInt32();
            Unknown2          = reader.ReadInt32();
            StringTableOffset = reader.ReadInt32();
            StringTableSize   = reader.ReadInt32();
            Unknown5          = reader.ReadInt32();
            Unknown6          = reader.ReadInt32();
            Unknown7          = reader.ReadInt32();
        }
Example #9
0
        public static ColorSequence ReadColorSequence(EndianAwareBinaryReader reader)
        {
            var keypointCount = reader.ReadInt32();
            var keypoints     = new ColorSequenceKeypoint[keypointCount];

            for (var i = 0; i < keypointCount; i++)
            {
                var time = reader.ReadSingle();
                var r    = reader.ReadSingle();
                var g    = reader.ReadSingle();
                var b    = reader.ReadSingle();
                keypoints[i] = new ColorSequenceKeypoint(time, new Color3(r, g, b));
            }
            return(new ColorSequence(keypoints));
        }
Example #10
0
        public static NumberSequence ReadNumberSequence(EndianAwareBinaryReader reader)
        {
            var keypointCount = reader.ReadInt32();
            var keypoints     = new NumberSequenceKeypoint[keypointCount];

            for (var i = 0; i < keypointCount; i++)
            {
                var time     = reader.ReadSingle();
                var value    = reader.ReadSingle();
                var envelope = reader.ReadSingle();
                keypoints[i] = new NumberSequenceKeypoint(time, value, envelope);
            }

            return(new NumberSequence(keypoints));
        }
Example #11
0
        public static Tuple <int, int>[] ReadParentData(EndianAwareBinaryReader reader) // Tuple format is (Child, Parent)
        {
            reader.ReadByte();                                                          // Reserved
            var entryCount = reader.ReadInt32();

            var childReferents  = ReadReferentArray(reader, entryCount);
            var parentReferents = ReadReferentArray(reader, entryCount);

            var pairs = new Tuple <int, int> [entryCount];

            for (var i = 0; i < entryCount; i++)
            {
                pairs[i] = new Tuple <int, int>(childReferents[i], parentReferents[i]);
            }
            return(pairs);
        }
Example #12
0
        public DirectoryLayer(EndianAwareBinaryReader reader, Layer layer)
        {
            Layer   = layer;
            Entries = new Dictionary <ulong, DirectoryEntry>();

            Unknown1        = reader.ReadInt32();
            AmountOfEntries = reader.ReadInt32();
            Size            = reader.ReadInt32();
            Unknown2        = reader.ReadInt32();
            Unknown3        = reader.ReadInt32();
            Unknown4        = reader.ReadInt32();
            Unknown5        = reader.ReadInt32();

            ReadEntries(reader);
        }
Example #13
0
        public DirectoryEntry(EndianAwareBinaryReader reader, int size)
        {
            var offset = reader.BaseStream.Position + size;

            AssetID  = reader.ReadUInt64();
            Unknown2 = reader.ReadInt32(); // size of next block?
            Unknown3 = reader.ReadInt32(); // file hash of size?
            Unknown4 = reader.ReadInt32();
            Unknown5 = reader.ReadInt32();
            Unknown6 = reader.ReadInt32();
            Unknown7 = reader.ReadInt32();
            FileName = reader.ReadCString();

            reader.Seek(offset, SeekOrigin.Begin);
        }
Example #14
0
        public readonly int Unknown15; // always 0

        public TCES(EndianAwareBinaryReader reader)
        {
            Flags        = reader.ReadUInt32();
            Unknown2     = reader.ReadInt32();
            Unknown3     = reader.ReadInt32();
            Size         = reader.ReadInt32();
            Unknown5     = reader.ReadInt32();
            Unknown6     = reader.ReadInt32();
            PageOffset   = reader.ReadInt32(); // << 11
            SectionSize  = reader.ReadInt32();
            SectionSize2 = reader.ReadInt32();
            Unknown10    = reader.ReadInt32();
            Unknown11    = reader.ReadInt32();
            Unknown12    = reader.ReadInt32();
            Unknown13    = reader.ReadInt32();
            Unknown14    = reader.ReadInt32();
            Unknown15    = reader.ReadInt32();

            // flags are always BE
            if (!reader.IsBigEndian)
            {
                Flags = BinaryPrimitives.ReverseEndianness(Flags);
            }
        }
        /// <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);
            }
        }
Example #16
0
        /// <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;
        }
Example #17
0
        public readonly int Unknown15; // always 0

        public Layer(EndianAwareBinaryReader reader)
        {
            LayerType       = (LayerType)reader.ReadUInt32();
            Flags           = reader.ReadUInt32();
            Index           = reader.ReadInt32();
            TCES_Unknown3   = reader.ReadInt32();
            Unknown4        = reader.ReadInt32();
            Unknown5        = reader.ReadInt32();
            Unknown6        = reader.ReadInt32();
            PageOffset      = reader.ReadInt32();
            TotalLayerSize  = reader.ReadInt32();
            TotalLayerSize2 = reader.ReadInt32();
            PageSize        = reader.ReadInt32();
            Unknown11       = reader.ReadInt32();
            Unknown12       = reader.ReadInt32();
            Unknown13       = reader.ReadInt32();
            SubLayerOffset  = reader.ReadInt32();
            Unknown15       = reader.ReadInt32();

            // flags are always BE
            if (!reader.IsBigEndian)
            {
                Flags = BinaryPrimitives.ReverseEndianness(Flags);
            }
        }
Example #18
0
        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);
        }
Example #19
0
        public readonly int Unknown1; // always -1

        public AssetGroup(EndianAwareBinaryReader reader)
        {
            Count    = reader.ReadInt32();
            Unknown1 = reader.ReadInt32();
            reader.Seek(0x18, SeekOrigin.Current); // padding, 0x74 filled
        }
Example #20
0
 /// <summary>
 /// Loads the IMAGE_DATA_DIRECTORY from the reader.
 /// </summary>
 /// <param name="reader">The reader.</param>
 public void Read(EndianAwareBinaryReader reader)
 {
     VirtualAddress = reader.ReadUInt32();
     Size = reader.ReadInt32();
 }
Example #21
0
    /// <summary>
    /// 
    /// </summary>
    /// <param name="bytController"></param>
    /// <param name="cntlMsgs"></param>
    /// <param name="strControllerData"></param>
    public static void DecodeControllerData(byte[] bytController, ref ControllerMessages cntlMsgs)
    {
        //UnityEngine.Debug.Log("Entered DecodeControllerData()");
        int intMsgType = 0;

        MemoryStream stream = new MemoryStream(bytController);
        using (EndianAwareBinaryReader reader = new EndianAwareBinaryReader(stream, false))
        {
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                intMsgType = reader.ReadInt32();
                //UnityEngine.Debug.Log("intMsgType = " + intMsgType);

                if (intMsgType == ViewerConstants.MESSAGE_TOUCH_INPUT)
                {
                    UnityEngine.Debug.Log("reading touch");
                    cntlMsgs.TouchMessage.X = reader.ReadSingle();
                    cntlMsgs.TouchMessage.Y = reader.ReadSingle();
                    cntlMsgs.TouchMessage.Timestamp = reader.ReadInt64();
                    cntlMsgs.TouchMessage.Pointer = reader.ReadInt32();
                    cntlMsgs.TouchMessage.Active = reader.ReadInt32();
                }
                else if (intMsgType == ViewerConstants.MESSAGE_ACCELEROMETER_INPUT)
                {
                    //UnityEngine.Debug.Log("reading accel");
                    cntlMsgs.AccelMessage.X = reader.ReadSingle();
                    cntlMsgs.AccelMessage.Y = reader.ReadSingle();
                    cntlMsgs.AccelMessage.Z = reader.ReadSingle();
                    cntlMsgs.AccelMessage.Timestamp = reader.ReadInt32();
                }
                else if (intMsgType == ViewerConstants.MESSAGE_MAGNOTOMETER_INPUT)
                {
                    //UnityEngine.Debug.Log("reading magnotometer");
                    cntlMsgs.MagnoMessage.X = reader.ReadSingle();
                    cntlMsgs.MagnoMessage.Y = reader.ReadSingle();
                    cntlMsgs.MagnoMessage.Z = reader.ReadSingle();
                    cntlMsgs.MagnoMessage.Timestamp = reader.ReadInt32();
                }
                else if (intMsgType == ViewerConstants.MESSAGE_ATTITUDE_INPUT)
                {
                    //UnityEngine.Debug.Log("reading attitude");
                    cntlMsgs.AttMessage.X = reader.ReadSingle();
                    cntlMsgs.AttMessage.Y = reader.ReadSingle();
                    cntlMsgs.AttMessage.Z = reader.ReadSingle();
                    cntlMsgs.AttMessage.Timestamp = reader.ReadInt32();
                }

                else if (intMsgType == ViewerConstants.MESSAGE_TRACKBALL_INPUT)
                {
                    UnityEngine.Debug.Log("reading trackball");
                    cntlMsgs.TrackMessage.X = reader.ReadSingle();
                    cntlMsgs.TrackMessage.Y = reader.ReadSingle();
                }
                else if (intMsgType == ViewerConstants.MESSAGE_OPTIONS)
                {
                    UnityEngine.Debug.Log("reading options");
                    cntlMsgs.OptionMessage.ScreenWidth = reader.ReadInt32();
                    cntlMsgs.OptionMessage.ScreenHeight = reader.ReadInt32();
                }
                else if (intMsgType == ViewerConstants.MESSAGE_KEY)
                {
                    UnityEngine.Debug.Log("reading keys");
                    cntlMsgs.KeyMessage.State = reader.ReadInt32();
                    cntlMsgs.KeyMessage.Key = reader.ReadInt32();
                    cntlMsgs.KeyMessage.Unicode = reader.ReadInt32();
                }
            }
        }
    }
Example #22
0
 /// <summary>
 /// Loads the IMAGE_DATA_DIRECTORY from the reader.
 /// </summary>
 /// <param name="reader">The reader.</param>
 public void Read(EndianAwareBinaryReader reader)
 {
     VirtualAddress = reader.ReadUInt32();
     Size           = reader.ReadInt32();
 }
Example #23
0
        private static PropertyBlock Deserialize(EndianAwareBinaryReader reader, TypeHeader[] typeHeaders)
        {
            var typeId   = reader.ReadInt32();
            var name     = Util.ReadLengthPrefixedString(reader);
            var dataType = (PropertyType)reader.ReadByte();

            var typeHeader = typeHeaders.FirstOrDefault(n => n.TypeId == typeId);

            if (typeHeader == null)
            {
                throw new ArgumentException("No type header matches type id specified in property block.");
            }

            switch (dataType)
            {
            case PropertyType.String:
            {
                var values = Util.ReadStringArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <string>(name, dataType, typeId, values));
            }

            case PropertyType.Boolean:
            {
                var values = Util.ReadBoolArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <bool>(name, dataType, typeId, values));
            }

            case PropertyType.Int32:
            {
                var values = Util.ReadInt32Array(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <int>(name, dataType, typeId, values));
            }

            case PropertyType.Float:
            {
                var values = Util.ReadFloatArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <float>(name, dataType, typeId, values));
            }

            case PropertyType.Double:
            {
                var values = Util.ReadDoubleArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <double>(name, dataType, typeId, values));
            }

            case PropertyType.UDim2:
            {
                var values = Util.ReadUDim2Array(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <UDim2>(name, dataType, typeId, values));
            }

            case PropertyType.Ray:
            {
                var values = Util.ReadRayArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <Ray>(name, dataType, typeId, values));
            }

            case PropertyType.Faces:
            {
                var values = Util.ReadFacesArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <Faces>(name, dataType, typeId, values));
            }

            case PropertyType.Axis:
            {
                var values = Util.ReadAxisArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <Axis>(name, dataType, typeId, values));
            }

            case PropertyType.BrickColor:
            {
                var values = Util.ReadBrickColorArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <BrickColor>(name, dataType, typeId, values));
            }

            case PropertyType.Color3:
            {
                var values = Util.ReadColor3Array(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <Color3>(name, dataType, typeId, values));
            }

            case PropertyType.Vector2:
            {
                var values = Util.ReadVector2Array(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <Vector2>(name, dataType, typeId, values));
            }

            case PropertyType.Vector3:
            {
                var values = Util.ReadVector3Array(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <Vector3>(name, dataType, typeId, values));
            }

            case PropertyType.CFrame:
            {
                var values = Util.ReadCFrameArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <CFrame>(name, dataType, typeId, values));
            }

            case PropertyType.Enumeration:
            {
                var values = Util.ReadEnumerationArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <int>(name, dataType, typeId, values));
            }

            case PropertyType.Referent:
            {
                var values = Util.ReadReferentArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <int>(name, dataType, typeId, values));
            }

            case PropertyType.NumberSequence:
            {
                var values = Util.ReadNumberSequenceArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <NumberSequence>(name, dataType, typeId, values));
            }

            case PropertyType.ColorSequence:
            {
                var values = Util.ReadColorSequenceArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <ColorSequence>(name, dataType, typeId, values));
            }

            case PropertyType.NumberRange:
            {
                var values = Util.ReadNumberRangeArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <NumberRange>(name, dataType, typeId, values));
            }

            case PropertyType.Rectangle:
            {
                var values = Util.ReadRectangleArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <Rectangle>(name, dataType, typeId, values));
            }

            case PropertyType.PhysicalProperties:
            {
                var values = Util.ReadPhysicalPropertiesArray(reader, typeHeader.InstanceCount);
                return(new PropertyBlock <PhysicalProperties>(name, dataType, typeId, values));
            }

            default:
            {
                return(null);
            }
            }
        }
Example #24
0
        /// <summary>
        /// Reads the root structure from the metadata.
        /// </summary>
        /// <param name="header">The <see cref="CliHeader"/> which denotes the location of the information.</param>
        /// <param name="originalStream">The <see cref="FileStream"/> being read from.</param>
        /// <param name="reader">The <see cref="EndianAwareBinaryReader"/> which handles reads.</param>
        /// <param name="relativeVirtualAddress">The <see cref="UInt32"/> value which denotes the relative
        /// virtual address of the metadata header.</param>
        /// <param name="sourceImage"></param>
        internal void Read(CliHeader header, FileStream originalStream, EndianAwareBinaryReader reader, uint relativeVirtualAddress, PEImage sourceImage)
        {
            this.originalStream = originalStream;
            this.header         = header;
            this.streamPosition = relativeVirtualAddress;
            this.signature      = reader.ReadUInt32();
            if (this.signature != metadataSignature)
            {
                throw new BadImageFormatException();
            }
            this.depreciatedVersion.Read(reader);
            this.reserved          = reader.ReadUInt32();
            this.realVersionLength = reader.ReadInt32();
            byte[] version = new byte[(this.realVersionLength + 3) & ~3];//Make it a multiple of four.
            reader.Read(version, 0, version.Length);
            this.version       = version;
            this.reservedFlags = reader.ReadUInt16();
            int streamCount = 0;

            streamCount      = reader.ReadUInt16();
            this.sourceImage = sourceImage;
            for (int i = 0; i < streamCount; i++)
            {
                var currentHeader = new CliMetadataStreamHeader();
                currentHeader.Read(reader, sourceImage);
                switch (currentHeader.Name)
                {
                case "#Strings":
                    if (this.strings != null)
                    {
                        goto sectionExists;
                    }
                    this.strings = new CliMetadataStringsHeaderAndHeap(currentHeader, sourceImage.SyncObject);
                    this.ScanAndReadSection(sourceImage, strings, this.strings.Read);
                    break;

                case "#Blob":
                    if (this.blob != null)
                    {
                        goto sectionExists;
                    }
                    this.blob = new CliMetadataBlobHeaderAndHeap(currentHeader, this);
                    this.ScanAndReadSection(sourceImage, blob, this.blob.Read);
                    break;

                case "#US":
                    if (this.userStrings != null)
                    {
                        goto sectionExists;
                    }
                    this.userStrings = new CliMetadataUserStringsHeaderAndHeap(currentHeader, sourceImage.SyncObject);
                    this.ScanAndReadSection(sourceImage, this.userStrings, this.userStrings.Read);
                    break;

                case "#GUID":
                    if (this.guids != null)
                    {
                        goto sectionExists;
                    }
                    this.guids = new CliMetadataGuidHeaderAndHeap(currentHeader, sourceImage.SyncObject);
                    this.ScanAndReadSection(sourceImage, guids, this.guids.Read);
                    break;

                case "#-":     //https://github.com/jbevain/cecil/blob/8b689ecdc890cbf3715ba8775de1d713d71f09f3/Mono.Cecil.PE/ImageReader.cs#L359
                case "#~":
                    if (this.tableStream != null)
                    {
                        goto sectionExists;
                    }
                    this.tableStream = new CliMetadataTableStreamAndHeader(currentHeader);
                    this.ScanAndReadSection(sourceImage, tableStream, sdReader => this.tableStream.Read(sdReader, this));
                    break;
                }
                continue;
sectionExists:
                throw new BadImageFormatException(string.Format("Duplicate {0} section encountered.", currentHeader.Name));
            }
            if (this.tableStream == null)
            {
                throw new BadImageFormatException("#~ or #- stream not present in image.");
            }
        }