]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/content/subgames.cpp
Deprecate game.conf name, use title instead (#12030)
[dragonfireclient.git] / src / content / subgames.cpp
index 4b0e37f7cdcae31fed90a1c457339cde5a2f1d17..d0de926ef367e1fdc059bf4a3af82d4901dbbd6a 100644 (file)
@@ -17,26 +17,54 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include <common/c_internal.h>
 #include "content/subgames.h"
 #include "porting.h"
 #include "filesys.h"
 #include "settings.h"
 #include "log.h"
 #include "util/strfnd.h"
-#include "defaultsettings.h" // for override_default_settings
-#include "mapgen/mapgen.h"   // for MapgenParams
+#include "defaultsettings.h" // for set_default_settings
+#include "map_settings_manager.h"
 #include "util/string.h"
 
 #ifndef SERVER
 #include "client/tile.h" // getImagePath
 #endif
 
+// The maximum number of identical world names allowed
+#define MAX_WORLD_NAMES 100
+
+namespace
+{
+
 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());
 }
 
+}
+
+
+void SubgameSpec::checkAndLog() const
+{
+       // Log deprecation messages
+       auto handling_mode = get_deprecated_handling_mode();
+       if (!deprecation_msgs.empty() && handling_mode != DeprecatedHandlingMode::Ignore) {
+               std::ostringstream os;
+               os << "Game " << title << " at " << path << ":" << std::endl;
+               for (auto msg : deprecation_msgs)
+                       os << "\t" << msg << std::endl;
+
+               if (handling_mode == DeprecatedHandlingMode::Error)
+                       throw ModError(os.str());
+               else
+                       warningstream << os.str();
+       }
+}
+
+
 struct GameFindPath
 {
        std::string path;
@@ -67,15 +95,19 @@ SubgameSpec findSubgame(const std::string &id)
        std::vector<GameFindPath> find_paths;
        while (!search_paths.at_end()) {
                std::string path = search_paths.next(PATH_DELIM);
-               find_paths.emplace_back(path + DIR_DELIM + id, false);
-               find_paths.emplace_back(path + DIR_DELIM + id + "_game", false);
+               path.append(DIR_DELIM).append(id);
+               find_paths.emplace_back(path, false);
+               path.append("_game");
+               find_paths.emplace_back(path, false);
        }
-       find_paths.emplace_back(
-                       user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true);
-       find_paths.emplace_back(user + DIR_DELIM + "games" + DIR_DELIM + id, true);
-       find_paths.emplace_back(
-                       share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false);
-       find_paths.emplace_back(share + DIR_DELIM + "games" + DIR_DELIM + id, false);
+
+       std::string game_base = DIR_DELIM;
+       game_base = game_base.append("games").append(DIR_DELIM).append(id);
+       std::string game_suffixed = game_base + "_game";
+       find_paths.emplace_back(user + game_suffixed, true);
+       find_paths.emplace_back(user + game_base, true);
+       find_paths.emplace_back(share + game_suffixed, false);
+       find_paths.emplace_back(share + game_base, false);
 
        // Find game directory
        std::string game_path;
@@ -95,22 +127,27 @@ SubgameSpec findSubgame(const std::string &id)
        std::string gamemod_path = game_path + DIR_DELIM + "mods";
 
        // Find mod directories
-       std::set<std::string> mods_paths;
-       if (!user_game)
-               mods_paths.insert(share + DIR_DELIM + "mods");
-       if (user != share || user_game)
-               mods_paths.insert(user + DIR_DELIM + "mods");
+       std::unordered_map<std::string, std::string> mods_paths;
+       mods_paths["mods"] = user + DIR_DELIM + "mods";
+       if (!user_game && user != share)
+               mods_paths["share"] = share + DIR_DELIM + "mods";
+
+       for (const std::string &mod_path : getEnvModPaths()) {
+               mods_paths[fs::AbsolutePath(mod_path)] = mod_path;
+       }
 
        // Get meta
        std::string conf_path = game_path + DIR_DELIM + "game.conf";
        Settings conf;
        conf.readConfigFile(conf_path.c_str());
 
-       std::string game_name;
-       if (conf.exists("name"))
-               game_name = conf.get("name");
+       std::string game_title;
+       if (conf.exists("title"))
+               game_title = conf.get("title");
+       else if (conf.exists("name"))
+               game_title = conf.get("name");
        else
-               game_name = id;
+               game_title = id;
 
        std::string game_author;
        if (conf.exists("author"))
@@ -125,8 +162,14 @@ SubgameSpec findSubgame(const std::string &id)
        menuicon_path = getImagePath(
                        game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
 #endif
-       return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
+
+       SubgameSpec spec(id, game_path, gamemod_path, mods_paths, game_title,
                        menuicon_path, game_author, game_release);
+
+       if (conf.exists("name") && !conf.exists("title"))
+               spec.deprecation_msgs.push_back("\"name\" setting in game.conf is deprecated, please use \"title\" instead");
+
+       return spec;
 }
 
 SubgameSpec findWorldSubgame(const std::string &world_path)
@@ -144,10 +187,12 @@ SubgameSpec findWorldSubgame(const std::string &world_path)
                std::string conf_path = world_gamepath + DIR_DELIM + "game.conf";
                conf.readConfigFile(conf_path.c_str());
 
-               if (conf.exists("name"))
-                       gamespec.name = conf.get("name");
+               if (conf.exists("title"))
+                       gamespec.title = conf.get("title");
+               else if (conf.exists("name"))
+                       gamespec.title = conf.get("name");
                else
-                       gamespec.name = world_gameid;
+                       gamespec.title = world_gameid;
 
                return gamespec;
        }
