content.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Minetest
  3. Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
  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 <fstream>
  17. #include "content/content.h"
  18. #include "content/subgames.h"
  19. #include "content/mods.h"
  20. #include "filesys.h"
  21. #include "settings.h"
  22. ContentType getContentType(const std::string &path)
  23. {
  24. std::ifstream modpack_is((path + DIR_DELIM + "modpack.txt").c_str());
  25. if (modpack_is.good()) {
  26. modpack_is.close();
  27. return ContentType::MODPACK;
  28. }
  29. std::ifstream modpack2_is((path + DIR_DELIM + "modpack.conf").c_str());
  30. if (modpack2_is.good()) {
  31. modpack2_is.close();
  32. return ContentType::MODPACK;
  33. }
  34. std::ifstream init_is((path + DIR_DELIM + "init.lua").c_str());
  35. if (init_is.good()) {
  36. init_is.close();
  37. return ContentType::MOD;
  38. }
  39. std::ifstream game_is((path + DIR_DELIM + "game.conf").c_str());
  40. if (game_is.good()) {
  41. game_is.close();
  42. return ContentType::GAME;
  43. }
  44. std::ifstream txp_is((path + DIR_DELIM + "texture_pack.conf").c_str());
  45. if (txp_is.good()) {
  46. txp_is.close();
  47. return ContentType::TXP;
  48. }
  49. return ContentType::UNKNOWN;
  50. }
  51. void parseContentInfo(ContentSpec &spec)
  52. {
  53. std::string conf_path;
  54. switch (getContentType(spec.path)) {
  55. case ContentType::MOD:
  56. spec.type = "mod";
  57. conf_path = spec.path + DIR_DELIM + "mod.conf";
  58. break;
  59. case ContentType::MODPACK:
  60. spec.type = "modpack";
  61. conf_path = spec.path + DIR_DELIM + "modpack.conf";
  62. break;
  63. case ContentType::GAME:
  64. spec.type = "game";
  65. conf_path = spec.path + DIR_DELIM + "game.conf";
  66. break;
  67. case ContentType::TXP:
  68. spec.type = "txp";
  69. conf_path = spec.path + DIR_DELIM + "texture_pack.conf";
  70. break;
  71. default:
  72. spec.type = "unknown";
  73. break;
  74. }
  75. Settings conf;
  76. if (!conf_path.empty() && conf.readConfigFile(conf_path.c_str())) {
  77. if (conf.exists("title"))
  78. spec.title = conf.get("title");
  79. else if (spec.type == "game" && conf.exists("name"))
  80. spec.title = conf.get("name");
  81. if (spec.type != "game" && conf.exists("name"))
  82. spec.name = conf.get("name");
  83. if (conf.exists("title"))
  84. spec.title = conf.get("title");
  85. if (spec.type == "game") {
  86. if (spec.title.empty())
  87. spec.title = spec.name;
  88. spec.name = "";
  89. }
  90. if (conf.exists("description"))
  91. spec.desc = conf.get("description");
  92. if (conf.exists("author"))
  93. spec.author = conf.get("author");
  94. if (conf.exists("release"))
  95. spec.release = conf.getS32("release");
  96. if (conf.exists("textdomain"))
  97. spec.textdomain = conf.get("textdomain");
  98. }
  99. if (spec.name.empty())
  100. spec.name = fs::GetFilenameFromPath(spec.path.c_str());
  101. if (spec.textdomain.empty())
  102. spec.textdomain = spec.name;
  103. if (spec.desc.empty()) {
  104. std::ifstream is((spec.path + DIR_DELIM + "description.txt").c_str());
  105. spec.desc = std::string((std::istreambuf_iterator<char>(is)),
  106. std::istreambuf_iterator<char>());
  107. }
  108. }