/// <summary>
        /// Deserializes and creates a <see cref="MessageScript"/> from a stream.
        /// </summary>
        public static MessageScript FromStream(Stream stream, FormatVersion version = FormatVersion.Version1, Encoding encoding = null, bool leaveOpen = false)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var binary = MessageScriptBinary.FromStream(stream, leaveOpen);

            return(FromBinary(binary, version, encoding));
        }
Example #2
0
        static void ScanForMessageScripts(string prettyFileName, Stream stream, CancellationToken cancellationToken)
        {
            byte[] magic = new byte[4];

            while (stream.Position <= stream.Length)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                if (stream.Position + BinaryHeader.SIZE < stream.Length)
                {
                    // Read 4 bytes
                    magic[0] = ( byte )stream.ReadByte();
                    magic[1] = ( byte )stream.ReadByte();
                    magic[2] = ( byte )stream.ReadByte();
                    magic[3] = ( byte )stream.ReadByte();

                    if (magic.SequenceEqual(BinaryHeader.MAGIC_V0) ||
                        magic.SequenceEqual(BinaryHeader.MAGIC_V1) ||
                        magic.SequenceEqual(BinaryHeader.MAGIC_V1_BE))
                    {
                        long scriptStartPosition = stream.Position - 12;
                        var  scriptBinary        = MessageScriptBinary.FromStream(new StreamView(stream, scriptStartPosition, stream.Length - scriptStartPosition));
                        var  script = MessageScript.FromBinary(scriptBinary);

                        Console.WriteLine($"Found message script at 0x{scriptStartPosition:X8}. Writing to file...");

                        if (UseDecompiler)
                        {
                            WriteMessageScriptWithDecompiler($"{prettyFileName} @ 0x{scriptStartPosition:X8}", script);
                        }
                        else
                        {
                            WriteMessageScript($"{prettyFileName} @ 0x{scriptStartPosition:X8}", script);
                        }

                        stream.Position = scriptStartPosition + scriptBinary.Header.FileSize;
                    }
                    else if (DisableScanAlignment)
                    {
                        // Scan alignment is disabled, so we make sure to retry every byte
                        // 4 steps forward, 3 steps back
                        stream.Position -= 3;
                    }
                }
                else
                {
                    break;
                }
            }
        }
        public void FromStream_ShouldPassIntegrityCheck_Version1BigEndian()
        {
            var script = MessageScriptBinary.FromStream(File.OpenRead("TestResources\\Version1BigEndian.bmd"));

            PerformIntegrityCheckForVersion1BigEndian(script);
        }