Exemplo n.º 1
0
        private void ProcessRegions(
            IMutagenReadStream stream,
            MajorRecordFrame majorFrame,
            long fileOffset)
        {
            if (!majorFrame.TryLocateSubrecordFrame(RecordTypes.RDAT, out var rdatFrame, out var rdatIndex))
            {
                return;
            }
            int amount = 0;
            SortedList <uint, RangeInt64> rdats = new SortedList <uint, RangeInt64>();
            bool foundNext = true;

            while (foundNext)
            {
                foundNext = majorFrame.TryLocateSubrecordFrame(RecordTypes.RDAT, offset: rdatIndex + rdatFrame.TotalLength, out var nextRdatFrame, out var nextRdatIndex);
                var index = rdatFrame.Content.UInt32();
                rdats[index] =
                    new RangeInt64(
                        rdatIndex + fileOffset,
                        foundNext ? nextRdatIndex - 1 + fileOffset : fileOffset + majorFrame.TotalLength - 1);
                rdatFrame = nextRdatFrame;
                rdatIndex = nextRdatIndex;
            }

            foreach (var item in rdats.Reverse())
            {
                if (item.Key == (int)RegionData.RegionDataType.Icon)
                {
                    continue;
                }
                this._Instructions.SetMove(
                    loc: fileOffset + majorFrame.TotalLength,
                    section: item.Value);
            }

            if (rdats.ContainsKey((int)RegionData.RegionDataType.Icon))
            { // Need to create icon record
                if (!majorFrame.TryLocateSubrecordFrame("EDID", out var edidFrame, out var edidLoc))
                {
                    throw new ArgumentException();
                }
                var locToPlace = fileOffset + edidLoc + edidFrame.TotalLength;

                // Get icon string
                var iconLoc = rdats[(int)RegionData.RegionDataType.Icon];
                stream.Position = iconLoc.Min + 20;
                var iconStr = stream.ReadZString((int)(iconLoc.Max - stream.Position));

                // Get icon bytes
                MemoryStream memStream = new MemoryStream();
                using (var writer = new MutagenWriter(memStream, this.GameRelease))
                {
                    using (HeaderExport.Header(
                               writer,
                               new RecordType("ICON"),
                               ObjectType.Subrecord))
                    {
                        writer.Write(iconStr);
                        writer.Write(default(byte));
                    }
                }

                var arr = memStream.ToArray();
                this._Instructions.SetAddition(
                    loc: locToPlace,
                    addition: arr);
                this._Instructions.SetRemove(
                    section: iconLoc);
                amount += arr.Length;
                amount -= (int)iconLoc.Width;
            }

            ProcessLengths(
                majorFrame,
                amount,
                fileOffset);
        }