subgame.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #ifndef SERVER
  22. #include "tile.h" // getImagePath
  23. #endif
  24. #include "util/string.h"
  25. bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
  26. {
  27. std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
  28. return conf.readConfigFile(conf_path.c_str());
  29. }
  30. bool getGameConfig(const std::string &game_path, Settings &conf)
  31. {
  32. std::string conf_path = game_path + DIR_DELIM + "game.conf";
  33. return conf.readConfigFile(conf_path.c_str());
  34. }
  35. std::string getGameName(const std::string &game_path)
  36. {
  37. Settings conf;
  38. if(!getGameConfig(game_path, conf))
  39. return "";
  40. if(!conf.exists("name"))
  41. return "";
  42. return conf.get("name");
  43. }
  44. struct GameFindPath
  45. {
  46. std::string path;
  47. bool user_specific;
  48. GameFindPath(const std::string &path, bool user_specific):
  49. path(path),
  50. user_specific(user_specific)
  51. {}
  52. };
  53. SubgameSpec findSubgame(const std::string &id)
  54. {
  55. if(id == "")
  56. return SubgameSpec();
  57. std::string share = porting::path_share;
  58. std::string user = porting::path_user;
  59. std::vector<GameFindPath> find_paths;
  60. find_paths.push_back(GameFindPath(
  61. user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
  62. find_paths.push_back(GameFindPath(
  63. user + DIR_DELIM + "games" + DIR_DELIM + id, true));
  64. find_paths.push_back(GameFindPath(
  65. share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
  66. find_paths.push_back(GameFindPath(
  67. 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(u32 i=0; i<find_paths.size(); i++){
  72. const std::string &try_path = find_paths[i].path;
  73. if(fs::PathExists(try_path)){
  74. game_path = try_path;
  75. user_game = find_paths[i].user_specific;
  76. break;
  77. }
  78. }
  79. if(game_path == "")
  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. std::string game_name = getGameName(game_path);
  89. if(game_name == "")
  90. game_name = id;
  91. std::string menuicon_path;
  92. #ifndef SERVER
  93. menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
  94. #endif
  95. return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
  96. menuicon_path);
  97. }
  98. SubgameSpec findWorldSubgame(const std::string &world_path)
  99. {
  100. std::string world_gameid = getWorldGameId(world_path, true);
  101. // See if world contains an embedded game; if so, use it.
  102. std::string world_gamepath = world_path + DIR_DELIM + "game";
  103. if(fs::PathExists(world_gamepath)){
  104. SubgameSpec gamespec;
  105. gamespec.id = world_gameid;
  106. gamespec.path = world_gamepath;
  107. gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
  108. gamespec.name = getGameName(world_gamepath);
  109. if(gamespec.name == "")
  110. gamespec.name = "unknown";
  111. return gamespec;
  112. }
  113. return findSubgame(world_gameid);
  114. }
  115. std::set<std::string> getAvailableGameIds()
  116. {
  117. std::set<std::string> gameids;
  118. std::set<std::string> gamespaths;
  119. gamespaths.insert(porting::path_share + DIR_DELIM + "games");
  120. gamespaths.insert(porting::path_user + DIR_DELIM + "games");
  121. for(std::set<std::string>::const_iterator i = gamespaths.begin();
  122. i != gamespaths.end(); i++){
  123. std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
  124. for(u32 j=0; j<dirlist.size(); j++){
  125. if(!dirlist[j].dir)
  126. continue;
  127. // If configuration file is not found or broken, ignore game
  128. Settings conf;
  129. if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
  130. continue;
  131. // Add it to result
  132. const char *ends[] = {"_game", NULL};
  133. std::string shorter = removeStringEnd(dirlist[j].name, ends);
  134. if(shorter != "")
  135. gameids.insert(shorter);
  136. else
  137. gameids.insert(dirlist[j].name);
  138. }
  139. }
  140. return gameids;
  141. }
  142. std::vector<SubgameSpec> getAvailableGames()
  143. {
  144. std::vector<SubgameSpec> specs;
  145. std::set<std::string> gameids = getAvailableGameIds();
  146. for(std::set<std::string>::const_iterator i = gameids.begin();
  147. i != gameids.end(); i++)
  148. specs.push_back(findSubgame(*i));
  149. return specs;
  150. }
  151. #define LEGACY_GAMEID "minetest"
  152. bool getWorldExists(const std::string &world_path)
  153. {
  154. return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
  155. fs::PathExists(world_path + DIR_DELIM + "world.mt"));
  156. }
  157. std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
  158. {
  159. std::string conf_path = world_path + DIR_DELIM + "world.mt";
  160. Settings conf;
  161. bool succeeded = conf.readConfigFile(conf_path.c_str());
  162. if(!succeeded){
  163. if(can_be_legacy){
  164. // If map_meta.txt exists, it is probably an old minetest world
  165. if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
  166. return LEGACY_GAMEID;
  167. }
  168. return "";
  169. }
  170. if(!conf.exists("gameid"))
  171. return "";
  172. // The "mesetint" gameid has been discarded
  173. if(conf.get("gameid") == "mesetint")
  174. return "minetest";
  175. return conf.get("gameid");
  176. }
  177. std::vector<WorldSpec> getAvailableWorlds()
  178. {
  179. std::vector<WorldSpec> worlds;
  180. std::set<std::string> worldspaths;
  181. worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
  182. infostream<<"Searching worlds..."<<std::endl;
  183. for(std::set<std::string>::const_iterator i = worldspaths.begin();
  184. i != worldspaths.end(); i++){
  185. infostream<<" In "<<(*i)<<": "<<std::endl;
  186. std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
  187. for(u32 j=0; j<dirvector.size(); j++){
  188. if(!dirvector[j].dir)
  189. continue;
  190. std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
  191. std::string name = dirvector[j].name;
  192. // Just allow filling in the gameid always for now
  193. bool can_be_legacy = true;
  194. std::string gameid = getWorldGameId(fullpath, can_be_legacy);
  195. WorldSpec spec(fullpath, name, gameid);
  196. if(!spec.isValid()){
  197. infostream<<"(invalid: "<<name<<") ";
  198. } else {
  199. infostream<<name<<" ";
  200. worlds.push_back(spec);
  201. }
  202. }
  203. infostream<<std::endl;
  204. }
  205. // Check old world location
  206. do{
  207. std::string fullpath = porting::path_user + DIR_DELIM + "world";
  208. if(!fs::PathExists(fullpath))
  209. break;
  210. std::string name = "Old World";
  211. std::string gameid = getWorldGameId(fullpath, true);
  212. WorldSpec spec(fullpath, name, gameid);
  213. infostream<<"Old world found."<<std::endl;
  214. worlds.push_back(spec);
  215. }while(0);
  216. infostream<<worlds.size()<<" found."<<std::endl;
  217. return worlds;
  218. }
  219. bool initializeWorld(const std::string &path, const std::string &gameid)
  220. {
  221. infostream<<"Initializing world at "<<path<<std::endl;
  222. // Create world.mt if does not already exist
  223. std::string worldmt_path = path + DIR_DELIM + "world.mt";
  224. if(!fs::PathExists(worldmt_path)){
  225. infostream<<"Creating world.mt ("<<worldmt_path<<")"<<std::endl;
  226. fs::CreateAllDirs(path);
  227. std::ofstream of(worldmt_path.c_str(), std::ios::binary);
  228. of<<"gameid = "<<gameid<<"\n";
  229. }
  230. return true;
  231. }