public void OpenTR2(string TR2fileName) { tr2Path = TR2fileName; loadedTR2 = new TR2(tr2Path); // Adding header column DataGridViewTextBoxColumn headColumn = new DataGridViewTextBoxColumn(); headColumn.ValueType = Type.GetType("System.Int32"); headColumn.Name = loadedTR2.fileName; dataTable.Columns.Add(headColumn); columnListBox.Items.Add(loadedTR2.fileName); // Adding dynamic columns for (int i = 0; i < loadedTR2.columnNum; i++) { DataGridViewTextBoxColumn currColumn = new DataGridViewTextBoxColumn(); if (loadedTR2.columnsList[i].dataLenght == loadedTR2.columnsList[i].singleDataLenght) { int int8 = 0; Int16 int16 = 0; Int32 int32 = 0; UInt16 uint16 = 0; switch (loadedTR2.columnsList[i].dataType.ToLower()) { case "int8": currColumn.ValueType = int8.GetType(); break; case "uint8": currColumn.ValueType = int8.GetType(); break; case "int16": currColumn.ValueType = int16.GetType(); break; case "uint16": currColumn.ValueType = uint16.GetType(); break; case "int32": currColumn.ValueType = int32.GetType(); break; case "utf-8": currColumn.ValueType = "".GetType(); break; case "ascii": currColumn.ValueType = "".GetType(); break; default: currColumn.ValueType = Type.GetType(loadedTR2.columnsList[i].dataType); break; } } else { currColumn.ValueType = "".GetType(); } currColumn.Name = loadedTR2.columnsList[i].name; dataTable.Columns.Add(currColumn); columnListBox.Items.Add(currColumn.Name); } columnListBox.SelectedIndex = 0; // Adding Rows for (int i = 0; i < loadedTR2.rowNum; i++) { DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell[] cells = new DataGridViewTextBoxCell[loadedTR2.columnNum + 1]; cells[0] = new DataGridViewTextBoxCell(); cells[0].Value = loadedTR2.rowHeaderValues[i]; for (int j = 1; j <= loadedTR2.columnNum; j++) { cells[j] = new DataGridViewTextBoxCell(); if (loadedTR2.columnsList[j - 1].dataLenght != loadedTR2.columnsList[j - 1].singleDataLenght) { int doneTot = 0; string currValue = ""; byte[] dataBuffer = (byte[])loadedTR2.columnsList[j - 1].data[i]; if (dataBuffer != null) { while (doneTot != dataBuffer.Length) { byte[] valueBuffer = new byte[loadedTR2.columnsList[j - 1].singleDataLenght]; Buffer.BlockCopy(dataBuffer, doneTot, valueBuffer, 0, valueBuffer.Length); currValue += TR2Utils.ConvertNumber(valueBuffer); doneTot += valueBuffer.Length; if (doneTot != dataBuffer.Length) { currValue += " | "; } } } else { currValue = null; } cells[j].Value = currValue; } else { if (loadedTR2.columnsList[j - 1].dataLenght == -1) { string laStringa = (string)loadedTR2.columnsList[j - 1].data[i]; cells[j].Value = laStringa.Replace("\n", "\\n"); } else { cells[j].Value = loadedTR2.columnsList[j - 1].data[i]; } } } row.Cells.AddRange(cells); dataTable.Rows.Add(row); } }
private void tR2MergerToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "tr2 database (.tr2)|*.tr2"; ofd.ShowDialog(); if ((ofd.FileName != "") && (ofd.FileName != null)) { TR2 filler = new TR2(ofd.FileName); DataGridView fillerView = new DataGridView(); fillerView.Rows.Clear(); fillerView.Columns.Clear(); // Adding header column DataGridViewTextBoxColumn headColumn = new DataGridViewTextBoxColumn(); headColumn.Name = filler.fileName; fillerView.Columns.Add(headColumn); // Adding dynamic columns for (int i = 0; i < filler.columnNum; i++) { DataGridViewTextBoxColumn currColumn = new DataGridViewTextBoxColumn(); if (filler.columnsList[i].dataLenght == filler.columnsList[i].singleDataLenght) { currColumn.ValueType = Type.GetType(filler.columnsList[i].dataType); } else { currColumn.ValueType = "".GetType(); } currColumn.Name = filler.columnsList[i].name; fillerView.Columns.Add(currColumn); } // Adding Rows for (int i = 0; i < filler.rowNum; i++) { DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell[] cells = new DataGridViewTextBoxCell[filler.columnNum + 1]; cells[0] = new DataGridViewTextBoxCell(); cells[0].Value = filler.rowHeaderValues[i]; for (int j = 1; j <= filler.columnNum; j++) { cells[j] = new DataGridViewTextBoxCell(); if (filler.columnsList[j - 1].dataLenght != filler.columnsList[j - 1].singleDataLenght) { int doneTot = 0; string currValue = ""; byte[] dataBuffer = (byte[])filler.columnsList[j - 1].data[i]; if (dataBuffer != null) { while (doneTot != dataBuffer.Length) { byte[] valueBuffer = new byte[filler.columnsList[j - 1].singleDataLenght]; Buffer.BlockCopy(dataBuffer, doneTot, valueBuffer, 0, valueBuffer.Length); currValue += TR2Utils.ConvertNumber(valueBuffer); doneTot += valueBuffer.Length; if (doneTot != filler.columnsList[j - 1].dataLenght) { currValue += " | "; } } } else { currValue = null; } cells[j].Value = currValue; } else { cells[j].Value = filler.columnsList[j - 1].data[i]; } } row.Cells.AddRange(cells); fillerView.Rows.Add(row); } // Checking Fillers for (int i = 0; i < dataTable.Rows.Count; i++) { for (int index = 0; index < fillerView.Rows.Count - 1; index++) { if (fillerView.Rows[index].Cells[0].Value.Equals(dataTable.Rows[i].Cells[0].Value)) { for (int j = 0; j < fillerView.Rows[index].Cells.Count; j++) { dataTable.Rows[i].Cells[j].Value = fillerView.Rows[index].Cells[j].Value; } break; } } } } }
public TR2(string tr2path) { FileStream fs = new FileStream(tr2path, FileMode.Open, FileAccess.Read); // Reading TR2 type fs.Seek(0x06, SeekOrigin.Begin); byte[] buffer = new byte[2]; fs.Read(buffer, 0, 2); tableType = TR2Utils.ConvertNumber(buffer); // Reading real TR2 name buffer = new byte[0x30]; fs.Read(buffer, 0, 0x30); fileName = Encoding.UTF8.GetString(buffer).Replace("\0", ""); // Reading number of tables fs.Seek(0x3C, SeekOrigin.Begin); buffer = new byte[4]; fs.Read(buffer, 0, 4); columnNum = TR2Utils.ConvertNumber(buffer); // Columns Information Reader Cicle columnsList = new TR2Info.Column[columnNum]; for (int i = 0; i < columnNum; i++) { buffer = new byte[0x14]; fs.Read(buffer, 0, 0x14); byte[] tempBuffer = new byte[4]; Buffer.BlockCopy(buffer, 0, tempBuffer, 0, 0x04); // Getting the column id columnsList[i].id = (uint)TR2Utils.ConvertNumber(tempBuffer); Buffer.BlockCopy(buffer, 0x04, tempBuffer, 0, 0x04); // Getting the column offset columnsList[i].offset = (uint)TR2Utils.ConvertNumber(tempBuffer); Buffer.BlockCopy(buffer, 0x0C, tempBuffer, 0, 0x04); // Getting the column dimension columnsList[i].lenght = (uint)TR2Utils.ConvertNumber(tempBuffer); } // Reading rows number buffer = new byte[4]; fs.Read(buffer, 0, 4); rowNum = TR2Utils.ConvertNumber(buffer); // Reading Row Headers Values rowHeaderValues = new List <int>(); for (int i = 0; i < rowNum; i++) { buffer = new byte[4]; fs.Read(buffer, 0, 4); rowHeaderValues.Add(TR2Utils.ConvertNumber(buffer)); } // Column Reader Cicle for (int i = 0; i < columnNum; i++) { fs.Seek(columnsList[i].offset, SeekOrigin.Begin); byte[] colBuffer = new byte[columnsList[i].lenght]; fs.Read(colBuffer, 0, (int)columnsList[i].lenght); // Reading Column Name byte[] tempBuffer = new byte[0x3C]; Buffer.BlockCopy(colBuffer, 0, tempBuffer, 0, 0x3C); columnsList[i].name = Encoding.UTF8.GetString(tempBuffer).Replace("\0", ""); // Reading Column Data Type tempBuffer = new byte[0x30]; Buffer.BlockCopy(colBuffer, 0x40, tempBuffer, 0, 0x30); columnsList[i].dataType = Encoding.UTF8.GetString(tempBuffer).Replace("\0", ""); // Reading Column Data Type Bytes tempBuffer = new byte[0x0C]; Buffer.BlockCopy(colBuffer, 0x70, tempBuffer, 0, 0x0C); columnsList[i].dataTypeBytes = tempBuffer; // Table data reader cycle columnsList[i].data = new Object[rowNum]; if ((columnsList[i].dataType == "INT16") || (columnsList[i].dataType == "INT32") || (columnsList[i].dataType == "UINT8") || (columnsList[i].dataType == "INT8") || (columnsList[i].dataType == "UINT16")) { byte[] dataLenghtBuffer = new byte[4]; Buffer.BlockCopy(colBuffer, 0x80, dataLenghtBuffer, 0, 0x04); int lowDataLimit = TR2Utils.ConvertNumber(dataLenghtBuffer); Buffer.BlockCopy(colBuffer, 0x84, dataLenghtBuffer, 0, 0x04); int highDataLimit = TR2Utils.ConvertNumber(dataLenghtBuffer); columnsList[i].dataLenght = highDataLimit - lowDataLimit; } else { columnsList[i].dataLenght = -1; // Variable Data Lenght -> Strings } for (int x = 0; x < rowNum; x++) { byte[] limitBuffer = new byte[4]; Buffer.BlockCopy(colBuffer, (0x80 + (x * 0x04)), limitBuffer, 0, 0x04); int lowLimit = TR2Utils.ConvertNumber(limitBuffer); Buffer.BlockCopy(colBuffer, (0x84 + (x * 0x04)), limitBuffer, 0, 0x04); int highLimit = TR2Utils.ConvertNumber(limitBuffer); byte[] dataBuffer = new byte[highLimit - lowLimit]; Buffer.BlockCopy(colBuffer, lowLimit, dataBuffer, 0, (highLimit - lowLimit)); if (highLimit != lowLimit) { // Numerical Data if (columnsList[i].dataLenght >= 0) { int realDataLenght = 1; switch (columnsList[i].dataType) { case "INT16": realDataLenght = 2; break; case "INT32": realDataLenght = 4; break; case "UINT8": realDataLenght = 1; break; case "INT8": realDataLenght = 1; break; case "UINT16": realDataLenght = 2; break; } if (columnsList[i].dataLenght == realDataLenght) { columnsList[i].data[x] = (Object)TR2Utils.ConvertNumber(dataBuffer); } else { columnsList[i].data[x] = (Object)dataBuffer; } columnsList[i].singleDataLenght = realDataLenght; } // Text Data if (columnsList[i].dataLenght < 0) { columnsList[i].data[x] = (Object)TR2Utils.ConvertString(dataBuffer, columnsList[i].dataType); columnsList[i].singleDataLenght = -1; } } else { columnsList[i].data[x] = null; } } } // Reading Ending Pointer endingPointers = new byte[0x0C]; fs.Seek(columnsList[columnNum - 1].offset + columnsList[columnNum - 1].lenght, SeekOrigin.Begin); if (fs.Read(endingPointers, 0, 0x0C) != 0x0C) { hasFinalHeader = false; } else { hasFinalHeader = true; } fs.Close(); fs.Dispose(); }