void IDefinition.FromArray(byte[] buffer)
 {
     BinaryReader bin = new BinaryReader(new MemoryStream(buffer));
     bin.BaseStream.Seek(12, SeekOrigin.Begin);
     Shader = bin.ReadTagID();
 }
示例#2
0
        public MapStream(string filename)
            : base(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
        {
            this.MemoryBlocks = new MemoryMappedAddress[2];
            //HEADER
            BinaryReader bin = new BinaryReader(this, Encoding.UTF8);

            this.Lock(0, 2048);
            this.Seek(0, SeekOrigin.Begin);
            if (bin.ReadTagClass() != (TagClass)"head")
                throw new InvalidDataException("Not a halo-map file");

            //this.Seek(36, SeekOrigin.Begin);
            //var version = bin.ReadInt32();
            //switch (version)
            //{
            //    case 0:
            //        BuildVersion = Version.XBOX_RETAIL;
            //        break;
            //    case -1:
            //        BuildVersion = Version.PC_RETAIL;
            //        break;
            //    default:
            //}
            BuildVersion = Version.PC_RETAIL;
            this.Seek(16, SeekOrigin.Begin);

            int indexAddress = bin.ReadInt32();
            int indexLength = bin.ReadInt32();
            int tagCacheLength = bin.ReadInt32();

            if (BuildVersion == Version.PC_RETAIL)
                this.Seek(12, SeekOrigin.Current);

            this.Seek(332, SeekOrigin.Current);

            int stringTableLength = bin.ReadInt32();
            this.Seek(4, SeekOrigin.Current);
            int stringTableAddress = bin.ReadInt32();

            this.Seek(36, SeekOrigin.Current);

            MapName = bin.ReadFixedString(32);

            this.Seek(4, SeekOrigin.Current);

            Scenario = bin.ReadFixedString(256);

            this.Seek(4, SeekOrigin.Current);
            int pathsCount = bin.ReadInt32();
            int pathsTableAddress = bin.ReadInt32();
            int pathsTableLength = bin.ReadInt32();

            this.Unlock(0, 2048);

            this.Seek(pathsTableAddress, SeekOrigin.Begin);
            var Paths = Encoding.UTF8.GetString(bin.ReadBytes(pathsTableLength - 1)).Split(char.MinValue);

            //STRINGS

            this.Seek(stringTableAddress, SeekOrigin.Begin);
            Strings = Encoding.UTF8.GetString(bin.ReadBytes(stringTableLength - 1)).Split(char.MinValue);

            //  INDEX
            /*
             *  Vista doesn't use memory addresses for the following address-values. (they are instead 0-based from the index-address)
             *
             *  0x00    Address to Classes array
             *  0x04    Classes array length
             *  0x08    Address to Tags array
             *  0x0C    Scenario        tag_id
             *  0x10    Match-Globals   tag_id
             *  0x14    ~
             *  0x18    Tags array length
             *  0xC0    'sgat'          four_cc
             *
             *  */
            this.Seek(indexAddress, SeekOrigin.Begin);
            int tagClassTableVirtualAddress = bin.ReadInt32();
            this.IndexVirtualAddress = tagClassTableVirtualAddress - 32;
            this.Seek(4, SeekOrigin.Current);
            int tagDatumTableVirtualAddress = bin.ReadInt32();
            var ScenarioID = bin.ReadTagID();
            var GlobalsID = bin.ReadTagID();
            int tagDatumTableOffset = tagDatumTableVirtualAddress - tagClassTableVirtualAddress;
            this.Seek(4, SeekOrigin.Current);
            int tagDatumCount = bin.ReadInt32();

            this.Seek(4 + tagDatumTableOffset, SeekOrigin.Current);
            Tags = new Tag[tagDatumCount];
            for (int i = 0; i < tagDatumCount; i++)
            {
                Tags[i] = new Tag()
                {
                    Type = bin.ReadTagType(),
                    Identifier = bin.ReadInt32(),
                    VirtualAddress = bin.ReadInt32(),
                    Length = bin.ReadInt32()
                };
                if (i == 0)
                {
                    SecondaryMagic = Tags[0].VirtualAddress - (indexAddress + indexLength);
                }

                //Borky vista fix - broken paths are broken
                //if (Tags[i].VirtualAddress == 0) continue;
                //var tag = Tags[i];
                //Tags[i].Path = Paths[Tags[i].Identifier.SaltedIndex];
            }

            this.MemoryBlocks[1] = new MemoryMappedAddress()
            {
                Address = Tags[0].VirtualAddress,
                Length = tagCacheLength,
                Magic = SecondaryMagic,
            };

            /* Intent: read the sbsp and lightmap address and lengths from the scenario tag
             * and store them in the Tags array.
             */
            if (BuildVersion == Version.XBOX_RETAIL)
            {
                this.Seek(Tags[ScenarioID.Index].VirtualAddress - SecondaryMagic + 528, SeekOrigin.Begin);
                var count = bin.ReadInt32();
                var address = bin.ReadInt32();
                for (int i = 0; i < count; ++i)
                {
                    this.Seek(address - SecondaryMagic + i * 68, SeekOrigin.Begin);
                    var sbsp_offset = bin.ReadInt32();
                    var sbsp_length = bin.ReadInt32();
                    var sbsp_virtual_address = bin.ReadInt32();
                    if (i == 0)
                    {
                        this.PrimaryMagic = sbsp_virtual_address - sbsp_offset;
                        this.MemoryBlocks[0].Address = sbsp_virtual_address;
                        this.MemoryBlocks[0].Magic = this.PrimaryMagic;
                    }
                    this.MemoryBlocks[0].Length += sbsp_length;

                    Seek(8, SeekOrigin.Current);
                    var sbsp_identifier = bin.ReadTagID();
                    Seek(4, SeekOrigin.Current);
                    var ltmp_identifier = bin.ReadTagID();

                    var ltmp_offset = bin.ReadInt32();
                    var ltmp_length = sbsp_offset + sbsp_length - ltmp_offset;

                    Tags[sbsp_identifier.Index].VirtualAddress = sbsp_virtual_address;
                    Tags[sbsp_identifier.Index].Length = sbsp_length - ltmp_length;

                    if (ltmp_identifier != TagIdentifier.null_identifier)
                    {
                        Tags[ltmp_identifier.Index].VirtualAddress = sbsp_virtual_address + ltmp_offset;
                        Tags[ltmp_identifier.Index].Length = ltmp_length;
                    }
                }

                //UNICODE
                this.Seek(Tags[GlobalsID.Index].VirtualAddress - SecondaryMagic + 400, SeekOrigin.Begin);
                int unicodeCount = bin.ReadInt32();
                int unicodeTableLength = bin.ReadInt32();
                int unicodeIndexAddress = bin.ReadInt32();
                int unicodeTableAddress = bin.ReadInt32();

                Unicode = new UnicodeValueNamePair[unicodeCount];

                StringID[] strRefs = new StringID[unicodeCount];
                int[] strOffsets = new int[unicodeCount];

                this.Seek(unicodeIndexAddress, SeekOrigin.Begin);
                for (int i = 0; i < unicodeCount; i++)
                {
                    strRefs[i] = (StringID)bin.ReadInt32();
                    strOffsets[i] = bin.ReadInt32();
                }
                for (int i = 0; i < unicodeCount; i++)
                {
                    this.Seek(unicodeTableAddress + strOffsets[i], SeekOrigin.Begin);
                    StringBuilder unicodeString = new StringBuilder(byte.MaxValue);
                    while (bin.PeekChar() != char.MinValue)
                        unicodeString.Append(bin.ReadChar());
                    Unicode[i] = new UnicodeValueNamePair { Name = strRefs[i], Value = unicodeString.ToString() };
                }
            }
        }