2012-03-11 08:54:23 -04:00
|
|
|
/*
|
2013-02-24 12:40:43 -05:00
|
|
|
Minetest
|
2013-02-24 13:38:45 -05:00
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-03-11 08:54:23 -04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 10:56:56 -04:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2012-03-11 08:54:23 -04:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-05 10:56:56 -04:00
|
|
|
GNU Lesser General Public License for more details.
|
2012-03-11 08:54:23 -04:00
|
|
|
|
2012-06-05 10:56:56 -04:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2012-03-11 08:54:23 -04:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "subgame.h"
|
|
|
|
#include "porting.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "settings.h"
|
2012-03-11 14:45:14 -04:00
|
|
|
#include "log.h"
|
2014-09-06 13:21:46 -04:00
|
|
|
#include "strfnd.h"
|
2013-02-15 14:13:53 -05:00
|
|
|
#ifndef SERVER
|
|
|
|
#include "tile.h" // getImagePath
|
|
|
|
#endif
|
2012-06-16 18:29:13 -04:00
|
|
|
#include "util/string.h"
|
2012-03-11 08:54:23 -04:00
|
|
|
|
2013-03-21 15:42:23 -04:00
|
|
|
bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
|
|
|
|
{
|
|
|
|
std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
|
|
|
|
return conf.readConfigFile(conf_path.c_str());
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:48:21 -04:00
|
|
|
bool getGameConfig(const std::string &game_path, Settings &conf)
|
2012-03-26 16:02:12 -04:00
|
|
|
{
|
|
|
|
std::string conf_path = game_path + DIR_DELIM + "game.conf";
|
2013-03-21 12:48:21 -04:00
|
|
|
return conf.readConfigFile(conf_path.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getGameName(const std::string &game_path)
|
|
|
|
{
|
2012-03-26 16:02:12 -04:00
|
|
|
Settings conf;
|
2013-03-21 12:48:21 -04:00
|
|
|
if(!getGameConfig(game_path, conf))
|
2012-03-26 16:02:12 -04:00
|
|
|
return "";
|
|
|
|
if(!conf.exists("name"))
|
|
|
|
return "";
|
|
|
|
return conf.get("name");
|
|
|
|
}
|
|
|
|
|
2012-03-26 16:16:02 -04:00
|
|
|
struct GameFindPath
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
bool user_specific;
|
|
|
|
GameFindPath(const std::string &path, bool user_specific):
|
|
|
|
path(path),
|
|
|
|
user_specific(user_specific)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2014-09-06 13:21:46 -04:00
|
|
|
Strfnd getSubgamePathEnv() {
|
2014-10-28 15:13:14 -04:00
|
|
|
std::string sp;
|
|
|
|
char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2014-10-28 15:13:14 -04:00
|
|
|
if(subgame_path) {
|
|
|
|
sp = std::string(subgame_path);
|
|
|
|
}
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2014-10-28 15:13:14 -04:00
|
|
|
return Strfnd(sp);
|
2014-09-06 13:21:46 -04:00
|
|
|
}
|
|
|
|
|
2012-03-11 08:54:23 -04:00
|
|
|
SubgameSpec findSubgame(const std::string &id)
|
|
|
|
{
|
|
|
|
if(id == "")
|
|
|
|
return SubgameSpec();
|
2012-03-19 14:44:07 -04:00
|
|
|
std::string share = porting::path_share;
|
|
|
|
std::string user = porting::path_user;
|
2012-03-26 16:16:02 -04:00
|
|
|
std::vector<GameFindPath> find_paths;
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2014-10-28 15:13:14 -04:00
|
|
|
Strfnd search_paths = getSubgamePathEnv();
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2014-10-28 15:13:14 -04:00
|
|
|
while(!search_paths.atend()) {
|
|
|
|
std::string path = search_paths.next(":");
|
|
|
|
find_paths.push_back(GameFindPath(
|
|
|
|
path + DIR_DELIM + id, false));
|
|
|
|
find_paths.push_back(GameFindPath(
|
|
|
|
path + DIR_DELIM + id + "_game", false));
|
|
|
|
}
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2012-03-26 16:16:02 -04:00
|
|
|
find_paths.push_back(GameFindPath(
|
|
|
|
user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
|
|
|
|
find_paths.push_back(GameFindPath(
|
2012-03-26 16:17:29 -04:00
|
|
|
user + DIR_DELIM + "games" + DIR_DELIM + id, true));
|
2012-03-26 16:16:02 -04:00
|
|
|
find_paths.push_back(GameFindPath(
|
|
|
|
share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
|
2012-03-26 16:17:29 -04:00
|
|
|
find_paths.push_back(GameFindPath(
|
|
|
|
share + DIR_DELIM + "games" + DIR_DELIM + id, false));
|
2012-03-11 08:54:23 -04:00
|
|
|
// Find game directory
|
2012-03-26 16:16:02 -04:00
|
|
|
std::string game_path;
|
2012-03-11 08:54:23 -04:00
|
|
|
bool user_game = true; // Game is in user's directory
|
2012-03-26 16:16:02 -04:00
|
|
|
for(u32 i=0; i<find_paths.size(); i++){
|
|
|
|
const std::string &try_path = find_paths[i].path;
|
|
|
|
if(fs::PathExists(try_path)){
|
|
|
|
game_path = try_path;
|
|
|
|
user_game = find_paths[i].user_specific;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-11 08:54:23 -04:00
|
|
|
}
|
2012-03-26 16:16:02 -04:00
|
|
|
if(game_path == "")
|
2012-03-11 08:54:23 -04:00
|
|
|
return SubgameSpec();
|
2012-12-08 12:10:54 -05:00
|
|
|
std::string gamemod_path = game_path + DIR_DELIM + "mods";
|
2012-03-19 19:06:44 -04:00
|
|
|
// Find mod directories
|
|
|
|
std::set<std::string> mods_paths;
|
2012-03-11 08:54:23 -04:00
|
|
|
if(!user_game)
|
2013-05-18 11:00:47 -04:00
|
|
|
mods_paths.insert(share + DIR_DELIM + "mods");
|
2012-03-19 19:06:44 -04:00
|
|
|
if(user != share || user_game)
|
2013-05-18 11:00:47 -04:00
|
|
|
mods_paths.insert(user + DIR_DELIM + "mods");
|
2012-03-26 16:02:12 -04:00
|
|
|
std::string game_name = getGameName(game_path);
|
|
|
|
if(game_name == "")
|
|
|
|
game_name = id;
|
2013-02-15 14:13:53 -05:00
|
|
|
std::string menuicon_path;
|
|
|
|
#ifndef SERVER
|
2013-05-02 17:08:59 -04:00
|
|
|
menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
|
2013-02-15 14:13:53 -05:00
|
|
|
#endif
|
|
|
|
return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
|
2013-05-04 03:03:56 -04:00
|
|
|
menuicon_path);
|
2012-03-11 08:54:23 -04:00
|
|
|
}
|
|
|
|
|
2012-04-08 16:15:50 -04:00
|
|
|
SubgameSpec findWorldSubgame(const std::string &world_path)
|
|
|
|
{
|
|
|
|
std::string world_gameid = getWorldGameId(world_path, true);
|
|
|
|
// See if world contains an embedded game; if so, use it.
|
|
|
|
std::string world_gamepath = world_path + DIR_DELIM + "game";
|
|
|
|
if(fs::PathExists(world_gamepath)){
|
|
|
|
SubgameSpec gamespec;
|
|
|
|
gamespec.id = world_gameid;
|
|
|
|
gamespec.path = world_gamepath;
|
2012-12-08 12:10:54 -05:00
|
|
|
gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
|
2012-04-08 16:15:50 -04:00
|
|
|
gamespec.name = getGameName(world_gamepath);
|
|
|
|
if(gamespec.name == "")
|
|
|
|
gamespec.name = "unknown";
|
|
|
|
return gamespec;
|
|
|
|
}
|
|
|
|
return findSubgame(world_gameid);
|
|
|
|
}
|
|
|
|
|
2012-03-11 08:54:23 -04:00
|
|
|
std::set<std::string> getAvailableGameIds()
|
|
|
|
{
|
|
|
|
std::set<std::string> gameids;
|
|
|
|
std::set<std::string> gamespaths;
|
2012-03-19 14:44:07 -04:00
|
|
|
gamespaths.insert(porting::path_share + DIR_DELIM + "games");
|
|
|
|
gamespaths.insert(porting::path_user + DIR_DELIM + "games");
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2014-10-28 15:13:14 -04:00
|
|
|
Strfnd search_paths = getSubgamePathEnv();
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2014-10-28 15:13:14 -04:00
|
|
|
while(!search_paths.atend()) {
|
|
|
|
gamespaths.insert(search_paths.next(":"));
|
|
|
|
}
|
2014-09-06 13:21:46 -04:00
|
|
|
|
2012-03-11 08:54:23 -04:00
|
|
|
for(std::set<std::string>::const_iterator i = gamespaths.begin();
|
|
|
|
i != gamespaths.end(); i++){
|
|
|
|
std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
|
|
|
|
for(u32 j=0; j<dirlist.size(); j++){
|
|
|
|
if(!dirlist[j].dir)
|
|
|
|
continue;
|
2013-03-21 12:48:21 -04:00
|
|
|
// If configuration file is not found or broken, ignore game
|
|
|
|
Settings conf;
|
|
|
|
if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
|
|
|
|
continue;
|
|
|
|
// Add it to result
|
2012-03-26 16:16:02 -04:00
|
|
|
const char *ends[] = {"_game", NULL};
|
|
|
|
std::string shorter = removeStringEnd(dirlist[j].name, ends);
|
|
|
|
if(shorter != "")
|
|
|
|
gameids.insert(shorter);
|
|
|
|
else
|
|
|
|
gameids.insert(dirlist[j].name);
|
2012-03-11 08:54:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return gameids;
|
|
|
|
}
|
|
|
|
|
2012-03-12 18:06:37 -04:00
|
|
|
std::vector<SubgameSpec> getAvailableGames()
|
|
|
|
{
|
|
|
|
std::vector<SubgameSpec> specs;
|
|
|
|
std::set<std::string> gameids = getAvailableGameIds();
|
|
|
|
for(std::set<std::string>::const_iterator i = gameids.begin();
|
|
|
|
i != gameids.end(); i++)
|
|
|
|
specs.push_back(findSubgame(*i));
|
|
|
|
return specs;
|
|
|
|
}
|
|
|
|
|
2012-03-25 13:29:56 -04:00
|
|
|
#define LEGACY_GAMEID "minetest"
|
|
|
|
|
|
|
|
bool getWorldExists(const std::string &world_path)
|
|
|
|
{
|
|
|
|
return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
|
|
|
|
fs::PathExists(world_path + DIR_DELIM + "world.mt"));
|
|
|
|
}
|
2012-03-11 14:45:14 -04:00
|
|
|
|
|
|
|
std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
|
2012-03-11 08:54:23 -04:00
|
|
|
{
|
|
|
|
std::string conf_path = world_path + DIR_DELIM + "world.mt";
|
|
|
|
Settings conf;
|
|
|
|
bool succeeded = conf.readConfigFile(conf_path.c_str());
|
2012-03-11 14:45:14 -04:00
|
|
|
if(!succeeded){
|
|
|
|
if(can_be_legacy){
|
|
|
|
// If map_meta.txt exists, it is probably an old minetest world
|
|
|
|
if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
|
|
|
|
return LEGACY_GAMEID;
|
|
|
|
}
|
2012-03-11 08:54:23 -04:00
|
|
|
return "";
|
2012-03-11 14:45:14 -04:00
|
|
|
}
|
2012-03-11 08:54:23 -04:00
|
|
|
if(!conf.exists("gameid"))
|
|
|
|
return "";
|
2012-03-25 13:29:56 -04:00
|
|
|
// The "mesetint" gameid has been discarded
|
|
|
|
if(conf.get("gameid") == "mesetint")
|
|
|
|
return "minetest";
|
2012-03-11 08:54:23 -04:00
|
|
|
return conf.get("gameid");
|
|
|
|
}
|
|
|
|
|
2012-03-11 14:45:14 -04:00
|
|
|
std::vector<WorldSpec> getAvailableWorlds()
|
|
|
|
{
|
|
|
|
std::vector<WorldSpec> worlds;
|
|
|
|
std::set<std::string> worldspaths;
|
2012-03-19 14:44:07 -04:00
|
|
|
worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
|
2012-03-11 14:45:14 -04:00
|
|
|
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;
|
2012-03-25 13:29:56 -04:00
|
|
|
// Just allow filling in the gameid always for now
|
|
|
|
bool can_be_legacy = true;
|
|
|
|
std::string gameid = getWorldGameId(fullpath, can_be_legacy);
|
2012-03-11 14:45:14 -04:00
|
|
|
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{
|
2012-03-18 11:17:05 -04:00
|
|
|
std::string fullpath = porting::path_user + DIR_DELIM + "world";
|
2012-03-11 14:45:14 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-12 18:06:37 -04:00
|
|
|
bool initializeWorld(const std::string &path, const std::string &gameid)
|
|
|
|
{
|
|
|
|
infostream<<"Initializing world at "<<path<<std::endl;
|
|
|
|
// Create world.mt if does not already exist
|
|
|
|
std::string worldmt_path = path + DIR_DELIM + "world.mt";
|
|
|
|
if(!fs::PathExists(worldmt_path)){
|
|
|
|
infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
|
|
|
|
fs::CreateAllDirs(path);
|
2013-08-13 13:15:06 -04:00
|
|
|
std::ostringstream ss(std::ios_base::binary);
|
2014-07-07 16:57:11 -04:00
|
|
|
ss<<"gameid = "<<gameid<< "\nbackend = sqlite3\n";
|
2013-08-13 13:15:06 -04:00
|
|
|
fs::safeWriteToFile(worldmt_path, ss.str());
|
2012-03-12 18:06:37 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|