Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate WORLD::world_path into a function #23416

Merged
merged 2 commits into from Apr 13, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

Encapsulate WORLD::world_path into a function

Saves us from updating it whenever the name of the world changes.
  • Loading branch information...
BevapDin committed Apr 7, 2018
commit 43804bf996324a2d3ec183532d1847ee23ba4490
@@ -13760,5 +13760,5 @@ std::string game::get_player_base_save_path() const

std::string game::get_world_base_save_path() const
{
return world_generator->active_world->world_path;
return world_generator->active_world->path();

This comment has been minimized.

Copy link
@codemime

codemime Apr 8, 2018

Member

It would be nice to rename it to something like folder_path(). Just path has a too generic meaning.

}
@@ -383,7 +383,7 @@ void mod_manager::load_mod_info( std::string info_file_path )

std::string mod_manager::get_mods_list_file( const WORLDPTR world )
{
return world->world_path + "/mods.json";
return world->path() + "/mods.json";
}

void mod_manager::save_mods_list( WORLDPTR world ) const
@@ -63,15 +63,17 @@ std::string get_next_valid_worldname()
WORLD::WORLD()
{
world_name = get_next_valid_worldname();
std::ostringstream path;
path << FILENAMES["savedir"] << utf8_to_native( world_name );
world_path = path.str();
WORLD_OPTIONS = get_options().get_world_defaults();

world_saves.clear();
active_mod_order = world_generator->get_mod_manager().get_default_mods();
}

std::string WORLD::path() const
{
return FILENAMES["savedir"] + utf8_to_native( world_name );
}

bool WORLD::save_exists( const save_t &name ) const
{
return std::find( world_saves.begin(), world_saves.end(), name ) != world_saves.end();
@@ -108,10 +110,6 @@ WORLDPTR worldfactory::add_world( WORLDPTR retworld )
// add world to world list
all_worlds[ retworld->world_name ] = retworld;

std::ostringstream path;
path << FILENAMES[ "savedir" ] << utf8_to_native( retworld->world_name );
retworld->world_path = path.str();

if( !save_world( retworld ) ) {
std::string worldname = retworld->world_name;
if( all_worlds[ worldname ] != retworld ) {
@@ -206,10 +204,6 @@ WORLDPTR worldfactory::make_new_world(special_game_id special_type)
// add world to world list!
all_worlds[worldname] = special_world;

std::ostringstream path;
path << FILENAMES["savedir"] << utf8_to_native( worldname );
special_world->world_path = path.str();

if (!save_world(special_world)) {
delete all_worlds[worldname];
delete special_world;
@@ -231,17 +225,13 @@ WORLDPTR worldfactory::convert_to_world(std::string origin_path)
WORLDPTR newworld = new WORLD();
newworld->world_name = worldname;

std::ostringstream path;
path << FILENAMES["savedir"] << utf8_to_native( worldname );
newworld->world_path = path.str();

// save world as conversion world
if (save_world(newworld, true)) {
// move files from origin_path into new world path
for( auto &origin_file : get_files_from_path(".", origin_path, false) ) {
std::string filename = origin_file.substr( origin_file.find_last_of( "/\\" ) );

rename( origin_file.c_str(), std::string( newworld->world_path + filename ).c_str() );
rename( origin_file.c_str(), std::string( newworld->path() + filename ).c_str() );
}

DebugLog( D_INFO, DC_ALL ) << "worldfactory::convert_to_world -- World Converted Successfully!";
@@ -269,14 +259,14 @@ bool worldfactory::save_world(WORLDPTR world, bool is_conversion)
return false;
}

if (!assure_dir_exist(world->world_path)) {
if (!assure_dir_exist(world->path())) {
DebugLog( D_ERROR, DC_ALL ) << "Unable to create or open world[" << world->world_name <<
"] directory for saving";
return false;
}

if (!is_conversion) {
const auto savefile = world->world_path + "/" + FILENAMES["worldoptions"];
const auto savefile = world->path() + "/" + FILENAMES["worldoptions"];
const bool saved = write_to_file( savefile, [&]( std::ostream &fout ) {
JsonOut jout( fout );

@@ -343,8 +333,6 @@ void worldfactory::init()
for( auto &world_sav_file : world_sav_files ) {
all_worlds[worldname]->world_saves.push_back( save_t::from_base_path( world_sav_file ) );
}
// set world path
all_worlds[worldname]->world_path = world_dir;
mman->load_mods_list(all_worlds[worldname]);

// load options into the world
@@ -358,7 +346,7 @@ void worldfactory::init()
// check to see if there exists a worldname "save" which denotes that a world exists in the save
// directory and not in a sub-world directory
if( has_world( "save" ) ) {
WORLDPTR converted_world = convert_to_world(all_worlds["save"]->world_path);
WORLDPTR converted_world = convert_to_world(all_worlds["save"]->path());
if (converted_world) {
converted_world->world_saves = all_worlds["save"]->world_saves;
converted_world->WORLD_OPTIONS = all_worlds["save"]->WORLD_OPTIONS;
@@ -1396,12 +1384,12 @@ bool worldfactory::load_world_options(WORLDPTR &world)
world->WORLD_OPTIONS = get_options().get_world_defaults();

using namespace std::placeholders;
const auto path = world->world_path + "/" + FILENAMES["worldoptions"];
const auto path = world->path() + "/" + FILENAMES["worldoptions"];
if( read_from_file_optional_json( path, std::bind( &WORLD::load_options, world, _1 ) ) ) {
return true;
}

const auto legacy_path = world->world_path + "/" + FILENAMES["legacy_worldoptions"];
const auto legacy_path = world->path() + "/" + FILENAMES["legacy_worldoptions"];
if( read_from_file_optional( legacy_path, std::bind( &WORLD::load_legacy_options, world, _1 ) ) ) {
if( save_world( world ) ) {
// Remove old file as the options have been saved to the new file.
@@ -1451,7 +1439,7 @@ static bool isForbidden(std::string candidate)

void worldfactory::delete_world( const std::string &worldname, const bool delete_folder )
{
std::string worldpath = get_world( worldname )->world_path;
std::string worldpath = get_world( worldname )->path();
std::set<std::string> directory_paths;

auto file_paths = get_files_from_path("", worldpath, true, true);
@@ -47,23 +47,25 @@ class save_t
};

struct WORLD {
std::string world_path;
std::string world_name;
options_manager::options_container WORLD_OPTIONS;
std::vector<save_t> world_saves;
/**
* A (possibly empty) list of (idents of) mods that
* should be loaded for this world.
*/
std::vector<mod_id> active_mod_order;

WORLD();

bool save_exists( const save_t &name ) const;
void add_save( const save_t &name );

void load_options( JsonIn &jsin );
void load_legacy_options( std::istream &fin );
public:
std::string path() const;

std::string world_name;
options_manager::options_container WORLD_OPTIONS;
std::vector<save_t> world_saves;
/**
* A (possibly empty) list of (idents of) mods that
* should be loaded for this world.
*/
std::vector<mod_id> active_mod_order;

WORLD();

bool save_exists( const save_t &name ) const;
void add_save( const save_t &name );

void load_options( JsonIn &jsin );
void load_legacy_options( std::istream &fin );
};

class mod_manager;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.