Пример #1
0
        public static void Write <T>(DBEntry <T> entry, string savepath) where T : class
        {
            if (entry.Rows.HasDuplicateKeys)
            {
                throw new Exception("Collection contains duplicate keys");
            }


            using (var fs = new FileStream(savepath, FileMode.Create))
                using (var ms = new MemoryStream())
                    using (var bw = new BinaryWriter(ms))
                    {
                        StringTable stringtable = new StringTable(entry.Header.ExtendedStringTable); //Preloads null byte(s)
                        entry.Header.WriteHeader(bw, entry);

                        if (entry.Header.IsTypeOf <WDB5>() && !entry.Header.HasOffsetTable && entry.Header.CopyTableSize > 0)
                        {
                            WriteIntoFile(entry, bw, entry.GetUniqueRows().ToArray(), ref stringtable); //Insert unique rows
                        }
                        else
                        {
                            WriteIntoFile(entry, bw, entry.Rows.AsEnumerable(), ref stringtable); //Insert all rows
                        }

                        //Copy StringTable and StringTable size if it doesn't have inline strings
                        if (stringtable.Size > 0 && !entry.Header.HasOffsetTable)
                        {
                            long pos = bw.BaseStream.Position;
                            bw.Scrub(entry.Header.StringTableOffset);
                            bw.Write(stringtable.Size);
                            bw.Scrub(pos);
                            stringtable.CopyTo(bw.BaseStream);
                        }
                        stringtable.Dispose();

                        //Legion+ only
                        if (entry.Header.IsLegionFile)
                        {
                            //WCH7 Map
                            if (entry.Header.IsTypeOf <WCH7>())
                            {
                                bw.WriteArray(((WCH7)entry.Header).WCH7Table);
                            }

                            //OffsetMap
                            if (entry.Header.HasOffsetTable)
                            {
                                //Update StringTableOffset with current position
                                long pos = bw.BaseStream.Position;
                                bw.Scrub(entry.Header.StringTableOffset);
                                bw.Write((int)pos);
                                bw.Scrub(pos);

                                entry.Header.WriteOffsetMap(bw, entry, OffsetMap);

                                OffsetMap.Clear(); //Cleanup
                            }

                            //Index Table
                            if (entry.Header.HasIndexTable)
                            {
                                entry.Header.WriteIndexTable(bw, entry);
                            }

                            //CopyTable - WDB5 only
                            if (entry.Header.IsTypeOf <WDB5>())
                            {
                                ((WDB5)entry.Header).WriteCopyTable(bw, entry);
                            }

                            //CommonDataTable
                            if (entry.Header.IsTypeOf <WDB6>())
                            {
                                ((WDB6)entry.Header).WriteCommonDataTable(bw, entry);
                            }
                        }

                        //Copy data to file
                        ms.Position = 0;
                        ms.CopyTo(fs);
                    }
        }