PSF Loader simplified

This commit is contained in:
Nekotekina 2016-01-26 21:13:36 +03:00
parent 7417033d7f
commit 128ee67bba
9 changed files with 349 additions and 564 deletions

View File

@ -530,7 +530,7 @@ template<typename T> class se_t<T, true>
static_assert(!std::is_pointer<type>::value, "se_t<> error: invalid type (pointer)");
static_assert(!std::is_reference<type>::value, "se_t<> error: invalid type (reference)");
static_assert(!std::is_array<type>::value, "se_t<> error: invalid type (array)");
static_assert(!std::is_enum<type>::value, "se_t<> error: invalid type (enumeration), use integral type instead");
//static_assert(!std::is_enum<type>::value, "se_t<> error: invalid type (enumeration), use integral type instead");
static_assert(alignof(type) == alignof(stype), "se_t<> error: unexpected alignment");
template<typename T2, typename = void> struct bool_converter
@ -642,7 +642,7 @@ template<typename T> class se_t<T, false>
static_assert(!std::is_pointer<type>::value, "se_t<> error: invalid type (pointer)");
static_assert(!std::is_reference<type>::value, "se_t<> error: invalid type (reference)");
static_assert(!std::is_array<type>::value, "se_t<> error: invalid type (array)");
static_assert(!std::is_enum<type>::value, "se_t<> error: invalid type (enumeration), use integral type instead");
//static_assert(!std::is_enum<type>::value, "se_t<> error: invalid type (enumeration), use integral type instead");
public:
se_t() = default;

View File

@ -199,6 +199,16 @@ namespace fs
CHECK_ASSERTION(seek(0) != -1 && read(result));
return result;
}
// Read full file to std::vector
template<typename T>
std::enable_if_t<std::is_pod<T>::value && !std::is_pointer<T>::value, std::vector<T>> to_vector() const
{
std::vector<T> result;
result.resize(size() / sizeof(T));
CHECK_ASSERTION(seek(0) != -1 && read(result));
return result;
}
};
// TODO

View File

@ -17,18 +17,45 @@ struct vfsStream
virtual u64 Write(const void* src, u64 count) = 0;
template<typename T> force_inline bool SWrite(const T& data, u64 count = sizeof(T))
template<typename T>
force_inline bool SWrite(const T& data, u64 count = sizeof(T))
{
return Write(&data, count) == count;
}
virtual u64 Read(void* dst, u64 count) = 0;
template<typename T> force_inline bool SRead(T& data, u64 count = sizeof(T))
template<typename T>
force_inline bool SRead(T& data, u64 count = sizeof(T))
{
return Read(&data, count) == count;
}
template<typename T>
bool VWrite(const std::vector<T>& vec)
{
return IsOpened() && Write(vec.data(), vec.size() * sizeof(T)) == vec.size() * sizeof(T);
}
template<typename T>
std::vector<T> VRead()
{
std::vector<T> result;
if (IsOpened() == false)
{
return result;
}
result.resize(GetSize() / sizeof(T));
if (Seek(0) == -1 || Read(result.data(), result.size() * sizeof(T)) != result.size() * sizeof(T))
{
result.clear();
}
return result;
}
virtual u64 Seek(s64 offset, fs::seek_mode whence = fs::seek_set) = 0;
virtual u64 Tell() const = 0;

View File

