/** * Reads the string block from the DBC file and saves it to the stringsMap * The position is saved into the map value so that spell records can * reverse lookup strings. */ public void ReadStringBlock() { string StringBlock; using (FileStream fileStream = new FileStream(_filePath, FileMode.Open)) { using (BinaryReader reader = new BinaryReader(fileStream)) { reader.BaseStream.Position = _filePosition; _stringsMap = new Dictionary <uint, VirtualStrTableEntry>(); StringBlock = Encoding.UTF8.GetString(reader.ReadBytes(_header.StringBlockSize)); string temp = ""; uint lastString = 0; uint counter = 0; int length = new System.Globalization.StringInfo(StringBlock).LengthInTextElements; while (counter < length) { var t = StringBlock[(int)counter]; if (t == '\0') { VirtualStrTableEntry n = new VirtualStrTableEntry(); n.Value = temp; n.NewValue = 0; _stringsMap.Add(lastString, n); lastString += (uint)Encoding.UTF8.GetByteCount(temp) + 1; temp = ""; } else { temp += t; } ++counter; } } } }
public bool LoadDBCFile(MainWindow window) { main = window; try { FileStream fileStream = new FileStream("DBC/Spell.dbc", FileMode.Open); fileSize = fileStream.Length; int count = Marshal.SizeOf(typeof(DBC_Header)); byte[] readBuffer = new byte[count]; BinaryReader reader = new BinaryReader(fileStream); readBuffer = reader.ReadBytes(count); GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned); header = (DBC_Header)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DBC_Header)); handle.Free(); body.records = new Spell_DBC_RecordMap[header.RecordCount]; count = Marshal.SizeOf(typeof(Spell_DBC_Record)); if (header.RecordSize != count) { throw new Exception("This Spell DBC version is not supported! It is not 3.3.5a."); } for (UInt32 i = 0; i < header.RecordCount; ++i) { readBuffer = new byte[count]; readBuffer = reader.ReadBytes(count); handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned); body.records[i].record = (Spell_DBC_Record)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Spell_DBC_Record)); handle.Free(); } string StringBlock; Dictionary <UInt32, VirtualStrTableEntry> strings = new Dictionary <UInt32, VirtualStrTableEntry>(); StringBlock = Encoding.UTF8.GetString(reader.ReadBytes(header.StringBlockSize)); string temp = ""; UInt32 lastString = 0; UInt32 counter = 0; Int32 length = new System.Globalization.StringInfo(StringBlock).LengthInTextElements; while (counter < length) { var t = StringBlock[(int)counter]; if (t == '\0') { VirtualStrTableEntry n = new VirtualStrTableEntry(); n.Value = temp; n.NewValue = 0; strings.Add(lastString, n); lastString += (UInt32)Encoding.UTF8.GetByteCount(temp) + 1; temp = ""; } else { temp += t; } ++counter; } StringBlock = null; for (int i = 0; i < body.records.Length; ++i) { body.records[i].spellName = new string[9]; body.records[i].spellRank = new string[9]; body.records[i].spellDesc = new string[9]; body.records[i].spellTool = new string[9]; for (int j = 0; j < 9; ++j) { body.records[i].spellName[j] = strings[body.records[i].record.SpellName[j]].Value; body.records[i].spellRank[j] = strings[body.records[i].record.SpellRank[j]].Value; body.records[i].spellDesc[j] = strings[body.records[i].record.SpellDescription[j]].Value; body.records[i].spellTool[j] = strings[body.records[i].record.SpellToolTip[j]].Value; } } reader.Close(); fileStream.Close(); foreach (Spell_DBC_RecordMap map in body.records) { tryGenerate(map, 0, window); } } catch (Exception ex) { main.HandleErrorMessage(ex.Message); return(false); } return(true); }
// End Files public bool LoadDBCFile(MainWindow window) { main = window; try { FileStream fileStream = new FileStream("DBC/Spell.dbc", FileMode.Open); fileSize = fileStream.Length; int count = Marshal.SizeOf(typeof(DBC_Header)); byte[] readBuffer = new byte[count]; BinaryReader reader = new BinaryReader(fileStream); readBuffer = reader.ReadBytes(count); GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned); header = (DBC_Header)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DBC_Header)); handle.Free(); body.records = new Spell_DBC_RecordMap[header.RecordCount]; count = Marshal.SizeOf(typeof(Spell_DBC_Record)); if (header.RecordSize != count) throw new Exception("This Spell DBC version is not supported! It is not 3.3.5a."); for (UInt32 i = 0; i < header.RecordCount; ++i) { readBuffer = new byte[count]; readBuffer = reader.ReadBytes(count); handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned); body.records[i].record = (Spell_DBC_Record)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Spell_DBC_Record)); handle.Free(); } string StringBlock; Dictionary<UInt32, VirtualStrTableEntry> strings = new Dictionary<UInt32, VirtualStrTableEntry>(); StringBlock = Encoding.UTF8.GetString(reader.ReadBytes(header.StringBlockSize)); string temp = ""; UInt32 lastString = 0; UInt32 counter = 0; Int32 length = new System.Globalization.StringInfo(StringBlock).LengthInTextElements; while (counter < length) { var t = StringBlock[(int)counter]; if (t == '\0') { VirtualStrTableEntry n = new VirtualStrTableEntry(); n.Value = temp; n.NewValue = 0; strings.Add(lastString, n); lastString += (UInt32)Encoding.UTF8.GetByteCount(temp) + 1; temp = ""; } else { temp += t; } ++counter; } StringBlock = null; for (int i = 0; i < body.records.Length; ++i) { body.records[i].spellName = new string[9]; body.records[i].spellRank = new string[9]; body.records[i].spellDesc = new string[9]; body.records[i].spellTool = new string[9]; for (int j = 0; j < 9; ++j) { body.records[i].spellName[j] = strings[body.records[i].record.SpellName[j]].Value; body.records[i].spellRank[j] = strings[body.records[i].record.SpellRank[j]].Value; body.records[i].spellDesc[j] = strings[body.records[i].record.SpellDescription[j]].Value; body.records[i].spellTool[j] = strings[body.records[i].record.SpellToolTip[j]].Value; } } reader.Close(); fileStream.Close(); } catch (Exception ex) { main.HandleErrorMessage(ex.Message); return false; } return true; }