diff --git a/src/mapbuffer.cpp b/src/mapbuffer.cpp index cc40e39d9f27f..6c32db50189be 100644 --- a/src/mapbuffer.cpp +++ b/src/mapbuffer.cpp @@ -21,6 +21,18 @@ #define dbg(x) DebugLog((x),D_MAP) << __FILE__ << ":" << __LINE__ << ": " +static std::string find_quad_path( const std::string &dirname, const tripoint &om_addr ) +{ + return string_format( "%s/%d.%d.%d.map", dirname, om_addr.x, om_addr.y, om_addr.z ); +} + +static std::string find_dirname( const tripoint &om_addr ) +{ + const tripoint segment_addr = omt_to_seg_copy( om_addr ); + return string_format( "%s/maps/%d.%d.%d", g->get_world_base_save_path(), segment_addr.x, + segment_addr.y, segment_addr.z ); +} + mapbuffer MAPBUFFER; mapbuffer::mapbuffer() = default; @@ -103,8 +115,7 @@ submap *mapbuffer::lookup_submap( const tripoint &p ) void mapbuffer::save( bool delete_after_save ) { - const std::string map_directory = g->get_world_base_save_path() + "/maps"; - assure_dir_exist( map_directory ); + assure_dir_exist( g->get_world_base_save_path() + "/maps" ); int num_saved_submaps = 0; int num_total_submaps = submaps.size(); @@ -137,11 +148,8 @@ void mapbuffer::save( bool delete_after_save ) // A segment is a chunk of 32x32 submap quads. // We're breaking them into subdirectories so there aren't too many files per directory. // Might want to make a set for this one too so it's only checked once per save(). - tripoint segment_addr = omt_to_seg_copy( om_addr ); - const std::string dirname = string_format( "%s/%d.%d.%d", map_directory, segment_addr.x, - segment_addr.y, segment_addr.z ); - const std::string quad_path = string_format( "%s/%d.%d.%d.map", dirname, om_addr.x, om_addr.y, - om_addr.z ); + const std::string dirname = find_dirname( om_addr ); + const std::string quad_path = find_quad_path( dirname, om_addr ); // delete_on_save deletes everything, otherwise delete submaps // outside the current map. @@ -240,11 +248,20 @@ submap *mapbuffer::unserialize_submaps( const tripoint &p ) { // Map the tripoint to the submap quad that stores it. const tripoint om_addr = sm_to_omt_copy( p ); - const tripoint segment_addr = omt_to_seg_copy( om_addr ); - const std::string dirname = string_format( "%s/maps/%d.%d.%d", g->get_world_base_save_path(), - segment_addr.x, segment_addr.y, segment_addr.z ); - const std::string quad_path = string_format( "%s/%d.%d.%d.map", dirname, om_addr.x, om_addr.y, - om_addr.z ); + const std::string dirname = find_dirname( om_addr ); + std::string quad_path = find_quad_path( dirname, om_addr ); + + if( !file_exist( quad_path ) ) { + // Fix for old saves where the path was generated using std::stringstream, which + // did format the number using the current locale. That formatting may insert + // thousands separators, so the resulting path is "map/1,234.7.8.map" instead + // of "map/1234.7.8.map". + std::ostringstream buffer; + buffer << dirname << "/" << om_addr.x << "." << om_addr.y << "." << om_addr.z << ".map"; + if( file_exist( buffer.str() ) ) { + quad_path = buffer.str(); + } + } using namespace std::placeholders; if( !read_from_file_optional_json( quad_path, std::bind( &mapbuffer::deserialize, this, _1 ) ) ) {