fixed GameViewer crash if entry not exists

fixed psf::entry::as_string & psf::entry::value(string)
This commit is contained in:
DHrpcs3 2016-01-08 01:53:03 +02:00 committed by Nekotekina
parent 290bdc4566
commit e8a940172c
3 changed files with 52 additions and 20 deletions

View file

@ -124,14 +124,14 @@ void GameViewer::LoadPSF()
GameInfo game;
game.root = m_games[i];
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();
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);
if (game.serial.length() == 9)
{

View file

@ -28,7 +28,7 @@ namespace psf
std::string entry::as_string() const
{
if (m_format != entry_format::string || m_format != entry_format::string_not_null_term)
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");
}
@ -85,15 +85,15 @@ namespace psf
m_value_string = value_;
if (m_max_size)
if (m_max_size && m_value_string.size() > m_max_size)
{
if (m_format != entry_format::string_not_null_term)
{
m_value_string.resize(m_max_size);
m_value_string.erase(m_max_size);
}
else
{
m_value_string.resize(m_max_size - 1);
m_value_string.erase(m_max_size - 1);
}
}
@ -233,16 +233,25 @@ namespace psf
const u32 size = indices[i].param_len;
std::unique_ptr<char[]> str(new char[size + 1]);
if (stream.Read(str.get(), size) != size)
if (size > 0)
{
return false;
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 });
}
}
str.get()[size] = '\0';
entry_.value(str.get());
}
else
{
@ -377,4 +386,24 @@ namespace psf
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;
}
}

View file

@ -84,6 +84,9 @@ namespace psf
//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();