RecipieLoader.cpp 18 KB

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