From ec222c0b245c3d93070635f8ab0f67712fbb729b Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 29 Oct 2016 02:35:27 -0400 Subject: [PATCH] Catch exceptions in stockpile (un)serialization From #964, protobuf exceptions in loadstock/savestock would either fail by only logging an error to the console (when run from the Lua UI) or by crashing the game entirely (when run from the console). Figuring out what actually causes the exceptions in the first place (possibly a misunderstood structure layout?) would be a better solution than this, but this will at least stop crashes for now. --- plugins/stockpiles/stockpiles.cpp | 32 +++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/plugins/stockpiles/stockpiles.cpp b/plugins/stockpiles/stockpiles.cpp index 3cd2cb31a..ab9d9c8f7 100644 --- a/plugins/stockpiles/stockpiles.cpp +++ b/plugins/stockpiles/stockpiles.cpp @@ -244,11 +244,20 @@ static command_result savestock ( color_ostream &out, vector & paramete cereal.enable_debug ( out ); if ( !is_dfstockfile ( file ) ) file += ".dfstock"; - if ( !cereal.serialize_to_file ( file ) ) + try { - out.printerr ( "could not save to %s\n", file.c_str() ); + if ( !cereal.serialize_to_file ( file ) ) + { + out.printerr ( "could not save to %s\n", file.c_str() ); + return CR_FAILURE; + } + } + catch ( std::exception &e ) + { + out.printerr ( "serialization failed: protobuf exception: %s\n", e.what() ); return CR_FAILURE; } + return CR_OK; } @@ -296,9 +305,17 @@ static command_result loadstock ( color_ostream &out, vector & paramete StockpileSerializer cereal ( sp ); if ( debug ) cereal.enable_debug ( out ); - if ( !cereal.unserialize_from_file ( file ) ) + try { - out.printerr ( "unserialization failed\n" ); + if ( !cereal.unserialize_from_file ( file ) ) + { + out.printerr ( "unserialization failed: %s\n", file.c_str() ); + return CR_FAILURE; + } + } + catch ( std::exception &e ) + { + out.printerr ( "unserialization failed: protobuf exception: %s\n", e.what() ); return CR_FAILURE; } return CR_OK; @@ -508,13 +525,16 @@ static int stockpiles_list_settings ( lua_State *L ) return 1; } +const std::string err_title = "Stockpile Settings Error"; +const std::string err_help = "Does the folder exist?\nCheck the console for more information."; + static void stockpiles_load ( color_ostream &out, std::string filename ) { std::vector params; params.push_back ( filename ); command_result r = loadstock ( out, params ); if ( r != CR_OK ) - show_message_box ( "Stockpile Settings Error", "Couldn't load. Does the folder exist?", true ); + show_message_box ( err_title, "Couldn't load. " + err_help, true ); } @@ -524,7 +544,7 @@ static void stockpiles_save ( color_ostream &out, std::string filename ) params.push_back ( filename ); command_result r = savestock ( out, params ); if ( r != CR_OK ) - show_message_box ( "Stockpile Settings Error", "Couldn't save. Does the folder exist?", true ); + show_message_box ( err_title, "Couldn't save. " + err_help, true ); } DFHACK_PLUGIN_LUA_FUNCTIONS