1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-23 03:02:53 +01:00

Reimplemented psf loader

This commit is contained in:
DHrpcs3 2016-01-08 00:12:33 +02:00 committed by Nekotekina
parent 8e9f456029
commit 220aab1fd0
6 changed files with 512 additions and 402 deletions

View File

@ -79,25 +79,25 @@ s32 cellHddGameCheck(PPUThread& ppu, u32 version, vm::cptr<char> dirName, u32 er
{
// 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 PSFLoader psf(f);
const psf::object psf(f);
if (!psf)
{
return CELL_HDDGAME_ERROR_BROKEN;
}
get->getParam.parentalLevel = psf.GetInteger("PARENTAL_LEVEL");
get->getParam.attribute = psf.GetInteger("ATTRIBUTE");
get->getParam.resolution = psf.GetInteger("RESOLUTION");
get->getParam.soundFormat = psf.GetInteger("SOUND_FORMAT");
std::string title = psf.GetString("TITLE");
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.GetString("APP_VER");
std::string app_ver = psf["APP_VER"].as_string();
strcpy_trunc(get->getParam.dataVersion, app_ver);
strcpy_trunc(get->getParam.titleId, dir);
for (u32 i = 0; i < CELL_HDDGAME_SYSP_LANGUAGE_NUM; i++)
{
title = psf.GetString(fmt::format("TITLE_%02d", i));
title = psf[fmt::format("TITLE_%02d", i)].as_string();
strcpy_trunc(get->getParam.titleLang[i], title);
}
}
@ -168,7 +168,7 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
}
vfsFile f("/app_home/../PARAM.SFO");
const PSFLoader psf(f);
const psf::object psf(f);
if (!psf)
{
@ -176,7 +176,7 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
cellGame.error("cellGameBootCheck(): Cannot read PARAM.SFO.");
}
std::string category = psf.GetString("CATEGORY");
std::string category = psf["CATEGORY"].as_string();
if (category.substr(0, 2) == "DG")
{
*type = CELL_GAME_GAMETYPE_DISC;
@ -190,7 +190,7 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
}
else if (category.substr(0, 2) == "HG")
{
std::string titleId = psf.GetString("TITLE_ID");
std::string titleId = psf["TITLE_ID"].as_string();
*type = CELL_GAME_GAMETYPE_HDD;
*attributes = 0; // TODO
if (dirName) strcpy_trunc(*dirName, titleId);
@ -202,7 +202,7 @@ s32 cellGameBootCheck(vm::ptr<u32> type, vm::ptr<u32> attributes, vm::ptr<CellGa
}
else if (category.substr(0, 2) == "GD")
{
std::string titleId = psf.GetString("TITLE_ID");
std::string titleId = psf["TITLE_ID"].as_string();
*type = CELL_GAME_GAMETYPE_DISC;
*attributes = CELL_GAME_ATTRIBUTE_PATCH; // TODO
if (dirName) strcpy_trunc(*dirName, titleId); // ???
@ -235,21 +235,21 @@ s32 cellGamePatchCheck(vm::ptr<CellGameContentSize> size, vm::ptr<void> reserved
}
vfsFile f("/app_home/../PARAM.SFO");
const PSFLoader psf(f);
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;
}
std::string category = psf.GetString("CATEGORY");
std::string category = psf["CATEGORY"].as_string();
if (category.substr(0, 2) != "GD")
{
cellGame.error("cellGamePatchCheck(): CELL_GAME_ERROR_NOTPATCH");
return CELL_GAME_ERROR_NOTPATCH;
}
if (!fxm::make<content_permission_t>("/dev_hdd0/game/" + psf.GetString("TITLE_ID"), false))
if (!fxm::make<content_permission_t>("/dev_hdd0/game/" + psf["TITLE_ID"].as_string(), false))
{
return CELL_GAME_ERROR_BUSY;
}
@ -378,7 +378,7 @@ s32 cellGameDataCheckCreate2(PPUThread& ppu, u32 version, vm::cptr<char> dirName
}
vfsFile f("/app_home/../PARAM.SFO");
const PSFLoader psf(f);
const psf::object psf(f);
if (!psf)
{
cellGame.error("cellGameDataCheckCreate2(): CELL_GAMEDATA_ERROR_BROKEN (cannot read PARAM.SFO)");
@ -406,10 +406,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.GetInteger("PARENTAL_LEVEL");
strcpy_trunc(cbGet->getParam.dataVersion, psf.GetString("APP_VER"));
strcpy_trunc(cbGet->getParam.titleId, psf.GetString("TITLE_ID"));
strcpy_trunc(cbGet->getParam.title, psf.GetString("TITLE"));
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());
// TODO: write lang titles
funcStat(ppu, cbResult, cbGet, cbSet);
@ -507,24 +507,26 @@ s32 cellGameGetParamInt(u32 id, vm::ptr<u32> value)
// TODO: Access through cellGame***Check functions
vfsFile f("/app_home/../PARAM.SFO");
const PSFLoader psf(f);
const psf::object psf(f);
if (!psf)
{
return CELL_GAME_ERROR_FAILURE;
}
std::string key;
switch(id)
{
case CELL_GAME_PARAMID_PARENTAL_LEVEL: *value = psf.GetInteger("PARENTAL_LEVEL"); break;
case CELL_GAME_PARAMID_RESOLUTION: *value = psf.GetInteger("RESOLUTION"); break;
case CELL_GAME_PARAMID_SOUND_FORMAT: *value = psf.GetInteger("SOUND_FORMAT"); break;
case CELL_GAME_PARAMID_PARENTAL_LEVEL: key = "PARENTAL_LEVEL"; break;
case CELL_GAME_PARAMID_RESOLUTION: key = "RESOLUTION"; break;
case CELL_GAME_PARAMID_SOUND_FORMAT: key = "SOUND_FORMAT"; break;
default:
cellGame.error("cellGameGetParamInt(): Unimplemented parameter (%d)", id);
return CELL_GAME_ERROR_INVALID_ID;
}
//TODO: check format?
*value = psf[key].as_integer();
return CELL_OK;
}
@ -534,48 +536,56 @@ s32 cellGameGetParamString(u32 id, vm::ptr<char> buf, u32 bufsize)
// TODO: Access through cellGame***Check functions
vfsFile f("/app_home/../PARAM.SFO");
const PSFLoader psf(f);
const psf::object psf(f);
if (!psf)
{
return CELL_GAME_ERROR_FAILURE;
}
std::string data;
std::string key;
switch(id)
{
case CELL_GAME_PARAMID_TITLE: data = psf.GetString("TITLE"); break; // TODO: Is this value correct?
case CELL_GAME_PARAMID_TITLE_DEFAULT: data = psf.GetString("TITLE"); break;
case CELL_GAME_PARAMID_TITLE_JAPANESE: data = psf.GetString("TITLE_00"); break;
case CELL_GAME_PARAMID_TITLE_ENGLISH: data = psf.GetString("TITLE_01"); break;
case CELL_GAME_PARAMID_TITLE_FRENCH: data = psf.GetString("TITLE_02"); break;
case CELL_GAME_PARAMID_TITLE_SPANISH: data = psf.GetString("TITLE_03"); break;
case CELL_GAME_PARAMID_TITLE_GERMAN: data = psf.GetString("TITLE_04"); break;
case CELL_GAME_PARAMID_TITLE_ITALIAN: data = psf.GetString("TITLE_05"); break;
case CELL_GAME_PARAMID_TITLE_DUTCH: data = psf.GetString("TITLE_06"); break;
case CELL_GAME_PARAMID_TITLE_PORTUGUESE: data = psf.GetString("TITLE_07"); break;
case CELL_GAME_PARAMID_TITLE_RUSSIAN: data = psf.GetString("TITLE_08"); break;
case CELL_GAME_PARAMID_TITLE_KOREAN: data = psf.GetString("TITLE_09"); break;
case CELL_GAME_PARAMID_TITLE_CHINESE_T: data = psf.GetString("TITLE_10"); break;
case CELL_GAME_PARAMID_TITLE_CHINESE_S: data = psf.GetString("TITLE_11"); break;
case CELL_GAME_PARAMID_TITLE_FINNISH: data = psf.GetString("TITLE_12"); break;
case CELL_GAME_PARAMID_TITLE_SWEDISH: data = psf.GetString("TITLE_13"); break;
case CELL_GAME_PARAMID_TITLE_DANISH: data = psf.GetString("TITLE_14"); break;
case CELL_GAME_PARAMID_TITLE_NORWEGIAN: data = psf.GetString("TITLE_15"); break;
case CELL_GAME_PARAMID_TITLE_POLISH: data = psf.GetString("TITLE_16"); break;
case CELL_GAME_PARAMID_TITLE_PORTUGUESE_BRAZIL: data = psf.GetString("TITLE_17"); break;
case CELL_GAME_PARAMID_TITLE_ENGLISH_UK: data = psf.GetString("TITLE_18"); break;
case CELL_GAME_PARAMID_TITLE: key = "TITLE"; break; // TODO: Is this value correct?
case CELL_GAME_PARAMID_TITLE_DEFAULT: key = "TITLE"; break;
case CELL_GAME_PARAMID_TITLE_JAPANESE: key = "TITLE_00"; break;
case CELL_GAME_PARAMID_TITLE_ENGLISH: key = "TITLE_01"; break;
case CELL_GAME_PARAMID_TITLE_FRENCH: key = "TITLE_02"; break;
case CELL_GAME_PARAMID_TITLE_SPANISH: key = "TITLE_03"; break;
case CELL_GAME_PARAMID_TITLE_GERMAN: key = "TITLE_04"; break;
case CELL_GAME_PARAMID_TITLE_ITALIAN: key = "TITLE_05"; break;
case CELL_GAME_PARAMID_TITLE_DUTCH: key = "TITLE_06"; break;
case CELL_GAME_PARAMID_TITLE_PORTUGUESE: key = "TITLE_07"; break;
case CELL_GAME_PARAMID_TITLE_RUSSIAN: key = "TITLE_08"; break;
case CELL_GAME_PARAMID_TITLE_KOREAN: key = "TITLE_09"; break;
case CELL_GAME_PARAMID_TITLE_CHINESE_T: key = "TITLE_10"; break;
case CELL_GAME_PARAMID_TITLE_CHINESE_S: key = "TITLE_11"; break;
case CELL_GAME_PARAMID_TITLE_FINNISH: key = "TITLE_12"; break;
case CELL_GAME_PARAMID_TITLE_SWEDISH: key = "TITLE_13"; break;
case CELL_GAME_PARAMID_TITLE_DANISH: key = "TITLE_14"; break;
case CELL_GAME_PARAMID_TITLE_NORWEGIAN: key = "TITLE_15"; break;
case CELL_GAME_PARAMID_TITLE_POLISH: key = "TITLE_16"; break;
case CELL_GAME_PARAMID_TITLE_PORTUGUESE_BRAZIL: key = "TITLE_17"; break;
case CELL_GAME_PARAMID_TITLE_ENGLISH_UK: key = "TITLE_18"; break;
case CELL_GAME_PARAMID_TITLE_ID: data = psf.GetString("TITLE_ID"); break;
case CELL_GAME_PARAMID_VERSION: data = psf.GetString("PS3_SYSTEM_VER"); break;
case CELL_GAME_PARAMID_APP_VER: data = psf.GetString("APP_VER"); break;
case CELL_GAME_PARAMID_TITLE_ID: key = "TITLE_ID"; break;
case CELL_GAME_PARAMID_VERSION: key = "PS3_SYSTEM_VER"; break;
case CELL_GAME_PARAMID_APP_VER: key = "APP_VER"; break;
default:
cellGame.error("cellGameGetParamString(): Unimplemented parameter (%d)", id);
return CELL_GAME_ERROR_INVALID_ID;
}
if (data.size() >= bufsize) data.resize(bufsize - 1);
memcpy(buf.get_ptr(), data.c_str(), data.size() + 1);
//TODO: check format?
std::string value = psf[key].as_string();
if (value.size() >= bufsize)
{
value.resize(bufsize - 1);
}
std::copy_n(value.c_str(), value.size() + 1, buf.get_ptr());
return CELL_OK;
}

