RecipieLoader.cpp 3.4 KB

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