昨天闲着无聊把wow2.33中的技能说明都给提取了出来,想在自己单机版的武器上添加绿字技能玩,下面是代码(我比较懒所以只提取的技能ID,名称和说明部分,好多地方的实现不是很好)
Spell.dbc结构:
Header(文件头20字节)
Records.................行数(4字节)
Fields....................列数(4字节)
Record Size...............每行所占字节(4字节)
String Block Size.....字符区所占字节(4字节)
Data(数据区 共Records * Record Size字节)-----存技能各种数据
row1.....(占Record Size字节)
row2.....(占Record Size字节)
Text(字符区 共String Block Size字节)------存技能文字说明等字符信息
第一行总为\0,后面的遇到\0为一行
例:技能名称提取 先由Data中找出技能名称所在字段,在127列*4byte=508byte位置得到技能名称在Text区的偏移量,String Block Size+偏移量就能得到技能名称的位置
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace WowItemTool
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
[Serializable]
public class spellInfo
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
public int id;
public string name, type, memo;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class spell
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private byte[] m_stringTable;
private byte[] rowData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public List<spellInfo> GetSpellList()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int rows, cols, rlen, tlen;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<spellInfo> list=new List<spellInfo>();
//打开文件
FileStream file = new FileStream("c:\dbc\spell.dbc",FileMode.Open,FileAccess.Read);
BinaryReader br= new BinaryReader(file);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取文件头信息
if (br.ReadUInt32() != 0x43424457) // WDBC标志
return list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rows = br.ReadInt32();//行数
cols = br.ReadInt32();//列数
rlen = br.ReadInt32();//行占字节
tlen = br.ReadInt32();//文本块字节
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//读取文本块
br.BaseStream.Position = br.BaseStream.Length-tlen;
byte[] stringTable = br.ReadBytes(tlen);
m_stringTable = stringTable;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//读技能信息
br.BaseStream.Position = 20;
for (int i = 0; i < rows; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
spellInfo spell = new spellInfo();
rowData = br.ReadBytes(rlen);
spell.id = GetInt32(rowData, 0);//技能ID
spell.name = GetString(rowData, 127).Replace('', ' ').Trim();//技能名称
spell.type = GetString(rowData, 144).Replace('', ' ').Trim();//技能属性
spell.memo = GetString(rowData, 161).Replace('', ' ').Trim();//技能介绍
list.Add(spell);
}
file.Close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SaveTxt("c:\MyFile.txt",list);
//BINserializer("c:\MyFile.txt", list);
//List<spellInfo> l = (List<spellInfo>)BINDeserialize("c:\MyFile.txt");
//XMLserializer("c:\record.xml", list, new Type[] { typeof(spellInfo) });
//sp1 = (List<spellInfo>)XMLDeserialize("c:\record.xml",list, new Type[] { typeof(spellInfo) });
return list;
}
//导出到纯文本
public void SaveTxt(string fn, List<spellInfo> list)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
FileStream file = new FileStream(fn, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(file);
for (int i = 0; i < list.Count; i++)
sw.WriteLine(list[i].id + " " + list[i].name + " " + list[i].type + " " + list[i].memo);
sw.Close();
}
//序列化二进制文件
public void BINserializer(string fn, object o)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(fn, FileMode.Create,FileAccess.Write, FileShare.None);
formatter.Serialize(stream, o);
stream.Close();
}
//反序列化二进制文件
private object BINDeserialize(string fn)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(fn, FileMode.Open,FileAccess.Read, FileShare.Read);
object ro = formatter.Deserialize(stream);
stream.Close();
return ro;
}
//序列化XML文件
public void XMLserializer(string fn,object o,Type[] type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
XmlSerializer serializer = new XmlSerializer(o.GetType(), type);
TextWriter writer = new StreamWriter(fn);
serializer.Serialize(writer, o);
writer.Close();
}
//反序列化XML文件
private object XMLDeserialize(string fn, object o,Type[] type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
XmlSerializer Serializer = new XmlSerializer(o.GetType(), type);
TextReader read = new StreamReader(fn);
object ro=Serializer.Deserialize(read);
read.Close();
return ro;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string GetString(byte[] data, uint field)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding();
uint startOffset = GetUInt32(data, field);
byte[] s=new byte[1024];
int i = 0;
while ((s[i] = m_stringTable[startOffset++]) != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
i++;
}
return converter.GetString(s);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected static unsafe uint GetUInt32(byte[] data, uint field)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
uint startIndex = field * 4;
if (startIndex + 4 > data.Length)
return uint.MaxValue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fixed (byte* pData = &data[startIndex])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return *(uint*)pData;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected static unsafe int GetInt32(byte[] data, uint field)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
uint startIndex = field * 4;
if (startIndex + 4 > data.Length)
return int.MaxValue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fixed (byte* pData = &data[startIndex])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return *(int*)pData;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
本文原创,由csdn搬家过来