RecipieLoader.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. int count = 0;
  35. if (valid)
  36. {
  37. for (JSONValue* recipie : *valid->asArray())
  38. loadRecipie(recipie->asObject());
  39. count = valid->asArray()->getLength();
  40. valid->release();
  41. }
  42. std::cout << count << " recipies were loaded.\n";
  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
  56. = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
  57. int y
  58. = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
  59. ItemFilter* filter
  60. = loadFilter(input->asObject()->zValue("filter")->asObject());
  61. CombinedItemModifier* resultingModifier
  62. = new CombinedItemModifier();
  63. for (JSONValue* modifier :
  64. *input->asObject()->zValue("modifiers")->asArray())
  65. resultingModifier->addModifier(
  66. loadModifier(modifier->asObject()));
  67. recipie->setIngredient(x, y, filter, resultingModifier);
  68. }
  69. int outputCount = (int)zRecipie->asObject()
  70. ->zValue("outputCount")
  71. ->asNumber()
  72. ->getNumber();
  73. Framework::Text outputType = zRecipie->asObject()
  74. ->zValue("output")
  75. ->asObject()
  76. ->zValue("itemType")
  77. ->asString()
  78. ->getString();
  79. Item* item = 0;
  80. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  81. {
  82. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  83. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  84. ->getName()
  85. .istGleich(outputType))
  86. {
  87. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
  88. ->createItem();
  89. break;
  90. }
  91. }
  92. recipie->addOutput(item, outputCount);
  93. if (!zRecipieList(group)) registerRecipieList(group);
  94. zRecipieList(group)->addRecipie(recipie);
  95. }
  96. else if (zRecipie->zValue("type")->asString()->getString().istGleich(
  97. "unordered"))
  98. {
  99. UnshapedRecipie* recipie = new UnshapedRecipie();
  100. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  101. {
  102. int count = (int)input->asObject()
  103. ->zValue("count")
  104. ->asNumber()
  105. ->getNumber();
  106. ItemFilter* filter
  107. = loadFilter(input->asObject()->zValue("filter")->asObject());
  108. CombinedItemModifier* resultingModifier
  109. = new CombinedItemModifier();
  110. for (JSONValue* modifier :
  111. *input->asObject()->zValue("modifiers")->asArray())
  112. resultingModifier->addModifier(
  113. loadModifier(modifier->asObject()));
  114. recipie->addIngredient(filter, count, resultingModifier);
  115. }
  116. for (JSONValue* output : *zRecipie->zValue("outputs")->asArray())
  117. {
  118. int count = (int)output->asObject()
  119. ->zValue("count")
  120. ->asNumber()
  121. ->getNumber();
  122. Framework::Text outputType = output->asObject()
  123. ->zValue("item")
  124. ->asObject()
  125. ->zValue("itemType")
  126. ->asString()
  127. ->getString();
  128. Item* item = 0;
  129. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount();
  130. i++)
  131. {
  132. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  133. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  134. ->getName()
  135. .istGleich(outputType))
  136. {
  137. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
  138. ->createItem();
  139. break;
  140. }
  141. }
  142. recipie->addOutput(item, count);
  143. }
  144. if (!zRecipieList(group)) registerRecipieList(group);
  145. zRecipieList(group)->addRecipie(recipie);
  146. }
  147. }
  148. ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
  149. {
  150. if (zFilter->hasValue("itemType"))
  151. {
  152. Framework::Text type
  153. = zFilter->zValue("itemType")->asString()->getString();
  154. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  155. {
  156. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  157. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  158. ->getName()
  159. .istGleich(type))
  160. return new TypeItemFilter(
  161. StaticRegistry<ItemType>::INSTANCE.zElement(i));
  162. }
  163. }
  164. else if (zFilter->hasValue("operator"))
  165. {
  166. Framework::Text op = zFilter->zValue("operator")
  167. ->asString()
  168. ->getString();
  169. ItemFilter* left = loadFilter(zFilter->zValue("left")->asObject());
  170. ItemFilter* right = loadFilter(zFilter->zValue("right")->asObject());
  171. if (op.istGleich("&&"))
  172. {
  173. return new CombinedItemFilter(
  174. left, right, [](bool a, bool b) { return a && b; });
  175. }
  176. else if (op.istGleich("||"))
  177. {
  178. return new CombinedItemFilter(
  179. left, right, [](bool a, bool b) { return a || b; });
  180. }
  181. else if (op.istGleich("=="))
  182. {
  183. return new CombinedItemFilter(
  184. left, right, [](bool a, bool b) { return a == b; });
  185. }
  186. else if (op.istGleich("!="))
  187. {
  188. return new CombinedItemFilter(
  189. left, right, [](bool a, bool b) { return a != b; });
  190. }
  191. else
  192. {
  193. left->release();
  194. right->release();
  195. }
  196. }
  197. return 0;
  198. }
  199. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  200. {
  201. for (RecipieList* l : lists)
  202. {
  203. if (l->getName().istGleich(name)) return l;
  204. }
  205. return 0;
  206. }
  207. void RecipieLoader::registerRecipieList(const char* name)
  208. {
  209. if (zRecipieList(name))
  210. throw new std::invalid_argument("the recipie list already exists");
  211. lists.add(new RecipieList(name));
  212. }
  213. Framework::Text RecipieLoader::getCrafingUIML(ItemType* zTargetType)
  214. {
  215. Framework::Text result = "<dialog id=\"crafting_";
  216. result.append() << zTargetType->getId() << "\"><craftingRecipies>";
  217. for (RecipieList* list : lists)
  218. {
  219. Framework::RCArray<Recipie> recipies;
  220. list->findRecipies(zTargetType, recipies);
  221. if (recipies.getEintragAnzahl() > 0)
  222. {
  223. result.append()
  224. << "<craftingRecipieGroup name=\"" << list->getName();
  225. if (list->getName().istGleich("inventory"))
  226. {
  227. result.append()
  228. << "\" itemIcon=\"" << ItemTypeEnum::CRAFTING_TABLE;
  229. }
  230. result += "\">";
  231. for (Recipie* recipie : recipies)
  232. {
  233. result += recipie->getRecipieUIML();
  234. }
  235. result += "</craftingRecipieGroup>";
  236. }
  237. }
  238. result += "</craftingRecipies></dialog>";
  239. return result;
  240. }
  241. ItemModifier* RecipieLoader::loadModifier(
  242. Framework::JSON::JSONObject* zModifier)
  243. {
  244. return new AttributeItemModifier(
  245. zModifier->zValue("attribute")->asString()->getString(),
  246. zModifier->zValue("value")->asString()->getString());
  247. }
  248. JSONValidator* RecipieLoader::zRecipieValidator()
  249. {
  250. if (validator) return validator;
  251. Framework::RCArray<Framework::Text> itemTypes;
  252. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  253. {
  254. if (StaticRegistry<ItemType>::INSTANCE.zElement(i))
  255. {
  256. itemTypes.add(new Framework::Text(
  257. StaticRegistry<ItemType>::INSTANCE.zElement(i)
  258. ->getName()
  259. .getText()));
  260. }
  261. }
  262. JSONValidator* typeFilterValidator
  263. = JSONValidator::buildForObject()
  264. ->setObjectReferenceId("typeFilter")
  265. ->withRequiredString("itemType")
  266. ->whichIsOneOf(itemTypes)
  267. ->finishString()
  268. ->finishObject();
  269. Framework::RCArray<Framework::Text> operators;
  270. operators.add(new Framework::Text("=="));
  271. operators.add(new Framework::Text("!="));
  272. operators.add(new Framework::Text("&&"));
  273. operators.add(new Framework::Text("||"));
  274. JSONValidator* operatorFilterValidator
  275. = JSONValidator::buildForObject()
  276. ->setObjectReferenceId("operatorFilter")
  277. ->withRequiredString("operator")
  278. ->whichIsOneOf(operators)
  279. ->finishString()
  280. ->withRequiredAttribute(
  281. "left", JSONValidator::buildForObjectReference("typeFilter"))
  282. ->withRequiredAttribute("left",
  283. JSONValidator::buildForObjectReference("operatorFilter"))
  284. ->withRequiredAttribute(
  285. "right", JSONValidator::buildForObjectReference("typeFilter"))
  286. ->withRequiredAttribute("right",
  287. JSONValidator::buildForObjectReference("operatorFilter"))
  288. ->finishObject();
  289. JSONValidator* outputValidator = JSONValidator::buildForObject()
  290. ->withRequiredString("itemType")
  291. ->whichIsOneOf(itemTypes)
  292. ->finishString()
  293. ->finishObject();
  294. validator
  295. = JSONValidator::buildForArray()
  296. ->typeSpecifiedByAttribute("type")
  297. ->removeInvalidEntries()
  298. ->addAcceptedTypeInArray(
  299. JSONValidator::buildForObject()
  300. ->withRequiredString("type")
  301. ->withExactMatch("shaped")
  302. ->finishString()
  303. ->withRequiredString("group")
  304. ->finishString()
  305. ->withRequiredNumber("width")
  306. ->whichIsGreaterThen(0)
  307. ->finishNumber()
  308. ->withRequiredNumber("height")
  309. ->whichIsGreaterThen(0)
  310. ->finishNumber()
  311. ->withRequiredAttribute("inputs",
  312. JSONValidator::buildForArray()
  313. ->withDefault(new JSONArray())
  314. ->addAcceptedTypeInArray(
  315. JSONValidator::buildForObject()
  316. ->withRequiredNumber("x")
  317. ->whichIsGreaterOrEqual(0)
  318. ->finishNumber()
  319. ->withRequiredNumber("y")
  320. ->whichIsGreaterOrEqual(0)
  321. ->finishNumber()
  322. ->withRequiredAttribute("filter",
  323. dynamic_cast<JSONValidator*>(
  324. typeFilterValidator->getThis()))
  325. ->withRequiredAttribute("filter",
  326. dynamic_cast<JSONValidator*>(
  327. operatorFilterValidator
  328. ->getThis()))
  329. ->withRequiredAttribute("modifiers",
  330. JSONValidator::buildForArray()
  331. ->withDefault(Parser::getValue(
  332. "[{\"attribute\": \"hp\", "
  333. "\"value\": \"=0\"}]")
  334. ->asArray())
  335. ->addAcceptedTypeInArray(
  336. JSONValidator::
  337. buildForObject()
  338. ->withRequiredString(
  339. "attribute")
  340. ->finishString()
  341. ->withRequiredString(
  342. "value")
  343. ->finishString()
  344. ->finishObject())
  345. ->finishArray())
  346. ->finishObject())
  347. ->finishArray())
  348. ->withRequiredAttribute("output",
  349. dynamic_cast<JSONValidator*>(
  350. outputValidator->getThis()))
  351. ->withRequiredNumber("outputCount")
  352. ->withDefault(1)
  353. ->whichIsGreaterThen(0)
  354. ->finishNumber()
  355. ->finishObject())
  356. ->addAcceptedTypeInArray(
  357. JSONValidator::buildForObject()
  358. ->withRequiredString("type")
  359. ->withExactMatch("unordered")
  360. ->finishString()
  361. ->withRequiredString("group")
  362. ->finishString()
  363. ->withRequiredAttribute("inputs",
  364. JSONValidator::buildForArray()
  365. ->withDefault(new JSONArray())
  366. ->addAcceptedTypeInArray(
  367. JSONValidator::buildForObject()
  368. ->withRequiredNumber("count")
  369. ->withDefault(1)
  370. ->whichIsGreaterThen(0)
  371. ->finishNumber()
  372. ->withRequiredAttribute("filter",
  373. dynamic_cast<JSONValidator*>(
  374. typeFilterValidator->getThis()))
  375. ->withRequiredAttribute("filter",
  376. dynamic_cast<JSONValidator*>(
  377. operatorFilterValidator
  378. ->getThis()))
  379. ->withRequiredAttribute("modifiers",
  380. JSONValidator::buildForArray()
  381. ->withDefault(Parser::getValue(
  382. "[{\"attribute\": \"hp\", "
  383. "\"value\": \"=0\"}]")
  384. ->asArray())
  385. ->addAcceptedTypeInArray(
  386. JSONValidator::
  387. buildForObject()
  388. ->withRequiredString(
  389. "attribute")
  390. ->finishString()
  391. ->withRequiredString(
  392. "value")
  393. ->finishString()
  394. ->finishObject())
  395. ->finishArray())
  396. ->finishObject())
  397. ->finishArray())
  398. ->withRequiredAttribute("output",
  399. JSONValidator::buildForArray()
  400. ->addAcceptedTypeInArray(
  401. JSONValidator::buildForObject()
  402. ->withRequiredAttribute("item",
  403. dynamic_cast<JSONValidator*>(
  404. outputValidator->getThis()))
  405. ->withRequiredNumber("count")
  406. ->withDefault(1)
  407. ->whichIsGreaterThen(0)
  408. ->finishNumber()
  409. ->finishObject())
  410. ->finishArray())
  411. ->finishObject())
  412. ->finishArray();
  413. typeFilterValidator->release();
  414. operatorFilterValidator->release();
  415. outputValidator->release();
  416. return validator;
  417. }