Exemplo n.º 1
0
        public static IWowClientDBReader GetReader(string file, Table def)
        {
            IWowClientDBReader reader;

            var ext = Path.GetExtension(file).ToUpperInvariant();
            if (ext == ".DBC")
                reader = new DBCReader(file);
            else if (ext == ".DB2")
                try
                {
                    reader = new DB2Reader(file);
                }
                catch
                {
                    try
                    {
                        reader = new DB3Reader(file);
                    }
                    catch
                    {
                        try
                        {
                            reader = new DB4Reader(file);
                        }
                        catch
                        {
                            reader = new DB5Reader(file, def);
                        }
                    }
                }
            else if (ext == ".ADB")
                reader = new ADBReader(file);
            else if (ext == ".WDB")
                reader = new WDBReader(file);
            else if (ext == ".STL")
                reader = new STLReader(file);
            else
                throw new InvalidDataException(string.Format("Unknown file type {0}", ext));

            return reader;
        }
Exemplo n.º 2
0
        private Table CreateDefaultDefinition()
        {
            var ext = Path.GetExtension(m_mainForm.DBCFile).ToUpperInvariant();

            if (ext != ".DBC" && ext != ".DB2") // only for dbc and db2, as other formats have no fields count stored
                return null;

            using (var br = new BinaryReader(new FileStream(m_mainForm.DBCFile, FileMode.Open)))
            {
                var magic = br.ReadUInt32();
                var recordsCount = br.ReadUInt32();
                var fieldsCount = br.ReadUInt32();
                var recordsize = br.ReadUInt32();

                if (magic == DBCReader.DBCFmtSig)
                {
                    // only for files with 4 byte fields (most of dbc's)
                    if ((recordsize % fieldsCount == 0) && (fieldsCount * 4 == recordsize))
                    {
                        var def = new Table();

                        def.Name = m_mainForm.DBCName;
                        def.Fields = new List<Field>();

                        for (int i = 0; i < fieldsCount; i++)
                        {
                            var field = new Field();

                            if (i == 0)
                            {
                                field.IsIndex = true;
                                field.Name = "m_ID";
                            }
                            else
                            {
                                field.Name = string.Format("field{0}", i);
                            }

                            field.Type = "int";

                            def.Fields.Add(field);
                        }

                        m_changed = true;
                        return def;
                    }
                }
                else if (magic == DB5Reader.DB5FmtSig)
                {
                    br.BaseStream.Position = 0;

                    DB5Reader db5 = new DB5Reader(br, true);

                    var def = new Table();

                    def.Name = m_mainForm.DBCName;
                    def.Fields = new List<Field>();

                    for (int i = 0; i < db5.Meta.Count; i++)
                    {
                        var field = new Field();

                        if (i == db5.IdIndex)
                        {
                            field.IsIndex = true;
                            field.Name = "m_ID";
                            field.Type = "int";
                        }
                        else
                        {
                            field.Name = string.Format("field{0:X2}", db5.Meta[i].Offset);

                            int bits = db5.Meta[i].Bits;

                            if (bits == 0x18)
                                field.Type = "byte";
                            else if (bits == 0x10)
                                field.Type = "ushort";
                            else if (bits == 0x08)
                                field.Type = "int";
                            else if (bits == 0x00)
                                field.Type = "int";
                            else if (bits == -32)
                                field.Type = "ulong";
                            else
                                throw new Exception("New Bits value detected!");

                            int byteCount = (32 - bits) >> 3;

                            // array
                            if (i + 1 < db5.Meta.Count && db5.Meta[i].Offset + byteCount != db5.Meta[i + 1].Offset)
                            {
                                int arraySize = (db5.Meta[i + 1].Offset - db5.Meta[i].Offset) / byteCount;

                                field.ArraySize = arraySize;
                            }

                            // array (last field)
                            int rowSize = db5.HasIndexTable ? db5.RecordSize + 4 : db5.RecordSize;
                            if (i + 1 == db5.Meta.Count && db5.Meta[i].Offset + byteCount != rowSize)
                            {
                                int diff = rowSize - db5.Meta[i].Offset;

                                if (diff >= byteCount * 2)
                                {
                                    int arraySize = diff / byteCount;

                                    field.ArraySize = arraySize;
                                }
                            }
                        }

                        def.Fields.Add(field);
                    }

                    m_changed = true;
                    return def;
                }
            }

            return null;
        }
