RecipieLoader.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "RecipieLoader.h"
  2. #include <Datei.h>
  3. #include <iostream>
  4. #include <Logging.h>
  5. #include <stdexcept>
  6. #include "Game.h"
  7. #include "ItemType.h"
  8. #include "JsonUtils.h"
  9. using namespace Framework::JSON;
  10. using namespace Validator;
  11. RecipieLoader::RecipieLoader()
  12. : Framework::ReferenceCounter(),
  13. validator(0)
  14. {}
  15. RecipieLoader::~RecipieLoader()
  16. {
  17. if (validator) validator->release();
  18. }
  19. void RecipieLoader::loadRecipies(const char* path)
  20. {
  21. loadAllJsonsFromDirectory(
  22. path, [this](JSONValue* zJson, Framework::Text path) {
  23. Framework::Logging::info()
  24. << "loading recipies from '" << path << "'";
  25. JSONValidator* validator
  26. = Framework::JSON::Validator::JSONValidator::buildForArray()
  27. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  28. ->getValidator<Recipie>())
  29. ->removeInvalidEntries()
  30. ->finishArray();
  31. Framework::RCArray<JSONValidationResult> invalidParts;
  32. JSONValue* valid = validator->getValidParts(zJson, &invalidParts);
  33. for (JSONValidationResult* invalidPart : invalidParts)
  34. {
  35. Framework::Logging::error() << invalidPart->getInvalidInfo();
  36. }
  37. int count = 0;
  38. if (valid)
  39. {
  40. for (JSONValue* value : *valid->asArray())
  41. {
  42. Recipie* recipie
  43. = Game::INSTANCE->zTypeRegistry()->fromJson<Recipie>(
  44. value);
  45. if (recipie)
  46. {
  47. Framework::Text group = recipie->getGroupName();
  48. RecipieList* list = zRecipieList(group);
  49. if (!list)
  50. {
  51. registerRecipieList(group);
  52. list = zRecipieList(group);
  53. }
  54. list->addRecipie(recipie);
  55. count++;
  56. }
  57. }
  58. }
  59. Framework::Logging::info() << count << " recipies were loaded.";
  60. });
  61. }
  62. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  63. {
  64. for (RecipieList* l : lists)
  65. {
  66. if (l->getName().istGleich(name)) return l;
  67. }
  68. return 0;
  69. }
  70. void RecipieLoader::registerRecipieList(const char* name)
  71. {
  72. if (zRecipieList(name))
  73. throw new std::invalid_argument("the recipie list already exists");
  74. lists.add(new RecipieList(name));
  75. }
  76. Framework::Text RecipieLoader::getCrafingUIML(int itemTypeId)
  77. {
  78. Framework::Text result = "<dialog id=\"crafting_";
  79. result.append() << itemTypeId << "\"><craftingRecipies>";
  80. for (RecipieList* list : lists)
  81. {
  82. Framework::RCArray<Recipie> recipies;
  83. list->findRecipies(itemTypeId, recipies);
  84. if (recipies.getEintragAnzahl() > 0)
  85. {
  86. result.append()
  87. << "<craftingRecipieGroup name=\"" << list->getName();
  88. if (list->getName().istGleich("inventory"))
  89. {
  90. result.append()
  91. << "\" itemIcon=\""
  92. << Game::INSTANCE->getItemTypeId("Crafting Table");
  93. }
  94. result += "\">";
  95. for (Recipie* recipie : recipies)
  96. {
  97. result += recipie->getRecipieUIML();
  98. }
  99. result += "</craftingRecipieGroup>";
  100. }
  101. }
  102. result += "</craftingRecipies></dialog>";
  103. return result;
  104. }