From c2250d95c4da368d1535794a1c7f2092ce479d7a Mon Sep 17 00:00:00 2001
From: Perttu Ahola <celeron55@gmail.com>
Date: Thu, 21 Mar 2013 21:42:23 +0200
Subject: [PATCH] Support game-specific minetest.conf

---
 doc/lua_api.txt         | 3 +++
 src/defaultsettings.cpp | 9 +++++++++
 src/defaultsettings.h   | 1 +
 src/server.cpp          | 8 ++++++++
 src/subgame.cpp         | 6 ++++++
 src/subgame.h           | 3 +++
 6 files changed, 30 insertions(+)

diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 19fa81473..af8b1cc9a 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -57,6 +57,9 @@ eg.
 
 Common mods are loaded from the pseudo-game "common".
 
+The game directory can contain the file minetest.conf, which will be used
+to set default settings when running the particular game.
+
 Mod load path
 -------------
 Generic:
diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp
index b0ae271ce..25edffe32 100644
--- a/src/defaultsettings.cpp
+++ b/src/defaultsettings.cpp
@@ -243,3 +243,12 @@ void set_default_settings(Settings *settings)
 
 }
 
+void override_default_settings(Settings *settings, Settings *from)
+{
+	std::vector<std::string> names = from->getNames();
+	for(size_t i=0; i<names.size(); i++){
+		const std::string &name = names[i];
+		settings->setDefault(name, from->get(name));
+	}
+}
+
diff --git a/src/defaultsettings.h b/src/defaultsettings.h
index 37e3f717f..00aacad87 100644
--- a/src/defaultsettings.h
+++ b/src/defaultsettings.h
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 class Settings;
 
 void set_default_settings(Settings *settings);
+void override_default_settings(Settings *settings, Settings *from);
 
 #endif
 
diff --git a/src/server.cpp b/src/server.cpp
index 2dcab63b8..f77ac6ad9 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -58,6 +58,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/mathconstants.h"
 #include "rollback.h"
 #include "util/serialize.h"
+#include "defaultsettings.h"
 
 void * ServerThread::Thread()
 {
@@ -687,6 +688,13 @@ Server::Server(
 	infostream<<"- config: "<<m_path_config<<std::endl;
 	infostream<<"- game:   "<<m_gamespec.path<<std::endl;
 
+	// Initialize default settings and override defaults with those provided
+	// by the game
+	set_default_settings(g_settings);
+	Settings gamedefaults;
+	getGameMinetestConfig(gamespec.path, gamedefaults);
+	override_default_settings(g_settings, &gamedefaults);
+
 	// Create biome definition manager
 	m_biomedef = new BiomeDefManager(this);
 
diff --git a/src/subgame.cpp b/src/subgame.cpp
index 8678ae37f..19ad4e636 100644
--- a/src/subgame.cpp
+++ b/src/subgame.cpp
@@ -24,6 +24,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "util/string.h"
 
+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());
+}
+
 bool getGameConfig(const std::string &game_path, Settings &conf)
 {
 	std::string conf_path = game_path + DIR_DELIM + "game.conf";
diff --git a/src/subgame.h b/src/subgame.h
index 996714be0..b120b7542 100644
--- a/src/subgame.h
+++ b/src/subgame.h
@@ -54,6 +54,9 @@ struct SubgameSpec
 	}
 };
 
+// minetest.conf
+bool getGameMinetestConfig(const std::string &game_path, Settings &conf);
+// game.conf
 bool getGameConfig(const std::string &game_path, Settings &conf);
 
 std::string getGameName(const std::string &game_path);
-- 
GitLab