stoneharry

Untitled

Apr 18th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. public bool SaveDBCFile(string fileName)
  2. {
  3.     try
  4.     {
  5.         // This gets complicated fast
  6.  
  7.         UInt32 stringBlockOffset = 0;
  8.         for (UInt32 i = 0; i < header.record_count; ++i)
  9.         {
  10.             // Generate new string block offsets
  11.             for (UInt32 j = 0; j < 9; ++j)
  12.             {
  13.                 if (body.records[i].SpellName[j] != 0)
  14.                 {
  15.                     VirtualStrTableEntry temp;
  16.                     body.strings.TryGetValue(body.records[i].SpellName[j] - 1, out temp);
  17.                     temp.newValue = stringBlockOffset;
  18.                     body.records[i].SpellName[j] = stringBlockOffset;
  19.                     if (temp.value.Length == 0 && stringBlockOffset == 0)
  20.                         ++stringBlockOffset;
  21.                     else
  22.                         stringBlockOffset += (UInt32)temp.value.Length + 1;
  23.                 }
  24.                 if (body.records[i].ToolTip[j] != 0)
  25.                 {
  26.                     VirtualStrTableEntry temp;
  27.                     body.strings.TryGetValue(body.records[i].ToolTip[j] - 1, out temp);
  28.                     temp.newValue = stringBlockOffset;
  29.                     body.records[i].ToolTip[j] = stringBlockOffset;
  30.                     if (temp.value.Length == 0 && stringBlockOffset == 0)
  31.                         ++stringBlockOffset;
  32.                     else
  33.                         stringBlockOffset += (UInt32)temp.value.Length + 1;
  34.                 }
  35.                 if (body.records[i].Description[j] != 0)
  36.                 {
  37.                     VirtualStrTableEntry temp;
  38.                     body.strings.TryGetValue(body.records[i].Description[j] - 1, out temp);
  39.                     temp.newValue = stringBlockOffset;
  40.                     body.records[i].Description[j] = stringBlockOffset;
  41.                     if (temp.value.Length == 0 && stringBlockOffset == 0)
  42.                         ++stringBlockOffset;
  43.                     else
  44.                         stringBlockOffset += (UInt32)temp.value.Length + 1;
  45.                 }
  46.                 if (body.records[i].Rank[j] != 0)
  47.                 {
  48.                     VirtualStrTableEntry temp;
  49.                     body.strings.TryGetValue(body.records[i].Rank[j] - 1, out temp);
  50.                     temp.newValue = stringBlockOffset;
  51.                     body.records[i].Rank[j] = stringBlockOffset;
  52.                     if (temp.value.Length == 0 && stringBlockOffset == 0)
  53.                         ++stringBlockOffset;
  54.                     else
  55.                         stringBlockOffset += (UInt32)temp.value.Length + 1;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         // Here was 2317797
  61.         // becomes 5142725
  62.         header.string_block_size = (int)stringBlockOffset;
  63.  
  64.         if (File.Exists(fileName))
  65.             File.Delete(fileName);
  66.         FileStream fs = new FileStream(fileName, FileMode.Create);
  67.         BinaryWriter writer = new BinaryWriter(fs);
  68.  
  69.         // Write header
  70.         int count = Marshal.SizeOf(typeof(SpellDBC_Header));
  71.         byte[] buffer = new byte[count];
  72.         GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  73.         Marshal.StructureToPtr(header, gcHandle.AddrOfPinnedObject(), true);
  74.         writer.Write(buffer, 0, count);
  75.         gcHandle.Free();
  76.  
  77.         // Write records
  78.         for (UInt32 i = 0; i < header.record_count; ++i)
  79.         {
  80.             // Write main body
  81.             count = Marshal.SizeOf(typeof(SpellDBC_Record));
  82.             buffer = new byte[count];
  83.             gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  84.             Marshal.StructureToPtr(body.records[i], gcHandle.AddrOfPinnedObject(), true);
  85.             writer.Write(buffer, 0, count);
  86.             gcHandle.Free();
  87.         }
  88.  
  89.         // Write string block
  90.         foreach (KeyValuePair<UInt32, VirtualStrTableEntry> entry in body.strings)
  91.         {
  92.             string toWrite = entry.Value.value + "\0";
  93.             writer.Write(Encoding.UTF8.GetBytes(toWrite), 0, entry.Value.value.Length + 1);
  94.         }
  95.  
  96.         writer.Close();
  97.         fs.Close();
  98.     }
  99.     catch (Exception ex)
  100.     {
  101.         Console.WriteLine(ex.ToString());
  102.         return false;
  103.     }
  104.  
  105.     return true;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment