LSLib.Granny.GR2.WritableSection.WriteStruct C# (CSharp) Method

WriteStruct() private method

private WriteStruct ( StructDefinition definition, object node, bool allowRecursion = true ) : void
definition StructDefinition
node object
allowRecursion bool
return void
        internal void WriteStruct(StructDefinition definition, object node, bool allowRecursion = true)
        {
            if (node == null) throw new ArgumentNullException();

            AlignWrite();
            StoreObjectOffset(node);

            var tag = GR2.Header.tag;
            foreach (var member in definition.Members)
            {
                if (member.ShouldSerialize(tag))
                {
                    var value = member.CachedField.GetValue(node);
                    if (member.SerializationKind == SerializationKind.UserRaw)
                        member.Serializer.Write(this.GR2, this, member, value);
                    else
                        WriteInstance(member, member.CachedField.FieldType, value);
                }
            }

            // When the struct is empty, we need to write a dummy byte to make sure that another 
            // struct won't have the same address.
            if (definition.Members.Count == 0)
            {
                Writer.Write((Byte)0);
            }

            if (Writer == MainWriter && allowRecursion)
            {
                // We need to write all child structs directly after the parent struct
                // (at least this is how granny2.dll does it)
                GR2.FlushPendingWrites();
            }
        }

Same methods

WritableSection::WriteStruct ( Type type, object node, bool allowRecursion = true ) : void
WritableSection::WriteStruct ( object node, bool allowRecursion = true ) : void

Usage Example

コード例 #1
0
ファイル: Writer.cs プロジェクト: Norbyte/lslib
        public byte[] Write(object root)
        {
            using (this.Writer = new BinaryWriter(Stream))
            {
                this.Magic = InitMagic();
                WriteMagic(Magic);

                this.Header = InitHeader();
                WriteHeader(Header);

                for (int i = 0; i < Header.numSections; i++)
                {
                    var section = new WritableSection((SectionType)i, this);
                    WriteSectionHeader(section.Header);
                    Sections.Add(section);
                }

                Magic.headersSize = (UInt32)Stream.Position;

                CurrentSection = Sections[(int)SectionType.Main];
                CurrentSection.WriteStruct(root);

                while (ArrayWrites.Count > 0 || StructWrites.Count > 0 || StringWrites.Count > 0)
                {
                    FlushPendingWrites();
                }

                foreach (var defn in Types.Values)
                {
                    Sections[(int)SectionType.Discardable].WriteStructDefinition(defn);
                }

                // We need to do this again to flush strings written by WriteMemberDefinition()
                FlushPendingWrites();

                FinalizeOffsets();

                foreach (var section in Sections)
                {
                    section.Header.first16bit = (UInt32)section.MainStream.Length;
                    section.Header.first8bit = (UInt32)section.MainStream.Length;
                    section.Finish();
                }

                var relocSection = Sections[(int)SectionType.Discardable];
                foreach (var section in Sections)
                {
                    relocSection.WriteSectionRelocations(section);
                    relocSection.WriteSectionMixedMarshallingRelocations(section);
                }

                foreach (var section in Sections)
                {
                    // Pad section size to a multiple of 4
                    while (section.MainStream.Position % 4 > 0)
                        section.Writer.Write((Byte)0);

                    section.MainStream.Flush();
                    section.Header.offsetInFile = (UInt32)Stream.Position;
                    section.Header.uncompressedSize = (UInt32)section.MainStream.Length;
                    section.Header.compressedSize = (UInt32)section.MainStream.Length;
                    Writer.Write(section.MainStream.ToArray());
                }

                foreach (var section in Sections)
                {
                    section.Header.relocationsOffset += relocSection.Header.offsetInFile;
                    section.Header.mixedMarshallingDataOffset += relocSection.Header.offsetInFile;
                }

                var rootStruct = LookupStructDefinition(root.GetType());
                Header.rootType = ObjectOffsets[rootStruct];
                Header.rootNode = new SectionReference(SectionType.Main, 0);
                Header.fileSize = (UInt32)Stream.Length;

                Stream.Seek(Magic.MagicSize + Header.HeaderSize, SeekOrigin.Begin);

                foreach (var section in Sections)
                {
                    WriteSectionHeader(section.Header);
                }

                Header.crc = Header.CalculateCRC(Stream);
                Stream.Seek(0, SeekOrigin.Begin);
                WriteMagic(Magic);
                WriteHeader(Header);

                return Stream.ToArray();
            }
        }
All Usage Examples Of LSLib.Granny.GR2.WritableSection::WriteStruct