Skip to content

Commit b85a5a5

Browse files
authored
Merge pull request #82675 from akrieger/delteeted
2 parents 2735569 + f9f91c6 commit b85a5a5

16 files changed

+1017
-430
lines changed

src/cata_utility.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,15 @@ bool read_from_file_optional_json( const cata_path &path,
602602
return file_exist( path.get_unrelative_path() ) && read_from_file_json( path, reader );
603603
}
604604

605-
bool read_from_zzip_optional( const std::shared_ptr<zzip> &z,
605+
bool read_from_zzip_optional( const zzip &z,
606606
const std::filesystem::path &file,
607607
const std::function<void( std::string_view )> &reader )
608608
{
609-
if( !z || !z->has_file( file ) ) {
609+
if( !z.has_file( file ) ) {
610610
return false;
611611
}
612612
try {
613-
std::vector<std::byte> file_data = z->get_file( file );
613+
std::vector<std::byte> file_data = z.get_file( file );
614614
std::string_view sv{ reinterpret_cast<const char *>( file_data.data() ), file_data.size() };
615615
reader( sv );
616616
return true;

src/cata_utility.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ bool read_from_file_optional_json( const cata_path &path,
368368
* error handling logic.
369369
*/
370370
/**@{*/
371-
bool read_from_zzip_optional( const std::shared_ptr<zzip> &z,
371+
bool read_from_zzip_optional( const zzip &z,
372372
const std::filesystem::path &file,
373373
const std::function<void( std::string_view )> &reader );
374374
bool read_from_zzip_optional( const std::shared_ptr<zzip_stack> &z,

src/debug_menu.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,9 @@ void write_min_archive()
482482
min_mmr_save_rel +
483483
".temp" ).get_unrelative_path();
484484
{
485-
std::shared_ptr<zzip> min_mmr_temp_zzip = zzip::load( min_mmr_temp_zzip_path, mmr_dict );
485+
std::optional<zzip> min_mmr_temp_zzip = zzip::load( min_mmr_temp_zzip_path, mmr_dict );
486486
if( !min_mmr_temp_zzip ||
487-
!mmr_stack->copy_files_to( trimmed_mmr_entries, min_mmr_temp_zzip ) ||
488-
!min_mmr_temp_zzip->compact( 0 ) ) {
487+
!mmr_stack->copy_files_to( trimmed_mmr_entries, *min_mmr_temp_zzip ) ) {
489488
popup( _( "Failed to create minimized archive" ) );
490489
return;
491490
}

src/filesystem.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ bool rename_file( const std::filesystem::path &old_path, const std::filesystem::
178178
{
179179
setFsNeedsSync();
180180
std::error_code ec;
181-
std::filesystem::rename( old_path, new_path, ec );
181+
int attempts = 0;
182+
do {
183+
std::filesystem::rename( old_path, new_path, ec );
184+
} while( ec && ++attempts < 3 );
182185
return !ec;
183186
}
184187

src/game.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,8 +3254,9 @@ bool game::load( const save_t &name )
32543254
u.set_save_id( name.decoded_name() );
32553255

32563256
if( world_generator->active_world->has_compression_enabled() ) {
3257-
std::shared_ptr<zzip> z = zzip::load( ( save_file_path + ".zzip" ).get_unrelative_path() );
3258-
abort = !read_from_zzip_optional( z, save_file_path.get_unrelative_path().filename(),
3257+
std::optional<zzip> z = zzip::load( ( save_file_path + ".zzip" ).get_unrelative_path() );
3258+
abort = !z.has_value() ||
3259+
!read_from_zzip_optional( z.value(), save_file_path.get_unrelative_path().filename(),
32593260
[this]( std::string_view sv ) {
32603261
unserialize( std::string{ sv } );
32613262
} );
@@ -3519,11 +3520,12 @@ bool game::save_player_data()
35193520
if( world_generator->active_world->has_compression_enabled() ) {
35203521
std::stringstream save;
35213522
serialize_json( save );
3522-
std::shared_ptr<zzip> z = zzip::load( ( playerfile + SAVE_EXTENSION +
3523-
".zzip" ).get_unrelative_path() );
3523+
std::optional<zzip> z = zzip::load( ( playerfile + SAVE_EXTENSION +
3524+
".zzip.tmp" ).get_unrelative_path() );
35243525
saved_data = z->add_file( ( playerfile + SAVE_EXTENSION ).get_unrelative_path().filename(),
35253526
save.str() );
3526-
saved_data = saved_data && z->compact( 1.0 );
3527+
saved_data = saved_data && z->compact_to( ( playerfile + SAVE_EXTENSION +
3528+
".zzip" ).get_unrelative_path(), 0.0 );
35273529
} else {
35283530
saved_data = write_to_file( playerfile + SAVE_EXTENSION, [&]( std::ostream & fout ) {
35293531
serialize_json( fout );

src/mapbuffer.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <exception>
66
#include <filesystem>
77
#include <functional>
8+
#include <optional>
89
#include <set>
910
#include <sstream>
1011
#include <stdexcept>
@@ -159,9 +160,9 @@ bool mapbuffer::submap_exists_approx( const tripoint_abs_sm &p )
159160
if( !file_exist( zzip_name ) ) {
160161
return false;
161162
}
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 ) );
165166
} else {
166167
return file_exist( dirname / file_name );
167168
}
@@ -246,13 +247,13 @@ void mapbuffer::save_quad(
246247
bool reverted_to_uniform = false;
247248
bool file_exists = false;
248249

249-
std::shared_ptr<zzip> z;
250+
std::optional<zzip> z;
251+
cata_path zzip_name = dirname;
252+
zzip_name += ".zzip";
250253
// The number of uniform submaps is so enormous that the filesystem overhead
251254
// for this step of just checking if the quad exists approaches 70% of the
252255
// total cost of saving the mapbuffer, in one test save I had.
253256
if( world_generator->active_world->has_compression_enabled() ) {
254-
cata_path zzip_name = dirname;
255-
zzip_name += ".zzip";
256257
z = zzip::load( zzip_name.get_unrelative_path(),
257258
( PATH_INFO::world_base_save_path() / "maps.dict" ).get_unrelative_path() );
258259
if( !z ) {
@@ -335,7 +336,6 @@ void mapbuffer::save_quad(
335336

336337
if( z ) {
337338
z->add_file( filename.get_relative_path().filename(), s );
338-
z->compact( 2.0 );
339339
} else {
340340
// Don't create the directory if it would be empty
341341
assure_dir_exist( dirname );
@@ -351,6 +351,13 @@ void mapbuffer::save_quad(
351351
std::filesystem::remove( filename.get_unrelative_path() );
352352
}
353353
}
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+
}
354361
}
355362

356363
// 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 )
372379
if( !file_exist( zzip_name ) ) {
373380
return false;
374381
}
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 ) ) {
378385
return false;
379386
}
380387
std::vector<std::byte> contents = z->get_file( file_name_path );

0 commit comments

Comments
 (0)