5
5
#include < exception>
6
6
#include < filesystem>
7
7
#include < functional>
8
+ #include < optional>
8
9
#include < set>
9
10
#include < sstream>
10
11
#include < stdexcept>
@@ -159,9 +160,9 @@ bool mapbuffer::submap_exists_approx( const tripoint_abs_sm &p )
159
160
if ( !file_exist ( zzip_name ) ) {
160
161
return false ;
161
162
}
162
- std::shared_ptr <zzip> z = zzip::load ( zzip_name.get_unrelative_path (),
163
- ( PATH_INFO::world_base_save_path () / " maps.dict" ).get_unrelative_path () );
164
- return z->has_file ( std::filesystem::u8path ( file_name ) );
163
+ std::optional <zzip> z = zzip::load ( zzip_name.get_unrelative_path (),
164
+ ( PATH_INFO::world_base_save_path () / " maps.dict" ).get_unrelative_path () );
165
+ return z && z ->has_file ( std::filesystem::u8path ( file_name ) );
165
166
} else {
166
167
return file_exist ( dirname / file_name );
167
168
}
@@ -246,13 +247,13 @@ void mapbuffer::save_quad(
246
247
bool reverted_to_uniform = false ;
247
248
bool file_exists = false ;
248
249
249
- std::shared_ptr<zzip> z;
250
+ std::optional<zzip> z;
251
+ cata_path zzip_name = dirname;
252
+ zzip_name += " .zzip" ;
250
253
// The number of uniform submaps is so enormous that the filesystem overhead
251
254
// for this step of just checking if the quad exists approaches 70% of the
252
255
// total cost of saving the mapbuffer, in one test save I had.
253
256
if ( world_generator->active_world ->has_compression_enabled () ) {
254
- cata_path zzip_name = dirname;
255
- zzip_name += " .zzip" ;
256
257
z = zzip::load ( zzip_name.get_unrelative_path (),
257
258
( PATH_INFO::world_base_save_path () / " maps.dict" ).get_unrelative_path () );
258
259
if ( !z ) {
@@ -335,7 +336,6 @@ void mapbuffer::save_quad(
335
336
336
337
if ( z ) {
337
338
z->add_file ( filename.get_relative_path ().filename (), s );
338
- z->compact ( 2.0 );
339
339
} else {
340
340
// Don't create the directory if it would be empty
341
341
assure_dir_exist ( dirname );
@@ -351,6 +351,13 @@ void mapbuffer::save_quad(
351
351
std::filesystem::remove ( filename.get_unrelative_path () );
352
352
}
353
353
}
354
+ if ( z ) {
355
+ cata_path tmp_path = zzip_name + " .tmp" ;
356
+ if ( z->compact_to ( tmp_path.get_unrelative_path (), 2.0 ) ) {
357
+ z.reset ();
358
+ rename_file ( tmp_path, zzip_name );
359
+ }
360
+ }
354
361
}
355
362
356
363
// We're reading in way too many entities here to mess around with creating sub-objects and
@@ -372,9 +379,9 @@ submap *mapbuffer::unserialize_submaps( const tripoint_abs_sm &p )
372
379
if ( !file_exist ( zzip_name ) ) {
373
380
return false ;
374
381
}
375
- std::shared_ptr <zzip> z = zzip::load ( zzip_name.get_unrelative_path (),
376
- ( PATH_INFO::world_base_save_path () / " maps.dict" ).get_unrelative_path () );
377
- if ( !z->has_file ( file_name_path ) ) {
382
+ std::optional <zzip> z = zzip::load ( zzip_name.get_unrelative_path (),
383
+ ( PATH_INFO::world_base_save_path () / " maps.dict" ).get_unrelative_path () );
384
+ if ( !z || !z ->has_file ( file_name_path ) ) {
378
385
return false ;
379
386
}
380
387
std::vector<std::byte> contents = z->get_file ( file_name_path );
0 commit comments