subgames.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "content/subgames.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/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. struct GameFindPath
  34. {
  35. std::string path;
  36. bool user_specific;
  37. GameFindPath(const std::string &path, bool user_specific) :
  38. path(path), user_specific(user_specific)
  39. {
  40. }
  41. };
  42. std::string getSubgamePathEnv()
  43. {
  44. char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
  45. return subgame_path ? std::string(subgame_path) : "";
  46. }
  47. SubgameSpec findSubgame(const std::string &id)
  48. {
  49. if (id.empty())
  50. return SubgameSpec();
  51. std::string share = porting::path_share;
  52. std::string user = porting::path_user;
  53. // Get games install locations
  54. Strfnd search_paths(getSubgamePathEnv());
  55. // Get all possible paths fo game
  56. std::vector<GameFindPath> find_paths;
  57. while (!search_paths.at_end()) {
  58. std::string path = search_paths.next(PATH_DELIM);
  59. find_paths.emplace_back(path + DIR_DELIM + id, false);
  60. find_paths.emplace_back(path + DIR_DELIM + id + "_game", false);
  61. }
  62. find_paths.emplace_back(
  63. user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true);
  64. find_paths.emplace_back(user + DIR_DELIM + "games" + DIR_DELIM + id, true);
  65. find_paths.emplace_back(
  66. share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false);
  67. find_paths.emplace_back(share + DIR_DELIM + "games" + DIR_DELIM + id, false);
  68. // Find game directory
  69. std::string game_path;
  70. bool user_game = true; // Game is in user's directory
  71. for (const GameFindPath &find_path : find_paths) {
  72. const std::string &try_path = find_path.path;
  73. if (fs::PathExists(try_path)) {
  74. game_path = try_path;
  75. user_game = find_path.user_specific;
  76. break;
  77. }
  78. }
  79. if (game_path.empty())
  80. return SubgameSpec();
  81. std::string gamemod_path = game_path + DIR_DELIM + "mods";
  82. // Find mod directories
  83. std::set<std::string> mods_paths;
  84. if (!user_game)
  85. mods_paths.insert(share + DIR_DELIM + "mods");
  86. if (user != share || user_game)
  87. mods_paths.insert(user + DIR_DELIM + "mods");
  88. // Get meta
  89. std::string conf_path = game_path + DIR_DELIM + "game.conf";
  90. Settings conf;
  91. conf.readConfigFile(conf_path.c_str());
  92. std::string game_name;
  93. if (conf.exists("name"))
  94. game_name = conf.get("name");
  95. else
  96. game_name = id;
  97. std::string game_author;
  98. if (conf.exists("author"))
  99. game_author = conf.get("author");
  100. int game_release = 0;
  101. if (conf.exists("release"))
  102. game_release = conf.getS32("release");
  103. std::string menuicon_path;
  104. #ifndef SERVER
  105. menuicon_path = getImagePath(
  106. game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
  107. #endif
  108. return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
  109. menuicon_path, game_author, game_release);
  110. }
  111. SubgameSpec findWorldSubgame(const std::string &world_path)
  112. {
  113. std::string world_gameid = getWorldGameId(world_path, true);
  114. // See if world contains an embedded game; if so, use it.
  115. std::string world_gamepath = world_path + DIR_DELIM + "game";
  116. if (fs::PathExists(world_gamepath)) {
  117. SubgameSpec gamespec;
  118. gamespec.id = world_gameid;
  119. gamespec.path = world_gamepath;
  120. gamespec.gamemods_path = world_gamepath + DIR_DELIM + "mods";
  121. Settings conf;
  122. std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
  123. conf.readConfigFile(conf_path.c_str());
  124. if (conf.exists("name"))
  125. gamespec.name = conf.get("name");
  126. else
  127. gamespec.name = world_gameid;
  128. return gamespec;
  129. }
  130. return findSubgame(world_gameid);
  131. }
  132. std::set<std::string> getAvailableGameIds()
  133. {
  134. std::set<std::string> gameids;
  135. std::set<std::string> gamespaths;
  136. gamespaths.insert(porting::path_share + DIR_DELIM + "games");
  137. gamespaths.insert(porting::path_user + DIR_DELIM + "games");
  138. Strfnd search_paths(getSubgamePathEnv());
  139. while (!search_paths.at_end())
  140. gamespaths.insert(search_paths.next(PATH_DELIM));
  141. for (const std::string &gamespath : gamespaths) {
  142. std::vector<fs::DirListNode> dirlist = fs::GetDirListing(gamespath);
  143. for (const fs::DirListNode &dln : dirlist) {
  144. if (!dln.dir)
  145. continue;
  146. // If configuration file is not found or broken, ignore game
  147. Settings conf;
  148. std::string conf_path = gamespath + DIR_DELIM + dln.name +
  149. DIR_DELIM + "game.conf";
  150. if (!conf.readConfigFile(conf_path.c_str()))
  151. continue;
  152. // Add it to result
  153. const char *ends[] = {"_game", NULL};
  154. std::string shorter = removeStringEnd(dln.name, ends);
  155. if (!shorter.empty())
  156. gameids.insert(shorter);
  157. else
  158. gameids.insert(dln.name);
  159. }
  160. }
  161. return gameids;
  162. }
  163. std::vector<SubgameSpec> getAvailableGames()
  164. {
  165. std::vector<SubgameSpec> specs;
  166. std::set<std::string> gameids = getAvailableGameIds();
  167. for (const auto &gameid : gameids)
  168. specs.push_back(findSubgame(gameid));
  169. return specs;
  170. }
  171. #define LEGACY_GAMEID "minetest"
  172. bool getWorldExists(const std::string &world_path)
  173. {
  174. return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
  175. fs::PathExists(world_path + DIR_DELIM + "world.mt"));
  176. }
  177. std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
  178. {
  179. std::string conf_path = world_path + DIR_DELIM + "world.mt";
  180. Settings conf;
  181. bool succeeded = conf.readConfigFile(conf_path.c_str());
  182. if (!succeeded) {
  183. if (can_be_legacy) {
  184. // If map_meta.txt exists, it is probably an old minetest world
  185. if (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
  186. return LEGACY_GAMEID;
  187. }
  188. return "";
  189. }
  190. if (!conf.exists("gameid"))
  191. return "";
  192. // The "mesetint" gameid has been discarded
  193. if (conf.get("gameid") == "mesetint")
  194. return "minetest";
  195. return conf.get("gameid");
  196. }
  197. std::string getWorldPathEnv()
  198. {
  199. char *world_path = getenv("MINETEST_WORLD_PATH");
  200. return world_path ? std::string(world_path) : "";
  201. }
  202. std::vector<WorldSpec> getAvailableWorlds()
  203. {
  204. std::vector<WorldSpec> worlds;
  205. std::set<std::string> worldspaths;
  206. Strfnd search_paths(getWorldPathEnv());
  207. while (!search_paths.at_end())
  208. worldspaths.insert(search_paths.next(PATH_DELIM));
  209. worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
  210. infostream << "Searching worlds..." << std::endl;
  211. for (const std::string &worldspath : worldspaths) {
  212. infostream << " In " << worldspath << ": " << std::endl;
  213. std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
  214. for (const fs::DirListNode &dln : dirvector) {
  215. if (!dln.dir)
  216. continue;
  217. std::string fullpath = worldspath + DIR_DELIM + dln.name;
  218. std::string name = dln.name;
  219. // Just allow filling in the gameid always for now
  220. bool can_be_legacy = true;
  221. std::string gameid = getWorldGameId(fullpath, can_be_legacy);
  222. WorldSpec spec(fullpath, name, gameid);
  223. if (!spec.isValid()) {
  224. infostream << "(invalid: " << name << ") ";
  225. } else {
  226. infostream << name << " ";
  227. worlds.push_back(spec);
  228. }
  229. }
  230. infostream << std::endl;
  231. }
  232. // Check old world location
  233. do {
  234. std::string fullpath = porting::path_user + DIR_DELIM + "world";
  235. if (!fs::PathExists(fullpath))
  236. break;
  237. std::string name = "Old World";
  238. std::string gameid = getWorldGameId(fullpath, true);
  239. WorldSpec spec(fullpath, name, gameid);
  240. infostream << "Old world found." << std::endl;
  241. worlds.push_back(spec);
  242. } while (false);
  243. infostream << worlds.size() << " found." << std::endl;
  244. return worlds;
  245. }
  246. bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
  247. {
  248. // Override defaults with those provided by the game.
  249. // We clear and reload the defaults because the defaults
  250. // might have been overridden by other subgame config
  251. // files that were loaded before.
  252. g_settings->clearDefaults();
  253. set_default_settings(g_settings);
  254. Settings game_defaults;
  255. getGameMinetestConfig(gamespec.path, game_defaults);
  256. override_default_settings(g_settings, &game_defaults);
  257. infostream << "Initializing world at " << path << std::endl;
  258. fs::CreateAllDirs(path);
  259. // Create world.mt if does not already exist
  260. std::string worldmt_path = path + DIR_DELIM "world.mt";
  261. if (!fs::PathExists(worldmt_path)) {
  262. Settings conf;
  263. conf.set("gameid", gamespec.id);
  264. conf.set("backend", "sqlite3");
  265. conf.set("player_backend", "sqlite3");
  266. conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
  267. conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
  268. if (!conf.updateConfigFile(worldmt_path.c_str()))
  269. return false;
  270. }
  271. // Create map_meta.txt if does not already exist
  272. std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
  273. if (!fs::PathExists(map_meta_path)) {
  274. verbosestream << "Creating map_meta.txt (" << map_meta_path << ")"
  275. << std::endl;
  276. fs::CreateAllDirs(path);
  277. std::ostringstream oss(std::ios_base::binary);
  278. Settings conf;
  279. MapgenParams params;
  280. params.readParams(g_settings);
  281. params.writeParams(&conf);
  282. conf.writeLines(oss);
  283. oss << "[end_of_params]\n";
  284. fs::safeWriteToFile(map_meta_path, oss.str());
  285. }
  286. return true;
  287. }