subgame.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "subgame.h"
  17. #include "porting.h"
  18. #include "filesys.h"
  19. #include "settings.h"
  20. #include "log.h"
  21. #include "util/strfnd.h"
  22. #include "defaultsettings.h" // for override_default_settings
  23. #include "mapgen.h" // for MapgenParams
  24. #include "util/string.h"
  25. #ifndef SERVER
  26. #include "client/tile.h" // getImagePath
  27. #endif
  28. bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
  29. {
  30. std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
  31. return conf.readConfigFile(conf_path.c_str());
  32. }
  33. bool getGameConfig(const std::string &game_path, Settings &conf)
  34. {
  35. std::string conf_path = game_path + DIR_DELIM + "game.conf";
  36. return conf.readConfigFile(conf_path.c_str());
  37. }
  38. std::string getGameName(const std::string &game_path)
  39. {
  40. Settings conf;
  41. if(!getGameConfig(game_path, conf))
  42. return "";
  43. if(!conf.exists("name"))
  44. return "";
  45. return conf.get("name");
  46. }
  47. struct GameFindPath
  48. {
  49. std::string path;
  50. bool user_specific;
  51. GameFindPath(const std::string &path, bool user_specific):
  52. path(path),
  53. user_specific(user_specific)
  54. {}
  55. };
  56. std::string getSubgamePathEnv()
  57. {
  58. char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
  59. return subgame_path ? std::string(subgame_path) : "";
  60. }
  61. SubgameSpec findSubgame(const std::string &id)
  62. {
  63. if (id.empty())
  64. return SubgameSpec();
  65. std::string share = porting::path_share;
  66. std::string user = porting::path_user;
  67. std::vector<GameFindPath> find_paths;
  68. Strfnd search_paths(getSubgamePathEnv());
  69. while (!search_paths.at_end()) {
  70. std::string path = search_paths.next(PATH_DELIM);
  71. find_paths.emplace_back(path + DIR_DELIM + id, false);
  72. find_paths.emplace_back(path + DIR_DELIM + id + "_game", false);
  73. }
  74. find_paths.emplace_back(user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true);
  75. find_paths.emplace_back(user + DIR_DELIM + "games" + DIR_DELIM + id, true);
  76. find_paths.emplace_back(share + DIR_DELIM + "games" + DIR_DELIM + id + "_game",
  77. false);
  78. find_paths.emplace_back(share + DIR_DELIM + "games" + DIR_DELIM + id, false);
  79. // Find game directory
  80. std::string game_path;
  81. bool user_game = true; // Game is in user's directory
  82. for (const GameFindPath &find_path : find_paths) {
  83. const std::string &try_path = find_path.path;
  84. if (fs::PathExists(try_path)) {
  85. game_path = try_path;
  86. user_game = find_path.user_specific;
  87. break;
  88. }
  89. }
  90. if (game_path.empty())
  91. return SubgameSpec();
  92. std::string gamemod_path = game_path + DIR_DELIM + "mods";
  93. // Find mod directories
  94. std::set<std::string> mods_paths;
  95. if(!user_game)
  96. mods_paths.insert(share + DIR_DELIM + "mods");
  97. if(user != share || user_game)
  98. mods_paths.insert(user + DIR_DELIM + "mods");
  99. std::string game_name = getGameName(game_path);
  100. if (game_name.empty())
  101. game_name = id;
  102. std::string menuicon_path;
  103. #ifndef SERVER
  104. menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
  105. #endif
  106. return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
  107. menuicon_path);
  108. }
  109. SubgameSpec findWorldSubgame(const std::string &world_path)
  110. {
  111. std::string world_gameid = getWorldGameId(world_path, true);
  112. // See if world contains an embedded game; if so, use it.
  113. std::string world_gamepath = world_path + DIR_DELIM + "game";
  114. if(fs::PathExists(world_gamepath)){
  115. SubgameSpec gamespec;
  116. gamespec.id = world_gameid;
  117. gamespec.path = world_gamepath;
  118. gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
  119. gamespec.name = getGameName(world_gamepath);
  120. if (gamespec.name.empty())
  121. gamespec.name = "unknown";
  122. return gamespec;
  123. }
  124. return findSubgame(world_gameid);
  125. }
  126. std::set<std::string> getAvailableGameIds()
  127. {
  128. std::set<std::string> gameids;
  129. std::set<std::string> gamespaths;
  130. gamespaths.insert(porting::path_share + DIR_DELIM + "games");
  131. gamespaths.insert(porting::path_user + DIR_DELIM + "games");
  132. Strfnd search_paths(getSubgamePathEnv());
  133. while (!search_paths.at_end())
  134. gamespaths.insert(search_paths.next(PATH_DELIM));
  135. for (const std::string &gamespath : gamespaths) {
  136. std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
  137. for (const fs::DirListNode &dln : dirlist) {
  138. if(!dln.dir)
  139. continue;
  140. // If configuration file is not found or broken, ignore game
  141. Settings conf;
  142. if(!getGameConfig(gamespath + DIR_DELIM + dln.name, conf))
  143. continue;
  144. // Add it to result
  145. const char *ends[] = {"_game", NULL};
  146. std::string shorter = removeStringEnd(dln.name, ends);
  147. if (!shorter.empty())
  148. gameids.insert(shorter);
  149. else
  150. gameids.insert(dln.name);
  151. }
  152. }
  153. return gameids;
  154. }
  155. std::vector<SubgameSpec> getAvailableGames()
  156. {
  157. std::vector<SubgameSpec> specs;
  158. std::set<std::string> gameids = getAvailableGameIds();
  159. for (const auto &gameid : gameids)
  160. specs.push_back(findSubgame(gameid));
  161. return specs;
  162. }
  163. #define LEGACY_GAMEID "minetest"
  164. bool getWorldExists(const std::string &world_path)
  165. {
  166. return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
  167. fs::PathExists(world_path + DIR_DELIM + "world.mt"));
  168. }
  169. std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
  170. {
  171. std::string conf_path = world_path + DIR_DELIM + "world.mt";
  172. Settings conf;
  173. bool succeeded = conf.readConfigFile(conf_path.c_str());
  174. if(!succeeded){
  175. if(can_be_legacy){
  176. // If map_meta.txt exists, it is probably an old minetest world
  177. if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
  178. return LEGACY_GAMEID;
  179. }
  180. return "";
  181. }
  182. if(!conf.exists("gameid"))
  183. return "";
  184. // The "mesetint" gameid has been discarded
  185. if(conf.get("gameid") == "mesetint")
  186. return "minetest";
  187. return conf.get("gameid");
  188. }
  189. std::string getWorldPathEnv()
  190. {
  191. char *world_path = getenv("MINETEST_WORLD_PATH");
  192. return world_path ? std::string(world_path) : "";
  193. }
  194. std::vector<WorldSpec> getAvailableWorlds()
  195. {
  196. std::vector<WorldSpec> worlds;
  197. std::set<std::string> worldspaths;
  198. Strfnd search_paths(getWorldPathEnv());
  199. while (!search_paths.at_end())
  200. worldspaths.insert(search_paths.next(PATH_DELIM));
  201. worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
  202. infostream << "Searching worlds..." << std::endl;
  203. for (const std::string &worldspath : worldspaths) {
  204. infostream << " In " << worldspath << ": " <<std::endl;
  205. std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
  206. for (const fs::DirListNode &dln : dirvector) {
  207. if(!dln.dir)
  208. continue;
  209. std::string fullpath = worldspath + DIR_DELIM + dln.name;
  210. std::string name = dln.name;
  211. // Just allow filling in the gameid always for now
  212. bool can_be_legacy = true;
  213. std::string gameid = getWorldGameId(fullpath, can_be_legacy);
  214. WorldSpec spec(fullpath, name, gameid);
  215. if(!spec.isValid()){
  216. infostream<<"(invalid: "<<name<<") ";
  217. } else {
  218. infostream<<name<<" ";
  219. worlds.push_back(spec);
  220. }
  221. }
  222. infostream<<std::endl;
  223. }
  224. // Check old world location
  225. do{
  226. std::string fullpath = porting::path_user + DIR_DELIM + "world";
  227. if(!fs::PathExists(fullpath))
  228. break;
  229. std::string name = "Old World";
  230. std::string gameid = getWorldGameId(fullpath, true);
  231. WorldSpec spec(fullpath, name, gameid);
  232. infostream<<"Old world found."<<std::endl;
  233. worlds.push_back(spec);
  234. }while(false);
  235. infostream<<worlds.size()<<" found."<<std::endl;
  236. return worlds;
  237. }
  238. bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
  239. {
  240. // Override defaults with those provided by the game.
  241. // We clear and reload the defaults because the defaults
  242. // might have been overridden by other subgame config
  243. // files that were loaded before.
  244. g_settings->clearDefaults();
  245. set_default_settings(g_settings);
  246. Settings game_defaults;
  247. getGameMinetestConfig(gamespec.path, game_defaults);
  248. override_default_settings(g_settings, &game_defaults);
  249. infostream << "Initializing world at " << path << std::endl;
  250. fs::CreateAllDirs(path);
  251. // Create world.mt if does not already exist
  252. std::string worldmt_path = path + DIR_DELIM "world.mt";
  253. if (!fs::PathExists(worldmt_path)) {
  254. Settings conf;
  255. conf.set("gameid", gamespec.id);
  256. conf.set("backend", "sqlite3");
  257. conf.set("player_backend", "sqlite3");
  258. conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
  259. conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
  260. if (!conf.updateConfigFile(worldmt_path.c_str()))
  261. return false;
  262. }
  263. // Create map_meta.txt if does not already exist
  264. std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
  265. if (!fs::PathExists(map_meta_path)){
  266. verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
  267. fs::CreateAllDirs(path);
  268. std::ostringstream oss(std::ios_base::binary);
  269. Settings conf;
  270. MapgenParams params;
  271. params.readParams(g_settings);
  272. params.writeParams(&conf);
  273. conf.writeLines(oss);
  274. oss << "[end_of_params]\n";
  275. fs::safeWriteToFile(map_meta_path, oss.str());
  276. }
  277. return true;
  278. }