commit ad2d11ab
Added exception handleling to the reading of JSON files. Just clearing the data in that case.
Changed files
| M | source/code/highscore.cpp before |
| M | source/code/puzzlehandler.cpp before |
| M | source/code/stageclearhandler.cpp before |
diff --git a/source/code/highscore.cpp b/source/code/highscore.cpp
index 99a6bbe..96c06df 100644
--- a/source/code/highscore.cpp
+++ b/source/code/highscore.cpp
@@ -54,10 +54,17 @@ Highscore::Highscore(const std::string& type) {
filename = type+".json.dat";
std::string readFileContent = sago::GetFileContent(filename.c_str());
if (readFileContent.length() > 0) {
- std::stringstream ss(readFileContent);
- {
- cereal::JSONInputArchive archive(ss);
- archive(cereal::make_nvp("highscore", table));
+ try {
+ std::stringstream ss(readFileContent);
+ {
+ cereal::JSONInputArchive archive(ss);
+ archive(cereal::make_nvp("highscore", table));
+ }
+ }
+ catch (cereal::Exception &e) {
+ std::cerr << "Failed to read highscore " << filename << " due to formatting errors. Resetting the file. Reason: " <<
+ e.what() << std::endl;
+ table.clear();
}
}
if (table.size() < top) {
@@ -75,7 +82,7 @@ Highscore::Highscore(const std::string& type) {
void Highscore::writeFile() {
- std::stringstream ss;
+ std::stringstream ss;
{
cereal::JSONOutputArchive archive(ss);
archive(cereal::make_nvp("highscore", table));
diff --git a/source/code/puzzlehandler.cpp b/source/code/puzzlehandler.cpp
index 8769b6f..9fa815d 100644
--- a/source/code/puzzlehandler.cpp
+++ b/source/code/puzzlehandler.cpp
@@ -73,8 +73,14 @@ void LoadClearData() {
if (readFileContent.length() > 0) {
std::stringstream ss(readFileContent);
{
- cereal::JSONInputArchive archive(ss);
- archive(cereal::make_nvp("cleared", puzzleCleared));
+ try {
+ cereal::JSONInputArchive archive(ss);
+ archive(cereal::make_nvp("cleared", puzzleCleared));
+ }
+ catch (cereal::Exception &e) {
+ std::cerr << "Failed to read \"" << puzzleSavePath << "\". File will be regenerated. Reason: " << e.what() << std::endl;
+ puzzleCleared.clear();
+ }
}
}
else {
diff --git a/source/code/stageclearhandler.cpp b/source/code/stageclearhandler.cpp
index d901d08..53fac6c 100644
--- a/source/code/stageclearhandler.cpp
+++ b/source/code/stageclearhandler.cpp
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
Source information and contacts persons can be found at
-http://blockattack.net
+http://www.blockattack.net
===========================================================================
*/
@@ -30,6 +30,7 @@ http://blockattack.net
#include "cereal/types/vector.hpp"
#include "cereal/archives/json.hpp"
#include "sago/SagoMisc.hpp"
+#include "Libs/include/cereal/details/helpers.hpp"
//paths
const char* const stageClearSaveName = "stageClear.json.SCsave";
@@ -83,8 +84,14 @@ void LoadStageClearStages() {
if (readFileContent.length() > 0) {
std::stringstream ss(readFileContent);
{
- cereal::JSONInputArchive archive(ss);
- archive(cereal::make_nvp("stages", stages));
+ try {
+ cereal::JSONInputArchive archive(ss);
+ archive(cereal::make_nvp("stages", stages));
+ }
+ catch (cereal::Exception& e) {
+ std::cerr << "Failed to load file \"" << stageClearSaveName << "\". Reason: " << e.what() << std::endl;
+ stages.clear();
+ }
}
}
else {