@ -78,27 +78,19 @@ s32 cellHddGameCheck(PPUThread& ppu, u32 version, vm::cptr<char> dirName, u32 er
else
{
// TODO: Is cellHddGameCheck really responsible for writing the information in get->getParam ? (If not, delete this else)
vfsFile f("/dev_hdd0/game/" + dir + "/PARAM.SFO");
const psf::object psf(f);
if (!psf)
{
return CELL_HDDGAME_ERROR_BROKEN;
}
const auto& psf = psf::load(vfsFile("/dev_hdd0/game/" + dir + "/PARAM.SFO").VRead<char>());
get->getParam.parentalLevel = psf["PARENTAL_LEVEL"].as_integer();
get->getParam.attribute = psf["ATTRIBUTE"].as_integer();
get->getParam.resolution = psf["RESOLUTION"].as_integer();
get->getParam.soundFormat = psf["SOUND_FORMAT"].as_integer();
std::string title = psf["TITLE"].as_string();
strcpy_trunc(get->getParam.title, title);
std::string app_ver = psf["APP_VER"].as_string();
strcpy_trunc(get->getParam.dataVersion, app_ver);
strcpy_trunc(get->getParam.titleId, dir);
get->getParam.parentalLevel = psf.at("PARENTAL_LEVEL").as_integer();
get->getParam.attribute = psf.at("ATTRIBUTE").as_integer();
get->getParam.resolution = psf.at("RESOLUTION").as_integer();
get->getParam.soundFormat = psf.at("SOUND_FORMAT").as_integer();
strcpy_trunc(get->getParam.title, psf.at("TITLE").as_string());
strcpy_trunc(get->getParam.dataVersion, psf.at("APP_VER").as_string());
strcpy_trunc(get->getParam.titleId, psf.at("TITLE_ID").as_string());
for (u32 i = 0; i < CELL_HDDGAME_SYSP_LANGUAGE_NUM; i++)
{
title = psf[fmt::format("TITLE_%02d", i)].as_string();
strcpy_trunc(get->getParam.titleLang[i], title);
strcpy_trunc(get->getParam.titleLang[i], psf::get_string(psf, fmt::format("TITLE_%02d", i)));
}
}
@ -167,17 +159,17 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
size->sysSizeKB = 0;
}
vfsFile f("/app_home/../PARAM.SFO");
const psf::object psf(f);
const auto& psf = psf::load(vfsFile("/app_home/../PARAM.SFO").VRead<char>());
if (!psf)
if (psf.empty())
{
// According to testing (in debug mode) cellGameBootCheck doesn't return an error code, when PARAM.SFO doesn't exist.
cellGame.error("cellGameBootCheck(): Cannot read PARAM.SFO.");
return CELL_GAME_RET_OK;
}
std::string category = psf["CATEGORY"].as_string();
if (category.substr(0, 2) == "DG")
const std::string& category = psf.at("CATEGORY").as_string();
if (category == "DG")
{
*type = CELL_GAME_GAMETYPE_DISC;
*attributes = 0; // TODO
@ -188,9 +180,9 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
return CELL_GAME_ERROR_BUSY;
}
}
else if (category.substr(0, 2) == "HG")
else if (category == "HG")
{
std::string titleId = psf["TITLE_ID"].as_string();
const std::string& titleId = psf.at("TITLE_ID").as_string();
*type = CELL_GAME_GAMETYPE_HDD;
*attributes = 0; // TODO
if (dirName) strcpy_trunc(*dirName, titleId);
@ -200,9 +192,9 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
return CELL_GAME_ERROR_BUSY;
}
}
else if (category.substr(0, 2) == "GD")
else if (category == "GD")
{
std::string titleId = psf["TITLE_ID"].as_string();
const std::string& titleId = psf.at("TITLE_ID").as_string();
*type = CELL_GAME_GAMETYPE_DISC;
*attributes = CELL_GAME_ATTRIBUTE_PATCH; // TODO
if (dirName) strcpy_trunc(*dirName, titleId); // ???
@ -212,7 +204,7 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
return CELL_GAME_ERROR_BUSY;
}
}
else if (psf)
else
{
cellGame.error("cellGameBootCheck(): Unknown CATEGORY.");
}
@ -234,22 +226,16 @@ s32 cellGamePatchCheck(vm::ptr<CellGameContentSize> size, vm::ptr<void> reserved
size->sysSizeKB = 0;
}
vfsFile f("/app_home/../PARAM.SFO");
const psf::object psf(f);
if (!psf)
{
cellGame.error("cellGamePatchCheck(): CELL_GAME_ERROR_ACCESS_ERROR (cannot read PARAM.SFO)");
return CELL_GAME_ERROR_ACCESS_ERROR;
}
const auto& psf = psf::load(vfsFile("/app_home/../PARAM.SFO").VRead<char>());
std::string category = psf["CATEGORY"].as_string();
if (category.substr(0, 2) != "GD")
const std::string& category = psf.at("CATEGORY").as_string();
if (category != "GD")
{
cellGame.error("cellGamePatchCheck(): CELL_GAME_ERROR_NOTPATCH");
return CELL_GAME_ERROR_NOTPATCH;
}
if (!fxm::make<content_permission_t>("/dev_hdd0/game/" + psf["TITLE_ID"].as_string(), false))
if (!fxm::make<content_permission_t>("/dev_hdd0/game/" + psf.at("TITLE_ID").as_string(), false))
{
return CELL_GAME_ERROR_BUSY;
}
@ -377,13 +363,7 @@ s32 cellGameDataCheckCreate2(PPUThread& ppu, u32 version, vm::cptr<char> dirName
return CELL_GAMEDATA_RET_OK;
}
vfsFile f("/app_home/../PARAM.SFO");
const psf::object psf(f);
if (!psf)
{
cellGame.error("cellGameDataCheckCreate2(): CELL_GAMEDATA_ERROR_BROKEN (cannot read PARAM.SFO)");
return CELL_GAMEDATA_ERROR_BROKEN;
}
const auto& psf = psf::load(vfsFile("/app_home/../PARAM.SFO").VRead<char>());
vm::var<CellGameDataCBResult> cbResult;
vm::var<CellGameDataStatGet> cbGet;
@ -406,10 +386,10 @@ s32 cellGameDataCheckCreate2(PPUThread& ppu, u32 version, vm::cptr<char> dirName
cbGet->sysSizeKB = 0;
cbGet->getParam.attribute = CELL_GAMEDATA_ATTR_NORMAL;
cbGet->getParam.parentalLevel = psf["PARENTAL_LEVEL"].as_integer();
strcpy_trunc(cbGet->getParam.dataVersion, psf["APP_VER"].as_string());
strcpy_trunc(cbGet->getParam.titleId, psf["TITLE_ID"].as_string());
strcpy_trunc(cbGet->getParam.title, psf["TITLE"].as_string());
cbGet->getParam.parentalLevel = psf.at("PARENTAL_LEVEL").as_integer();
strcpy_trunc(cbGet->getParam.dataVersion, psf.at("APP_VER").as_string());
strcpy_trunc(cbGet->getParam.titleId, psf.at("TITLE_ID").as_string());
strcpy_trunc(cbGet->getParam.title, psf.at("TITLE").as_string());
// TODO: write lang titles
funcStat(ppu, cbResult, cbGet, cbSet);
@ -505,13 +485,7 @@ s32 cellGameGetParamInt(u32 id, vm::ptr<u32> value)
{
cellGame.warning("cellGameGetParamInt(id=%d, value=*0x%x)", id, value);
// TODO: Access through cellGame***Check functions
vfsFile f("/app_home/../PARAM.SFO");
const psf::object psf(f);
if (!psf)
{
return CELL_GAME_ERROR_FAILURE;
}
const auto& psf = psf::load(vfsFile("/app_home/../PARAM.SFO").VRead<char>());
std::string key;
@ -525,8 +499,7 @@ s32 cellGameGetParamInt(u32 id, vm::ptr<u32> value)
return CELL_GAME_ERROR_INVALID_ID;
}
//TODO: check format?
*value = psf[key].as_integer();
*value = psf.at(key).as_integer();
return CELL_OK;
}
@ -534,13 +507,7 @@ s32 cellGameGetParamString(u32 id, vm::ptr<char> buf, u32 bufsize)
{
cellGame.warning("cellGameGetParamString(id=%d, buf=*0x%x, bufsize=%d)", id, buf, bufsize);
// TODO: Access through cellGame***Check functions
vfsFile f("/app_home/../PARAM.SFO");
const psf::object psf(f);
if (!psf)
{
return CELL_GAME_ERROR_FAILURE;
}
const auto& psf = psf::load(vfsFile("/app_home/../PARAM.SFO").VRead<char>());
std::string key;
switch(id)
@ -576,13 +543,7 @@ s32 cellGameGetParamString(u32 id, vm::ptr<char> buf, u32 bufsize)
return CELL_GAME_ERROR_INVALID_ID;
}
//TODO: check format?
std::string value = psf[key].as_string();
if (value.size() >= bufsize)
{
value.resize(bufsize - 1);
}
const std::string& value = psf.at(key).as_string().substr(0, bufsize - 1);
std::copy_n(value.c_str(), value.size() + 1, buf.get_ptr());

