RecipieLoader.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. CombinedItemModifier* resultingModifier = new CombinedItemModifier();
  59. for (JSONValue* modifier : *input->asObject()->zValue("modifiers")->asArray())
  60. resultingModifier->addModifier(loadModifier(modifier->asObject()));
  61. recipie->setIngredient(x, y, filter, resultingModifier);
  62. }
  63. int outputCount = (int)zRecipie->asObject()->zValue("outputCount")->asNumber()->getNumber();
  64. Framework::Text outputType = zRecipie->asObject()->zValue("output")->asObject()->zValue("itemType")->asString()->getString();
  65. Item* item = 0;
  66. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  67. {
  68. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().istGleich(outputType))
  69. {
  70. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)->createItem();
  71. break;
  72. }
  73. }
  74. recipie->setOutput(item, outputCount);
  75. if (!zShapedRecipieList(group))
  76. registerShapedRecipieList(group);
  77. zShapedRecipieList(group)->addRecipie(recipie);
  78. }
  79. else if (zRecipie->zValue("type")->asString()->getString().istGleich("unordered"))
  80. {
  81. Recipie* recipie = new Recipie();
  82. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  83. {
  84. int count = (int)input->asObject()->zValue("count")->asNumber()->getNumber();
  85. ItemFilter* filter = loadFilter(input->asObject()->zValue("filter")->asObject());
  86. recipie->addIngredient(filter, count);
  87. }
  88. for (JSONValue* output : *zRecipie->zValue("outputs")->asArray())
  89. {
  90. int count = (int)output->asObject()->zValue("count")->asNumber()->getNumber();
  91. Framework::Text outputType = output->asObject()->zValue("item")->asObject()->zValue("itemType")->asString()->getString();
  92. Item* item = 0;
  93. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  94. {
  95. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().istGleich(outputType))
  96. {
  97. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)->createItem();
  98. break;
  99. }
  100. }
  101. recipie->addOutput(item, count);
  102. }
  103. if (!zRecipieList(group))
  104. registerRecipieList(group);
  105. zRecipieList(group)->addRecipie(recipie);
  106. }
  107. }
  108. ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
  109. {
  110. Framework::Text type = zFilter->zValue("itemType")->asString()->getString();
  111. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  112. {
  113. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().istGleich(type))
  114. return new TypeItemFilter(StaticRegistry<ItemType>::INSTANCE.zElement(i));
  115. }
  116. return 0;
  117. }
  118. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  119. {
  120. for (RecipieList* l : lists)
  121. {
  122. if (l->getName().istGleich(name))
  123. return l;
  124. }
  125. return 0;
  126. }
  127. ShapedRecipieList* RecipieLoader::zShapedRecipieList(const char* name) const
  128. {
  129. for (ShapedRecipieList* l : shapedLists)
  130. {
  131. if (l->getName().istGleich(name))
  132. return l;
  133. }
  134. return 0;
  135. }
  136. void RecipieLoader::registerRecipieList(const char* name)
  137. {
  138. if (zRecipieList(name))
  139. throw new std::invalid_argument("the recipie list already exists");
  140. lists.add(new RecipieList(name));
  141. }
  142. void RecipieLoader::registerShapedRecipieList(const char* name)
  143. {
  144. if (zShapedRecipieList(name))
  145. throw new std::invalid_argument("the recipie list already exists");
  146. shapedLists.add(new ShapedRecipieList(name));
  147. }
  148. ItemModifier* RecipieLoader::loadModifier(Framework::JSON::JSONObject* zModifier)
  149. {
  150. return new AttributeItemModifier(zModifier->zValue("attribute")->asString()->getString(), zModifier->zValue("value")->asString()->getString());
  151. }
  152. JSONValidator* RecipieLoader::zRecipieValidator()
  153. {
  154. if (validator)
  155. return validator;
  156. Framework::RCArray<Framework::Text> itemTypes;
  157. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  158. itemTypes.add(new Framework::Text(StaticRegistry<ItemType>::INSTANCE.zElement(i)->getName().getText()));
  159. JSONValidator* filterValidator = JSONValidator::buildForObject()
  160. ->withRequiredString("itemType")->whichIsOneOf(itemTypes)->finishString()
  161. ->finishObject();
  162. JSONValidator* outputValidator = JSONValidator::buildForObject()
  163. ->withRequiredString("itemType")->whichIsOneOf(itemTypes)->finishString()
  164. ->finishObject();
  165. validator = JSONValidator::buildForArray()
  166. ->typeSpecifiedByAttribute("type")
  167. ->removeInvalidEntries()
  168. ->addAcceptedTypeInArray(
  169. JSONValidator::buildForObject()
  170. ->withRequiredString("type")->withExactMatch("shaped")->finishString()
  171. ->withRequiredString("group")->finishString()
  172. ->withRequiredNumber("width")->whichIsGreaterThen(0)->finishNumber()
  173. ->withRequiredNumber("height")->whichIsGreaterThen(0)->finishNumber()
  174. ->withRequiredAttribute("inputs",
  175. JSONValidator::buildForArray()
  176. ->withDefault(new JSONArray())
  177. ->addAcceptedTypeInArray(JSONValidator::buildForObject()
  178. ->withRequiredNumber("x")->whichIsGreaterOrEqual(0)->finishNumber()
  179. ->withRequiredNumber("y")->whichIsGreaterOrEqual(0)->finishNumber()
  180. ->withRequiredAttribute("filter", dynamic_cast<JSONValidator*>(filterValidator->getThis()))
  181. ->withRequiredAttribute("modifiers",
  182. JSONValidator::buildForArray()
  183. ->withDefault(Parser::getValue("[{\"attribute\": \"hp\", \"value\": \"=0\"}]")->asArray())
  184. ->addAcceptedTypeInArray(JSONValidator::buildForObject()
  185. ->withRequiredString("attribute")->finishString()
  186. ->withRequiredString("value")->finishString()
  187. ->finishObject())
  188. ->finishArray())
  189. ->finishObject())
  190. ->finishArray())
  191. ->withRequiredAttribute("output", dynamic_cast<JSONValidator*>(outputValidator->getThis()))
  192. ->withRequiredNumber("outputCount")->withDefault(1)->whichIsGreaterThen(0)->finishNumber()
  193. ->finishObject())
  194. ->addAcceptedTypeInArray(
  195. JSONValidator::buildForObject()
  196. ->withRequiredString("type")->withExactMatch("unordered")->finishString()
  197. ->withRequiredString("group")->finishString()
  198. ->withRequiredAttribute("inputs",
  199. JSONValidator::buildForArray()
  200. ->withDefault(new JSONArray())
  201. ->addAcceptedTypeInArray(
  202. JSONValidator::buildForObject()
  203. ->withRequiredNumber("count")->withDefault(1)->whichIsGreaterThen(0)->finishNumber()
  204. ->withRequiredAttribute("filter", dynamic_cast<JSONValidator*>(filterValidator->getThis()))
  205. ->finishObject())
  206. ->finishArray())
  207. ->withRequiredAttribute("output",
  208. JSONValidator::buildForArray()
  209. ->addAcceptedTypeInArray(JSONValidator::buildForObject()
  210. ->withRequiredAttribute("item", dynamic_cast<JSONValidator*>(outputValidator->getThis()))
  211. ->withRequiredNumber("count")->withDefault(1)->whichIsGreaterThen(0)->finishNumber()
  212. ->finishObject())
  213. ->finishArray())
  214. ->finishObject())
  215. ->finishArray();
  216. filterValidator->release();
  217. outputValidator->release();
  218. return validator;
  219. }