Esempio n. 1
0
        private static ScriptHeader GenerateConsoleHeader(Stream scriptStream)
        {
            var header = new ScriptHeader();
            var reader = new Reader(scriptStream);

            scriptStream.Seek(0, SeekOrigin.Begin);
            header.Rsc7Offset = reader.SReadUInt32() == 0x52534337 ? 0x10 : 0x0;
            scriptStream.Seek(header.Rsc7Offset, SeekOrigin.Begin);
            header.Magic            = reader.SReadInt32();   //0x0
            header.SubHeader        = reader.SReadPointer(); //0x4
            header.CodeBlocksOffset = reader.SReadPointer(); //0x8
            header.GlobalsVersion   = reader.SReadInt32();   //0x C
            header.CodeLength       = reader.SReadInt32();   //0x10
            header.ParameterCount   = reader.SReadInt32();   //0x14
            header.StaticsCount     = reader.SReadInt32();   //0x18
            header.GlobalsCount     = reader.SReadInt32();   //0x1C
            header.NativesCount     = reader.SReadInt32();   //0x20
            header.StaticsOffset    = reader.SReadPointer(); //0x24
            header.GlobalsOffset    = reader.SReadPointer(); //0x28
            header.NativesOffset    = reader.SReadPointer(); //0x2C
            header.Null1            = reader.SReadInt32();   //0x30
            header.Null2            = reader.SReadInt32();   //0x34
            header.NameHash         = reader.SReadInt32();
            header.Null3            = reader.SReadInt32();   //0x38
            header.ScriptNameOffset = reader.SReadPointer(); //0x40
            header.StringsOffset    = reader.SReadPointer(); //0x44
            header.StringsSize      = reader.SReadInt32();   //0x48
            header.Null4            = reader.ReadInt32();    //0x4C

            header.StringBlocks = (header.StringsSize + 0x3FFF) >> 14;
            header.CodeBlocks   = (header.CodeLength + 0x3FFF) >> 14;

            header.StringTableOffsets = new int[header.StringBlocks];
            scriptStream.Seek(header.StringsOffset + header.Rsc7Offset, SeekOrigin.Begin);
            for (var i = 0; i < header.StringBlocks; i++)
            {
                header.StringTableOffsets[i] = reader.SReadPointer() + header.Rsc7Offset;
            }


            header.CodeTableOffsets = new int[header.CodeBlocks];
            scriptStream.Seek(header.CodeBlocksOffset + header.Rsc7Offset, SeekOrigin.Begin);
            for (var i = 0; i < header.CodeBlocks; i++)
            {
                header.CodeTableOffsets[i] = reader.SReadPointer() + header.Rsc7Offset;
            }
            scriptStream.Position = header.ScriptNameOffset + header.Rsc7Offset;
            var data = scriptStream.ReadByte();

            header.ScriptName = "";
            while (data != 0 && data != -1)
            {
                header.ScriptName += (char)data;
                data = scriptStream.ReadByte();
            }

            return(header);
        }
        public ScriptFile(Stream scriptStream, OpcodeSet opCodeSet)
        {
            _file   = scriptStream;
            CodeSet = opCodeSet;

            _codeTable   = new List <byte>();
            Functions    = new List <Function>();
            AggFunctions = new List <Function>();
            FunctionLoc  = new Dictionary <int, FunctionName>();

            Header      = ScriptHeader.Generate(scriptStream);
            StringTable = new StringTable(scriptStream, Header.StringTableOffsets, Header.StringBlocks,
                                          Header.StringsSize);
            X64NativeTable = new X64NativeTable(scriptStream, Header.NativesOffset + Header.Rsc7Offset,
                                                Header.NativesCount, Header.CodeLength);
            Name = Header.ScriptName;

            for (var i = 0; i < Header.CodeBlocks; i++)
            {
                var tablesize = (i + 1) * 0x4000 >= Header.CodeLength ? Header.CodeLength % 0x4000 : 0x4000;
                var working   = new byte[tablesize];
                scriptStream.Position = Header.CodeTableOffsets[i];
                scriptStream.Read(working, 0, tablesize);
                _codeTable.AddRange(working);
            }

            GetStaticInfo();
            GetFunctions();
            foreach (var func in Functions)
            {
                func.PreDecode();
            }
            Statics.Checkvars();

            var dirty = true;

            while (dirty)
            {
                dirty = false;
                foreach (var func in Functions)
                {
                    if (func.Dirty)
                    {
                        dirty      = true;
                        func.Dirty = false;
                        func.DecodeInstructionsForVarInfo();
                    }
                }
            }

            if (Program.AggregateFunctions)
            {
                foreach (var func in AggFunctions)
                {
                    func.PreDecode();
                }
            }
            foreach (var func in Functions)
            {
                func.Decode();
            }
            if (Program.AggregateFunctions)
            {
                foreach (var func in AggFunctions)
                {
                    func.Decode();
                }
            }
        }