private void Init(Stream input)
 {
     using (var br = new BinaryReader(input))
     {
         Header.Read(br);
         if (!Functions.CompareBytes(Header.Magic, new byte[] { 0, 0x50, 0x53, 0x46 }))
         {
             throw new Exception("Invalid PARAM.SFO Header Magic");
         }
         var tables = new List <index_table>();
         for (var i = 0; i < Header.IndexTableEntries; i++)
         {
             var t = new index_table();
             t.Read(br);
             tables.Add(t);
         }
         var xtables = new List <Table>();
         var count   = 0;
         foreach (var t in tables)
         {
             var x = new Table();
             x.index      = count;
             x.Indextable = t;
             x.Name       = ReadName(br, t);
             x.Value      = ReadValue(br, t);
             count++;
             xtables.Add(x);
         }
         Tables = xtables.ToArray();
         br.Close();
     }
 }
Example #2
0
 /// <summary>
 /// Start Reading the Parameter file
 /// </summary>
 /// <param name="input">Input Stream</param>
 private void Init(Stream input)
 {
     using (var br = new BinaryReader(input))
     {
         Header.Read(br);
         var tables = new List <index_table>();
         for (int i = 0; i < Header.IndexTableEntries; i++)
         {
             var t = new index_table();
             t.Read(br);
             tables.Add(t);
         }
         var xtables = new List <Table>();
         int count   = 0;
         foreach (index_table t in tables)
         {
             var x = new Table();
             x.index      = count;
             x.Indextable = t;
             x.Name       = ReadName(br, t);
             x.Value      = ReadValue(br, t);
             count++;
             xtables.Add(x);
         }
         Tables = xtables;
         br.Close();
     }
 }
        private string ReadName(BinaryReader br, index_table table)
        {
            br.BaseStream.Position = (Header.KeyTableStart + table.param_key_offset);
            var name = "";

            while (((byte)br.PeekChar()) != 0)
            {
                name += br.ReadChar();
            }
            br.BaseStream.Position++;
            return(name);
        }
        private string ReadValue(BinaryReader br, index_table table)
        {
            br.BaseStream.Position = ((Header.DataTableStart) + table.param_data_offset);
            switch (table.param_data_fmt)
            {
            case FMT.ASCII:
                return(Encoding.ASCII.GetString(br.ReadBytes((int)table.param_data_max_len)).Replace("\0", ""));

            case FMT.UINT32:
                return(br.ReadUInt32().ToString());

            case FMT.UTF_8:
                return(Encoding.UTF8.GetString(br.ReadBytes((int)table.param_data_max_len)).Replace("\0", ""));

            default:
                return(null);
            }
        }
Example #5
0
 private string ReadValue(BinaryReader br, index_table table)
 {
     br.BaseStream.Position = ((Header.DataTableStart) + table.param_data_offset);
     switch (table.param_data_fmt)
     {
         case FMT.ASCII:
             return Encoding.ASCII.GetString(br.ReadBytes((int) table.param_data_max_len)).Replace("\0", "");
         case FMT.UINT32:
             return br.ReadUInt32().ToString();
         case FMT.UTF_8:
             return Encoding.UTF8.GetString(br.ReadBytes((int) table.param_data_max_len)).Replace("\0", "");
         default:
             return null;
     }
 }
Example #6
0
 private string ReadName(BinaryReader br, index_table table)
 {
     br.BaseStream.Position = (Header.KeyTableStart + table.param_key_offset);
     string name = "";
     while (((byte) br.PeekChar()) != 0)
         name += br.ReadChar();
     br.BaseStream.Position++;
     return name;
 }
Example #7
0
 private void Init(Stream input)
 {
     using (var br = new BinaryReader(input))
     {
         Header.Read(br);
         if (!Functions.CompareBytes(Header.Magic, new byte[] {0, 0x50, 0x53, 0x46}))
             throw new Exception("Invalid PARAM.SFO Header Magic");
         var tables = new List<index_table>();
         for (int i = 0; i < Header.IndexTableEntries; i++)
         {
             var t = new index_table();
             t.Read(br);
             tables.Add(t);
         }
         var xtables = new List<Table>();
         int count = 0;
         foreach (index_table t in tables)
         {
             var x = new Table();
             x.index = count;
             x.Indextable = t;
             x.Name = ReadName(br, t);
             x.Value = ReadValue(br, t);
             count++;
             xtables.Add(x);
         }
         Tables = xtables.ToArray();
         br.Close();
     }
 }