Exemplo n.º 3
0
        public static IWowClientDBReader GetReader(string file, XmlElement definition)
        {
            IWowClientDBReader reader;

            var ext = Path.GetExtension(file).ToUpperInvariant();

            if (ext == ".DBC")
            {
                reader = new DBCReader(file);
            }
            else if (ext == ".DB2")
            {
                using (var binaryReader = BinaryReaderExtensions.FromFile(file))
                {
                    switch (binaryReader.ReadUInt32())
                    {
                    case DB2Reader.DB2FmtSig:
                    {
                        reader = new DB2Reader(file);
                        break;
                    }

                    case DB3Reader.DB3FmtSig:
                    {
                        reader = new DB2Reader(file);
                        break;
                    }

                    case DB4Reader.DB4FmtSig:
                    {
                        try
                        {
                            reader = new DB4Reader(file);
                        }
                        catch
                        {
                            reader = new DB4SparseReader(file);
                        }
                        break;
                    }

                    case DB5Reader.DB5FmtSig:
                    {
                        try
                        {
                            reader = new DB5Reader(file, definition);
                        }
                        catch
                        {
                            reader = new DB5SparseReader(file);
                        }
                        break;
                    }

                    default:
                        throw new InvalidDataException(string.Format("Unknown file type {0}", ext));
                    }
                }
            }
            else if (ext == ".ADB")
            {
                reader = new ADBReader(file);
            }
            else if (ext == ".WDB")
            {
                reader = new WDBReader(file);
            }
            else if (ext == ".STL")
            {
                reader = new STLReader(file);
            }
            else
            {
                throw new InvalidDataException(String.Format("Unknown file type {0}", ext));
            }

            return(reader);
        }
Exemplo n.º 4
0
        public static IWowClientDBReader GetReader(string file, XmlElement definition)
        {
            IWowClientDBReader reader;

            var ext = Path.GetExtension(file).ToUpperInvariant();
            if (ext == ".DBC")
                reader = new DBCReader(file);
            else if (ext == ".DB2")
            {
                using (var binaryReader = BinaryReaderExtensions.FromFile(file))
                {
                    switch (binaryReader.ReadUInt32())
                    {
                        case DB2Reader.DB2FmtSig:
                        {
                            reader = new DB2Reader(file);
                            break;
                        }
                        case DB3Reader.DB3FmtSig:
                        {
                            reader = new DB2Reader(file);
                            break;
                        }
                        case DB4Reader.DB4FmtSig:
                        {
                            try
                            {
                                reader = new DB4Reader(file);
                            }
                            catch
                            {
                                reader = new DB4SparseReader(file);
                            }
                            break;
                        }
                        case DB5Reader.DB5FmtSig:
                        {
                            try
                            {
                                reader = new DB5Reader(file, definition);
                            }
                            catch
                            {
                                reader = new DB5SparseReader(file);
                            }
                            break;
                        }
                        default:
                            throw new InvalidDataException(string.Format("Unknown file type {0}", ext));
                    }
                }
            }
            else if (ext == ".ADB")
                reader = new ADBReader(file);
            else if (ext == ".WDB")
                reader = new WDBReader(file);
            else if (ext == ".STL")
                reader = new STLReader(file);
            else
                throw new InvalidDataException(String.Format("Unknown file type {0}", ext));

            return reader;
        }
