RecipieLoader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "RecipieLoader.h"
  2. #include <Datei.h>
  3. #include <iostream>
  4. #include <stdexcept>
  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) validator->release();
  15. }
  16. void RecipieLoader::loadRecipies(const char* path)
  17. {
  18. std::cout << "loading recipies from '" << path << "'" << std::endl;
  19. Framework::Datei d;
  20. d.setDatei(path);
  21. if (d.istOrdner())
  22. {
  23. Framework::RCArray<Framework::Text>* files = d.getDateiListe();
  24. for (Framework::Text* f : *files)
  25. loadRecipies(Framework::Text(path) + "/" + *f);
  26. files->release();
  27. }
  28. else
  29. {
  30. JSONValue* json = loadJSONFromFile(path);
  31. JSONValidator* validator = zRecipieValidator();
  32. JSONValue* valid = validator->getValidParts(json);
  33. json->release();
  34. if (valid)
  35. {
  36. for (JSONValue* recipie : *valid->asArray())
  37. loadRecipie(recipie->asObject());
  38. valid->release();
  39. }
  40. }
  41. }
  42. void RecipieLoader::loadRecipie(JSONObject* zRecipie)
  43. {
  44. Framework::Text group = zRecipie->zValue("group")->asString()->getString();
  45. if (zRecipie->zValue("type")->asString()->getString().istGleich("shaped"))
  46. {
  47. int width = (int)zRecipie->zValue("width")->asNumber()->getNumber();
  48. int height = (int)zRecipie->zValue("height")->asNumber()->getNumber();
  49. ShapedRecipie* recipie = new ShapedRecipie(width, height);
  50. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  51. {
  52. int x
  53. = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
  54. int y
  55. = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
  56. ItemFilter* filter
  57. = loadFilter(input->asObject()->zValue("filter")->asObject());
  58. CombinedItemModifier* resultingModifier
  59. = new CombinedItemModifier();
  60. for (JSONValue* modifier :
  61. *input->asObject()->zValue("modifiers")->asArray())
  62. resultingModifier->addModifier(
  63. loadModifier(modifier->asObject()));
  64. recipie->setIngredient(x, y, filter, resultingModifier);
  65. }
  66. int outputCount = (int)zRecipie->asObject()
  67. ->zValue("outputCount")
  68. ->asNumber()
  69. ->getNumber();
  70. Framework::Text outputType = zRecipie->asObject()
  71. ->zValue("output")
  72. ->asObject()
  73. ->zValue("itemType")
  74. ->asString()
  75. ->getString();
  76. Item* item = 0;
  77. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  78. {
  79. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  80. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  81. ->getName()
  82. .istGleich(outputType))
  83. {
  84. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
  85. ->createItem();
  86. break;
  87. }
  88. }
  89. recipie->setOutput(item, outputCount);
  90. if (!zShapedRecipieList(group)) registerShapedRecipieList(group);
  91. zShapedRecipieList(group)->addRecipie(recipie);
  92. }
  93. else if (zRecipie->zValue("type")->asString()->getString().istGleich(
  94. "unordered"))
  95. {
  96. Recipie* recipie = new Recipie();
  97. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  98. {
  99. int count = (int)input->asObject()
  100. ->zValue("count")
  101. ->asNumber()
  102. ->getNumber();
  103. ItemFilter* filter
  104. = loadFilter(input->asObject()->zValue("filter")->asObject());
  105. recipie->addIngredient(filter, count);
  106. }
  107. for (JSONValue* output : *zRecipie->zValue("outputs")->asArray())
  108. {
  109. int count = (int)output->asObject()
  110. ->zValue("count")
  111. ->asNumber()
  112. ->getNumber();
  113. Framework::Text outputType = output->asObject()
  114. ->zValue("item")
  115. ->asObject()
  116. ->zValue("itemType")
  117. ->asString()
  118. ->getString();
  119. Item* item = 0;
  120. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount();
  121. i++)
  122. {
  123. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  124. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  125. ->getName()
  126. .istGleich(outputType))
  127. {
  128. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
  129. ->createItem();
  130. break;
  131. }
  132. }
  133. recipie->addOutput(item, count);
  134. }
  135. if (!zRecipieList(group)) registerRecipieList(group);
  136. zRecipieList(group)->addRecipie(recipie);
  137. }
  138. }
  139. ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
  140. {
  141. Framework::Text type = zFilter->zValue("itemType")->asString()->getString();
  142. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  143. {
  144. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  145. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  146. ->getName()
  147. .istGleich(type))
  148. return new TypeItemFilter(
  149. StaticRegistry<ItemType>::INSTANCE.zElement(i));
  150. }
  151. return 0;
  152. }
  153. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  154. {
  155. for (RecipieList* l : lists)
  156. {
  157. if (l->getName().istGleich(name)) return l;
  158. }
  159. return 0;
  160. }
  161. ShapedRecipieList* RecipieLoader::zShapedRecipieList(const char* name) const
  162. {
  163. for (ShapedRecipieList* l : shapedLists)
  164. {
  165. if (l->getName().istGleich(name)) return l;
  166. }
  167. return 0;
  168. }
  169. void RecipieLoader::registerRecipieList(const char* name)
  170. {
  171. if (zRecipieList(name))
  172. throw new std::invalid_argument("the recipie list already exists");
  173. lists.add(new RecipieList(name));
  174. }
  175. void RecipieLoader::registerShapedRecipieList(const char* name)
  176. {
  177. if (zShapedRecipieList(name))
  178. throw new std::invalid_argument("the recipie list already exists");
  179. shapedLists.add(new ShapedRecipieList(name));
  180. }
  181. ItemModifier* RecipieLoader::loadModifier(
  182. Framework::JSON::JSONObject* zModifier)
  183. {
  184. return new AttributeItemModifier(
  185. zModifier->zValue("attribute")->asString()->getString(),
  186. zModifier->zValue("value")->asString()->getString());
  187. }
  188. JSONValidator* RecipieLoader::zRecipieValidator()
  189. {
  190. if (validator) return validator;
  191. Framework::RCArray<Framework::Text> itemTypes;
  192. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  193. {
  194. if (StaticRegistry<ItemType>::INSTANCE.zElement(i))
  195. {
  196. itemTypes.add(new Framework::Text(
  197. StaticRegistry<ItemType>::INSTANCE.zElement(i)
  198. ->getName()
  199. .getText()));
  200. }
  201. }
  202. JSONValidator* filterValidator = JSONValidator::buildForObject()
  203. ->withRequiredString("itemType")
  204. ->whichIsOneOf(itemTypes)
  205. ->finishString()
  206. ->finishObject();
  207. JSONValidator* outputValidator = JSONValidator::buildForObject()
  208. ->withRequiredString("itemType")
  209. ->whichIsOneOf(itemTypes)
  210. ->finishString()
  211. ->finishObject();
  212. validator
  213. = JSONValidator::buildForArray()
  214. ->typeSpecifiedByAttribute("type")
  215. ->removeInvalidEntries()
  216. ->addAcceptedTypeInArray(
  217. JSONValidator::buildForObject()
  218. ->withRequiredString("type")
  219. ->withExactMatch("shaped")
  220. ->finishString()
  221. ->withRequiredString("group")
  222. ->finishString()
  223. ->withRequiredNumber("width")
  224. ->whichIsGreaterThen(0)
  225. ->finishNumber()
  226. ->withRequiredNumber("height")
  227. ->whichIsGreaterThen(0)
  228. ->finishNumber()
  229. ->withRequiredAttribute("inputs",
  230. JSONValidator::buildForArray()
  231. ->withDefault(new JSONArray())
  232. ->addAcceptedTypeInArray(
  233. JSONValidator::buildForObject()
  234. ->withRequiredNumber("x")
  235. ->whichIsGreaterOrEqual(0)
  236. ->finishNumber()
  237. ->withRequiredNumber("y")
  238. ->whichIsGreaterOrEqual(0)
  239. ->finishNumber()
  240. ->withRequiredAttribute("filter",
  241. dynamic_cast<JSONValidator*>(
  242. filterValidator->getThis()))
  243. ->withRequiredAttribute("modifiers",
  244. JSONValidator::buildForArray()
  245. ->withDefault(Parser::getValue(
  246. "[{\"attribute\": \"hp\", "
  247. "\"value\": \"=0\"}]")
  248. ->asArray())
  249. ->addAcceptedTypeInArray(
  250. JSONValidator::
  251. buildForObject()
  252. ->withRequiredString(
  253. "attribute")
  254. ->finishString()
  255. ->withRequiredString(
  256. "value")
  257. ->finishString()
  258. ->finishObject())
  259. ->finishArray())
  260. ->finishObject())
  261. ->finishArray())
  262. ->withRequiredAttribute("output",
  263. dynamic_cast<JSONValidator*>(
  264. outputValidator->getThis()))
  265. ->withRequiredNumber("outputCount")
  266. ->withDefault(1)
  267. ->whichIsGreaterThen(0)
  268. ->finishNumber()
  269. ->finishObject())
  270. ->addAcceptedTypeInArray(
  271. JSONValidator::buildForObject()
  272. ->withRequiredString("type")
  273. ->withExactMatch("unordered")
  274. ->finishString()
  275. ->withRequiredString("group")
  276. ->finishString()
  277. ->withRequiredAttribute("inputs",
  278. JSONValidator::buildForArray()
  279. ->withDefault(new JSONArray())
  280. ->addAcceptedTypeInArray(
  281. JSONValidator::buildForObject()
  282. ->withRequiredNumber("count")
  283. ->withDefault(1)
  284. ->whichIsGreaterThen(0)
  285. ->finishNumber()
  286. ->withRequiredAttribute("filter",
  287. dynamic_cast<JSONValidator*>(
  288. filterValidator->getThis()))
  289. ->finishObject())
  290. ->finishArray())
  291. ->withRequiredAttribute("output",
  292. JSONValidator::buildForArray()
  293. ->addAcceptedTypeInArray(
  294. JSONValidator::buildForObject()
  295. ->withRequiredAttribute("item",
  296. dynamic_cast<JSONValidator*>(
  297. outputValidator->getThis()))
  298. ->withRequiredNumber("count")
  299. ->withDefault(1)
  300. ->whichIsGreaterThen(0)
  301. ->finishNumber()
  302. ->finishObject())
  303. ->finishArray())
  304. ->finishObject())
  305. ->finishArray();
  306. filterValidator->release();
  307. outputValidator->release();
  308. return validator;
  309. }