stoneharry

Untitled

Oct 28th, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. //AbstractDBC.cs:
  2.  
  3.         public struct DBC_Header
  4.         {
  5.             public uint Magic;
  6.             public uint RecordCount;
  7.             public uint FieldCount;
  8.             public uint RecordSize;
  9.             public int StringBlockSize;
  10.         };
  11.  
  12.         public class DBC_Body<RecordMap>
  13.         {
  14.             public RecordMap[] records;
  15.         };
  16.  
  17. //SpellDBC.cs:
  18.  
  19.                 DBCReader reader = new DBCReader("DBC/Spell.dbc");
  20.                 DBC_Header header = reader.ReadDBCHeader();
  21.                 body = new DBC_Body<Spell_DBC_RecordMap>();
  22.                 reader.ReadDBCRecords<Spell_DBC_RecordMap, Spell_DBC_Record>(body, Marshal.SizeOf(typeof(Spell_DBC_Record)));
  23.  
  24. //DBCReader.cs:
  25.  
  26.         // RecordContainer =
  27.         /*
  28.         public struct Spell_DBC_RecordMap
  29.         {
  30.             public Spell_DBC_Record record;
  31.             public string[] spellName;
  32.             public string[] spellRank;
  33.             public string[] spellDesc;
  34.             public string[] spellTool;
  35.         };*/
  36.         // RecordStruct = Spell_DBC_Record (3.3.5a struct)
  37.         public void ReadDBCRecords<RecordContainer, RecordStruct>(DBC_Body<RecordContainer> body, int recordSize)
  38.         {
  39.             if (header.RecordSize != recordSize)
  40.                 throw new Exception($"This DBC [{ filePath }] is not supported! It's version is not 3.3.5a 12340, expected record size [{ header.RecordSize }] got [{ recordSize }].");
  41.  
  42.             body.records = new RecordContainer[header.RecordCount];
  43.             byte[] readBuffer;
  44.             using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
  45.             {
  46.                 using (BinaryReader reader = new BinaryReader(fileStream))
  47.                 {
  48.                     reader.BaseStream.Position = filePosition;
  49.                     for (uint i = 0; i < header.RecordCount; ++i)
  50.                     {
  51.                         readBuffer = new byte[recordSize];
  52.                         readBuffer = reader.ReadBytes(recordSize);
  53.  
  54.                         // This line does read the data correctly and has all the correct values
  55.                         RecordStruct record = ReadStruct<RecordStruct>(reader, readBuffer);
  56.                         FieldInfo field = typeof(RecordContainer).GetField("record");
  57.                         field.SetValue(body.records[i], record);
  58.                         // The above 2 lines do not seem to update the array :(
  59.                     }
  60.                     filePosition = reader.BaseStream.Position;
  61.                 }
  62.             }
  63.         }
Advertisement
Add Comment
Please, Sign In to add comment