View File

@ -96,7 +96,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
// PSF parameters
vfsFile f(base_dir + entry->name + "/PARAM.SFO");
const PSFLoader psf(f);
const psf::object psf(f);
if (!psf)
{
@ -104,11 +104,11 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
}
SaveDataEntry save_entry2;
save_entry2.dirName = psf.GetString("SAVEDATA_DIRECTORY");
save_entry2.listParam = psf.GetString("SAVEDATA_LIST_PARAM");
save_entry2.title = psf.GetString("TITLE");
save_entry2.subtitle = psf.GetString("SUB_TITLE");
save_entry2.details = psf.GetString("DETAIL");
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.size = 0;
@ -340,14 +340,15 @@ 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";
PSFLoader psf;
psf::object psf;
// Load PARAM.SFO
{
vfsFile f(sfo_path);
psf.Load(f);
vfsFile file(sfo_path);
psf.load(file);
}
const psf::object& psf_readonly = psf;
// Get save stats
{
std::string dir_local_path;
@ -368,11 +369,11 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
statGet->dir.ctime = save_entry.ctime = dir_info.ctime;
strcpy_trunc(statGet->dir.dirName, save_entry.dirName);
statGet->getParam.attribute = psf.GetInteger("ATTRIBUTE"); // ???
strcpy_trunc(statGet->getParam.title, save_entry.title = psf.GetString("TITLE"));
strcpy_trunc(statGet->getParam.subTitle, save_entry.subtitle = psf.GetString("SUB_TITLE"));
strcpy_trunc(statGet->getParam.detail, save_entry.details = psf.GetString("DETAIL"));
strcpy_trunc(statGet->getParam.listParam, save_entry.listParam = psf.GetString("SAVEDATA_LIST_PARAM"));
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());
statGet->bind = 0;
statGet->sizeKB = save_entry.size / 1024;
@ -410,7 +411,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
{
file.fileType = CELL_SAVEDATA_FILETYPE_CONTENT_SND0;
}
else if (psf.GetInteger("*" + entry->name)) // let's put the list of protected files in PARAM.SFO (int param = 1 if protected)
else if (psf["*" + entry->name].as_integer()) // let's put the list of protected files in PARAM.SFO (int param = 1 if protected)
{
file.fileType = CELL_SAVEDATA_FILETYPE_SECUREFILE;
}
@ -438,20 +439,20 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
if (statSet->setParam)
{
psf.Clear();
psf.clear();
// Update PARAM.SFO
psf.SetString("ACCOUNT_ID", ""); // ???
psf.SetInteger("ATTRIBUTE", statSet->setParam->attribute);
psf.SetString("CATEGORY", "SD"); // ???
psf.SetString("PARAMS", ""); // ???
psf.SetString("PARAMS2", ""); // ???
psf.SetInteger("PARENTAL_LEVEL", 0); // ???
psf.SetString("DETAIL", statSet->setParam->detail);
psf.SetString("SAVEDATA_DIRECTORY", save_entry.dirName);
psf.SetString("SAVEDATA_LIST_PARAM", statSet->setParam->listParam);
psf.SetString("SUB_TITLE", statSet->setParam->subTitle);
psf.SetString("TITLE", statSet->setParam->title);
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;
}
else if (!psf)
{
@ -572,7 +573,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
}
}
psf.SetInteger("*" + file_path, fileSet->fileType == CELL_SAVEDATA_FILETYPE_SECUREFILE);
psf["*" + file_path] = fileSet->fileType == CELL_SAVEDATA_FILETYPE_SECUREFILE;
std::string local_path;
@ -623,8 +624,8 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
// Write PARAM.SFO
if (psf)
{
vfsFile f(sfo_path, fom::rewrite);
psf.Save(f);
vfsFile file(sfo_path, fom::rewrite);
psf.save(file);
}
return CELL_OK;

