Skip to content

Commit

Permalink
refactor: Remember save format in the WORLDINFO struct
Browse files Browse the repository at this point in the history
Signed-off-by: David Li <jiawei.davidli@gmail.com>
  • Loading branch information
randombk committed Dec 1, 2024
1 parent 9557cd3 commit 344b84e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
42 changes: 42 additions & 0 deletions src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ WORLDINFO::WORLDINFO()
{
world_name = world_generator->get_next_valid_worldname();
WORLD_OPTIONS = get_options().get_world_defaults();
save_format = save_format::V1;

world_saves.clear();
active_mod_order = world_generator->get_mod_manager().get_default_mods();
Expand All @@ -56,6 +57,7 @@ void WORLDINFO::COPY_WORLD( const WORLDINFO *world_to_copy )
{
world_name = world_to_copy->world_name + "_copy";
WORLD_OPTIONS = world_to_copy->WORLD_OPTIONS;
save_format = world_to_copy->save_format;
active_mod_order = world_to_copy->active_mod_order;
}

Expand Down Expand Up @@ -133,6 +135,46 @@ bool WORLDINFO::load_options()
}, true );
}

bool WORLDINFO::save( const bool is_conversion ) const
{
if( !assure_dir_exist( folder_path() ) ) {
DebugLog( DL::Error, DC::Main ) << "Unable to create or open world[" << world_name
<< "] directory for saving";
return false;
}

if( !is_conversion ) {
const auto savefile = folder_path() + "/" + PATH_INFO::worldoptions();
const bool saved = write_to_file( savefile, [&]( std::ostream & fout ) {
JsonOut jout( fout );

jout.start_array();

for( auto &elem : WORLD_OPTIONS ) {
// Skip hidden option because it is set by mod and should not be saved
if( !elem.second.getDefaultText().empty() ) {
jout.start_object();

jout.member( "info", elem.second.getTooltip() );
jout.member( "default", elem.second.getDefaultText( false ) );
jout.member( "name", elem.first );
jout.member( "value", elem.second.getValue( true ) );

jout.end_object();
}
}

jout.end_array();
}, _( "world data" ) );
if( !saved ) {
return false;
}
}

world_generator->get_mod_manager().save_mods_list( const_cast<WORLDINFO *>( this ) );
return true;
}

void load_world_option( const JsonObject &jo )
{
auto arr = jo.get_array( "options" );
Expand Down
7 changes: 7 additions & 0 deletions src/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class save_t
save_t &operator=( const save_t & ) = default;
};

enum save_format : int {
/** Original save layout; uncompressed JSON as loose files */
V1 = 0,
};

/**
* Structure containing metadata about a world. No actual world data is processed here.
*
Expand All @@ -54,6 +59,8 @@ struct WORLDINFO {
std::string world_name;
options_manager::options_container WORLD_OPTIONS;
std::vector<save_t> world_saves;
save_format save_format;

Check failure on line 62 in src/world.h

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Curses

declaration of ‘save_format WORLDINFO::save_format’ changes meaning of ‘save_format’ [-fpermissive]

Check failure on line 62 in src/world.h

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Tiles, Sound, Lua, CMake, Languages

declaration of ‘save_format WORLDINFO::save_format’ changes meaning of ‘save_format’ [-fpermissive]

Check failure on line 62 in src/world.h

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Tiles, Sound, Lua, CMake, Languages

declaration of ‘save_format WORLDINFO::save_format’ changes meaning of ‘save_format’ [-fpermissive]

Check failure on line 62 in src/world.h

View workflow job for this annotation

GitHub Actions / GCC 12, Ubuntu, Tiles, Sound, Lua

declaration of ‘save_format WORLDINFO::save_format’ changes meaning of ‘save_format’ [-fpermissive]

/**
* A (possibly empty) list of (idents of) mods that
* should be loaded for this world.
Expand Down
42 changes: 2 additions & 40 deletions src/worldfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,46 +165,6 @@ void worldfactory::set_active_world( WORLDINFO *new_world )
}
}

bool WORLDINFO::save( const bool is_conversion ) const
{
if( !assure_dir_exist( folder_path() ) ) {
DebugLog( DL::Error, DC::Main ) << "Unable to create or open world[" << world_name
<< "] directory for saving";
return false;
}

if( !is_conversion ) {
const auto savefile = folder_path() + "/" + PATH_INFO::worldoptions();
const bool saved = write_to_file( savefile, [&]( std::ostream & fout ) {
JsonOut jout( fout );

jout.start_array();

for( auto &elem : WORLD_OPTIONS ) {
// Skip hidden option because it is set by mod and should not be saved
if( !elem.second.getDefaultText().empty() ) {
jout.start_object();

jout.member( "info", elem.second.getTooltip() );
jout.member( "default", elem.second.getDefaultText( false ) );
jout.member( "name", elem.first );
jout.member( "value", elem.second.getValue( true ) );

jout.end_object();
}
}

jout.end_array();
}, _( "world data" ) );
if( !saved ) {
return false;
}
}

world_generator->get_mod_manager().save_mods_list( const_cast<WORLDINFO *>( this ) );
return true;
}

void worldfactory::init()
{
load_last_world_info();
Expand Down Expand Up @@ -236,6 +196,8 @@ void worldfactory::init()
all_worlds[worldname] = std::make_unique<WORLDINFO>();
// give the world a name
all_worlds[worldname]->world_name = worldname;
// Record the world save format. Only one exists at this time.
all_worlds[worldname]->save_format = save_format::V1;
// add sav files
for( auto &world_sav_file : world_sav_files ) {
all_worlds[worldname]->world_saves.push_back( save_t::from_base_path( world_sav_file ) );
Expand Down

0 comments on commit 344b84e

Please sign in to comment.