RecipieLoader.cpp 7.4 KB

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