From 6bc4cad0eddd7a7cf593ca1471599e2d75727379 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Thu, 11 Sep 2014 18:22:05 -0400 Subject: [PATCH] Split settings into seperate source and header files This also cleans up settings a bit --- build/android/jni/Android.mk | 3 +- src/CMakeLists.txt | 155 +++--- src/client.cpp | 17 +- src/clientiface.cpp | 7 +- src/clientmedia.cpp | 5 +- src/content_cao.cpp | 19 +- src/content_mapblock.cpp | 7 +- src/content_sao.cpp | 5 +- src/database-leveldb.cpp | 5 +- src/database-sqlite3.cpp | 1 + src/emerge.cpp | 6 +- src/environment.cpp | 33 +- src/guiEngine.cpp | 7 +- src/inventorymanager.cpp | 1 + src/keycode.cpp | 1 + src/map.cpp | 30 +- src/mapgen.cpp | 2 +- src/mapgen.h | 4 +- src/nodedef.cpp | 3 +- src/player.cpp | 22 +- src/script/lua_api/l_mapgen.cpp | 2 + src/script/lua_api/l_util.cpp | 3 +- src/settings.cpp | 702 +++++++++++++++++++++++++++ src/settings.h | 836 ++++---------------------------- src/socket.cpp | 29 +- src/test.cpp | 14 +- src/tile.cpp | 12 +- 27 files changed, 993 insertions(+), 938 deletions(-) create mode 100644 src/settings.cpp diff --git a/build/android/jni/Android.mk b/build/android/jni/Android.mk index 9b2057781..96693fa4d 100644 --- a/build/android/jni/Android.mk +++ b/build/android/jni/Android.mk @@ -206,7 +206,8 @@ LOCAL_SRC_FILES := \ jni/src/util/string.cpp \ jni/src/util/timetaker.cpp \ jni/src/touchscreengui.cpp \ - jni/src/database-leveldb.cpp + jni/src/database-leveldb.cpp \ + jni/src/settings.cpp # lua api LOCAL_SRC_FILES += \ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index de1ea2c28..4c1522fac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -333,74 +333,75 @@ add_subdirectory(script) add_subdirectory(util) set(common_SRCS - version.cpp - rollback_interface.cpp - rollback.cpp - genericobject.cpp - voxelalgorithms.cpp - sound.cpp - quicktune.cpp - subgame.cpp - inventorymanager.cpp - mods.cpp + ban.cpp + base64.cpp + biome.cpp + cavegen.cpp + clientiface.cpp + collision.cpp + connection.cpp content_abm.cpp + content_mapnode.cpp + content_nodemeta.cpp + content_sao.cpp + convert_json.cpp craftdef.cpp - nameidmapping.cpp + database-dummy.cpp + database-leveldb.cpp + database-redis.cpp + database-sqlite3.cpp + database.cpp + debug.cpp + defaultsettings.cpp + dungeongen.cpp + emerge.cpp + environment.cpp + filesys.cpp + genericobject.cpp + gettext.cpp + httpfetch.cpp + inventory.cpp + inventorymanager.cpp itemdef.cpp - nodedef.cpp - object_properties.cpp + light.cpp log.cpp - content_sao.cpp - emerge.cpp + map.cpp + mapblock.cpp mapgen.cpp + mapgen_singlenode.cpp mapgen_v6.cpp mapgen_v7.cpp - mapgen_singlenode.cpp - treegen.cpp - dungeongen.cpp - cavegen.cpp - content_nodemeta.cpp - content_mapnode.cpp - collision.cpp + mapnode.cpp + mapsector.cpp + mods.cpp + nameidmapping.cpp + nodedef.cpp nodemetadata.cpp nodetimer.cpp - serverobject.cpp noise.cpp + object_properties.cpp + pathfinder.cpp + player.cpp porting.cpp - tool.cpp - defaultsettings.cpp - mapnode.cpp - voxel.cpp - inventory.cpp - debug.cpp + quicktune.cpp + rollback.cpp + rollback_interface.cpp serialization.cpp - light.cpp - filesys.cpp - connection.cpp - environment.cpp server.cpp - clientiface.cpp - socket.cpp - mapblock.cpp - mapsector.cpp - map.cpp - database.cpp - database-dummy.cpp - database-leveldb.cpp - database-sqlite3.cpp - database-redis.cpp - player.cpp - test.cpp + serverlist.cpp + serverobject.cpp + settings.cpp sha1.cpp - base64.cpp - ban.cpp - biome.cpp + socket.cpp + sound.cpp staticobject.cpp - serverlist.cpp - pathfinder.cpp - convert_json.cpp - gettext.cpp - httpfetch.cpp + subgame.cpp + test.cpp + tool.cpp + treegen.cpp + version.cpp + voxel.cpp + voxelalgorithms.cpp ${JTHREAD_SRCS} ${common_SCRIPT_SRCS} ${UTIL_SRCS} @@ -429,38 +430,38 @@ endif() set(minetest_SRCS ${common_SRCS} ${sound_SRCS} - localplayer.cpp - sky.cpp - clientmap.cpp - content_cso.cpp - content_mapblock.cpp - content_cao.cpp - mesh.cpp - mapblock_mesh.cpp - keycode.cpp camera.cpp - clouds.cpp - particles.cpp - clientobject.cpp chat.cpp - hud.cpp - guiKeyChangeMenu.cpp - guiFormSpecMenu.cpp - guiTable.cpp - guiPasswordChange.cpp - guiVolumeChange.cpp - guiChatConsole.cpp client.cpp + clientmap.cpp clientmedia.cpp + clientobject.cpp + clouds.cpp + content_cao.cpp + content_cso.cpp + content_mapblock.cpp + convert_json.cpp + drawscene.cpp filecache.cpp - tile.cpp - shader.cpp game.cpp - main.cpp + guiChatConsole.cpp guiEngine.cpp guiFileSelectMenu.cpp - convert_json.cpp - drawscene.cpp + guiFormSpecMenu.cpp + guiKeyChangeMenu.cpp + guiPasswordChange.cpp + guiTable.cpp + guiVolumeChange.cpp + hud.cpp + keycode.cpp + localplayer.cpp + main.cpp + mapblock_mesh.cpp + mesh.cpp + particles.cpp + shader.cpp + sky.cpp + tile.cpp ${minetest_SCRIPT_SRCS} ) list(SORT minetest_SRCS) diff --git a/src/client.cpp b/src/client.cpp index accff4b01..5a276e306 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -17,13 +17,19 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "client.h" #include #include -#include "clientserver.h" +#include +#include #include "jthread/jmutexautolock.h" +#include "util/directiontables.h" +#include "util/pointedthing.h" +#include "util/serialize.h" +#include "util/string.h" +#include "strfnd.h" +#include "client.h" +#include "clientserver.h" #include "main.h" -#include #include "filesys.h" #include "porting.h" #include "mapsector.h" @@ -37,18 +43,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "nodedef.h" #include "itemdef.h" #include "shader.h" -#include #include "base64.h" #include "clientmap.h" #include "clientmedia.h" #include "sound.h" -#include "util/string.h" #include "IMeshCache.h" #include "serialization.h" -#include "util/serialize.h" #include "config.h" -#include "util/directiontables.h" -#include "util/pointedthing.h" #include "version.h" #include "drawscene.h" diff --git a/src/clientiface.cpp b/src/clientiface.cpp index ebbbc65bc..cd2e2d4d9 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -20,6 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "clientiface.h" +#include "util/numeric.h" +#include "util/mathconstants.h" #include "player.h" #include "settings.h" #include "mapblock.h" @@ -28,11 +30,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "map.h" #include "emerge.h" #include "serverobject.h" // TODO this is used for cleanup of only - -#include "util/numeric.h" -#include "util/mathconstants.h" - #include "main.h" // for g_settings +#include "log.h" const char *ClientInterface::statenames[] = { "Invalid", diff --git a/src/clientmedia.cpp b/src/clientmedia.cpp index e2679ed46..434eeb248 100644 --- a/src/clientmedia.cpp +++ b/src/clientmedia.cpp @@ -18,10 +18,13 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "clientmedia.h" +#include "util/serialize.h" +#include "util/string.h" #include "httpfetch.h" #include "client.h" #include "clientserver.h" #include "filecache.h" +#include "filesys.h" #include "hex.h" #include "sha1.h" #include "debug.h" @@ -29,8 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" #include "settings.h" #include "main.h" -#include "util/serialize.h" -#include "util/string.h" static std::string getMediaCacheDir() { diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 02622f5b4..d1de23d2a 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -17,14 +17,20 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include +#include +#include +#include +#include +#include #include "content_cao.h" +#include "util/numeric.h" // For IntervalLimiter +#include "util/serialize.h" +#include "util/mathconstants.h" #include "tile.h" #include "environment.h" #include "collision.h" #include "settings.h" -#include -#include -#include #include "serialization.h" // For decompressZlib #include "gamedef.h" #include "clientobject.h" @@ -36,15 +42,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "sound.h" #include "nodedef.h" #include "localplayer.h" -#include "util/numeric.h" // For IntervalLimiter -#include "util/serialize.h" -#include "util/mathconstants.h" #include "map.h" #include "main.h" // g_settings #include "camera.h" // CameraModes -#include -#include -#include +#include "log.h" class Settings; struct ToolCapabilities; diff --git a/src/content_mapblock.cpp b/src/content_mapblock.cpp index b7a48d57e..c94ff590e 100644 --- a/src/content_mapblock.cpp +++ b/src/content_mapblock.cpp @@ -18,15 +18,16 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "content_mapblock.h" - +#include "util/numeric.h" +#include "util/directiontables.h" #include "main.h" // For g_settings #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector #include "settings.h" #include "nodedef.h" #include "tile.h" #include "gamedef.h" -#include "util/numeric.h" -#include "util/directiontables.h" +#include "log.h" + // Create a cuboid. // collector - the MeshCollector for the resulting polygons diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 4ee92f4d3..40313f7a5 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -18,6 +18,8 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "content_sao.h" +#include "util/serialize.h" +#include "util/mathconstants.h" #include "collision.h" #include "environment.h" #include "settings.h" @@ -29,8 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "player.h" #include "scripting_game.h" #include "genericobject.h" -#include "util/serialize.h" -#include "util/mathconstants.h" +#include "log.h" std::map ServerActiveObject::m_types; diff --git a/src/database-leveldb.cpp b/src/database-leveldb.cpp index d57462be9..1681b0195 100644 --- a/src/database-leveldb.cpp +++ b/src/database-leveldb.cpp @@ -20,10 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "config.h" #if USE_LEVELDB -/* -LevelDB databases -*/ - #include "database-leveldb.h" #include "leveldb/db.h" @@ -35,6 +31,7 @@ LevelDB databases #include "main.h" #include "settings.h" #include "log.h" +#include "filesys.h" #define ENSURE_STATUS_OK(s) \ if (!(s).ok()) { \ diff --git a/src/database-sqlite3.cpp b/src/database-sqlite3.cpp index 4d48796fc..8e1501786 100644 --- a/src/database-sqlite3.cpp +++ b/src/database-sqlite3.cpp @@ -44,6 +44,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "main.h" #include "settings.h" #include "log.h" +#include "filesys.h" Database_SQLite3::Database_SQLite3(ServerMap *map, std::string savedir) { diff --git a/src/emerge.cpp b/src/emerge.cpp index a2b265486..b6e2080a6 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -97,7 +97,7 @@ EmergeManager::EmergeManager(IGameDef *gamedef) { // if unspecified, leave a proc for the main thread and one for // some other misc thread - int nthreads = 0; + s16 nthreads = 0; if (!g_settings->getS16NoEx("num_emerge_threads", nthreads)) nthreads = porting::getNumberOfProcessors() - 2; if (nthreads < 1) @@ -117,8 +117,8 @@ EmergeManager::EmergeManager(IGameDef *gamedef) { if (qlimit_generate < 1) qlimit_generate = 1; - for (int i = 0; i != nthreads; i++) - emergethread.push_back(new EmergeThread((Server *)gamedef, i)); + for (s16 i = 0; i < nthreads; i++) + emergethread.push_back(new EmergeThread((Server *) gamedef, i)); infostream << "EmergeManager: using " << nthreads << " threads" << std::endl; } diff --git a/src/environment.cpp b/src/environment.cpp index c8af72f6b..66898f012 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -508,38 +508,29 @@ void ServerEnvironment::loadMeta() // Open file and deserialize std::ifstream is(path.c_str(), std::ios_base::binary); - if(is.good() == false) - { - infostream<<"ServerEnvironment::loadMeta(): Failed to open " - < +#include #include "scripting_mainmenu.h" +#include "util/numeric.h" #include "config.h" #include "version.h" #include "porting.h" @@ -31,14 +34,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "sound_openal.h" #include "clouds.h" #include "httpfetch.h" -#include "util/numeric.h" +#include "log.h" #ifdef __ANDROID__ #include "tile.h" #include #endif -#include -#include /******************************************************************************/ /** TextDestGuiEngine */ diff --git a/src/inventorymanager.cpp b/src/inventorymanager.cpp index 1c511e9cb..1ffcc3460 100644 --- a/src/inventorymanager.cpp +++ b/src/inventorymanager.cpp @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "craftdef.h" #include "rollback_interface.h" +#include "strfnd.h" #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" diff --git a/src/keycode.cpp b/src/keycode.cpp index 890c97cc2..c5f102b44 100644 --- a/src/keycode.cpp +++ b/src/keycode.cpp @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "log.h" #include "hex.h" +#include "debug.h" class UnknownKeycode : public BaseException { diff --git a/src/map.cpp b/src/map.cpp index 236972ae9..1fe0ca9ad 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -3065,37 +3065,25 @@ void ServerMap::loadMapMeta() { DSTACK(__FUNCTION_NAME); - /*infostream<<"ServerMap::loadMapMeta(): Loading map metadata" - <loadParamsFromSettings(¶ms); - verbosestream<<"ServerMap::loadMapMeta(): seed=" - << m_emerge->params.seed< +#include "util/numeric.h" #include "hud.h" #include "constants.h" #include "gamedef.h" #include "settings.h" #include "content_sao.h" -#include "util/numeric.h" +#include "filesys.h" +#include "log.h" Player::Player(IGameDef *gamedef): touching_ground(false), @@ -195,18 +199,10 @@ void Player::serialize(std::ostream &os) void Player::deSerialize(std::istream &is, std::string playername) { Settings args; - - for(;;) - { - if(is.eof()) - throw SerializationError - (("Player::deSerialize(): PlayerArgsEnd of player \"" + playername + "\" not found").c_str()); - std::string line; - std::getline(is, line); - std::string trimmedline = trim(line); - if(trimmedline == "PlayerArgsEnd") - break; - args.parseConfigLine(line); + + if (!args.parseConfigLines(is, "PlayerArgsEnd")) { + throw SerializationError("PlayerArgsEnd of player " + + playername + " not found!"); } //args.getS32("version"); // Version field value not used diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index c6d41050b..d31e84b3d 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -22,12 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_vmanip.h" #include "common/c_converter.h" #include "common/c_content.h" +#include "util/serialize.h" #include "server.h" #include "environment.h" #include "biome.h" #include "emerge.h" #include "mapgen_v7.h" #include "main.h" +#include "log.h" struct EnumString ModApiMapgen::es_BiomeTerrainType[] = diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index dda5b5abf..eb6c1835d 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -23,13 +23,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_content.h" #include "cpp_api/s_async.h" #include "serialization.h" +#include "json/json.h" #include "debug.h" #include "porting.h" #include "log.h" #include "tool.h" +#include "filesys.h" #include "settings.h" #include "main.h" //required for g_settings, g_settings_path -#include "json/json.h" // debug(...) // Writes a line to dstream diff --git a/src/settings.cpp b/src/settings.cpp new file mode 100644 index 000000000..760f07e21 --- /dev/null +++ b/src/settings.cpp @@ -0,0 +1,702 @@ +/* +Minetest +Copyright (C) 2010-2013 celeron55, Perttu Ahola + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "settings.h" +#include "irrlichttypes_bloated.h" +#include "exceptions.h" +#include "jthread/jmutexautolock.h" +#include "strfnd.h" +#include +#include +#include +#include "debug.h" +#include "log.h" +#include "util/serialize.h" +#include "filesys.h" +#include + + +Settings & Settings::operator += (const Settings &other) +{ + update(other); + + return *this; +} + + +Settings & Settings::operator = (const Settings &other) +{ + if (&other == this) + return *this; + + JMutexAutoLock lock(m_mutex); + JMutexAutoLock lock2(other.m_mutex); + + clearNoLock(); + updateNoLock(other); + + return *this; +} + + +bool Settings::parseConfigLines(std::istream &is, + const std::string &end) +{ + JMutexAutoLock lock(m_mutex); + + std::string name, value; + bool end_found = false; + + while (is.good() && !end_found) { + if (parseConfigObject(is, name, value, end, end_found)) { + m_settings[name] = value; + } + } + if (!end.empty() && !end_found) { + return false; + } + return true; +} + + +bool Settings::readConfigFile(const char *filename) +{ + std::ifstream is(filename); + if (!is.good()) + return false; + + JMutexAutoLock lock(m_mutex); + + std::string name, value; + + while (is.good()) { + if (parseConfigObject(is, name, value)) { + m_settings[name] = value; + } + } + + return true; +} + + +void Settings::writeLines(std::ostream &os) const +{ + JMutexAutoLock lock(m_mutex); + + for (std::map::const_iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { + os << i->first << " = " << i->second << '\n'; + } +} + + +bool Settings::updateConfigFile(const char *filename) +{ + std::list objects; + std::set updated; + bool changed = false; + + JMutexAutoLock lock(m_mutex); + + // Read the file and check for differences + { + std::ifstream is(filename); + while (is.good()) { + getUpdatedConfigObject(is, objects, + updated, changed); + } + } + + // If something not yet determined to have been changed, check if + // any new stuff was added + if (!changed) { + for (std::map::const_iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { + if (updated.find(i->first) == updated.end()) { + changed = true; + break; + } + } + } + + // If nothing was actually changed, skip writing the file + if (!changed) { + return true; + } + + // Write stuff back + { + std::ostringstream ss(std::ios_base::binary); + + // Write changes settings + for (std::list::const_iterator + i = objects.begin(); + i != objects.end(); ++i) { + ss << (*i); + } + + // Write new settings + for (std::map::const_iterator + i = m_settings.begin(); + i != m_settings.end(); ++i) { + if (updated.find(i->first) != updated.end()) + continue; + ss << i->first << " = " << i->second << '\n'; + } + + if (!fs::safeWriteToFile(filename, ss.str())) { + errorstream << "Error writing configuration file: \"" + << filename << "\"" << std::endl; + return false; + } + } + + return true; +} + + +bool Settings::parseCommandLine(int argc, char *argv[], + std::map &allowed_options) +{ + int nonopt_index = 0; + for (int i = 1; i < argc; i++) { + std::string arg_name = argv[i]; + if (arg_name.substr(0, 2) != "--") { + // If option doesn't start with -, read it in as nonoptX + if (arg_name[0] != '-'){ + std::string name = "nonopt"; + name += itos(nonopt_index); + set(name, arg_name); + nonopt_index++; + continue; + } + errorstream << "Invalid command-line parameter \"" + << arg_name << "\": --