View File

@ -95,20 +95,19 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
listGet->dirNum++;
// PSF parameters
vfsFile f(base_dir + entry->name + "/PARAM.SFO");
const psf::object psf(f);
const auto& psf = psf::load(vfsFile(base_dir + entry->name + "/PARAM.SFO").VRead<char>());
if (!psf)
if (psf.empty())
{
break;
}
SaveDataEntry save_entry2;
save_entry2.dirName = psf["SAVEDATA_DIRECTORY"].as_string();
save_entry2.listParam = psf["SAVEDATA_LIST_PARAM"].as_string();
save_entry2.title = psf["TITLE"].as_string();
save_entry2.subtitle = psf["SUB_TITLE"].as_string();
save_entry2.details = psf["DETAIL"].as_string();
save_entry2.dirName = psf.at("SAVEDATA_DIRECTORY").as_string();
save_entry2.listParam = psf.at("SAVEDATA_LIST_PARAM").as_string();
save_entry2.title = psf.at("TITLE").as_string();
save_entry2.subtitle = psf.at("SUB_TITLE").as_string();
save_entry2.details = psf.at("DETAIL").as_string();
save_entry2.size = 0;
@ -340,14 +339,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
std::string dir_path = base_dir + save_entry.dirName + "/";
std::string sfo_path = dir_path + "PARAM.SFO";
psf::object psf;
{
vfsFile file(sfo_path);
psf.load(file);
}
const psf::object& psf_readonly = psf;
auto&& psf = psf::load(vfsFile(sfo_path).VRead<char>());
// Get save stats
{
@ -362,18 +354,21 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
}
statGet->hddFreeSizeKB = 40 * 1024 * 1024; // 40 GB
statGet->isNewData = save_entry.isNew = !psf;
statGet->isNewData = save_entry.isNew = psf.empty();
statGet->dir.atime = save_entry.atime = dir_info.atime;
statGet->dir.mtime = save_entry.mtime = dir_info.mtime;
statGet->dir.ctime = save_entry.ctime = dir_info.ctime;
strcpy_trunc(statGet->dir.dirName, save_entry.dirName);
statGet->getParam.attribute = psf_readonly["ATTRIBUTE"].as_integer(); // ???
strcpy_trunc(statGet->getParam.title, save_entry.title = psf_readonly["TITLE"].as_string());
strcpy_trunc(statGet->getParam.subTitle, save_entry.subtitle = psf_readonly["SUB_TITLE"].as_string());
strcpy_trunc(statGet->getParam.detail, save_entry.details = psf_readonly["DETAIL"].as_string());
strcpy_trunc(statGet->getParam.listParam, save_entry.listParam = psf_readonly["SAVEDATA_LIST_PARAM"].as_string());
if (!psf.empty())
{
statGet->getParam.attribute = psf.at("ATTRIBUTE").as_integer(); // ???
strcpy_trunc(statGet->getParam.title, save_entry.title = psf.at("TITLE").as_string());
strcpy_trunc(statGet->getParam.subTitle, save_entry.subtitle = psf.at("SUB_TITLE").as_string());
strcpy_trunc(statGet->getParam.detail, save_entry.details = psf.at("DETAIL").as_string());
strcpy_trunc(statGet->getParam.listParam, save_entry.listParam = psf.at("SAVEDATA_LIST_PARAM").as_string());
}
statGet->bind = 0;
statGet->sizeKB = save_entry.size / 1024;
@ -411,7 +406,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
{
file.fileType = CELL_SAVEDATA_FILETYPE_CONTENT_SND0;
}
else if (psf["*" + entry->name].as_integer()) // let's put the list of protected files in PARAM.SFO (int param = 1 if protected)
else if (psf::get_integer(psf, "*" + entry->name)) // let's put the list of protected files in PARAM.SFO (int param = 1 if protected)
{
file.fileType = CELL_SAVEDATA_FILETYPE_SECUREFILE;
}
@ -439,22 +434,23 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
if (statSet->setParam)
{
psf.clear();
// Update PARAM.SFO
psf["ACCOUNT_ID"] = ""; // ???
psf["ATTRIBUTE"] = statSet->setParam->attribute;
psf["CATEGORY"] = "SD"; // ???
psf["PARAMS"] = ""; // ???
psf["PARAMS2"] = ""; // ???
psf["PARENTAL_LEVEL"] = 0; // ???
psf["DETAIL"] = statSet->setParam->detail;
psf["SAVEDATA_DIRECTORY"] = save_entry.dirName;
psf["SAVEDATA_LIST_PARAM"] = statSet->setParam->listParam;
psf["SUB_TITLE"] = statSet->setParam->subTitle;
psf["TITLE"] = statSet->setParam->title;
psf.insert(
{
{ "ACCOUNT_ID", psf::array(16, "0000000000000000") }, // ???
{ "ATTRIBUTE", statSet->setParam->attribute.value() },
{ "CATEGORY", psf::string(4, "SD") }, // ???
{ "PARAMS", psf::string(16, {}) }, // ???
{ "PARAMS2", psf::string(16, {}) }, // ???
{ "PARENTAL_LEVEL", 0 }, // ???
{ "DETAIL", psf::string(1024, statSet->setParam->detail) },
{ "SAVEDATA_DIRECTORY", psf::string(256, save_entry.dirName) },
{ "SAVEDATA_LIST_PARAM", psf::string(8, statSet->setParam->listParam) },
{ "SUB_TITLE", psf::string(128, statSet->setParam->subTitle) },
{ "TITLE", psf::string(128, statSet->setParam->title) },
});
}
else if (!psf)
else if (psf.empty())
{
// setParam is NULL for new savedata: abort operation
@ -506,7 +502,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
}
// Create save directory if necessary
if (psf && save_entry.isNew && !Emu.GetVFS().CreateDir(dir_path))
if (psf.size() && save_entry.isNew && !Emu.GetVFS().CreateDir(dir_path))
{
// Let's ignore this error for now
}
@ -573,7 +569,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
}
}
psf["*" + file_path] = fileSet->fileType == CELL_SAVEDATA_FILETYPE_SECUREFILE;
psf.emplace("*" + file_path, fileSet->fileType == CELL_SAVEDATA_FILETYPE_SECUREFILE);
std::string local_path;
@ -622,10 +618,9 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
}
// Write PARAM.SFO
if (psf)
if (psf.size())
{
vfsFile file(sfo_path, fom::rewrite);
psf.save(file);
vfsFile(sfo_path, fom::rewrite).VWrite(psf::save(psf));
}
return CELL_OK;