@@ -209,6 +254,21 @@ bool getWorldExists(const std::string &world_path)
                        fs::PathExists(world_path + DIR_DELIM + "world.mt"));
 }
 
+//! Try to get the displayed name of a world
+std::string getWorldName(const std::string &world_path, const std::string &default_name)
+{
+       std::string conf_path = world_path + DIR_DELIM + "world.mt";
+       Settings conf;
+       bool succeeded = conf.readConfigFile(conf_path.c_str());
+       if (!succeeded) {
+               return default_name;
+       }
+
+       if (!conf.exists("world_name"))
+               return default_name;
+       return conf.get("world_name");
+}
+
 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
 {
        std::string conf_path = world_path + DIR_DELIM + "world.mt";
@@ -249,13 +309,13 @@ std::vector<WorldSpec> getAvailableWorlds()
        worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
        infostream << "Searching worlds..." << std::endl;
        for (const std::string &worldspath : worldspaths) {
-               infostream << "  In " << worldspath << ": " << std::endl;
+               infostream << "  In " << worldspath << ": ";
                std::vector<fs::DirListNode> dirvector = fs::GetDirListing(worldspath);
                for (const fs::DirListNode &dln : dirvector) {
                        if (!dln.dir)
                                continue;
                        std::string fullpath = worldspath + DIR_DELIM + dln.name;
-                       std::string name = dln.name;
+                       std::string name = getWorldName(fullpath, dln.name);
                        // Just allow filling in the gameid always for now
                        bool can_be_legacy = true;
                        std::string gameid = getWorldGameId(fullpath, can_be_legacy);
@@ -284,55 +344,80 @@ std::vector<WorldSpec> getAvailableWorlds()
        return worlds;
 }
 
-bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamespec)
+void loadGameConfAndInitWorld(const std::string &path, const std::string &name,
+               const SubgameSpec &gamespec, bool create_world)
 {
-       // 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);
+       std::string final_path = path;
+
+       // If we're creating a new world, ensure that the path isn't already taken
+       if (create_world) {
+               int counter = 1;
+               while (fs::PathExists(final_path) && counter < MAX_WORLD_NAMES) {
+                       final_path = path + "_" + std::to_string(counter);
+                       counter++;
+               }
 
-       infostream << "Initializing world at " << path << std::endl;
+               if (fs::PathExists(final_path)) {
+                       throw BaseException("Too many similar filenames");
+               }
+       }
+
+       Settings *game_settings = Settings::getLayer(SL_GAME);
+       const bool new_game_settings = (game_settings == nullptr);
+       if (new_game_settings) {
+               // Called by main-menu without a Server instance running
+               // -> create and free manually
+               game_settings = Settings::createLayer(SL_GAME);
+       }
 
-       fs::CreateAllDirs(path);
+       getGameMinetestConfig(gamespec.path, *game_settings);
+       game_settings->removeSecureSettings();
+
+       infostream << "Initializing world at " << final_path << std::endl;
+
+       fs::CreateAllDirs(final_path);
 
        // Create world.mt if does not already exist
-       std::string worldmt_path = path + DIR_DELIM "world.mt";
+       std::string worldmt_path = final_path + DIR_DELIM "world.mt";
        if (!fs::PathExists(worldmt_path)) {
                Settings conf;
 
+               conf.set("world_name", name);
                conf.set("gameid", gamespec.id);
                conf.set("backend", "sqlite3");
                conf.set("player_backend", "sqlite3");
                conf.set("auth_backend", "sqlite3");
+               conf.set("mod_storage_backend", "sqlite3");
                conf.setBool("creative_mode", g_settings->getBool("creative_mode"));
                conf.setBool("enable_damage", g_settings->getBool("enable_damage"));
 
-               if (!conf.updateConfigFile(worldmt_path.c_str()))
-                       return false;
+               if (!conf.updateConfigFile(worldmt_path.c_str())) {
+                       throw BaseException("Failed to update the config file");
+               }
        }
 
        // Create map_meta.txt if does not already exist
-       std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
+       std::string map_meta_path = final_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;
+               MapSettingsManager mgr(map_meta_path);
 
-               params.readParams(g_settings);
-               params.writeParams(&conf);
-               conf.writeLines(oss);
-               oss << "[end_of_params]\n";
+               mgr.setMapSetting("seed", g_settings->get("fixed_map_seed"));
 
-               fs::safeWriteToFile(map_meta_path, oss.str());
+               mgr.makeMapgenParams();
+               mgr.saveMapMeta();
        }
-       return true;
+
+       // The Settings object is no longer needed for created worlds
+       if (new_game_settings)
+               delete game_settings;
+}
+
+std::vector<std::string> getEnvModPaths()
+{
+       const char *c_mod_path = getenv("MINETEST_MOD_PATH");
+       std::vector<std::string> paths;
+       Strfnd search_paths(c_mod_path ? c_mod_path : "");
+       while (!search_paths.at_end())
+               paths.push_back(search_paths.next(PATH_DELIM));
+       return paths;
 }