+std::string getWorldPathEnv()
+{
+ char *world_path = getenv("MINETEST_WORLD_PATH");
+ return world_path ? std::string(world_path) : "";
+}
+
+std::vector<WorldSpec> getAvailableWorlds()
+{
+ std::vector<WorldSpec> worlds;
+ std::set<std::string> worldspaths;
+
+ Strfnd search_paths(getWorldPathEnv());
+
+ while (!search_paths.at_end())
+ worldspaths.insert(search_paths.next(PATH_DELIM));
+
+ worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
+ infostream << "Searching worlds..." << std::endl;
+ for (std::set<std::string>::const_iterator i = worldspaths.begin();
+ i != worldspaths.end(); ++i) {
+ infostream << " In " << (*i) << ": " <<std::endl;
+ std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
+ for(u32 j=0; j<dirvector.size(); j++){
+ if(!dirvector[j].dir)
+ continue;
+ std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
+ std::string name = dirvector[j].name;
+ // Just allow filling in the gameid always for now
+ bool can_be_legacy = true;
+ std::string gameid = getWorldGameId(fullpath, can_be_legacy);
+ WorldSpec spec(fullpath, name, gameid);
+ if(!spec.isValid()){
+ infostream<<"(invalid: "<<name<<") ";
+ } else {
+ infostream<<name<<" ";
+ worlds.push_back(spec);
+ }
+ }
+ infostream<<std::endl;
+ }
+ // Check old world location
+ do{
+ std::string fullpath = porting::path_user + DIR_DELIM + "world";
+ if(!fs::PathExists(fullpath))
+ break;
+ std::string name = "Old World";
+ std::string gameid = getWorldGameId(fullpath, true);
+ WorldSpec spec(fullpath, name, gameid);
+ infostream<<"Old world found."<<std::endl;
+ worlds.push_back(spec);
+ }while(0);
+ infostream<<worlds.size()<<" found."<<std::endl;
+ return worlds;
+}
+
+bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
+{
+ // Override defaults with those provided by the game.
+ // We clear and reload the defaults because the defaults
+ // might have been overridden by other subgame config
+ // files that were loaded before.
+ g_settings->clearDefaults();
+ set_default_settings(g_settings);
+ Settings game_defaults;
+ getGameMinetestConfig(gamespec.path, game_defaults);
+ override_default_settings(g_settings, &game_defaults);
+
+ infostream << "Initializing world at " << path << std::endl;
+
+ fs::CreateAllDirs(path);
+
+ // Create world.mt if does not already exist
+ std::string worldmt_path = path + DIR_DELIM "world.mt";
+ if (!fs::PathExists(worldmt_path)) {
+ std::ostringstream ss(std::ios_base::binary);
+ ss << "gameid = " << gamespec.id
+ << "\nbackend = sqlite3"
+ << "\ncreative_mode = " << g_settings->get("creative_mode")
+ << "\nenable_damage = " << g_settings->get("enable_damage")
+ << "\n";
+ if (!fs::safeWriteToFile(worldmt_path, ss.str()))
+ return false;
+
+ infostream << "Wrote world.mt (" << worldmt_path << ")" << std::endl;
+ }
+
+ // Create map_meta.txt if does not already exist
+ std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
+ if (!fs::PathExists(map_meta_path)){
+ verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
+ fs::CreateAllDirs(path);
+ std::ostringstream oss(std::ios_base::binary);
+
+ Settings conf;
+ MapgenParams params;
+
+ params.readParams(g_settings);
+ params.writeParams(&conf);
+ conf.writeLines(oss);
+ oss << "[end_of_params]\n";
+
+ fs::safeWriteToFile(map_meta_path, oss.str());
+ }
+ return true;
+}
+