RecipieLoader.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Framework::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. DataValidator* validator
  22. = Framework::Validator::DataValidator::buildForArray()
  23. ->addAcceptedTypeInArray(
  24. Game::INSTANCE->zTypeRegistry()->getValidator<Recipie>())
  25. ->removeInvalidEntries()
  26. ->finishArray();
  27. loadAllJsonsFromDirectory(
  28. path, [this, &validator](JSONValue* zJson, Framework::Text path) {
  29. Framework::Logging::info()
  30. << "loading recipies from '" << path << "'";
  31. Framework::RCArray<ValidationResult> invalidParts;
  32. JSONValue* valid = validator->getValidParts(zJson, &invalidParts);
  33. for (ValidationResult* 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. Framework::JSON::JSONObject* schema = validator->getJsonSchema();
  62. Framework::Datei syntaxFile;
  63. syntaxFile.setDatei("data/syntax/schema/recipies.json");
  64. syntaxFile.erstellen();
  65. syntaxFile.open(Framework::Datei::Style::schreiben);
  66. syntaxFile.schreibe(schema->toString(), schema->toString().getLength());
  67. syntaxFile.close();
  68. schema->release();
  69. validator->release();
  70. }
  71. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  72. {
  73. for (RecipieList* l : lists)
  74. {
  75. if (l->getName().istGleich(name)) return l;
  76. }
  77. return 0;
  78. }
  79. void RecipieLoader::registerRecipieList(const char* name)
  80. {
  81. if (zRecipieList(name))
  82. throw new std::invalid_argument("the recipie list already exists");
  83. lists.add(new RecipieList(name));
  84. }
  85. Framework::Text RecipieLoader::getCrafingUIML(int itemTypeId)
  86. {
  87. Framework::Text result = "<dialog id=\"crafting_";
  88. result.append() << itemTypeId << "\"><craftingRecipies>";
  89. for (RecipieList* list : lists)
  90. {
  91. Framework::RCArray<Recipie> recipies;
  92. list->findRecipies(itemTypeId, recipies);
  93. if (recipies.getEintragAnzahl() > 0)
  94. {
  95. result.append()
  96. << "<craftingRecipieGroup name=\"" << list->getName();
  97. if (list->getName().istGleich("inventory"))
  98. {
  99. result.append()
  100. << "\" itemIcon=\""
  101. << Game::INSTANCE->getItemTypeId("Crafting Table");
  102. }
  103. result += "\">";
  104. for (Recipie* recipie : recipies)
  105. {
  106. result += recipie->getRecipieUIML();
  107. }
  108. result += "</craftingRecipieGroup>";
  109. }
  110. }
  111. result += "</craftingRecipies></dialog>";
  112. return result;
  113. }