Skip to content

Instantly share code, notes, and snippets.

@vermie
Created July 4, 2010 06:29
Show Gist options
  • Save vermie/463214 to your computer and use it in GitHub Desktop.
Save vermie/463214 to your computer and use it in GitHub Desktop.
class Record
{
public object[] Fields
{
get;
private set;
}
public Record(int fieldCount, BinaryReader input)
{
this.Fields = new object[fieldCount];
for (int i = 0; i < fieldCount; i++)
this.Fields[i] = input.ReadInt32();
}
}
class DBCFile
{
private Record[] records;
private char[] stringData;
public DBCFile(string path)
{
// load dbc into memory
BinaryReader input = new BinaryReader(
new FileStream(path, FileMode.Open), // file
Encoding.UTF8); // encoding
// read magic string
if (new string(input.ReadChars(4)) != "WDBC")
throw new Exception(string.Format("{0} is not a DBC file.", path));
// get record count
this.records = new Record[input.ReadInt32()];
// get field count
int fieldCount = input.ReadInt32();
// get record size
/*int recordSize = */input.ReadInt32();
// get string size
int stringSize = input.ReadInt32();
// load records
for (int i = 0; i < this.records.Length; i++)
records[i] = new Record(fieldCount, input);
// read strings
this.stringData = input.ReadChars(stringSize);
// find and store likely strings
foreach (Record record in this.records)
{
for(int i = 0; i < fieldCount; i++)
if(isString((int)record.Fields[i]))
record.Fields[i] = getString((int)record.Fields[i]);
}
}
private bool isString(int index)
{
if (index <= 0 || index >= this.stringData.Length)
return false;
return this.stringData[index - 1] == '\0';
}
private string getString(int index)
{
StringBuilder builder = new StringBuilder();
char c;
while ((c = this.stringData[index++]) != '\0')
builder.Append(c);
return builder.ToString();
}
static void Main(string[] args)
{
new DBCFile(@"C:\wowemu\mangos\dbc\Achievement.dbc");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment