map_settings_manager.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  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 "debug.h"
  17. #include "filesys.h"
  18. #include "log.h"
  19. #include "mapgen/mapgen.h"
  20. #include "settings.h"
  21. #include "map_settings_manager.h"
  22. MapSettingsManager::MapSettingsManager(const std::string &map_meta_path):
  23. m_map_meta_path(map_meta_path),
  24. m_hierarchy(g_settings)
  25. {
  26. /*
  27. * We build our own hierarchy which falls back to the global one.
  28. * It looks as follows: (lowest prio first)
  29. * 0: whatever is picked up from g_settings (incl. engine defaults)
  30. * 1: defaults set by scripts (override_meta = false)
  31. * 2: settings present in map_meta.txt or overridden by scripts
  32. */
  33. m_defaults = new Settings("", &m_hierarchy, 1);
  34. m_map_settings = new Settings("[end_of_params]", &m_hierarchy, 2);
  35. }
  36. MapSettingsManager::~MapSettingsManager()
  37. {
  38. delete m_defaults;
  39. delete m_map_settings;
  40. delete mapgen_params;
  41. }
  42. bool MapSettingsManager::getMapSetting(
  43. const std::string &name, std::string *value_out) const
  44. {
  45. return m_map_settings->getNoEx(name, *value_out);
  46. }
  47. bool MapSettingsManager::getMapSettingNoiseParams(
  48. const std::string &name, NoiseParams *value_out) const
  49. {
  50. // TODO: Rename to "getNoiseParams"
  51. return m_map_settings->getNoiseParams(name, *value_out);
  52. }
  53. bool MapSettingsManager::setMapSetting(
  54. const std::string &name, const std::string &value, bool override_meta)
  55. {
  56. if (mapgen_params)
  57. return false;
  58. if (override_meta)
  59. m_map_settings->set(name, value);
  60. else
  61. m_defaults->set(name, value);
  62. return true;
  63. }
  64. bool MapSettingsManager::setMapSettingNoiseParams(
  65. const std::string &name, const NoiseParams *value, bool override_meta)
  66. {
  67. if (mapgen_params)
  68. return false;
  69. if (override_meta)
  70. m_map_settings->setNoiseParams(name, *value);
  71. else
  72. m_defaults->setNoiseParams(name, *value);
  73. return true;
  74. }
  75. bool MapSettingsManager::loadMapMeta()
  76. {
  77. std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
  78. if (!is.good()) {
  79. errorstream << "loadMapMeta: could not open "
  80. << m_map_meta_path << std::endl;
  81. return false;
  82. }
  83. if (!m_map_settings->parseConfigLines(is)) {
  84. errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl;
  85. return false;
  86. }
  87. return true;
  88. }
  89. bool MapSettingsManager::saveMapMeta()
  90. {
  91. // If mapgen params haven't been created yet; abort
  92. if (!mapgen_params) {
  93. infostream << "saveMapMeta: mapgen_params not present! "
  94. << "Server startup was probably interrupted." << std::endl;
  95. return false;
  96. }
  97. // Paths set up by subgames.cpp, but not in unittests
  98. if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
  99. errorstream << "saveMapMeta: could not create dirs to "
  100. << m_map_meta_path;
  101. return false;
  102. }
  103. mapgen_params->MapgenParams::writeParams(m_map_settings);
  104. mapgen_params->writeParams(m_map_settings);
  105. if (!m_map_settings->updateConfigFile(m_map_meta_path.c_str())) {
  106. errorstream << "saveMapMeta: could not write "
  107. << m_map_meta_path << std::endl;
  108. return false;
  109. }
  110. return true;
  111. }
  112. MapgenParams *MapSettingsManager::makeMapgenParams()
  113. {
  114. if (mapgen_params)
  115. return mapgen_params;
  116. assert(m_map_settings);
  117. assert(m_defaults);
  118. // Now, get the mapgen type so we can create the appropriate MapgenParams
  119. std::string mg_name;
  120. MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
  121. Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
  122. if (mgtype == MAPGEN_INVALID) {
  123. errorstream << "EmergeManager: mapgen '" << mg_name <<
  124. "' not valid; falling back to " <<
  125. Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
  126. mgtype = MAPGEN_DEFAULT;
  127. }
  128. // Create our MapgenParams
  129. MapgenParams *params = Mapgen::createMapgenParams(mgtype);
  130. if (!params)
  131. return nullptr;
  132. params->mgtype = mgtype;
  133. // Load the rest of the mapgen params from our active settings
  134. params->MapgenParams::readParams(m_map_settings);
  135. params->readParams(m_map_settings);
  136. // Hold onto our params
  137. mapgen_params = params;
  138. return params;
  139. }