Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool SaveDBCFile(string fileName)
- {
- try
- {
- // This gets complicated fast
- UInt32 stringBlockOffset = 0;
- for (UInt32 i = 0; i < header.record_count; ++i)
- {
- // Generate new string block offsets
- for (UInt32 j = 0; j < 9; ++j)
- {
- if (body.records[i].SpellName[j] != 0)
- {
- VirtualStrTableEntry temp;
- body.strings.TryGetValue(body.records[i].SpellName[j] - 1, out temp);
- temp.newValue = stringBlockOffset;
- body.records[i].SpellName[j] = stringBlockOffset;
- if (temp.value.Length == 0 && stringBlockOffset == 0)
- ++stringBlockOffset;
- else
- stringBlockOffset += (UInt32)temp.value.Length + 1;
- }
- if (body.records[i].ToolTip[j] != 0)
- {
- VirtualStrTableEntry temp;
- body.strings.TryGetValue(body.records[i].ToolTip[j] - 1, out temp);
- temp.newValue = stringBlockOffset;
- body.records[i].ToolTip[j] = stringBlockOffset;
- if (temp.value.Length == 0 && stringBlockOffset == 0)
- ++stringBlockOffset;
- else
- stringBlockOffset += (UInt32)temp.value.Length + 1;
- }
- if (body.records[i].Description[j] != 0)
- {
- VirtualStrTableEntry temp;
- body.strings.TryGetValue(body.records[i].Description[j] - 1, out temp);
- temp.newValue = stringBlockOffset;
- body.records[i].Description[j] = stringBlockOffset;
- if (temp.value.Length == 0 && stringBlockOffset == 0)
- ++stringBlockOffset;
- else
- stringBlockOffset += (UInt32)temp.value.Length + 1;
- }
- if (body.records[i].Rank[j] != 0)
- {
- VirtualStrTableEntry temp;
- body.strings.TryGetValue(body.records[i].Rank[j] - 1, out temp);
- temp.newValue = stringBlockOffset;
- body.records[i].Rank[j] = stringBlockOffset;
- if (temp.value.Length == 0 && stringBlockOffset == 0)
- ++stringBlockOffset;
- else
- stringBlockOffset += (UInt32)temp.value.Length + 1;
- }
- }
- }
- // Here was 2317797
- // becomes 5142725
- header.string_block_size = (int)stringBlockOffset;
- if (File.Exists(fileName))
- File.Delete(fileName);
- FileStream fs = new FileStream(fileName, FileMode.Create);
- BinaryWriter writer = new BinaryWriter(fs);
- // Write header
- int count = Marshal.SizeOf(typeof(SpellDBC_Header));
- byte[] buffer = new byte[count];
- GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
- Marshal.StructureToPtr(header, gcHandle.AddrOfPinnedObject(), true);
- writer.Write(buffer, 0, count);
- gcHandle.Free();
- // Write records
- for (UInt32 i = 0; i < header.record_count; ++i)
- {
- // Write main body
- count = Marshal.SizeOf(typeof(SpellDBC_Record));
- buffer = new byte[count];
- gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
- Marshal.StructureToPtr(body.records[i], gcHandle.AddrOfPinnedObject(), true);
- writer.Write(buffer, 0, count);
- gcHandle.Free();
- }
- // Write string block
- foreach (KeyValuePair<UInt32, VirtualStrTableEntry> entry in body.strings)
- {
- string toWrite = entry.Value.value + "\0";
- writer.Write(Encoding.UTF8.GetBytes(toWrite), 0, entry.Value.value.Length + 1);
- }
- writer.Close();
- fs.Close();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment