public void exportCVS(string path) { TextWriter writer = new StreamWriter(path); foreach (DataColumn col in table.Columns) { SHNColumn colz = GetColByName(col.ColumnName); writer.Write(colz.name + ", "); //"@" + colz.Lenght + "@" + colz.Type } writer.Write(writer.NewLine); //end all columns foreach (DataRow row in table.Rows) { for (int i = 0; i < table.Columns.Count; i++) { string s = row[i].ToString(); if (s.Contains('"')) { s = s.Replace('"', ' '); } writer.Write("\"" + s + "\""); //writer.Write(s); if (i + 1 == table.Columns.Count) { writer.Write(writer.NewLine); } else { writer.Write(','); } } } writer.Close(); }
public void EditColumnName(string from, string to) { DataColumn olddat = GetDataColByName(from); olddat.ColumnName = to; SHNColumn colz = GetColByName(from); colz.name = to; }
public uint GetDefaultRecLen() { uint start = 2; foreach (DataColumn colz in table.Columns) { SHNColumn col = GetColByName(colz.ColumnName); start += (uint)col.Lenght; } return(start); }
public bool Save(string file) { MemoryStream output = new MemoryStream(); BinaryWriter w = new BinaryWriter(output); try { //this.table.DefaultView.Sort = this.table.Columns[0].ColumnName; //this.table = this.table.DefaultView.ToTable(); w.Write(this.Header); w.Write(this.table.Rows.Count); //rowcount w.Write(GetDefaultRecLen()); w.Write(this.table.Columns.Count); for (int i = 0; i < this.table.Columns.Count; i++) { SHNColumn colz = GetColByName(this.table.Columns[displayToReal[i]].ColumnName); //converts the display to the row order if (colz.name.Contains("UnkCol")) { w.Write(new byte[0x30]); //empty name } else { this.WriteString(w, colz.name, 0x30); } w.Write(colz.Type); w.Write(colz.Lenght); } this.WriteRows(w); byte[] sourceArray = output.GetBuffer(); long length = output.Length; byte[] destinationArray = new byte[length]; Array.Copy(sourceArray, destinationArray, length); this.Decrypt(destinationArray, 0, destinationArray.Length); w.Close(); w = new BinaryWriter(File.Create(file)); w.Write(this.CryptHeader); w.Write((int)(destinationArray.Length + 0x24)); w.Write(destinationArray); w.Close(); Path = file; return(true); } catch (Exception ex) { w.Close(); MessageBox.Show("Could not save file: " + ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } }
public void CreateColumn(string name, int len, uint type, string defaultval) { SHNColumn newCol = new SHNColumn(); newCol.name = name; newCol.Lenght = len; newCol.Type = type; columns.Add(newCol); DataColumn column = new DataColumn(); column.ColumnName = name; column.DefaultValue = defaultval; column.DataType = GetType(newCol); table.Columns.Add(column); }
public Type GetType(SHNColumn col) { switch (col.Type) { default: return(typeof(object)); case 1: case 12: return(typeof(byte)); case 2: return(typeof(UInt16)); case 3: case 11: return(typeof(UInt32)); case 5: return(typeof(Single)); case 0x15: case 13: return(typeof(Int16)); case 0x10: return(typeof(byte)); case 0x12: case 0x1b: return(typeof(UInt32)); case 20: return(typeof(SByte)); case 0x16: return(typeof(Int32)); case 0x18: case 0x1a: case 9: return(typeof(string)); } }
private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e) { try { if (dataGrid.SelectedRows.Count < 1) { SHNColumn col = file.GetColByName(file.table.Columns[e.ColumnIndex].ColumnName); string type = file.table.Columns[e.ColumnIndex].DataType.ToString(); SQLStatus.Text = col.name + ": " + type + " || SHN type: " + col.Type + " len: " + col.Lenght; } else { SQLStatus.Text = "Ready. Columns: " + (dataGrid.Columns.Count).ToString() + " Row: " + dataGrid.CurrentCell.RowIndex + "/" + (file.table.Rows.Count - 1).ToString(); } } catch (Exception ex) { SQLStatus.Text = ("Ready. Error Occured: " + ex.Message); } }
public SHNFile(string path) { try { columns.Clear(); this.Path = path; if (System.IO.Path.GetFileNameWithoutExtension(path).ToLower().Contains("textdata")) { isTextData = true; } BinaryReaderEx r; using (r = new BinaryReaderEx(File.OpenRead(path))) { if (path.EndsWith(".shn")) { this.CryptHeader = r.ReadBytes(0x20); data = r.ReadBytes(r.ReadInt32() - 0x24); } else { data = r.ReadBytes((int)r.Length); } } this.Decrypt(data, 0, data.Length); r = new BinaryReaderEx(new MemoryStream(data)); this.Header = r.ReadUInt32(); //Parse columns this.RecordCount = r.ReadUInt32(); this.DefaultRecordLength = r.ReadUInt32(); this.ColumnCount = r.ReadUInt32(); this.ColumnNames = new string[this.ColumnCount]; this.ColumnTypes = new uint[this.ColumnCount]; this.ColumnLengths = new int[this.ColumnCount]; int num2 = 2; int unkCols = 0; for (uint i = 0; i < this.ColumnCount; i++) { string str = r.ReadString(0x30); uint num4 = r.ReadUInt32(); int num5 = r.ReadInt32(); SHNColumn col = new SHNColumn(); if (str.Length == 0 || String.IsNullOrWhiteSpace(str)) { str = "UnkCol" + unkCols.ToString(); unkCols++; } col.name = str; col.Type = num4; col.Lenght = num5; columns.Add(col); this.ColumnNames[i] = str; this.ColumnTypes[i] = num4; this.ColumnLengths[i] = num5; num2 += num5; } if (num2 != this.DefaultRecordLength) { throw new Exception("Wrong record length!"); } //generate columns this.GenerateColumns(table, columns); //add data into rows this.ReadRows(r, table); } catch (Exception e) { Stream X = new FileStream("unk.dat", FileMode.OpenOrCreate); BinaryWriter lol = new BinaryWriter(X); lol.Write(data, 0, data.Length); lol.Close(); //throw new Exception("Unknown File Type -- dec to unk.dat Reason: " + e.Message); throw new Exception("An error occured trying to open the file: " + e.Message); } }
private void WriteRows(BinaryWriter w) { for (int iz = 0; iz < this.table.Rows.Count; iz++) { DataRow row = this.table.Rows[iz]; long position = w.BaseStream.Position; w.Write((ushort)0); //show new start for (int i = 0; i < this.table.Columns.Count; i++) { object obj2 = row.ItemArray[displayToReal[i]]; if (obj2 == null) { obj2 = (string)"0"; } SHNColumn col = GetColByName(this.table.Columns[displayToReal[i]].ColumnName); switch (col.Type) { case 1: if (obj2 is string) { obj2 = byte.Parse((string)obj2); } w.Write((byte)obj2); break; case 2: if (obj2 is string) { obj2 = ushort.Parse((string)obj2); } w.Write((ushort)obj2); break; case 3: if (obj2 is string) { obj2 = uint.Parse((string)obj2); } w.Write((uint)obj2); break; case 5: if (obj2 is string) { obj2 = float.Parse((string)obj2); } w.Write((float)obj2); break; case 9: if (String.IsNullOrWhiteSpace(obj2.ToString())) { this.WriteString(w, obj2.ToString(), col.Lenght); } else { this.WriteString(w, (string)obj2, col.Lenght); } break; case 11: if (obj2 is string) { obj2 = uint.Parse((string)obj2); } w.Write((uint)obj2); break; case 12: if (obj2 is string) { obj2 = byte.Parse((string)obj2); } w.Write((byte)obj2); break; case 13: if (obj2 is string) { obj2 = short.Parse((string)obj2); } w.Write((short)obj2); break; case 0x10: if (obj2 is string) { obj2 = byte.Parse((string)obj2); } w.Write((byte)obj2); break; case 0x12: if (obj2 is string) { obj2 = uint.Parse((string)obj2); } w.Write((uint)obj2); break; case 20: if (obj2 is string) { obj2 = sbyte.Parse((string)obj2); } w.Write((sbyte)obj2); break; case 0x15: if (obj2 is string) { obj2 = short.Parse((string)obj2); } w.Write((short)obj2); break; case 0x16: if (obj2 is string) { obj2 = int.Parse((string)obj2); } w.Write((int)obj2); break; case 0x18: this.WriteString(w, (string)obj2, col.Lenght); break; case 0x1a: this.WriteString(w, (string)obj2, -1); break; case 0x1b: if (obj2 is string) { obj2 = uint.Parse((string)obj2); } w.Write((uint)obj2); break; } } long num3 = w.BaseStream.Position - position; long offset = w.BaseStream.Position; w.BaseStream.Seek(position, SeekOrigin.Begin); w.Write((ushort)num3); w.BaseStream.Seek(offset, SeekOrigin.Begin); } }