Skip to content

Commit

Permalink
Remove zone caching scheme and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingranade committed Jan 18, 2022
1 parent cf7abd2 commit dfa2bfa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 45 deletions.
46 changes: 14 additions & 32 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "cursesdef.h"
#include "debug.h"
#include "faction.h"
#include "filesystem.h"
#include "generic_factory.h"
#include "iexamine.h"
#include "item.h"
Expand Down Expand Up @@ -140,8 +139,6 @@ void zone_manager::clear()
// Do not clear types since it is needed for the next games.
area_cache.clear();
vzone_cache.clear();
// Invalidate cache
loaded = false;
}

std::string zone_type::name() const
Expand Down Expand Up @@ -1055,7 +1052,8 @@ void zone_manager::add( const std::string &name, const zone_type_id &type, const
//Create a regular zone
zones.push_back( new_zone );

//If not player defined, save them immediately
// personal/faction zones are saved when the zone manager exits,
// but other-faction zones are not, so save them here.
if( fac != faction_your_followers ) {
save_world_zones();
}
Expand Down Expand Up @@ -1207,28 +1205,25 @@ bool zone_manager::has_personal_zones() const

void zone_manager::serialize( JsonOut &json ) const
{
std::vector<zone_data> tmp;
std::copy_if( zones.begin(), zones.end(), std::back_inserter( tmp ), []( zone_data z ) {
return z.get_faction() == faction_your_followers;
} );
json.write( tmp );
json.write( zones );
}

void zone_manager::deserialize( const JsonValue &jv )
{
jv.read( zones );
for( auto it = zones.begin(); it != zones.end(); ++it ) {
for( auto it = zones.begin(); it != zones.end(); ) {
// need to keep track of number of personal zones on reload
if( it->get_is_personal() ) {
num_personal_zones++;
}
const zone_type_id zone_type = it->get_type();
if( !has_type( zone_type ) ) {
zones.erase( it );
it = zones.erase( it );
debugmsg( "Invalid zone type: %s", zone_type.c_str() );
}
if( it->get_faction() != faction_your_followers ) {
zones.erase( it );
} else if( it->get_faction() != faction_your_followers ) {
it = zones.erase( it );
} else {
it++;
}
}
}
Expand Down Expand Up @@ -1298,7 +1293,7 @@ void zone_data::deserialize( const JsonObject &data )

bool zone_manager::save_zones()
{
std::string savefile = PATH_INFO::player_base_save_path() + ".zones_cache.json";
std::string savefile = PATH_INFO::player_base_save_path() + ".zones.json";

added_vzones.clear();
changed_vzones.clear();
Expand All @@ -1311,14 +1306,13 @@ bool zone_manager::save_zones()

void zone_manager::load_zones()
{
std::string cache = PATH_INFO::player_base_save_path() + ".zones_cache.json";
std::string savefile = PATH_INFO::player_base_save_path() + ".zones.json";

const auto reader = [&]( std::istream & fin ) {
const auto reader = [this]( std::istream & fin ) {
JsonIn jsin( fin );
deserialize( jsin.get_value() );
};
if( !read_from_file_optional( loaded ? cache : savefile, reader ) ) {
if( !read_from_file_optional( savefile, reader ) ) {
// If no such file or failed to load, clear zones.
zones.clear();
}
Expand All @@ -1330,17 +1324,11 @@ void zone_manager::load_zones()

cache_data();
cache_vzones();
if( !loaded ) {
// validate cache.
remove_file( cache );
copy_file( savefile, cache );
}
loaded = true;
}

bool zone_manager::save_world_zones()
{
std::string savefile = PATH_INFO::world_base_save_path() + "/zones_cache.json";
std::string savefile = PATH_INFO::world_base_save_path() + "/zones.json";
std::vector<zone_data> tmp;
std::copy_if( zones.begin(), zones.end(), std::back_inserter( tmp ), []( zone_data z ) {
return z.get_faction() != faction_your_followers;
Expand All @@ -1354,9 +1342,8 @@ bool zone_manager::save_world_zones()
void zone_manager::load_world_zones()
{
std::string savefile = PATH_INFO::world_base_save_path() + "/zones.json";
std::string cache = PATH_INFO::world_base_save_path() + "/zones_cache.json";
std::vector<zone_data> tmp;
read_from_file_optional( loaded ? cache : savefile, [&]( std::istream & fin ) {
read_from_file_optional( savefile, [&]( std::istream & fin ) {
JsonIn jsin( fin );
jsin.read( tmp );
for( auto it = tmp.begin(); it != tmp.end(); ++it ) {
Expand All @@ -1371,11 +1358,6 @@ void zone_manager::load_world_zones()
}
std::copy( tmp.begin(), tmp.end(), std::back_inserter( zones ) );
} );
if( !loaded ) {
//validate cache
remove_file( cache );
copy_file( savefile, cache );
}
}

void zone_manager::zone_edited( zone_data &zone )
Expand Down
1 change: 0 additions & 1 deletion src/clzones.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ class zone_manager
const faction_id &fac = your_fac ) const;
std::unordered_set<tripoint_abs_ms> get_vzone_set( const zone_type_id &type,
const faction_id &fac = your_fac ) const;
bool loaded = false; // NOLINT(cata-serialize)
public:
zone_manager();
~zone_manager() = default;
Expand Down
11 changes: 0 additions & 11 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2880,16 +2880,6 @@ bool game::save_player_data()
;
}

bool game::save_zones()
{
return ( !file_exist( PATH_INFO::world_base_save_path() + "/zones_cache.json" ) ||
copy_file( PATH_INFO::world_base_save_path() + "/zones_cache.json",
PATH_INFO::world_base_save_path() + "/zones.json" ) ) &&
( !file_exist( PATH_INFO::player_base_save_path() + ".zones_cache.json" ) ||
copy_file( PATH_INFO::player_base_save_path() + ".zones_cache.json",
PATH_INFO::player_base_save_path() + ".zones.json" ) );
}

event_bus &game::events()
{
return *event_bus_ptr;
Expand Down Expand Up @@ -2929,7 +2919,6 @@ bool game::save()
!get_auto_pickup().save_character() ||
!get_auto_notes_settings().save() ||
!get_safemode().save_character() ||
!save_zones() ||
!write_to_file( PATH_INFO::world_base_save_path() + "/uistate.json", [&]( std::ostream & fout ) {
JsonOut jsout( fout );
uistate.serialize( jsout );
Expand Down
1 change: 0 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,6 @@ class game
void serialize_master( std::ostream &fout );
// returns false if saving failed for whatever reason
bool save_maps();
bool save_zones();
#if defined(__ANDROID__)
void save_shortcuts( std::ostream &fout );
#endif
Expand Down

0 comments on commit dfa2bfa

Please sign in to comment.