From e8a940172ce231b2846e85a8f50c3ab4bbca642a Mon Sep 17 00:00:00 2001 From: DHrpcs3 Date: Fri, 8 Jan 2016 01:53:03 +0200 Subject: [PATCH] fixed GameViewer crash if entry not exists fixed psf::entry::as_string & psf::entry::value(string) --- rpcs3/Gui/GameViewer.cpp | 16 ++++++------ rpcs3/Loader/PSF.cpp | 53 +++++++++++++++++++++++++++++++--------- rpcs3/Loader/PSF.h | 3 +++ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/rpcs3/Gui/GameViewer.cpp b/rpcs3/Gui/GameViewer.cpp index d3c586cf80..6ff0a124f3 100644 --- a/rpcs3/Gui/GameViewer.cpp +++ b/rpcs3/Gui/GameViewer.cpp @@ -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) { diff --git a/rpcs3/Loader/PSF.cpp b/rpcs3/Loader/PSF.cpp index b0fcb22410..6db9b058d7 100644 --- a/rpcs3/Loader/PSF.cpp +++ b/rpcs3/Loader/PSF.cpp @@ -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 str(new char[size + 1]); - - if (stream.Read(str.get(), size) != size) + if (size > 0) { - return false; + std::unique_ptr 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; + } } diff --git a/rpcs3/Loader/PSF.h b/rpcs3/Loader/PSF.h index 1a24768d1e..f7847ac6bd 100644 --- a/rpcs3/Loader/PSF.h +++ b/rpcs3/Loader/PSF.h @@ -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();