Example #1
0
std::map<std::string, WORLDPTR> worldfactory::get_all_worlds()
{
    std::map<std::string, WORLDPTR> retworlds;

    std::vector<std::string> qualifiers;
    qualifiers.push_back(WORLD_OPTION_FILE);
    qualifiers.push_back(SAVE_MASTER);

    if (!all_worlds.empty()) {
        for( auto &elem : all_worlds ) {
            delete elem.second;
        }
        all_worlds.clear();
        all_worldnames.clear();
    }
    // get the master files. These determine the validity of a world
    // worlds exist by having an option file
    // create worlds
    for( const auto &world_dir : get_directories_with(qualifiers, FILENAMES["savedir"], true) ) {
        // get the option file again
        // we can assume that there is only one master.gsav, so just collect the first path
        bool no_options = true;
        auto const detected_world_op = get_files_from_path( WORLD_OPTION_FILE, world_dir, false );
        if ( ! detected_world_op.empty() ) {
            no_options = false;
        }
        // get the save files
        auto world_sav_files = get_files_from_path( SAVE_EXTENSION, world_dir, false );
        // split the save file names between the directory and the extension
        for( auto &world_sav_file : world_sav_files ) {
            size_t save_index = world_sav_file.find( SAVE_EXTENSION );
            world_sav_file = world_sav_file.substr( world_dir.size() + 1,
                                                    save_index - ( world_dir.size() + 1 ) );
        }
        // the directory name is the name of the world
        std::string worldname;
        unsigned name_index = world_dir.find_last_of( "/\\" );
        worldname = world_dir.substr( name_index + 1 );

        // create and store the world
        retworlds[worldname] = new WORLD();
        // give the world a name
        retworlds[worldname]->world_name = worldname;
        all_worldnames.push_back(worldname);
        // add sav files
        for( auto &world_sav_file : world_sav_files ) {
            retworlds[worldname]->world_saves.push_back( world_sav_file );
        }
        // set world path
        retworlds[worldname]->world_path = world_dir;
        mman->load_mods_list(retworlds[worldname]);

        // load options into the world
        if ( no_options ) {
            for( auto &elem : OPTIONS ) {
                if( elem.second.getPage() == "world_default" ) {
                    retworlds[worldname]->world_options[elem.first] = elem.second;
                }
            }
            retworlds[worldname]->world_options["DELETE_WORLD"].setValue("yes");
            save_world(retworlds[worldname]);
        } else {
            retworlds[worldname]->world_options = get_world_options(detected_world_op[0]);
        }
    }

    // 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 (retworlds.find("save") != retworlds.end()) {
        WORLDPTR converted_world = convert_to_world(retworlds["save"]->world_path);
        if (converted_world) {
            converted_world->world_saves = retworlds["save"]->world_saves;
            converted_world->world_options = retworlds["save"]->world_options;

            std::vector<std::string>::iterator oldindex = std::find(all_worldnames.begin(),
                    all_worldnames.end(), "save");

            delete retworlds["save"];
            retworlds.erase("save");
            all_worldnames.erase(oldindex);

            retworlds[converted_world->world_name] = converted_world;
            all_worldnames.push_back(converted_world->world_name);
        }
    }
    all_worlds = retworlds;
    return retworlds;
}
Example #2
0
std::map<std::string, WORLDPTR> worldfactory::get_all_worlds()
{
    std::map<std::string, WORLDPTR> retworlds;

    std::vector<std::string> qualifiers;
    qualifiers.push_back(WORLD_OPTION_FILE);
    qualifiers.push_back(SAVE_MASTER);

    if (all_worlds.size() > 0) {
        for (std::map<std::string, WORLDPTR>::iterator it = all_worlds.begin(); it != all_worlds.end(); ++it) {
            delete it->second;
        }
        all_worlds.clear();
        all_worldnames.clear();
    }
    // get the master files. These determine the validity of a world
    std::vector<std::string> world_dirs = file_finder::get_directories_with(qualifiers, SAVE_DIR, true);

    // check to see if there are >0 world directories found
    if (world_dirs.size() > 0) {
        // worlds exist by having an option file
        // create worlds
        for (unsigned i = 0; i < world_dirs.size(); ++i) {
            // get the option file again
            // we can assume that there is only one master.gsav, so just collect the first path
            bool no_options = true;
            std::vector<std::string> detected_world_op = file_finder::get_files_from_path(WORLD_OPTION_FILE, world_dirs[i], false);
            std::string world_op_file = WORLD_OPTION_FILE;
            if ( ! detected_world_op.empty() ) {
                no_options = false;
            }
            // get the save files
            std::vector<std::string> world_sav_files = file_finder::get_files_from_path(SAVE_EXTENSION, world_dirs[i], false);
            // split the save file names between the directory and the extension
            for (unsigned j = 0; j < world_sav_files.size(); ++j) {
                size_t save_index = world_sav_files[j].find(SAVE_EXTENSION);
                world_sav_files[j] = world_sav_files[j].substr(world_dirs[i].size() + 1, save_index - (world_dirs[i].size() + 1));
            }
            // the directory name is the name of the world
            std::string worldname;
            unsigned name_index = world_dirs[i].find_last_of("/\\");
            worldname = world_dirs[i].substr(name_index + 1);

            // create and store the world
            retworlds[worldname] = new WORLD();
            // give the world a name
            retworlds[worldname]->world_name = worldname;
            all_worldnames.push_back(worldname);
            // add sav files
            for (unsigned j = 0; j < world_sav_files.size(); ++j) {
                retworlds[worldname]->world_saves.push_back(world_sav_files[j]);
            }
            // set world path
            retworlds[worldname]->world_path = world_dirs[i];

            // load options into the world
            if ( no_options ) {
                for (std::map<std::string, cOpt>::iterator it = OPTIONS.begin(); it != OPTIONS.end(); ++it) {
                    if (it->second.getPage() == "world_default") {
                        retworlds[worldname]->world_options[it->first] = it->second;
                    }
                }
                retworlds[worldname]->world_options["DELETE_WORLD"].setValue("yes");
                save_world(retworlds[worldname]);
            } else {
                retworlds[worldname]->world_options = get_world_options(detected_world_op[0]);
            }
        }
    }
    // 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 (retworlds.find("save") != retworlds.end()) {
        WORLDPTR converted_world = convert_to_world(retworlds["save"]->world_path);
        if (converted_world) {
            converted_world->world_saves = retworlds["save"]->world_saves;
            converted_world->world_options = retworlds["save"]->world_options;

            std::vector<std::string>::iterator oldindex = std::find(all_worldnames.begin(), all_worldnames.end(), "save");

            delete retworlds["save"];
            retworlds.erase("save");
            all_worldnames.erase(oldindex);

            retworlds[converted_world->world_name] = converted_world;
            all_worldnames.push_back(converted_world->world_name);
        }
    }
    all_worlds = retworlds;
    return retworlds;
}