Exemplo n.º 5
0
        private XmlElement CreateDefaultDefinition()
        {
            if (reader is DB3Reader)
            {
                if (reader.RecordSize / reader.FieldsCount == 4)
                {
                    var doc = new XmlDocument();

                    XmlElement newnode = doc.CreateElement(DBCName);
                    newnode.SetAttributeNode("build", "").Value = reader.Build.ToString();

                    for (int i = 0; i < reader.FieldsCount; ++i)
                    {
                        XmlElement ele = doc.CreateElement("field");
                        ele.SetAttributeNode("type", "").Value = "int";
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}", i);
                        newnode.AppendChild(ele);
                    }

                    return(newnode);
                }
                else
                {
                    var doc = new XmlDocument();

                    XmlElement newnode = doc.CreateElement(DBCName);
                    newnode.SetAttributeNode("build", "").Value = reader.Build.ToString();

                    for (int i = 0; i < reader.FieldsCount; ++i)
                    {
                        /*if (i == 0)
                         * {
                         *  XmlElement index = doc.CreateElement("index");
                         *  XmlNode primary = index.AppendChild(doc.CreateElement("primary"));
                         *  primary.InnerText = "field0";
                         *  newnode.AppendChild(index);
                         * }*/

                        XmlElement ele = doc.CreateElement("field");
                        ele.SetAttributeNode("type", "").Value = "ushort";
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}", i);
                        newnode.AppendChild(ele);
                    }

                    return(newnode);
                }
            }
            else if (reader is DB5Reader)
            {
                DB5Reader db5reader = (DB5Reader)reader;

                var doc = new XmlDocument();

                XmlElement newnode = doc.CreateElement(DBCName);
                newnode.SetAttributeNode("build", "").Value = reader.Build.ToString();

                XmlElement tmpnode = doc.CreateElement(DBCName);

                List <fType> types = new List <fType>();


                for (int i = 0; i < db5reader.typeList.Count; i++)
                {
                    if (i < db5reader.typeList.Count - 1 && ((32 - db5reader.typeList[i][0]) / 8) != db5reader.typeList[i + 1][1] - db5reader.typeList[i][1])
                    {
                        int count = (db5reader.typeList[i + 1][1] - db5reader.typeList[i][1]) / ((32 - db5reader.typeList[i][0]) / 8);


                        fType ftype1;
                        ftype1.typeName = typeCov(((32 - db5reader.typeList[i][0]) / 8));
                        ftype1.isArray  = 1;

                        types.Add(ftype1);

                        for (int j = 0; j < count - 1; j++)
                        {
                            fType ftype;
                            ftype.typeName = typeCov(((32 - db5reader.typeList[i][0]) / 8));
                            ftype.isArray  = 2;

                            types.Add(ftype);
                        }
                    }
                    else
                    {
                        fType ftype;
                        ftype.typeName = typeCov(((32 - db5reader.typeList[i][0]) / 8));
                        ftype.isArray  = 0;
                        types.Add(ftype);
                    }
                }

                int ar = 0;

                int temp = 0;


                for (int i = 0; i < types.Count; i++)
                {
                    XmlElement ele = doc.CreateElement("field");



                    ele.SetAttributeNode("type", "").Value = types[i].typeName;

                    if (types[i].isArray == 1)
                    {
                        ar = 1;
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}_{1}", i, ar);
                        temp = i;
                    }
                    else if (types[i].isArray == 2)
                    {
                        ar++;
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}_{1}", temp, ar);
                    }
                    else
                    {
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}", i);
                        ar   = 0;
                        temp = i;
                    }

                    tmpnode.AppendChild(ele);
                    // i++;
                }

                try
                {
                    dbreader = DBReaderFactory.GetReader(DBCFile);
                }
                catch (Exception ex)
                {
                }

                DataTable dt = new DataTable();

                dt = CreateColumns(dt, tmpnode.ChildNodes);

                for (int i = 0; i < reader.RecordsCount; i++)
                {
                    DataRow dataRow = dt.NewRow();

                    using (BinaryReader br = dbreader[i])
                    {
                        for (int j = 0; j < types.Count; j++)
                        {
                            switch (types[j].typeName)
                            {
                            case "long":
                                dataRow[j] = br.ReadInt64();
                                break;

                            case "ulong":
                                dataRow[j] = br.ReadUInt64();
                                break;

                            case "int":
                                dataRow[j] = br.ReadInt32();
                                break;

                            case "uint":
                                dataRow[j] = br.ReadUInt32();
                                break;

                            case "short":
                                dataRow[j] = br.ReadInt16();
                                break;

                            case "ushort":
                                dataRow[j] = br.ReadUInt16();
                                break;

                            case "sbyte":
                                dataRow[j] = br.ReadSByte();
                                break;

                            case "byte":
                                dataRow[j] = br.ReadByte();
                                break;

                            case "float":
                                dataRow[j] = br.ReadSingle();
                                break;

                            case "double":
                                dataRow[j] = br.ReadDouble();
                                break;

                            case "index":
                                dataRow[j] = m_dbreader.index[i];
                                break;

                            case "int3":
                                byte[] bytes = br.ReadBytes(3);

                                byte[] buffer = new byte[4];

                                Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);

                                dataRow[j] = BitConverter.ToInt32(buffer, 0);

                                break;

                            case "string":
                                if (m_dbreader is WDBReader)
                                {
                                    dataRow[j] = br.ReadStringNull();
                                }
                                else if (m_dbreader is STLReader)
                                {
                                    int offset = br.ReadInt32();
                                    dataRow[j] = (m_dbreader as STLReader).ReadString(offset);
                                }
                                else
                                {
                                    try
                                    {
                                        dataRow[j] = m_dbreader.StringTable[br.ReadInt32()];
                                    }
                                    catch
                                    {
                                        dataRow[j] = "Invalid string index!";
                                    }
                                }
                                break;

                            case "nstring":
                                try
                                {
                                    dataRow[j] = br.ReadStringNull();
                                }
                                catch
                                {
                                    dataRow[j] = "Invalid string index!";
                                }
                                break;

                            case "cstring":
                                dataRow[j] = br.ReadStringNumber();
                                break;

                            default:
                                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "Unknown field type {0}!", types[j]));
                            }
                        }
                    }
                    dt.Rows.Add(dataRow);
                }



                for (int i = 0; i < types.Count; i++)
                {
                    bool isString = true;

                    if (types[i].typeName.Equals("int"))
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (!reader.StringTable.ContainsKey((int)dr[i]))
                            {
                                isString = false;
                                break;
                            }
                        }
                        if (isString)
                        {
                            fType ft;
                            ft.typeName = "string";
                            ft.isArray  = types[i].isArray;

                            types[i] = ft;
                        }
                    }
                }

                if (db5reader.index.Count > 0)
                {
                    XmlElement elei = doc.CreateElement("field");
                    elei.SetAttributeNode("type", "").Value = "index";
                    elei.SetAttributeNode("name", "").Value = String.Format("m_ID");
                    newnode.AppendChild(elei);
                }


                ar = 0;

                temp = 0;


                for (int i = 0; i < types.Count; i++)
                {
                    XmlElement ele = doc.CreateElement("field");



                    ele.SetAttributeNode("type", "").Value = types[i].typeName;

                    if (types[i].isArray == 1)
                    {
                        ar = 1;
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}_{1}", i, ar);
                        temp = i;
                    }
                    else if (types[i].isArray == 2)
                    {
                        ar++;
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}_{1}", temp, ar);
                    }
                    else
                    {
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}", i);
                        ar   = 0;
                        temp = i;
                    }

                    newnode.AppendChild(ele);
                    // i++;
                }


                return(newnode);
            }
            else
            {
                if (reader.RecordSize % reader.FieldsCount == 0) // only for files with 4 byte fields
                {
                    var doc = new XmlDocument();

                    XmlElement newnode = doc.CreateElement(DBCName);
                    newnode.SetAttributeNode("build", "").Value = reader.Build.ToString();

                    for (int i = 0; i < reader.FieldsCount; ++i)
                    {
                        XmlElement ele = doc.CreateElement("field");
                        ele.SetAttributeNode("type", "").Value = "int";
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}", i);
                        newnode.AppendChild(ele);
                    }

                    return(newnode);
                }
                else
                {
                    var doc = new XmlDocument();

                    XmlElement newnode = doc.CreateElement(DBCName);
                    newnode.SetAttributeNode("build", "").Value = reader.Build.ToString();

                    for (int i = 0; i < reader.FieldsCount; ++i)
                    {
                        XmlElement ele = doc.CreateElement("field");
                        ele.SetAttributeNode("type", "").Value = "ushort";
                        ele.SetAttributeNode("name", "").Value = String.Format("field{0}", i);
                        newnode.AppendChild(ele);
                    }

                    return(newnode);
                }
            }

            //return null;
        }