View File

@ -231,9 +231,9 @@ void Emulator::Load()
LOG_NOTICE(LOADER, "");
f.Open("/app_home/../PARAM.SFO");
const psf::object psf(f);
std::string title = psf["TITLE"].as_string();
std::string title_id = psf["TITLE_ID"].as_string();
const auto& psf = psf::load(f.VRead<char>());
std::string title = psf::get_string(psf, "TITLE");
std::string title_id = psf::get_string(psf, "TITLE_ID");
LOG_NOTICE(LOADER, "Title: %s", title.c_str());
LOG_NOTICE(LOADER, "Serial: %s", title_id.c_str());

View File

@ -105,18 +105,12 @@ void GameViewer::LoadPSF()
}
vfsFile f;
if (!f.Open(sfo))
{
continue;
}
const psf::object psf(f);
if (!psf)
{
continue;
}
const auto& psf = psf::load(f.VRead<char>());
// get local path from VFS...
std::string local_path;
@ -124,14 +118,14 @@ void GameViewer::LoadPSF()
GameInfo game;
game.root = m_games[i];
game.serial = psf.get_string_or("TITLE_ID", "unknown");
game.name = psf.get_string_or("TITLE", "unknown");
game.app_ver = psf.get_string_or("APP_VER", "unknown");
game.category = psf.get_string_or("CATEGORY", "unknown");
game.fw = psf.get_string_or("PS3_SYSTEM_VER", "unknown");
game.parental_lvl = psf.get_integer_or("PARENTAL_LEVEL", 0);
game.resolution = psf.get_integer_or("RESOLUTION", 0);
game.sound_format = psf.get_integer_or("SOUND_FORMAT", 0);
game.serial = psf::get_string(psf, "TITLE_ID", "unknown");
game.name = psf::get_string(psf, "TITLE", "unknown");
game.app_ver = psf::get_string(psf, "APP_VER", "unknown");
game.category = psf::get_string(psf, "CATEGORY", "unknown");
game.fw = psf::get_string(psf, "PS3_SYSTEM_VER", "unknown");
game.parental_lvl = psf::get_integer(psf, "PARENTAL_LEVEL");
game.resolution = psf::get_integer(psf, "RESOLUTION");
game.sound_format = psf::get_integer(psf, "SOUND_FORMAT");
if (game.serial.length() == 9)
{

View File

@ -1,409 +1,257 @@
#include "stdafx.h"
#include "Emu/FS/vfsStream.h"
#include "PSF.h"
namespace psf
{
u32 entry::max_size() const
{
return m_max_size;
}
_log::channel log("PSF");
entry& entry::max_size(u32 value)
struct header_t
{
m_max_size = value;
return *this;
}
le_t<u32> magic;
le_t<u32> version;
le_t<u32> off_key_table;
le_t<u32> off_data_table;
le_t<u32> entries_num;
};
entry_format entry::format() const
struct def_table_t
{
return m_format;
}
le_t<u16> key_off;
le_t<format> param_fmt;
le_t<u32> param_len;
le_t<u32> param_max;
le_t<u32> data_off;
};
entry& entry::format(entry_format value)
const std::string& entry::as_string() const
{
m_format = value;
return *this;
}
std::string entry::as_string() const
{
if (m_format != entry_format::string && m_format != entry_format::string_not_null_term)
{
throw std::logic_error("psf entry as_string() error: bad format");
}
CHECK_ASSERTION(m_type == format::string || m_type == format::array);
return m_value_string;
}
u32 entry::as_integer() const
{
if (m_format != entry_format::integer)
{
throw std::logic_error("psf entry as_integer() error: bad format");
}
CHECK_ASSERTION(m_type == format::integer);
return m_value_integer;
}
std::string entry::to_string() const
entry& entry::operator =(const std::string& value)
{
switch (m_format)
{
case entry_format::string:
case entry_format::string_not_null_term:
return m_value_string;
case entry_format::integer:
return std::to_string(m_value_integer);
}
throw std::logic_error("psf entry to_string() error: bad format");
}
u32 entry::to_integer() const
{
switch (m_format)
{
case entry_format::string:
case entry_format::string_not_null_term:
return std::stoul(m_value_string);
case entry_format::integer:
return m_value_integer;
}
throw std::logic_error("psf entry to_integer() error: bad format");
}
entry& entry::value(const std::string &value_)
{
if (m_format != entry_format::string_not_null_term)
{
m_format = entry_format::string;
}
m_value_string = value_;
if (m_max_size && m_value_string.size() > m_max_size)
{
if (m_format != entry_format::string_not_null_term)
{
m_value_string.erase(m_max_size);
}
else
{
m_value_string.erase(m_max_size - 1);
}
}
CHECK_ASSERTION(m_type == format::string || m_type == format::array);
m_value_string = value;
return *this;
}
entry& entry::value(u32 value_)
entry& entry::operator =(u32 value)
{
m_format = entry_format::integer;
m_value_integer = value_;
if (m_max_size && m_max_size != 4)
{
throw std::logic_error("entry::value() error: bad integer max length");
}
CHECK_ASSERTION(m_type == format::integer);
m_value_integer = value;
return *this;
}
entry& entry::operator = (const std::string &value_)
u32 entry::size() const
{
return value(value_);
switch (m_type)
{
case format::string:
case format::array:
return std::min(m_max_size, gsl::narrow<u32>(m_value_string.size() + (m_type == format::string)));
case format::integer:
return SIZE_32(u32);
}
throw EXCEPTION("Invalid format (0x%x)", m_type);
}
entry& entry::operator = (u32 value_)
registry load(const std::vector<char>& data)
{
return value(value_);
}
registry result;
std::size_t entry::size() const
{
switch (m_format)
// Hack for empty input (TODO)
if (data.empty())
{
case entry_format::string:
return m_value_string.size() + 1;
case entry_format::string_not_null_term:
return m_value_string.size();
case entry_format::integer:
return sizeof(u32);
return result;
}
throw std::logic_error("entry::size(): bad format");
}
// Check size
CHECK_ASSERTION(data.size() >= sizeof(header_t));
CHECK_ASSERTION((std::uintptr_t)data.data() % 8 == 0);
bool object::load(vfsStream& stream)
{
clear();
// Get header
const header_t& header = reinterpret_cast<const header_t&>(data[0]);
header header_;
// Check magic and version
CHECK_ASSERTION(header.magic == *(u32*)"\0PSF");
CHECK_ASSERTION(header.version == 0x101);
CHECK_ASSERTION(sizeof(header_t) + header.entries_num * sizeof(def_table_t) <= header.off_key_table);
CHECK_ASSERTION(header.off_key_table <= header.off_data_table);
CHECK_ASSERTION(header.off_data_table <= data.size());
// load header
if (!stream.SRead(header_))
// Get indices (alignment should be fine)
const def_table_t* indices = reinterpret_cast<const def_table_t*>(data.data() + sizeof(header_t));
// Load entries
for (u32 i = 0; i < header.entries_num; ++i)
{
return false;
}
CHECK_ASSERTION(indices[i].key_off < header.off_data_table - header.off_key_table);
// check magic
if (header_.magic != *(u32*)"\0PSF")
{
LOG_ERROR(LOADER, "psf::load() failed: unknown magic (0x%x)", header_.magic);
return false;
}
// Get key name range
const auto name_ptr = data.begin() + header.off_key_table + indices[i].key_off;
const auto name_end = std::find(name_ptr , data.begin() + header.off_data_table, '\0');
// check version
if (header_.version != 0x101)
{
LOG_ERROR(LOADER, "psf::load() failed: unknown version (0x%x)", header_.version);
return false;
}
// Get name (must be unique)
std::string key(name_ptr, name_end);
// load indices
std::vector<def_table> indices;
CHECK_ASSERTION(result.count(key) == 0);
CHECK_ASSERTION(indices[i].param_len <= indices[i].param_max);
CHECK_ASSERTION(indices[i].data_off < data.size() - header.off_data_table);
CHECK_ASSERTION(indices[i].param_max < data.size() - indices[i].data_off);
indices.resize(header_.entries_num);
// Get data pointer
const auto value_ptr = data.begin() + header.off_data_table + indices[i].data_off;
if (!stream.SRead(indices[0], sizeof(def_table) * header_.entries_num))
{
return false;
}
// load key table
if (header_.off_key_table > header_.off_data_table)
{
LOG_ERROR(LOADER, "psf::load() failed: off_key_table=0x%x, off_data_table=0x%x", header_.off_key_table, header_.off_data_table);
return false;
}
const u32 key_table_size = header_.off_data_table - header_.off_key_table;
std::unique_ptr<char[]> keys(new char[key_table_size + 1]);
stream.Seek(header_.off_key_table);
if (stream.Read(keys.get(), key_table_size) != key_table_size)
{
return false;
}
keys.get()[key_table_size] = 0;
// load entries
for (u32 i = 0; i < header_.entries_num; ++i)
{
if (indices[i].key_off >= key_table_size)
if (indices[i].param_fmt == format::integer && indices[i].param_max == sizeof(u32) && indices[i].param_len == sizeof(u32))
{
return false;
// Integer data
result.emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(key)),
std::forward_as_tuple(reinterpret_cast<const le_t<u32>&>(*value_ptr)));
}
std::string key = keys.get() + indices[i].key_off;
entry &entry_ = (*this)[key];
entry_.format(indices[i].param_fmt);
entry_.max_size(indices[i].param_max);
// load data
stream.Seek(header_.off_data_table + indices[i].data_off);
if (indices[i].param_fmt == entry_format::integer && indices[i].param_len == 4 && indices[i].param_max >= indices[i].param_len)
else if (indices[i].param_fmt == format::string || indices[i].param_fmt == format::array)
{
// load int data
// String/array data
std::string value;
u32 value;
if (!stream.SRead(value))
if (indices[i].param_fmt == format::string)
{
return false;
// Find null terminator
value.assign(value_ptr, std::find(value_ptr, value_ptr + indices[i].param_len, '\0'));
}
else
{
value.assign(value_ptr, value_ptr + indices[i].param_len);
}
entry_.value(value);
}
else if ((indices[i].param_fmt == entry_format::string || indices[i].param_fmt == entry_format::string_not_null_term)
&& indices[i].param_max >= indices[i].param_len)
{
// load str data
const u32 size = indices[i].param_len;
if (size > 0)
{
std::unique_ptr<char[]> str(new char[size]);
if (stream.Read(str.get(), size) != size)
{
return false;
}
if (indices[i].param_fmt == entry_format::string)
{
str.get()[size - 1] = '\0';
entry_.value(str.get());
}
else
{
entry_.value(std::string{ str.get(), size });
}
}
result.emplace(std::piecewise_construct,
std::forward_as_tuple(std::move(key)),
std::forward_as_tuple(indices[i].param_fmt, indices[i].param_max, std::move(value)));
}
else
{
LOG_ERROR(LOADER, "psf::load() failed: (i=%d) fmt=0x%x, len=0x%x, max=0x%x", i, indices[i].param_fmt, indices[i].param_len, indices[i].param_max);
return false;
// Possibly unsupported format, entry ignored
log.error("Unknown entry format (key='%s', fmt=0x%x, len=0x%x, max=0x%x)", key, indices[i].param_fmt, indices[i].param_len, indices[i].param_max);
}
}
return true;
return result;
}
bool object::save(vfsStream& stream) const
std::vector<char> save(const registry& psf)
{
std::vector<def_table> indices;
std::vector<def_table_t> indices; indices.reserve(psf.size());
indices.resize(m_entries.size());
// generate header
header header_;
header_.magic = *(u32*)"\0PSF";
header_.version = 0x101;
header_.entries_num = static_cast<u32>(m_entries.size());
header_.off_key_table = sizeof(header) + sizeof(def_table) * header_.entries_num;
// Generate indices and calculate key table length
std::size_t key_offset = 0, data_offset = 0;
for (const auto& entry : psf)
{
// calculate key table length and generate indices
def_table_t index;
index.key_off = gsl::narrow<u32>(key_offset);
index.param_fmt = entry.second.type();
index.param_len = entry.second.size();
index.param_max = entry.second.max();
index.data_off = gsl::narrow<u32>(data_offset);
u32& key_offset = header_.off_data_table = 0;
u32 data_offset = 0;
std::size_t index = 0;
for (auto &entry : m_entries)
// Update offsets:
key_offset += gsl::narrow<u32>(entry.first.size() + 1); // key size
data_offset += index.param_max;
indices.push_back(index);
}
// Align next section (data) offset
key_offset = ::align(key_offset, 4);
// Generate header
header_t header;
header.magic = *(u32*)"\0PSF";
header.version = 0x101;
header.off_key_table = gsl::narrow<u32>(sizeof(header_t) + sizeof(def_table_t) * psf.size());
header.off_data_table = gsl::narrow<u32>(header.off_key_table + key_offset);
header.entries_num = gsl::narrow<u32>(psf.size());
// Save header and indices
std::vector<char> result; result.reserve(header.off_data_table + data_offset);
result.insert(result.end(), (char*)&header, (char*)&header + sizeof(header_t));
result.insert(result.end(), (char*)indices.data(), (char*)indices.data() + sizeof(def_table_t) * psf.size());
// Save key table
for (const auto& entry : psf)
{
result.insert(result.end(), entry.first.begin(), entry.first.end());
result.push_back('\0');
}
// Insert zero padding
result.insert(result.end(), header.off_data_table - result.size(), '\0');
// Save data
for (const auto& entry : psf)
{
const auto fmt = entry.second.type();
const u32 max = entry.second.max();
if (fmt == format::integer && max == sizeof(u32))
{
indices[index].key_off = key_offset;
indices[index].data_off = data_offset;
indices[index].param_fmt = entry.second.format();
const le_t<u32> value = entry.second.as_integer();
result.insert(result.end(), (char*)&value, (char*)&value + sizeof(u32));
}
else if (fmt == format::string || fmt == format::array)
{
const std::string& value = entry.second.as_string();
const std::size_t size = std::min<std::size_t>(max, value.size());
key_offset += static_cast<u32>(entry.first.size()) + 1; // key size
u32 max_size = entry.second.max_size();
if (max_size == 0)
if (value.size() + (fmt == format::string) > max)
{
max_size = entry.second.size();
// TODO: check real limitations of PSF format
log.error("Entry value shrinkage (key='%s', value='%s', size=0x%zx, max=0x%x)", entry.first, value, size, max);
}
data_offset += max_size;
result.insert(result.end(), value.begin(), value.begin() + size);
result.insert(result.end(), max - size, '\0'); // Write zeros up to max_size
}
}
header_.off_data_table += header_.off_key_table;
// save header
if (!stream.SWrite(header_))
{
return false;
}
// save indices
if (!stream.SWrite(indices[0], sizeof(def_table) * m_entries.size()))
{
return false;
}
// save key table
for (const auto& entry : m_entries)
{
if (!stream.SWrite(entry.first, entry.first.size() + 1))
else
{
return false;
throw EXCEPTION("Invalid entry format (key='%s', fmt=0x%x)", entry.first, fmt);
}
}
// save data
for (const auto& entry : m_entries)
return result;
}
std::string get_string(const registry& psf, const std::string& key, const std::string& def)
{
const auto found = psf.find(key);
if (found == psf.end() || (found->second.type() != format::string && found->second.type() != format::array))
{
switch (entry.second.format())
{
case entry_format::string:
{
std::string value = entry.second.as_string();
if (!stream.SWrite(value.data(), value.size() + 1))
{
return false;
}
break;
}
case entry_format::string_not_null_term:
{
std::string value = entry.second.as_string();
if (!stream.SWrite(value.data(), value.size()))
{
return false;
}
break;
}
case entry_format::integer:
{
if (!stream.SWrite(entry.second.as_integer()))
{
return false;
}
break;
}
}
return def;
}
return true;
return found->second.as_string();
}
void object::clear()
u32 get_integer(const registry& psf, const std::string& key, u32 def)
{
m_entries.clear();
}
const auto found = psf.find(key);
entry& object::operator[](const std::string &key)
{
return m_entries[key];
}
const entry& object::operator[](const std::string &key) const
{
return m_entries.at(key);
}
const entry* object::get(const std::string &key) const
{
auto found = m_entries.find(key);
if (found == m_entries.end())
if (found == psf.end() || found->second.type() != format::integer)
{
return nullptr;
return def;
}
return &found->second;
}
std::string object::get_string_or(const std::string &key, const std::string &default_value) const
{
if (const psf::entry *found = get(key))
{
return found->as_string();
}
return default_value;
}
u32 object::get_integer_or(const std::string &key, u32 default_value) const
{
if (const psf::entry *found = get(key))
{
return found->as_integer();
}
return default_value;
return found->second.as_integer();
}
}

