Exemple #1
0
        public static DbcSchema Load(Stream source, int specifiedColumns)
        {
            XDocument doc    = XDocument.Load(source);
            DbcSchema result = new DbcSchema();

            foreach (var e in doc.Root.Elements("Column"))
            {
                var col = new DbcColumnSchema()
                {
                    Position = int.Parse(e.Attribute("Position").Value),
                    Name     = e.Attribute("Name").Value,
                    Type     = (ColumnType)Enum.Parse(typeof(ColumnType), e.Attribute("Type").Value, true),
                    Width    = int.Parse(e.Attribute("Width").Value)
                };

                result.AddColumn(col);
            }

            while (result.ColumnCount > specifiedColumns)
            {
                result.columns.RemoveAt(result.ColumnCount - 1);
            }

            while (result.columns.Count < specifiedColumns)
            {
                var col = new DbcColumnSchema()
                {
                    Position = result.ColumnCount,
                    Name     = "Column" + result.ColumnCount,
                    Type     = ColumnType.Int32,
                    Width    = 100,
                };

                result.AddColumn(col);
            }

            return(result);
        }
Exemple #2
0
        public void Write(string destination, DbcSchema schema, DataGridView view)
        {
            if (!streamOwner)
            {
                throw new ArgumentException("streamOwner is false");
            }

            if (writer != null)
            {
                writer.Dispose();
            }

            List <DbcRow> rows = new List <DbcRow>();

            for (int y = 0; y < view.RowCount; y++)
            {
                if (y == view.RowCount - 1 && view.Rows[y].HasEmptyCell())
                {
                    continue;
                }

                rows.Add(new DbcRow(view.Rows[y]));
            }

            FileStream fileStream = new FileStream(destination, FileMode.Create);

            writer = new BinaryWriter(fileStream, Encoding.UTF8);
            writer.Write(magic);
            writer.Write(rows.Count);
            writer.Write(view.ColumnCount);
            writer.Write(view.ColumnCount * 4);
            writer.Write(0);//writer.Write(stringBlockLength);

            if (format != DbcFileFormat.Dbc)
            {
                writer.Write(hashTable);
                writer.Write(build);
                writer.Write(_lastWrittenTimestamp);
                writer.Write(minID);
                writer.Write(maxID);
                writer.Write(locale);
                writer.Write(unknown);
            }

            // convert it back into DbcRecord list
            DbcColumnSchema[] columnSchema = schema.Columns.ToArray();

            foreach (DbcRow row in rows)
            {
                for (int x = 0; x < ColumnCount; x++)
                {
                    switch (columnSchema[x].Type)
                    {
                    case ColumnType.Int32:
                    case ColumnType.Boolean:
                        writer.Write(row.GetInt32Value(x));
                        break;

                    case ColumnType.Int32Flags:
                        writer.Write(row.GetInt32Flags(x));
                        break;

                    case ColumnType.Single:
                        writer.Write(row.GetSingleValue(x));
                        break;

                    case ColumnType.String:
                        writer.Write(AddStringToTable(row.GetStringValue(x)));
                        break;

                    case ColumnType.UInt32:
                        writer.Write(row.GetUInt32Value(x));
                        break;
                    }
                }
            }

            // stringTable
            foreach (string str in stringTable.Values)
            {
                writer.Write(Encoding.UTF8.GetBytes(str));
                writer.Write((byte)0);
            }

            writer.BaseStream.Position = 16;

            if (stringTable.Count > 0)
            {
                writer.Write(stringTable.Last().Key + Encoding.UTF8.GetByteCount(stringTable.Last().Value) + 1);
            }

            // dispose
            writer.Close();
            writer.Dispose();
            writer = null;
        }