map_settings_manager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(Settings *user_settings,
  23. const std::string &map_meta_path):
  24. m_map_meta_path(map_meta_path),
  25. m_map_settings(new Settings()),
  26. m_user_settings(user_settings)
  27. {
  28. assert(m_user_settings != NULL);
  29. }
  30. MapSettingsManager::~MapSettingsManager()
  31. {
  32. delete m_map_settings;
  33. delete mapgen_params;
  34. }
  35. bool MapSettingsManager::getMapSetting(
  36. const std::string &name, std::string *value_out)
  37. {
  38. if (m_map_settings->getNoEx(name, *value_out))
  39. return true;
  40. // Compatibility kludge
  41. if (m_user_settings == g_settings && name == "seed")
  42. return m_user_settings->getNoEx("fixed_map_seed", *value_out);
  43. return m_user_settings->getNoEx(name, *value_out);
  44. }
  45. bool MapSettingsManager::getMapSettingNoiseParams(
  46. const std::string &name, NoiseParams *value_out)
  47. {
  48. return m_map_settings->getNoiseParams(name, *value_out) ||
  49. m_user_settings->getNoiseParams(name, *value_out);
  50. }
  51. bool MapSettingsManager::setMapSetting(
  52. const std::string &name, const std::string &value, bool override_meta)
  53. {
  54. if (mapgen_params)
  55. return false;
  56. if (override_meta)
  57. m_map_settings->set(name, value);
  58. else
  59. m_map_settings->setDefault(name, value);
  60. return true;
  61. }
  62. bool MapSettingsManager::setMapSettingNoiseParams(
  63. const std::string &name, const NoiseParams *value, bool override_meta)
  64. {
  65. if (mapgen_params)
  66. return false;
  67. m_map_settings->setNoiseParams(name, *value, !override_meta);
  68. return true;
  69. }
  70. bool MapSettingsManager::loadMapMeta()
  71. {
  72. std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
  73. if (!is.good()) {
  74. errorstream << "loadMapMeta: could not open "
  75. << m_map_meta_path << std::endl;
  76. return false;
  77. }
  78. if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
  79. errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool MapSettingsManager::saveMapMeta()
  85. {
  86. // If mapgen params haven't been created yet; abort
  87. if (!mapgen_params)
  88. return false;
  89. if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
  90. errorstream << "saveMapMeta: could not create dirs to "
  91. << m_map_meta_path;
  92. return false;
  93. }
  94. std::ostringstream oss(std::ios_base::binary);
  95. Settings conf;
  96. mapgen_params->MapgenParams::writeParams(&conf);
  97. mapgen_params->writeParams(&conf);
  98. conf.writeLines(oss);
  99. // NOTE: If there are ever types of map settings other than
  100. // those relating to map generation, save them here
  101. oss << "[end_of_params]\n";
  102. if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
  103. errorstream << "saveMapMeta: could not write "
  104. << m_map_meta_path << std::endl;
  105. return false;
  106. }
  107. return true;
  108. }
  109. MapgenParams *MapSettingsManager::makeMapgenParams()
  110. {
  111. if (mapgen_params)
  112. return mapgen_params;
  113. assert(m_user_settings != NULL);
  114. assert(m_map_settings != NULL);
  115. // At this point, we have (in order of precedence):
  116. // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or
  117. // explicit overrides from scripts
  118. // 2). m_mapgen_settings->m_defaults containing script-set mgparams without
  119. // overrides
  120. // 3). g_settings->m_settings containing all user-specified config file
  121. // settings
  122. // 4). g_settings->m_defaults containing any low-priority settings from
  123. // scripts, e.g. mods using Lua as an enhanced config file)
  124. // Now, get the mapgen type so we can create the appropriate MapgenParams
  125. std::string mg_name;
  126. MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
  127. Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
  128. if (mgtype == MAPGEN_INVALID) {
  129. errorstream << "EmergeManager: mapgen '" << mg_name <<
  130. "' not valid; falling back to " <<
  131. Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
  132. mgtype = MAPGEN_DEFAULT;
  133. }
  134. // Create our MapgenParams
  135. MapgenParams *params = Mapgen::createMapgenParams(mgtype);
  136. if (params == NULL)
  137. return NULL;
  138. params->mgtype = mgtype;
  139. // Load the rest of the mapgen params from our active settings
  140. params->MapgenParams::readParams(m_user_settings);
  141. params->MapgenParams::readParams(m_map_settings);
  142. params->readParams(m_user_settings);
  143. params->readParams(m_map_settings);
  144. // Hold onto our params
  145. mapgen_params = params;
  146. return params;
  147. }