View File

@ -1,125 +1,75 @@
#pragma once
struct vfsStream;
namespace psf
{
enum class entry_format : u16
enum class format : u16
{
unknown = 0,
string_not_null_term = 0x0004,
string = 0x0204,
array = 0x0004, // claimed to be a non-NTS string (char array)
string = 0x0204,
integer = 0x0404,
};
struct header
class entry final
{
u32 magic;
u32 version;
u32 off_key_table;
u32 off_data_table;
u32 entries_num;
};
struct def_table
{
u16 key_off;
entry_format param_fmt;
u32 param_len;
u32 param_max;
u32 data_off;
};
class entry
{
entry_format m_format = entry_format::unknown;
u32 m_max_size = 0;
u32 m_value_integer;
std::string m_value_string;
u32 m_value_integer; // TODO: is it really unsigned?
u32 m_max_size; // Entry max size (supplementary info, stored in PSF format)
format m_type;
public:
u32 max_size() const;
entry& max_size(u32 value);
entry_format format() const;
entry& format(entry_format value);
std::string as_string() const;
// Construct string entry, assign the value
entry(format type, u32 max_size, const std::string& value = {})
: m_type(type)
, m_max_size(max_size)
, m_value_string(value)
{
CHECK_ASSERTION(type == format::string || type == format::array);
CHECK_ASSERTION(max_size);
}
// Construct integer entry, assign the value
entry(u32 value)
: m_type(format::integer)
, m_max_size(sizeof(u32))
, m_value_integer(value)
{
}
const std::string& as_string() const;
u32 as_integer() const;
std::string to_string() const;
u32 to_integer() const;
entry& value(const std::string &value_);
entry& value(u32 value_);
entry& operator = (const std::string &value_);
entry& operator = (u32 value_);
std::size_t size() const;
entry& operator =(const std::string& value);
entry& operator =(u32 value);
format type() const { return m_type; }
u32 max() const { return m_max_size; }
u32 size() const;
};
class object
// Define PSF registry as a sorted map of entries:
using registry = std::map<std::string, entry>;
// Load PSF registry from binary data
registry load(const std::vector<char>&);
// Convert PSF registry to binary format
std::vector<char> save(const registry&);
// Get string value or default value
std::string get_string(const registry& psf, const std::string& key, const std::string& def = {});
// Get integer value or default value
u32 get_integer(const registry& psf, const std::string& key, u32 def = 0);
// Make string entry
inline entry string(u32 max_size, const std::string& value)
{
std::unordered_map<std::string, entry> m_entries;
return{ format::string, max_size, value };
}
public:
object() = default;
object(vfsStream& stream)
{
load(stream);
}
virtual ~object() = default;
bool load(vfsStream& stream);
bool save(vfsStream& stream) const;
void clear();
explicit operator bool() const
{
return !m_entries.empty();
}
entry& operator[](const std::string &key);
const entry& operator[](const std::string &key) const;
//returns pointer to entry or null, if not exists
const entry *get(const std::string &key) const;
std::string get_string_or(const std::string &key, const std::string &default_value) const;
u32 get_integer_or(const std::string &key, u32 default_value) const;
bool exists(const std::string &key) const
{
return m_entries.find(key) != m_entries.end();
}
std::unordered_map<std::string, entry>& entries()
{
return m_entries;
}
const std::unordered_map<std::string, entry>& entries() const
{
return m_entries;
}
std::unordered_map<std::string, entry>::iterator begin()
{
return m_entries.begin();
}
std::unordered_map<std::string, entry>::iterator end()
{
return m_entries.end();
}
std::unordered_map<std::string, entry>::const_iterator begin() const
{
return m_entries.begin();
}
std::unordered_map<std::string, entry>::const_iterator end() const
{
return m_entries.end();
}
};
// Make array entry
inline entry array(u32 max_size, const std::string& value)
{
return{ format::array, max_size, value };
}
}