public int CompareTo(object obj) { string objkeyname = null; SFOValue objvalue = obj as SFOValue; if (objvalue != null) { objkeyname = objvalue._keyName; } return(_keyName.CompareTo(objkeyname)); }
private void SetValue(string keyname, object newvaluedata) { var entries = from v in _values where v.KeyName == keyname select v; List <SFOValue> values = entries.ToList(); foreach (SFOValue value in values) { _values.Remove(value); } SFOValue newvalue = new SFOValue(keyname, newvaluedata); _values.Add(newvalue); }
public void SaveFile(string filename) { // Sort the values before we save them to the sfo file _values.Sort(); using (FileStream stream = File.Create(filename)) { using (StructWriter sw = new StructWriter(ByteOrder.LSB, stream)) { INDEX_TABLE_ENTRY[] indexes = new INDEX_TABLE_ENTRY[_values.Count]; string[] variablenames = new string[_values.Count]; int curkeynameoffset = 0; uint curvalueoffset = 0; for (int idx = 0; idx < _values.Count; idx++) { SFOValue value = _values[idx]; DATA_TYPE datatype = DATA_TYPE.BinaryData; uint datasize = 0; switch (value.ValueType) { case SFOType.Binary: { datatype = DATA_TYPE.BinaryData; datasize = (uint)value.ValueBinary.Length; break; } case SFOType.Int: { datatype = DATA_TYPE.Si32Integer; datasize = 4; break; } case SFOType.Text: { datatype = DATA_TYPE.Utf8Text; datasize = (uint)Encoding.UTF8.GetBytes(value.ValueString).Length + 1; break; } default: { throw new Exception("Unknown SFOType!"); } } indexes[idx].KeyNameOffset = (ushort)curkeynameoffset; indexes[idx].Unknown = 4; indexes[idx].DataType = datatype; indexes[idx].ValueDataSize = datasize; indexes[idx].ValueDataSizePlusPadding = GetPaddingSize(value.KeyName, datasize); indexes[idx].DataValueOffset = curvalueoffset; variablenames[idx] = value.KeyName; curkeynameoffset += value.KeyName.Length + 1; curvalueoffset += indexes[idx].ValueDataSizePlusPadding; } SFO_HEADER sfoheader = new SFO_HEADER(); sfoheader.magic = 0; sfoheader.signature = new char[] { 'P', 'S', 'F' }; sfoheader.FileVersionHigh = 1; sfoheader.FileVersionLow = 1; sfoheader.Unknown1 = 0; sfoheader.Start_of_Variable_Name_Table = PadOffset(Marshal.SizeOf(sfoheader) + (indexes.Length * Marshal.SizeOf(typeof(INDEX_TABLE_ENTRY)))); sfoheader.Start_of_Variable_Data_Table = PadOffset(sfoheader.Start_of_Variable_Name_Table + curkeynameoffset); sfoheader.NumberOfVariables = (uint)_values.Count; sw.WriteStruct(sfoheader); // Write variable information... sw.WriteStructs <INDEX_TABLE_ENTRY>(indexes); WritePadBytes(sw, sw.BaseStream.Position, sfoheader.Start_of_Variable_Name_Table); // Write variable names... sw.WriteStrings(StringType.NullTerminated, variablenames); WritePadBytes(sw, sw.BaseStream.Position, sfoheader.Start_of_Variable_Data_Table); // Write variable data... for (int idx = 0; idx < _values.Count; idx++) { SFOValue value = _values[idx]; switch (value.ValueType) { case SFOType.Binary: { sw.Write(value.ValueBinary); break; } case SFOType.Int: { sw.Write((Int32)value.ValueInt); break; } case SFOType.Text: { sw.WriteString(StringType.NullTerminated, value.ValueString); break; } } long pos = sw.BaseStream.Position; WritePadBytes(sw, indexes[idx].ValueDataSize, indexes[idx].ValueDataSizePlusPadding); } } } }
public SFO(string filename) { using (Stream fs = File.OpenRead(filename)) { using (StructReader sr = new StructReader(ByteOrder.LSB, fs)) { SFO_HEADER sfoheader = sr.ReadStruct <SFO_HEADER>(); if ( (sfoheader.magic != 0) || (sfoheader.signature[0] != 'P') || (sfoheader.signature[1] != 'S') || (sfoheader.signature[2] != 'F') ) { throw new Exception("Unknown file format!"); } // Read variable information... INDEX_TABLE_ENTRY[] indexes = sr.ReadStructs <INDEX_TABLE_ENTRY>((int)sfoheader.NumberOfVariables); // Read variable names... fs.Seek(sfoheader.Start_of_Variable_Name_Table, SeekOrigin.Begin); string[] variablenames = sr.ReadStrings(StringType.NullTerminated, indexes.Length); // Read variable data... for (int idx = 0; idx < indexes.Length; idx++) { INDEX_TABLE_ENTRY index = indexes[idx]; string indexname = variablenames[idx]; fs.Seek(sfoheader.Start_of_Variable_Data_Table + index.DataValueOffset, SeekOrigin.Begin); switch (index.DataType) { case DATA_TYPE.BinaryData: { byte[] valuedata = sr.ReadBytes((int)index.ValueDataSize); SFOValue newvalue = new SFOValue(indexname, valuedata); _values.Add(newvalue); break; } case DATA_TYPE.Si32Integer: { Int64 intvalue = sr.ReadInt64(); SFOValue newvalue = new SFOValue(indexname, intvalue); _values.Add(newvalue); break; } case DATA_TYPE.Utf8Text: { string str = sr.ReadString(StringType.PlainText, (int)index.ValueDataSize - 1); SFOValue newvalue = new SFOValue(indexname, str); _values.Add(newvalue); break; } default: string str1 = sr.ReadString(StringType.PlainText, (int)index.ValueDataSize - 1); SFOValue newvalue1 = new SFOValue(indexname, str1); _values.Add(newvalue1); break; } } } } }