RecipieLoader.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <stdexcept>
  2. #include <Datei.h>
  3. #include <iostream>
  4. #include "RecipieLoader.h"
  5. #include "ItemType.h"
  6. using namespace Framework::JSON;
  7. using namespace Validator;
  8. RecipieLoader::RecipieLoader()
  9. : Framework::ReferenceCounter(),
  10. validator(0)
  11. {}
  12. RecipieLoader::~RecipieLoader()
  13. {
  14. if (validator)
  15. validator->release();
  16. }
  17. void RecipieLoader::loadRecipies(const char* path)
  18. {
  19. shapedLists.leeren();
  20. lists.leeren();
  21. std::cout << "loading recipies from '" << path << "'" << std::endl;
  22. Framework::Datei d;
  23. d.setDatei(path);
  24. if (d.istOrdner())
  25. {
  26. Framework::RCArray<Framework::Text>* files = d.getDateiListe();
  27. for (Framework::Text* f : *files)
  28. loadRecipies(Framework::Text(path) + "/" + *f);
  29. files->release();
  30. }
  31. else
  32. {
  33. JSONValue* json = loadJSONFromFile(path);
  34. JSONValidator* validator = zRecipieValidator();
  35. JSONValue* valid = validator->getValidParts(json);
  36. json->release();
  37. if (valid)
  38. {
  39. for (JSONValue* recipie : *valid->asArray())
  40. loadRecipie(recipie->asObject());
  41. valid->release();
  42. }
  43. }
  44. }
  45. void RecipieLoader::loadRecipie(JSONObject* zRecipie)
  46. {
  47. Framework::Text group = zRecipie->zValue("group")->asString()->getString();
  48. if (zRecipie->zValue("type")->asString()->getString().istGleich("shaped"))
  49. {
  50. int width = (int)zRecipie->zValue("width")->asNumber()->getNumber();
  51. int height = (int)zRecipie->zValue("height")->asNumber()->getNumber();
  52. ShapedRecipie* recipie = new ShapedRecipie(width, height);
  53. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  54. {
  55. int x = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
  56. int y = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
  57. ItemFilter* filter = loadFilter(input->asObject()->zValue("filter")->asObject());
  58. recipie->setIngredient(x, y, filter);
  59. }
  60. int outputCount = (int)zRecipie->asObject()->zValue("outputCount")->asNumber()->getNumber();
  61. Framework::Text outputType = zRecipie->asObject()->zValue("output")->asObject()->zValue("itemType")->asString()->getString();
  62. Item* item = 0;
  63. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  64. {
  65. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().istGleich(outputType))
  66. {
  67. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)->createItem();
  68. break;
  69. }
  70. }
  71. recipie->setOutput(item, outputCount);
  72. if (!zShapedRecipieList(group))
  73. registerShapedRecipieList(group);
  74. zShapedRecipieList(group)->addRecipie(recipie);
  75. }
  76. else if (zRecipie->zValue("type")->asString()->getString().istGleich("unordered"))
  77. {
  78. Recipie* recipie = new Recipie();
  79. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  80. {
  81. int count = (int)input->asObject()->zValue("count")->asNumber()->getNumber();
  82. ItemFilter* filter = loadFilter(input->asObject()->zValue("filter")->asObject());
  83. recipie->addIngredient(filter, count);
  84. }
  85. for (JSONValue* output : *zRecipie->zValue("outputs")->asArray())
  86. {
  87. int count = (int)output->asObject()->zValue("count")->asNumber()->getNumber();
  88. Framework::Text outputType = output->asObject()->zValue("item")->asObject()->zValue("itemType")->asString()->getString();
  89. Item* item = 0;
  90. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  91. {
  92. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().istGleich(outputType))
  93. {
  94. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)->createItem();
  95. break;
  96. }
  97. }
  98. recipie->addOutput(item, count);
  99. }
  100. if (!zRecipieList(group))
  101. registerRecipieList(group);
  102. zRecipieList(group)->addRecipie(recipie);
  103. }
  104. }
  105. ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
  106. {
  107. Framework::Text type = zFilter->zValue("itemType")->asString()->getString();
  108. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  109. {
  110. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().istGleich(type))
  111. return new TypeItemFilter(StaticRegistry<ItemType>::INSTANCE.zElement(i));
  112. }
  113. return 0;
  114. }
  115. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  116. {
  117. for (RecipieList* l : lists)
  118. {
  119. if (l->getName().istGleich(name))
  120. return l;
  121. }
  122. return 0;
  123. }
  124. ShapedRecipieList* RecipieLoader::zShapedRecipieList(const char* name) const
  125. {
  126. for (ShapedRecipieList* l : shapedLists)
  127. {
  128. if (l->getName().istGleich(name))
  129. return l;
  130. }
  131. return 0;
  132. }
  133. void RecipieLoader::registerRecipieList(const char* name)
  134. {
  135. if (zRecipieList(name))
  136. throw new std::invalid_argument("the recipie list already exists");
  137. lists.add(new RecipieList(name));
  138. }
  139. void RecipieLoader::registerShapedRecipieList(const char* name)
  140. {
  141. if (zShapedRecipieList(name))
  142. throw new std::invalid_argument("the recipie list already exists");
  143. shapedLists.add(new ShapedRecipieList(name));
  144. }
  145. JSONValidator* RecipieLoader::zRecipieValidator()
  146. {
  147. if (validator)
  148. return validator;
  149. Framework::RCArray<Framework::Text> itemTypes;
  150. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  151. itemTypes.add(new Framework::Text(StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().getText()));
  152. JSONValidator* filterValidator = JSONValidator::buildForObject()
  153. ->withRequiredString("itemType")->whichIsOneOf(itemTypes)->finishString()
  154. ->finishObject();
  155. JSONValidator* outputValidator = JSONValidator::buildForObject()
  156. ->withRequiredString("itemType")->whichIsOneOf(itemTypes)->finishString()
  157. ->finishObject();
  158. validator = JSONValidator::buildForArray()
  159. ->typeSpecifiedByAttribute("type")
  160. ->removeInvalidEntries()
  161. ->addAcceptedTypeInArray(
  162. JSONValidator::buildForObject()
  163. ->withRequiredString("type")->withExactMatch("shaped")->finishString()
  164. ->withRequiredString("group")->finishString()
  165. ->withRequiredNumber("width")->whichIsGreaterThen(0)->finishNumber()
  166. ->withRequiredNumber("height")->whichIsGreaterThen(0)->finishNumber()
  167. ->withRequiredAttribute("inputs",
  168. JSONValidator::buildForArray()
  169. ->withDefault(new JSONArray())
  170. ->addAcceptedTypeInArray(JSONValidator::buildForObject()
  171. ->withRequiredNumber("x")->whichIsGreaterOrEqual(0)->finishNumber()
  172. ->withRequiredNumber("y")->whichIsGreaterOrEqual(0)->finishNumber()
  173. ->withRequiredAttribute("filter", dynamic_cast<JSONValidator*>(filterValidator->getThis()))
  174. ->finishObject())
  175. ->finishArray())
  176. ->withRequiredAttribute("output", dynamic_cast<JSONValidator*>(outputValidator->getThis()))
  177. ->withRequiredNumber("outputCount")->withDefault(1)->whichIsGreaterThen(0)->finishNumber()
  178. ->finishObject())
  179. ->addAcceptedTypeInArray(
  180. JSONValidator::buildForObject()
  181. ->withRequiredString("type")->withExactMatch("unordered")->finishString()
  182. ->withRequiredString("group")->finishString()
  183. ->withRequiredAttribute("inputs",
  184. JSONValidator::buildForArray()
  185. ->withDefault(new JSONArray())
  186. ->addAcceptedTypeInArray(
  187. JSONValidator::buildForObject()
  188. ->withRequiredNumber("count")->withDefault(1)->whichIsGreaterThen(0)->finishNumber()
  189. ->withRequiredAttribute("filter", dynamic_cast<JSONValidator*>(filterValidator->getThis()))
  190. ->finishObject())
  191. ->finishArray())
  192. ->withRequiredAttribute("output",
  193. JSONValidator::buildForArray()
  194. ->addAcceptedTypeInArray(JSONValidator::buildForObject()
  195. ->withRequiredAttribute("item", dynamic_cast<JSONValidator*>(outputValidator->getThis()))
  196. ->withRequiredNumber("count")->withDefault(1)->whichIsGreaterThen(0)->finishNumber()
  197. ->finishObject())
  198. ->finishArray())
  199. ->finishObject())
  200. ->finishArray();
  201. filterValidator->release();
  202. outputValidator->release();
  203. return validator;
  204. }