Example #1
0
        public void ProcessData()
        {
            if (mIsDataProcessed)
            {
                return;
            }

            mIsProcessingData = true;

            switch (Mode)
            {
            case SectionMode.Read when BaseStream == null || Reader == null:
                throw new InvalidOperationException("Section has not been read yet, cannot process data object");

            case SectionMode.Read:
            {
                if (mData == null)
                {
                    mData = new T();
                }

                foreach (var section in mSections)
                {
                    if (SectionInfo.SubSectionInfos.TryGetValue(section.SectionInfo, out var subSectionInfo))
                    {
                        subSectionInfo.ProcessPropertyForReading(section, this);
                    }
                }

                Reader.SeekBegin(DataOffset);
                {
                    Read(mData, Reader, DataSize);
                }
                Reader.SeekBegin(DataOffset + SectionSize);

                break;
            }

            case SectionMode.Write when BaseStream == null || Writer == null:
                throw new InvalidOperationException("Data object has not been written yet, cannot process data object");

            case SectionMode.Write:
            {
                mSections.Clear();

                if (DataSize > 0 && Writer is EnrsBinaryWriter enrsWriter)
                {
                    mSections.Add(new EnrsSection(SectionMode.Write, enrsWriter.CreateScopeDescriptors(DataSize)));
                }

                if (IsRelocationTableWorthWriting())
                {
                    ISection relocationTableSection;

                    var offsets = Writer.OffsetPositions.Select(x => x - Writer.BaseOffset).ToList();

                    if (AddressSpace == AddressSpace.Int64)
                    {
                        relocationTableSection = new RelocationTableSectionInt64(SectionMode.Write, offsets);
                    }

                    else
                    {
                        relocationTableSection = new RelocationTableSectionInt32(SectionMode.Write, offsets);
                    }

                    mSections.Add(relocationTableSection);
                }

                foreach (var subSectionInfo in SectionInfo.SubSectionInfos.Values.OrderBy(x => x.Priority))
                {
                    mSections.AddRange(subSectionInfo.ProcessPropertyForWriting(this));
                }

                if (mSections.Count > 0)
                {
                    mSections.Add(new EndOfFileSection(SectionMode.Write, this));
                }

                break;
            }
            }

            mIsDataProcessed  = true;
            mIsProcessingData = false;
        }
Example #2
0
        private void ProcessDataObject()
        {
            mIsProcessingObject = true;

            if (Mode == SectionMode.Read)
            {
                if (BaseStream == null || Reader == null)
                {
                    throw new InvalidOperationException("Section has not been read yet, cannot process data object");
                }

                if (mDataObject == null)
                {
                    mDataObject = new T();
                }

                foreach (var section in mSections)
                {
                    if (SectionInfo.SubSectionInfos.TryGetValue(section.SectionInfo, out var subSectionInfo))
                    {
                        subSectionInfo.ProcessPropertyForReading(section, this);
                    }
                }

                Reader.SeekBegin(DataOffset);
                {
                    Read(mDataObject, Reader, DataSize);
                }
                Reader.SeekBegin(DataOffset + SectionSize);
            }
            else if (Mode == SectionMode.Write)
            {
                if (BaseStream == null || Writer == null)
                {
                    throw new InvalidOperationException("Data object has not been written yet, cannot process data object");
                }

                mSections.Clear();
                if (Writer.OffsetPositions.Count > 0 && Flags.HasFlag(SectionFlags.HasRelocationTable))
                {
                    ISection relocationTableSection;

                    var offsets = Writer.OffsetPositions.Select(x => x - Writer.BaseOffset).ToList();
                    if (AddressSpace == AddressSpace.Int64)
                    {
                        relocationTableSection = new RelocationTableSectionInt64(SectionMode.Write, offsets);
                    }
                    else
                    {
                        relocationTableSection = new RelocationTableSectionInt32(SectionMode.Write, offsets);
                    }

                    mSections.Add(relocationTableSection);
                }

                foreach (var subSectionInfo in SectionInfo.SubSectionInfos.Values.OrderBy(x => x.Priority))
                {
                    mSections.AddRange(subSectionInfo.ProcessPropertyForWriting(this));
                }

                if (mSections.Count > 0)
                {
                    mSections.Add(new EndOfFileSection(SectionMode.Write, this));
                }
            }

            mIsObjectProcessed  = true;
            mIsProcessingObject = false;
        }