Ejemplo n.º 1
0
        private void LoadDataFromDBC(string Filename)
        {
            FileInfo DBCInfo = new FileInfo(Filename);

            ConfigFile    DBCLayout     = m_Engine.LoadFile(@"data\dbc_layouts.xml");
            ConfigElement LayoutElement = DBCLayout["layout", DBCInfo.Name];

            if (LayoutElement == null)
            {
                return;
            }

            DataTable DBCTable = m_Data.Tables["data"];

            if (DBCTable == null)
            {
                DBCTable = m_Data.Tables.Add("data");
            }

            DBCTable.BeginLoadData();

            #region Construct Columns

            string          LayoutID       = LayoutElement.GetString("id");
            ConfigElement[] ColumnElements = DBCLayout["column", "id", LayoutID];

            DataColumn PrimaryColumn = null;
            foreach (ConfigElement ColumnElement in ColumnElements)
            {
                Type   ColumnType = typeof(uint);
                string TypeName   = ColumnElement.GetString("type");

                if (TypeName != null)
                {
                    ColumnType = Type.GetType("System." + TypeName, false, true);
                    if (ColumnType == null)
                    {
                        throw new Exception("InvalidTypeException");
                    }
                }

                DataColumn CurrentColumn = DBCTable.Columns.Add(ColumnElement.GetString("name").ToLower(), ColumnType);
                if (ColumnElement.GetBoolean("primarykey"))
                {
                    PrimaryColumn = CurrentColumn;
                }
            }

            if (PrimaryColumn != null)
            {
                DBCTable.PrimaryKey = new DataColumn[1] {
                    PrimaryColumn
                };
            }

            #endregion
            #region Load DBC

            BinaryReader DBCReader = new BinaryReader(File.OpenRead(Filename));
            DBCHeader    Header    = new DBCHeader(DBCReader);

            long   StringTablePos = (Header.RowSize * Header.RowCount) + 20;
            byte[] Buffer         = new byte[Header.RowSize];

            #region Read each Row

            for (int i = 0; i <= Header.RowCount - 1; i++)
            {
                DataRow DBCRow = DBCTable.NewRow();

                DBCReader.Read(Buffer, 0, (int)Header.RowSize);
                long NextRowPosition = DBCReader.BaseStream.Position;

                BinaryReader RowReader = new BinaryReader(new MemoryStream(Buffer));

                foreach (ConfigElement ColumnElement in ColumnElements)
                {
                    string ColumnName = ColumnElement.GetString("name");
                    if (ColumnElement.Exists("position"))
                    {
                        RowReader.BaseStream.Seek((long)ColumnElement.GetUInt64("position"), SeekOrigin.Begin);
                    }

                    Type   ColumnType = typeof(uint);
                    string TypeName   = ColumnElement.GetString("type");

                    if (TypeName != null)
                    {
                        switch (TypeName.ToLower())
                        {
                            #region Type Read Cases
                        case "sbyte":
                            DBCRow[ColumnName] = RowReader.ReadSByte();
                            break;

                        case "byte":
                            DBCRow[ColumnName] = RowReader.ReadByte();
                            break;

                        case "short":
                            DBCRow[ColumnName] = RowReader.ReadInt16();
                            break;

                        case "ushort":
                            DBCRow[ColumnName] = RowReader.ReadUInt16();
                            break;

                        case "int":
                            DBCRow[ColumnName] = RowReader.ReadInt32();
                            break;

                        case "uint":
                            DBCRow[ColumnName] = RowReader.ReadUInt32();
                            break;

                        case "long":
                            DBCRow[ColumnName] = RowReader.ReadInt64();
                            break;

                        case "ulong":
                            DBCRow[ColumnName] = RowReader.ReadUInt64();
                            break;

                        case "float":
                            DBCRow[ColumnName] = RowReader.ReadSingle();
                            break;

                        case "double":
                            DBCRow[ColumnName] = RowReader.ReadDouble();
                            break;

                        case "string":
                            uint StringPos = RowReader.ReadUInt32();
                            DBCReader.BaseStream.Seek(StringTablePos + StringPos, SeekOrigin.Begin);

                            DBCRow[ColumnName] = Utilities.ReadString(DBCReader);

                            DBCReader.BaseStream.Seek(NextRowPosition, SeekOrigin.Begin);
                            break;
                            #endregion
                        }
                    }
                    else
                    {
                        DBCRow[ColumnName] = RowReader.ReadUInt32();
                    }
                }

                RowReader.Close();
                DBCTable.Rows.Add(DBCRow);
            }

            #endregion

            DBCReader.Close();

            #endregion

            DBCTable.EndLoadData();
        }