View File

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

View File

@ -111,7 +111,7 @@ void GameViewer::LoadPSF()
continue;
}
const PSFLoader psf(f);
const psf::object psf(f);
if (!psf)
{
@ -124,14 +124,14 @@ void GameViewer::LoadPSF()
GameInfo game;
game.root = m_games[i];
game.serial = psf.GetString("TITLE_ID");
game.name = psf.GetString("TITLE");
game.app_ver = psf.GetString("APP_VER");
game.category = psf.GetString("CATEGORY");
game.fw = psf.GetString("PS3_SYSTEM_VER");
game.parental_lvl = psf.GetInteger("PARENTAL_LEVEL");
game.resolution = psf.GetInteger("RESOLUTION");
game.sound_format = psf.GetInteger("SOUND_FORMAT");
game.serial = psf["TITLE_ID"].as_string();
game.name = psf["TITLE"].as_string();
game.app_ver = psf["APP_VER"].as_string();
game.category = psf["CATEGORY"].as_string();
game.fw = psf["PS3_SYSTEM_VER"].as_string();
game.parental_lvl = psf["PARENTAL_LEVEL"].as_integer();
game.resolution = psf["RESOLUTION"].as_integer();
game.sound_format = psf["SOUND_FORMAT"].as_integer();
if (game.serial.length() == 9)
{

View File

@ -2,299 +2,367 @@
#include "Emu/FS/vfsStream.h"
#include "PSF.h"
bool PSFLoader::Load(vfsStream& stream)
namespace psf
{
PSFHeader header;
// load header
if (!stream.SRead(header))
u32 entry::max_size() const
{
return false;
return m_max_size;
}
// check magic
if (header.magic != *(u32*)"\0PSF")
entry& entry::max_size(u32 value)
{
LOG_ERROR(LOADER, "PSFLoader::Load() failed: unknown magic (0x%x)", header.magic);
return false;
m_max_size = value;
return *this;
}
// check version
if (header.version != 0x101)
entry_format entry::format() const
{
LOG_ERROR(LOADER, "PSFLoader::Load() failed: unknown version (0x%x)", header.version);
return false;
return m_format;
}
// load indices
std::vector<PSFDefTable> indices;
indices.resize(header.entries_num);
if (!stream.SRead(indices[0], sizeof(PSFDefTable) * header.entries_num))
entry& entry::format(entry_format value)
{
return false;
m_format = value;
return *this;
}
// load key table
if (header.off_key_table > header.off_data_table)
std::string entry::as_string() const
{
LOG_ERROR(LOADER, "PSFLoader::Load() failed: off_key_table=0x%x, off_data_table=0x%x", header.off_key_table, header.off_data_table);
return false;
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");
}
return m_value_string;
}
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)
u32 entry::as_integer() const
{
return false;
if (m_format != entry_format::integer)
{
throw std::logic_error("psf entry as_integer() error: bad format");
}
return m_value_integer;
}
keys.get()[key_table_size] = 0;
// load entries
std::vector<PSFEntry> entries;
entries.resize(header.entries_num);
for (u32 i = 0; i < header.entries_num; ++i)
std::string entry::to_string() const
{
entries[i].fmt = indices[i].param_fmt;
switch (m_format)
{
case entry_format::string:
case entry_format::string_not_null_term:
return m_value_string;
if (indices[i].key_off >= key_table_size)
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)
{
if (m_format != entry_format::string_not_null_term)
{
m_value_string.resize(m_max_size);
}
else
{
m_value_string.resize(m_max_size - 1);
}
}
return *this;
}
entry& entry::value(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");
}
return *this;
}
entry& entry::operator = (const std::string &value_)
{
return value(value_);
}
entry& entry::operator = (u32 value_)
{
return value(value_);
}
std::size_t entry::size() const
{
switch (m_format)
{
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);
}
throw std::logic_error("entry::size(): bad format");
}
bool object::load(vfsStream& stream)
{
clear();
header header_;
// load header
if (!stream.SRead(header_))
{
return false;
}
entries[i].name = keys.get() + indices[i].key_off;
// load data
stream.Seek(header.off_data_table + indices[i].data_off);
if (indices[i].param_fmt == psf_entry_format::integer && indices[i].param_len == 4 && indices[i].param_max >= indices[i].param_len)
// check magic
if (header_.magic != *(u32*)"\0PSF")
{
// load int data
if (!stream.SRead(entries[i].vint))
{
return false;
}
}
else if ((indices[i].param_fmt == psf_entry_format::string || indices[i].param_fmt == psf_entry_format::string_not_null_term)
&& indices[i].param_max >= indices[i].param_len)
{
// load str data
const u32 size = indices[i].param_len;
std::unique_ptr<char[]> str(new char[size + 1]);
if (stream.Read(str.get(), size) != size)
{
return false;
}
str.get()[size] = 0;
entries[i].vstr = str.get();
}
else
{
LOG_ERROR(LOADER, "PSFLoader::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);
LOG_ERROR(LOADER, "psf::load() failed: unknown magic (0x%x)", header_.magic);
return false;
}
}
// reset data
m_entries = std::move(entries);
return true;
}
bool PSFLoader::Save(vfsStream& stream) const
{
std::vector<PSFDefTable> indices;
indices.resize(m_entries.size());
// generate header
PSFHeader header;
header.magic = *(u32*)"\0PSF";
header.version = 0x101;
header.entries_num = static_cast<u32>(m_entries.size());
header.off_key_table = sizeof(PSFHeader) + sizeof(PSFDefTable) * header.entries_num;
{
// calculate key table length and generate indices
u32& key_offset = header.off_data_table = 0;
u32 data_offset = 0;
for (u32 i = 0; i < m_entries.size(); i++)
// check version
if (header_.version != 0x101)
{
indices[i].key_off = key_offset;
indices[i].data_off = data_offset;
indices[i].param_fmt = m_entries[i].fmt;
key_offset += static_cast<u32>(m_entries[i].name.size()) + 1; // key size
switch (m_entries[i].fmt) // calculate data size
{
case psf_entry_format::string_not_null_term:
{
data_offset += (indices[i].param_len = indices[i].param_max = static_cast<u32>(m_entries[i].vstr.size()));
break;
}
case psf_entry_format::string:
{
data_offset += (indices[i].param_len = indices[i].param_max = static_cast<u32>(m_entries[i].vstr.size()) + 1);
break;
}
case psf_entry_format::integer:
{
data_offset += (indices[i].param_len = indices[i].param_max = 4);
break;
}
default:
{
data_offset += (indices[i].param_len = indices[i].param_max = 0);
LOG_ERROR(LOADER, "PSFLoader::Save(): (i=%d) unknown entry format (0x%x, key='%s')", i, m_entries[i].fmt, m_entries[i].name);
}
}
LOG_ERROR(LOADER, "psf::load() failed: unknown version (0x%x)", header_.version);
return false;
}
}
header.off_data_table += header.off_key_table;
// load indices
std::vector<def_table> indices;
// save header
if (!stream.SWrite(header))
{
return false;
}
indices.resize(header_.entries_num);
// save indices
if (!stream.SWrite(indices[0], sizeof(PSFDefTable) * m_entries.size()))
{
return false;
}
// save key table
for (const auto& entry : m_entries)
{
if (!stream.SWrite(entry.name[0], entry.name.size() + 1))
if (!stream.SRead(indices[0], sizeof(def_table) * header_.entries_num))
{
return false;
}
}
// save data
for (const auto& entry : m_entries)
{
switch (entry.fmt)
// load key table
if (header_.off_key_table > header_.off_data_table)
{
case psf_entry_format::string:
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)
{
if (!stream.SWrite(entry.vstr[0], entry.vstr.size() + 1))
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)
{
return false;
}
break;
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)
{
// load int data
u32 value;
if (!stream.SRead(value))
{
return false;
}
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;
std::unique_ptr<char[]> str(new char[size + 1]);
if (stream.Read(str.get(), size) != size)
{
return false;
}
str.get()[size] = '\0';
entry_.value(str.get());
}
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;
}
}
case psf_entry_format::string_not_null_term:
return true;
}
bool object::save(vfsStream& stream) const
{
std::vector<def_table> indices;
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;
{
if (!stream.SWrite(entry.vstr[0], entry.vstr.size()))
// calculate key table length and generate indices
u32& key_offset = header_.off_data_table = 0;
u32 data_offset = 0;
std::size_t index = 0;
for (auto &entry : m_entries)
{
indices[index].key_off = key_offset;
indices[index].data_off = data_offset;
indices[index].param_fmt = entry.second.format();
key_offset += static_cast<u32>(entry.first.size()) + 1; // key size
u32 max_size = entry.second.max_size();
if (max_size == 0)
{
max_size = entry.second.size();
}
data_offset += 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))
{
return false;
}
break;
}
case psf_entry_format::integer:
// save data
for (const auto& entry : m_entries)
{
if (!stream.SWrite(entry.vint))
switch (entry.second.format())
{
return false;
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;
}
}
break;
}
}
return true;
}
return true;
}
void PSFLoader::Clear()
{
m_entries.clear();
}
const PSFEntry* PSFLoader::SearchEntry(const std::string& key) const
{
for (auto& entry : m_entries)
void object::clear()
{
if (key == entry.name)
{
return &entry;
}
m_entries.clear();
}
return nullptr;
}
PSFEntry& PSFLoader::AddEntry(const std::string& key, psf_entry_format fmt)
{
for (auto& entry : m_entries)
entry& object::operator[](const std::string &key)
{
if (key == entry.name)
{
entry.fmt = fmt;
return entry;
}
return m_entries[key];
}
PSFEntry new_entry = {};
new_entry.fmt = fmt;
new_entry.name = key;
m_entries.push_back(new_entry);
return m_entries.back();
}
std::string PSFLoader::GetString(const std::string& key, std::string def) const
{
if (const auto entry = SearchEntry(key))
const entry& object::operator[](const std::string &key) const
{
if (entry->fmt == psf_entry_format::string || entry->fmt == psf_entry_format::string_not_null_term)
{
return entry->vstr;
}
return m_entries.at(key);
}
return def;
}
s32 PSFLoader::GetInteger(const std::string& key, s32 def) const
{
if (const auto entry = SearchEntry(key))
{
if (entry->fmt == psf_entry_format::integer)
{
return entry->vint;
}
}
return def;
}
void PSFLoader::SetString(const std::string& key, std::string value)
{
AddEntry(key, psf_entry_format::string).vstr = value;
}
void PSFLoader::SetInteger(const std::string& key, s32 value)
{
AddEntry(key, psf_entry_format::integer).vint = value;
}

