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. 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->addOutput(item, outputCount);
  90. if (!zRecipieList(group)) registerRecipieList(group);
  91. zRecipieList(group)->addRecipie(recipie);
  92. }
  93. else if (zRecipie->zValue("type")->asString()->getString().istGleich(
  94. "unordered"))
  95. {
  96. UnshapedRecipie* recipie = new UnshapedRecipie();
  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. CombinedItemModifier* resultingModifier
  106. = new CombinedItemModifier();
  107. for (JSONValue* modifier :
  108. *input->asObject()->zValue("modifiers")->asArray())
  109. resultingModifier->addModifier(
  110. loadModifier(modifier->asObject()));
  111. recipie->addIngredient(filter, count, resultingModifier);
  112. }
  113. for (JSONValue* output : *zRecipie->zValue("outputs")->asArray())
  114. {
  115. int count = (int)output->asObject()
  116. ->zValue("count")
  117. ->asNumber()
  118. ->getNumber();
  119. Framework::Text outputType = output->asObject()
  120. ->zValue("item")
  121. ->asObject()
  122. ->zValue("itemType")
  123. ->asString()
  124. ->getString();
  125. Item* item = 0;
  126. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount();
  127. i++)
  128. {
  129. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  130. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  131. ->getName()
  132. .istGleich(outputType))
  133. {
  134. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
  135. ->createItem();
  136. break;
  137. }
  138. }
  139. recipie->addOutput(item, count);
  140. }
  141. if (!zRecipieList(group)) registerRecipieList(group);
  142. zRecipieList(group)->addRecipie(recipie);
  143. }
  144. }
  145. ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
  146. {
  147. if (zFilter->hasValue("itemType"))
  148. {
  149. Framework::Text type
  150. = zFilter->zValue("itemType")->asString()->getString();
  151. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  152. {
  153. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  154. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  155. ->getName()
  156. .istGleich(type))
  157. return new TypeItemFilter(
  158. StaticRegistry<ItemType>::INSTANCE.zElement(i));
  159. }
  160. }
  161. else if (zFilter->hasValue("operator"))
  162. {
  163. Framework::Text op = zFilter->zValue("operator")
  164. ->asString()
  165. ->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. }