Example #8
0
        public void SaveFile(PARAM_SFO psfo, string filename)
        {
            // Sort the values before we save them to the sfo file
            //Array.Sort(psfo.Tables);

            using (FileStream stream = File.Create(filename))
            {
                using (StructWriter sw = new StructWriter(ByteOrder.LSB, stream))
                {
                    //build index table
                    INDEX_TABLE_ENTRY[]     indexes   = new INDEX_TABLE_ENTRY[psfo.Tables.Length];
                    PARAM_SFO.Table[]       mytables  = psfo.Tables;
                    PARAM_SFO.index_table[] myindexes = new index_table[psfo.Tables.Length];

                    string[] variablenames  = new string[psfo.Tables.Length];
                    string[] variablevalues = new string[psfo.Tables.Length];

                    int  curkeynameoffset = 0;
                    uint curvalueoffset   = 0;

                    for (int idx = 0; idx < psfo.Tables.Length; idx++)
                    {
                        PARAM_SFO.Table value = psfo.Tables[idx];


                        PARAM_SFO.FMT datatype = FMT.UINT32;
                        uint          datasize = 0;
                        switch (value.Indextable.param_data_fmt)
                        {
                        case FMT.ASCII:
                        {
                            datatype = FMT.ASCII;
                            datasize = (uint)Encoding.UTF8.GetBytes(value.Value.ToString()).Length + 1;
                            break;
                        }

                        case FMT.UINT32:
                        {
                            datatype = FMT.UINT32;
                            datasize = 4;
                            break;
                        }

                        case FMT.UTF_8:
                        {
                            datatype = FMT.UTF_8;
                            datasize = (uint)Encoding.UTF8.GetBytes(value.Value.ToString()).Length + 1;
                            break;
                        }

                        default:
                        {
                            throw new Exception("Unknown SFOType!");
                        }
                        }


                        if (value.Indextable.param_key_offset != (ushort)curkeynameoffset)
                        {
                            string breakpoint = "This is for debug testing";
                        }
                        //value.Indextable.param_key_offset = (ushort)curkeynameoffset;

                        if (value.Indextable.param_data_fmt != datatype)
                        {
                            string breakpoint = "This is for debug testing";
                        }
                        //value.Indextable.param_data_fmt = datatype;

                        if (value.Indextable.param_data_len != datasize)
                        {
                            string breakpoint = "This is for debug testing";
                        }
                        //value.Indextable.param_data_len = datasize;

                        //if (value.Indextable.param_data_max_len != GetPaddingSize(value.Name, datasize))
                        //{
                        //    string breakpoint = "This is for debug testing";
                        //}
                        //value.Indextable.param_data_max_len = GetPaddingSize(value.Name, datasize);

                        if (value.Indextable.param_data_offset != curvalueoffset)
                        {
                            string breakpoint = "This is for debug testing";
                        }
                        //value.Indextable.param_data_offset = curvalueoffset;

                        //we already have all the keynames

                        curvalueoffset += value.Indextable.param_data_max_len;



                        indexes[idx].KeyNameOffset = (ushort)curkeynameoffset;
                        indexes[idx].Unknown       = 4;
                        if (datatype == FMT.UTF_8)
                        {
                            indexes[idx].DataType = DATA_TYPE.BinaryData;
                        }
                        if (datatype == FMT.ASCII)
                        {
                            indexes[idx].DataType = DATA_TYPE.Utf8Text;
                        }
                        if (datatype == FMT.UINT32)
                        {
                            indexes[idx].DataType = DATA_TYPE.Si32Integer;
                        }
                        indexes[idx].ValueDataSize            = datasize;
                        indexes[idx].ValueDataSizePlusPadding = GetPaddingSize(value.Name, datasize);
                        indexes[idx].DataValueOffset          = curvalueoffset;


                        curkeynameoffset += value.Name.Length + 1;


                        variablenames[idx] = value.Name;

                        myindexes[idx]      = value.Indextable;
                        variablevalues[idx] = value.Value;
                    }


                    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 = Header.KeyTableStart;     //PadOffset(Marshal.SizeOf(sfoheader) + (psfo.Tables.Length * Marshal.SizeOf(typeof(PARAM_SFO.index_table))));//
                    sfoheader.Start_of_Variable_Data_Table = Header.DataTableStart;    //PadOffset(sfoheader.Start_of_Variable_Name_Table + curkeynameoffset);//
                    sfoheader.NumberOfVariables            = Header.IndexTableEntries; //(uint)psfo.Tables.Length;//

                    sw.WriteStruct(sfoheader);


                    // Write variable information...
                    sw.WriteStructs(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 < psfo.Tables.Length; idx++)
                    {
                        PARAM_SFO.Table value = psfo.Tables[idx];

                        switch (value.Indextable.param_data_fmt)
                        {
                        case FMT.UTF_8:
                        {
                            sw.Write(value.Value);
                            break;
                        }

                        case FMT.UINT32:
                        {
                            sw.Write(Convert.ToUInt32(value.Value));
                            break;
                        }

                        case FMT.ASCII:
                        {
                            sw.Write(value.Value);
                            break;
                        }
                        }

                        long pos = sw.BaseStream.Position;

                        WritePadBytes(sw, myindexes[idx].param_data_len, myindexes[idx].param_data_max_len);
                    }
                }
            }
        }