View File

@ -2,87 +2,118 @@
struct vfsStream;
enum class psf_entry_format : u16
namespace psf
{
string_not_null_term = 0x0004,
string = 0x0204,
integer = 0x0404,
};
struct PSFHeader
{
u32 magic;
u32 version;
u32 off_key_table;
u32 off_data_table;
u32 entries_num;
};
struct PSFDefTable
{
u16 key_off;
psf_entry_format param_fmt;
u32 param_len;
u32 param_max;
u32 data_off;
};
struct PSFEntry
{
psf_entry_format fmt;
std::string name;
s32 vint;
std::string vstr;
};
class PSFLoader
{
std::vector<PSFEntry> m_entries;
public:
PSFLoader() = default;
PSFLoader(vfsStream& stream)
enum class entry_format : u16
{
Load(stream);
}
unknown = 0,
string_not_null_term = 0x0004,
string = 0x0204,
integer = 0x0404,
};
virtual ~PSFLoader() = default;
bool Load(vfsStream& stream);
bool Save(vfsStream& stream) const;
void Clear();
explicit operator bool() const
struct header
{
return !m_entries.empty();
}
u32 magic;
u32 version;
u32 off_key_table;
u32 off_data_table;
u32 entries_num;
};
const PSFEntry* SearchEntry(const std::string& key) const;
PSFEntry& AddEntry(const std::string& key, psf_entry_format format);
std::string GetString(const std::string& key, std::string def = "") const;
s32 GetInteger(const std::string& key, s32 def = 0) const;
void SetString(const std::string& key, std::string value);
void SetInteger(const std::string& key, s32 value);
std::vector<PSFEntry>& entries()
struct def_table
{
return m_entries;
}
u16 key_off;
entry_format param_fmt;
u32 param_len;
u32 param_max;
u32 data_off;
};
const std::vector<PSFEntry>& entries() const
class entry
{
return m_entries;
}
entry_format m_format = entry_format::unknown;
u32 m_max_size = 0;
std::vector<PSFEntry>::iterator begin()
{
return m_entries.begin();
}
u32 m_value_integer;
std::string m_value_string;
std::vector<PSFEntry>::iterator end()
public:
u32 max_size() const;
entry& max_size(u32 value);
entry_format format() const;
entry& format(entry_format value);
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;
};
class object
{
return m_entries.end();
}
};
std::unordered_map<std::string, entry> m_entries;
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;
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();
}
};
}