Exemplo n.º 6
0
        private Table CreateDefaultDefinition()
        {
            var ext = Path.GetExtension(m_mainForm.DBCFile).ToUpperInvariant();

            if (ext != ".DBC" && ext != ".DB2") // only for dbc and db2, as other formats have no fields count stored
            {
                return(null);
            }

            using (var br = new BinaryReader(new FileStream(m_mainForm.DBCFile, FileMode.Open)))
            {
                var magic        = br.ReadUInt32();
                var recordsCount = br.ReadUInt32();
                var fieldsCount  = br.ReadUInt32();
                var recordsize   = br.ReadUInt32();

                if (magic == DBCReader.DBCFmtSig)
                {
                    // only for files with 4 byte fields (most of dbc's)
                    if ((recordsize % fieldsCount == 0) && (fieldsCount * 4 == recordsize))
                    {
                        var def = new Table();

                        def.Name   = m_mainForm.DBCName;
                        def.Fields = new List <Field>();

                        for (int i = 0; i < fieldsCount; i++)
                        {
                            var field = new Field();

                            if (i == 0)
                            {
                                field.IsIndex = true;
                                field.Name    = "m_ID";
                            }
                            else
                            {
                                field.Name = string.Format("field{0}", i);
                            }

                            field.Type = "int";

                            def.Fields.Add(field);
                        }

                        m_changed = true;
                        return(def);
                    }
                }
                else if (magic == DB5Reader.DB5FmtSig)
                {
                    br.BaseStream.Position = 0;

                    DB5Reader db5 = new DB5Reader(br, true);

                    var def = new Table();

                    def.Name   = m_mainForm.DBCName;
                    def.Fields = new List <Field>();

                    for (int i = 0; i < db5.Meta.Count; i++)
                    {
                        var field = new Field();

                        if (i == db5.IdIndex)
                        {
                            field.IsIndex = true;
                            field.Name    = "m_ID";
                            field.Type    = "int";
                        }
                        else
                        {
                            field.Name = string.Format("field{0:X2}", db5.Meta[i].Offset);

                            int bits = db5.Meta[i].Bits;

                            if (bits == 0x18)
                            {
                                field.Type = "byte";
                            }
                            else if (bits == 0x10)
                            {
                                field.Type = "ushort";
                            }
                            else if (bits == 0x08)
                            {
                                field.Type = "int";
                            }
                            else if (bits == 0x00)
                            {
                                field.Type = "int";
                            }
                            else if (bits == -32)
                            {
                                field.Type = "ulong";
                            }
                            else
                            {
                                throw new Exception("New Bits value detected!");
                            }

                            int byteCount = (32 - bits) >> 3;

                            // array
                            if (i + 1 < db5.Meta.Count && db5.Meta[i].Offset + byteCount != db5.Meta[i + 1].Offset)
                            {
                                int arraySize = (db5.Meta[i + 1].Offset - db5.Meta[i].Offset) / byteCount;

                                field.ArraySize = arraySize;
                            }

                            // array (last field)
                            int rowSize = db5.HasIndexTable ? db5.RecordSize + 4 : db5.RecordSize;
                            if (i + 1 == db5.Meta.Count && db5.Meta[i].Offset + byteCount != rowSize)
                            {
                                int diff = rowSize - db5.Meta[i].Offset;

                                if (diff >= byteCount * 2)
                                {
                                    int arraySize = diff / byteCount;

                                    field.ArraySize = arraySize;
                                }
                            }
                        }

                        def.Fields.Add(field);
                    }

                    m_changed = true;
                    return(def);
                }
            }

            return(null);
        }
Exemplo n.º 7
0
        public static IWowClientDBReader GetReader(string file)
        {
            IWowClientDBReader reader;

            var ext = Path.GetExtension(file).ToUpperInvariant();

            if (ext == ".DBC")
            {
                reader = new DBCReader(file);
            }
            else if (ext == ".DB2")
            {
                try
                {
                    reader = new DB2Reader(file);
                }
                catch
                {
                    try
                    {
                        reader = new DB3Reader(file);
                    }
                    catch
                    {
                        try
                        {
                            reader = new DB4Reader(file);
                        }
                        catch
                        {
                            reader = new DB5Reader(file);
                        }
                    }
                }
            }



            else if (ext == ".ADB")
            {
                try {
                    reader = new ADBReader(file);
                }
                catch
                {
                    try
                    {
                        reader = new ADB4Reader(file);
                    }
                    catch
                    {
                        reader = new ADB5Reader(file);
                    }
                }
            }
            else if (ext == ".WDB")
            {
                reader = new WDBReader(file);
            }
            else if (ext == ".STL")
            {
                reader = new STLReader(file);
            }
            else
            {
                throw new InvalidDataException(String.Format("Unknown file type {0}", ext));
            }

            return(reader);
        }