Skip to content

Commit

Permalink
Fix savegame loading map files with comma in name (#35660)
Browse files Browse the repository at this point in the history
* Extract repeated code into a function

* Extract repeated code into a function

* Fix for loading old saves where the map path name was constructed via std::ostringstream
  • Loading branch information
BevapDin authored and kevingranade committed Nov 23, 2019
1 parent 2c65122 commit e73f6f3
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/mapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) ) ) {
Expand Down

0 comments on commit e73f6f3

Please sign in to comment.