content.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. enum ContentType
  23. {
  24. ECT_UNKNOWN,
  25. ECT_MOD,
  26. ECT_MODPACK,
  27. ECT_GAME,
  28. ECT_TXP
  29. };
  30. ContentType getContentType(const ContentSpec &spec)
  31. {
  32. std::ifstream modpack_is((spec.path + DIR_DELIM + "modpack.txt").c_str());
  33. if (modpack_is.good()) {
  34. modpack_is.close();
  35. return ECT_MODPACK;
  36. }
  37. std::ifstream init_is((spec.path + DIR_DELIM + "init.lua").c_str());
  38. if (init_is.good()) {
  39. init_is.close();
  40. return ECT_MOD;
  41. }
  42. std::ifstream game_is((spec.path + DIR_DELIM + "game.conf").c_str());
  43. if (game_is.good()) {
  44. game_is.close();
  45. return ECT_GAME;
  46. }
  47. std::ifstream txp_is((spec.path + DIR_DELIM + "texture_pack.conf").c_str());
  48. if (txp_is.good()) {
  49. txp_is.close();
  50. return ECT_TXP;
  51. }
  52. return ECT_UNKNOWN;
  53. }
  54. void parseContentInfo(ContentSpec &spec)
  55. {
  56. std::string conf_path;
  57. switch (getContentType(spec)) {
  58. case ECT_MOD:
  59. spec.type = "mod";
  60. conf_path = spec.path + DIR_DELIM + "mod.conf";
  61. break;
  62. case ECT_MODPACK:
  63. spec.type = "modpack";
  64. conf_path = spec.path + DIR_DELIM + "mod.conf";
  65. break;
  66. case ECT_GAME:
  67. spec.type = "game";
  68. conf_path = spec.path + DIR_DELIM + "game.conf";
  69. break;
  70. case ECT_TXP:
  71. spec.type = "txp";
  72. conf_path = spec.path + DIR_DELIM + "texture_pack.conf";
  73. break;
  74. default:
  75. spec.type = "unknown";
  76. break;
  77. }
  78. Settings conf;
  79. if (!conf_path.empty() && conf.readConfigFile(conf_path.c_str())) {
  80. if (conf.exists("name"))
  81. spec.name = conf.get("name");
  82. if (conf.exists("description"))
  83. spec.desc = conf.get("description");
  84. if (conf.exists("author"))
  85. spec.author = conf.get("author");
  86. if (conf.exists("release"))
  87. spec.release = conf.getS32("release");
  88. }
  89. if (spec.desc.empty()) {
  90. std::ifstream is((spec.path + DIR_DELIM + "description.txt").c_str());
  91. spec.desc = std::string((std::istreambuf_iterator<char>(is)),
  92. std::istreambuf_iterator<char>());
  93. }
  94. }