rpcsx/rpcs3/Loader/PSF.h

91 lines
2 KiB
C
Raw Normal View History

#pragma once
#include "util/types.hpp"
2016-02-01 22:47:09 +01:00
#include <map>
#include <string>
#include <string_view>
2016-02-01 22:47:09 +01:00
namespace fs
{
class file;
}
2016-01-07 23:12:33 +01:00
namespace psf
{
2016-01-26 19:13:36 +01:00
enum class format : u16
2015-04-16 01:17:42 +02:00
{
2016-01-26 19:13:36 +01:00
array = 0x0004, // claimed to be a non-NTS string (char array)
string = 0x0204,
2016-01-07 23:12:33 +01:00
integer = 0x0404,
};
2015-04-16 01:17:42 +02:00
2016-01-26 19:13:36 +01:00
class entry final
{
2016-01-26 19:13:36 +01:00
format m_type;
u32 m_max_size; // Entry max size (supplementary info, stored in PSF format)
u32 m_value_integer; // TODO: is it really unsigned?
std::string m_value_string;
2016-01-07 23:12:33 +01:00
public:
2016-01-26 19:13:36 +01:00
// Construct string entry, assign the value
2016-05-13 16:01:48 +02:00
entry(format type, u32 max_size, const std::string& value = {});
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Construct integer entry, assign the value
2016-05-13 16:01:48 +02:00
entry(u32 value);
~entry();
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
const std::string& as_string() const;
u32 as_integer() const;
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
entry& operator =(const std::string& value);
entry& operator =(u32 value);
2016-01-08 00:07:55 +01:00
2016-01-26 19:13:36 +01:00
format type() const { return m_type; }
u32 max() const { return m_max_size; }
u32 size() const;
};
2016-01-26 19:13:36 +01:00
// Define PSF registry as a sorted map of entries:
using registry = std::map<std::string, entry>;
2016-01-07 23:12:33 +01:00
2016-05-13 16:01:48 +02:00
// Load PSF registry from SFO binary format
registry load_object(const fs::file&);
2016-01-07 23:12:33 +01:00
2016-02-01 22:47:09 +01:00
// Convert PSF registry to SFO binary format
2016-05-13 16:01:48 +02:00
void save_object(const fs::file&, const registry&);
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Get string value or default value
std::string_view get_string(const registry& psf, const std::string& key, std::string_view def = ""sv);
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Get integer value or default value
u32 get_integer(const registry& psf, const std::string& key, u32 def = 0);
2016-01-07 23:12:33 +01:00
// Assign new entry
inline void assign(registry& psf, const std::string& key, entry&& _entry)
{
const auto found = psf.find(key);
if (found == psf.end())
{
psf.emplace(key, std::move(_entry));
return;
}
found->second = std::move(_entry);
return;
}
2016-01-26 19:13:36 +01:00
// Make string entry
inline entry string(u32 max_size, std::string_view value)
2016-01-26 19:13:36 +01:00
{
return {format::string, max_size, std::string(value)};
2016-01-26 19:13:36 +01:00
}
2016-01-07 23:12:33 +01:00
2016-01-26 19:13:36 +01:00
// Make array entry
inline entry array(u32 max_size, std::string_view value)
2016-01-26 19:13:36 +01:00
{
return {format::array, max_size, std::string(value)};
2016-01-26 19:13:36 +01:00
}
2016-01-07 23:12:33 +01:00
}