private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string file = (string)e.Argument; try { m_dbreader = DBReaderFactory.GetReader(file, m_definition); } catch (Exception ex) { ShowErrorMessageBox(ex.Message); e.Cancel = true; return; } m_fields = m_definition.GetElementsByTagName("field"); string[] types = new string[m_fields.Count]; int[] sizes = new int[m_fields.Count]; for (int j = 0; j < m_fields.Count; ++j) { types[j] = m_fields[j].Attributes["type"].Value; sizes[j] = int.Parse(m_fields[j].Attributes["size"]?.Value ?? "1"); } m_formats = new XmlAttribute[sizes.Sum()]; for (int j = 0, k = 0; j < m_fields.Count; ++j, ++k) { for (var s = 0; s < sizes[j]; ++s) { m_formats[k] = m_fields[j].Attributes["format"]; } } // hack for *.adb files (because they don't have FieldsCount) bool notADB = !(m_dbreader is ADBReader); // hack for *.wdb files (because they don't have FieldsCount) bool notWDB = !(m_dbreader is WDBReader); // hack for *.wdb files (because they don't have FieldsCount) bool notSTL = !(m_dbreader is STLReader); // hack for *.db2 files v3 (because they don't have FieldsCount) bool notDB3 = !(m_dbreader is DB3Reader); if (m_fields.Count != m_dbreader.FieldsCount && notADB && notWDB && notSTL && notDB3) { string msg = String.Format(CultureInfo.InvariantCulture, "{0} has invalid definition!\nFields count mismatch: got {1}, expected {2}", Path.GetFileName(file), m_fields.Count, m_dbreader.FieldsCount); ShowErrorMessageBox(msg); e.Cancel = true; return; } m_dataTable = new DataTable(Path.GetFileName(file)); m_dataTable.Locale = CultureInfo.InvariantCulture; CreateColumns(); // Add columns CreateIndexes(); // Add indexes //bool extraData = false; foreach (var row in m_dbreader.Rows) // Add rows { DataRow dataRow = m_dataTable.NewRow(); using (BinaryReader br = row) { int j = 0; if (m_dbreader.HasSeparateIndexColumn) { dataRow[j++] = br.ReadUInt32(); } for (int c = 0; c < m_fields.Count; ++c) // Add cells { for (var arrSize = 0; arrSize < sizes[c]; ++arrSize, ++j) { var type = types[c]; if (type == "int" || type == "uint") { var typeOverride = m_dbreader.GetIntLength(c); if (typeOverride != null) { type = typeOverride; } } switch (type) { 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 "string": if (m_dbreader.HasInlineStrings) { 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 "intarray": { int columns = br.ReadByte(); var sb = new StringBuilder(); for (var c2 = 0; c2 < columns; ++c2) { sb.Append(br.ReadUInt32()).Append(", "); } dataRow[j] = sb.ToString(); break; } case "int24": dataRow[j] = br.ReadInt24(); break; case "uint24": dataRow[j] = br.ReadUInt24(); break; default: throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Unknown field type {0}!", type)); } } } } m_dataTable.Rows.Add(dataRow); int percent = (int)((float)m_dataTable.Rows.Count / m_dbreader.RecordsCount * 100.0f); (sender as BackgroundWorker).ReportProgress(percent); } //if (extraData) //{ // MessageBox.Show("extra data detected!"); //} if (dataGridView1.InvokeRequired) { SetDataViewDelegate d = new SetDataViewDelegate(SetDataSource); Invoke(d, new object[] { m_dataTable.DefaultView }); } else { SetDataSource(m_dataTable.DefaultView); } e.Result = file; }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { m_startTime = DateTime.Now; string file = (string)e.Argument; m_dbreader = DBReaderFactory.GetReader(file, m_definition); m_fields = new List <Field>(m_definition.Fields); string[] colNames = m_fields.Select(f => f.Name).ToArray(); int[] arraySizes = m_fields.Select(f => f.ArraySize).ToArray(); bool isDBCorDB2 = m_dbreader is DBCReader || m_dbreader is DB2Reader; m_dataTable = new DataTable(Path.GetFileName(file)); m_dataTable.Locale = CultureInfo.InvariantCulture; CreateColumns(); // Add columns CreateIndexes(); // Add indexes TypeCode[] types = m_dataTable.Columns.Cast <DataColumn>().Select(c => Type.GetTypeCode(c.DataType)).ToArray(); List <ColumnMeta> meta = null; if (m_dbreader is DB5Reader) { meta = (m_dbreader as DB5Reader).Meta; } if (m_dbreader is DB6Reader) { meta = (m_dbreader as DB6Reader).Meta; } Func <TypeCode, bool> isSmallType = (t) => { if (t == TypeCode.SByte || t == TypeCode.Byte || t == TypeCode.Int16 || t == TypeCode.UInt16) { return(true); } return(false); }; foreach (var row in m_dbreader.Rows) // Add rows { DataRow dataRow = m_dataTable.NewRow(); using (BinaryReader br = row) { int colIndex = 0; for (int j = 0; j < m_fields.Count; j++) // Add cells { for (int k = 0; k < arraySizes[j]; k++) { switch (types[colIndex]) { case TypeCode.SByte: dataRow.SetField(colIndex, br.ReadInt8(meta?[j])); break; case TypeCode.Byte: dataRow.SetField(colIndex, br.ReadUInt8(meta?[j])); break; case TypeCode.Int16: dataRow.SetField(colIndex, br.ReadInt16(meta?[j])); break; case TypeCode.UInt16: dataRow.SetField(colIndex, br.ReadUInt16(meta?[j])); break; case TypeCode.Int32: dataRow.SetField(colIndex, br.ReadInt32(meta?[j])); break; case TypeCode.UInt32: dataRow.SetField(colIndex, br.ReadUInt32(meta?[j])); break; case TypeCode.Int64: dataRow.SetField(colIndex, br.ReadInt64(meta?[j])); break; case TypeCode.UInt64: dataRow.SetField(colIndex, br.ReadUInt64(meta?[j])); break; case TypeCode.Single: dataRow.SetField(colIndex, br.ReadSingle(meta?[j])); break; case TypeCode.Double: dataRow.SetField(colIndex, br.ReadDouble(meta?[j])); break; case TypeCode.String: ReadStringField(colIndex, meta?[j], dataRow, br); break; default: throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Unknown field type {0} for column {1}!", types[colIndex], colNames[j])); } // small fields are padded with zeros in old format versions if next field isn't small long frem = br.BaseStream.Position % 4; if (isDBCorDB2 && frem != 0 && isSmallType(types[colIndex]) && (colIndex + 1 < types.Length) && !isSmallType(types[colIndex + 1])) { br.BaseStream.Position += (4 - frem); } colIndex++; } } } m_dataTable.Rows.Add(dataRow); int percent = (int)((float)m_dataTable.Rows.Count / m_dbreader.RecordsCount * 100.0f); (sender as BackgroundWorker).ReportProgress(percent); } e.Result = file; }
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; }
private void ReadDB(string file) { try { m_dbreader = DBReaderFactory.GetReader(file); } catch (Exception ex) { // e.Cancel = true; return; } m_fields = m_definition.GetElementsByTagName("field"); string[] types = new string[m_fields.Count]; for (int j = 0; j < m_fields.Count; ++j) { types[j] = m_fields[j].Attributes["type"].Value; } // hack for *.adb files (because they don't have FieldsCount) bool notADB = !(m_dbreader is ADBReader); // hack for *.wdb files (because they don't have FieldsCount) bool notWDB = !(m_dbreader is WDBReader); // hack for *.wdb files (because they don't have FieldsCount) bool notSTL = !(m_dbreader is STLReader); bool notDB5 = !(m_dbreader is DB5Reader); int fcount = GetFieldsCount(m_fields); if (fcount != m_dbreader.FieldsCount && notADB && notWDB && notSTL && notDB5) { if (!types[0].Equals("index")) { string msg = String.Format(CultureInfo.InvariantCulture, "{0} has invalid definition!\nFields count mismatch: got {1}, expected {2}", Path.GetFileName(file), fcount, m_dbreader.FieldsCount); return; } } m_dataTable = new PluginInterface.DataTable(Path.GetFileName(file)); m_dataTable.Locale = CultureInfo.InvariantCulture; CreateColumns(); // Add columns CreateIndexes(); // Add indexes //bool extraData = false; // Console.WriteLine(m_dbreader.RecordsCount); for (int i = 0; i < m_dbreader.RecordsCount; ++i) // Add rows { DataRow dataRow = m_dataTable.NewRow(); // Console.WriteLine(m_dbreader.RecordsCount); using (BinaryReader br = m_dbreader[i]) { // Console.WriteLine(m_dbreader[i].BaseStream.Length); for (int j = 0; j < m_fields.Count; ++j) // Add cells { // Console.WriteLine(m_fields.Count); switch (types[j]) { 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])); } } } m_dataTable.Rows.Add(dataRow); //int percent = (int)((float)m_dataTable.Rows.Count / (float)m_dbreader.RecordsCount * 100.0f); //(sender as BackgroundWorker).ReportProgress(percent); } Export2SQL.Export2SQL tosql = new Export2SQL.Export2SQL(); tosql.Run(m_dataTable); CloseFile(); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string file = (string)e.Argument; try { m_reader = DBReaderFactory.GetReader(file); } catch (Exception ex) { ShowErrorMessageBox(ex.Message); e.Cancel = true; return; } m_fields = m_definition.GetElementsByTagName("field"); string[] types = new string[m_fields.Count]; for (int j = 0; j < m_fields.Count; ++j) { types[j] = m_fields[j].Attributes["type"].Value; } // hack for *.adb files (because they don't have FieldsCount) bool notADB = !(m_reader is ADBReader); // hack for *.wdb files (because they don't have FieldsCount) bool notWDB = !(m_reader is WDBReader); if (GetFieldsCount(m_fields) != m_reader.FieldsCount && notADB && notWDB) { string msg = String.Format(CultureInfo.InvariantCulture, "{0} has invalid definition!\nFields count mismatch: got {1}, expected {2}", Path.GetFileName(file), m_fields.Count, m_reader.FieldsCount); ShowErrorMessageBox(msg); e.Cancel = true; return; } m_dataTable = new DataTable(Path.GetFileName(file)); m_dataTable.Locale = CultureInfo.InvariantCulture; CreateColumns(); // Add columns CreateIndexes(); // Add indexes for (int i = 0; i < m_reader.RecordsCount; ++i) // Add rows { DataRow dataRow = m_dataTable.NewRow(); BinaryReader br = m_reader[i]; for (int j = 0; j < m_fields.Count; ++j) // Add cells { switch (types[j]) { 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 "string": dataRow[j] = m_reader is WDBReader?br.ReadStringNull() : m_reader.StringTable[br.ReadInt32()]; break; default: throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "Unknown field type {0}!", types[j])); } } m_dataTable.Rows.Add(dataRow); int percent = (int)((float)m_dataTable.Rows.Count / (float)m_reader.RecordsCount * 100.0f); (sender as BackgroundWorker).ReportProgress(percent); } if (dataGridView1.InvokeRequired) { SetDataViewDelegate d = new SetDataViewDelegate(SetDataSource); Invoke(d, new object[] { m_dataTable.DefaultView }); } else { SetDataSource(m_dataTable.DefaultView); } e.Result = file; }