RecipieLoader.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 += zTargetType->getId();
  214. result += "\"><craftingRecipies>";
  215. for (RecipieList* list : lists)
  216. {
  217. Framework::RCArray<Recipie> recipies;
  218. list->findRecipies(zTargetType, recipies);
  219. if (recipies.getEintragAnzahl() > 0)
  220. {
  221. result += "<craftingRecipieGroup name=\"";
  222. result += list->getName();
  223. if (list->getName().istGleich("inventory"))
  224. {
  225. result += "\" itemIcon=\"";
  226. result += ItemTypeEnum::CRAFTING_TABLE;
  227. }
  228. result += "\">";
  229. for (Recipie* recipie : recipies)
  230. {
  231. result += recipie->getRecipieUIML();
  232. }
  233. result += "</craftingRecipieGroup>";
  234. }
  235. }
  236. result += "</craftingRecipies></dialog>";
  237. return result;
  238. }
  239. ItemModifier* RecipieLoader::loadModifier(
  240. Framework::JSON::JSONObject* zModifier)
  241. {
  242. return new AttributeItemModifier(
  243. zModifier->zValue("attribute")->asString()->getString(),
  244. zModifier->zValue("value")->asString()->getString());
  245. }
  246. JSONValidator* RecipieLoader::zRecipieValidator()
  247. {
  248. if (validator) return validator;
  249. Framework::RCArray<Framework::Text> itemTypes;
  250. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  251. {
  252. if (StaticRegistry<ItemType>::INSTANCE.zElement(i))
  253. {
  254. itemTypes.add(new Framework::Text(
  255. StaticRegistry<ItemType>::INSTANCE.zElement(i)
  256. ->getName()
  257. .getText()));
  258. }
  259. }
  260. JSONValidator* typeFilterValidator
  261. = JSONValidator::buildForObject()
  262. ->setObjectReferenceId("typeFilter")
  263. ->withRequiredString("itemType")
  264. ->whichIsOneOf(itemTypes)
  265. ->finishString()
  266. ->finishObject();
  267. Framework::RCArray<Framework::Text> operators;
  268. operators.add(new Framework::Text("=="));
  269. operators.add(new Framework::Text("!="));
  270. operators.add(new Framework::Text("&&"));
  271. operators.add(new Framework::Text("||"));
  272. JSONValidator* operatorFilterValidator
  273. = JSONValidator::buildForObject()
  274. ->setObjectReferenceId("operatorFilter")
  275. ->withRequiredString("operator")
  276. ->whichIsOneOf(operators)
  277. ->finishString()
  278. ->withRequiredAttribute(
  279. "left", JSONValidator::buildForObjectReference("typeFilter"))
  280. ->withRequiredAttribute("left",
  281. JSONValidator::buildForObjectReference("operatorFilter"))
  282. ->withRequiredAttribute(
  283. "right", JSONValidator::buildForObjectReference("typeFilter"))
  284. ->withRequiredAttribute("right",
  285. JSONValidator::buildForObjectReference("operatorFilter"))
  286. ->finishObject();
  287. JSONValidator* outputValidator = JSONValidator::buildForObject()
  288. ->withRequiredString("itemType")
  289. ->whichIsOneOf(itemTypes)
  290. ->finishString()
  291. ->finishObject();
  292. validator
  293. = JSONValidator::buildForArray()
  294. ->typeSpecifiedByAttribute("type")
  295. ->removeInvalidEntries()
  296. ->addAcceptedTypeInArray(
  297. JSONValidator::buildForObject()
  298. ->withRequiredString("type")
  299. ->withExactMatch("shaped")
  300. ->finishString()
  301. ->withRequiredString("group")
  302. ->finishString()
  303. ->withRequiredNumber("width")
  304. ->whichIsGreaterThen(0)
  305. ->finishNumber()
  306. ->withRequiredNumber("height")
  307. ->whichIsGreaterThen(0)
  308. ->finishNumber()
  309. ->withRequiredAttribute("inputs",
  310. JSONValidator::buildForArray()
  311. ->withDefault(new JSONArray())
  312. ->addAcceptedTypeInArray(
  313. JSONValidator::buildForObject()
  314. ->withRequiredNumber("x")
  315. ->whichIsGreaterOrEqual(0)
  316. ->finishNumber()
  317. ->withRequiredNumber("y")
  318. ->whichIsGreaterOrEqual(0)
  319. ->finishNumber()
  320. ->withRequiredAttribute("filter",
  321. dynamic_cast<JSONValidator*>(
  322. typeFilterValidator->getThis()))
  323. ->withRequiredAttribute("filter",
  324. dynamic_cast<JSONValidator*>(
  325. operatorFilterValidator
  326. ->getThis()))
  327. ->withRequiredAttribute("modifiers",
  328. JSONValidator::buildForArray()
  329. ->withDefault(Parser::getValue(
  330. "[{\"attribute\": \"hp\", "
  331. "\"value\": \"=0\"}]")
  332. ->asArray())
  333. ->addAcceptedTypeInArray(
  334. JSONValidator::
  335. buildForObject()
  336. ->withRequiredString(
  337. "attribute")
  338. ->finishString()
  339. ->withRequiredString(
  340. "value")
  341. ->finishString()
  342. ->finishObject())
  343. ->finishArray())
  344. ->finishObject())
  345. ->finishArray())
  346. ->withRequiredAttribute("output",
  347. dynamic_cast<JSONValidator*>(
  348. outputValidator->getThis()))
  349. ->withRequiredNumber("outputCount")
  350. ->withDefault(1)
  351. ->whichIsGreaterThen(0)
  352. ->finishNumber()
  353. ->finishObject())
  354. ->addAcceptedTypeInArray(
  355. JSONValidator::buildForObject()
  356. ->withRequiredString("type")
  357. ->withExactMatch("unordered")
  358. ->finishString()
  359. ->withRequiredString("group")
  360. ->finishString()
  361. ->withRequiredAttribute("inputs",
  362. JSONValidator::buildForArray()
  363. ->withDefault(new JSONArray())
  364. ->addAcceptedTypeInArray(
  365. JSONValidator::buildForObject()
  366. ->withRequiredNumber("count")
  367. ->withDefault(1)
  368. ->whichIsGreaterThen(0)
  369. ->finishNumber()
  370. ->withRequiredAttribute("filter",
  371. dynamic_cast<JSONValidator*>(
  372. typeFilterValidator->getThis()))
  373. ->withRequiredAttribute("filter",
  374. dynamic_cast<JSONValidator*>(
  375. operatorFilterValidator
  376. ->getThis()))
  377. ->withRequiredAttribute("modifiers",
  378. JSONValidator::buildForArray()
  379. ->withDefault(Parser::getValue(
  380. "[{\"attribute\": \"hp\", "
  381. "\"value\": \"=0\"}]")
  382. ->asArray())
  383. ->addAcceptedTypeInArray(
  384. JSONValidator::
  385. buildForObject()
  386. ->withRequiredString(
  387. "attribute")
  388. ->finishString()
  389. ->withRequiredString(
  390. "value")
  391. ->finishString()
  392. ->finishObject())
  393. ->finishArray())
  394. ->finishObject())
  395. ->finishArray())
  396. ->withRequiredAttribute("output",
  397. JSONValidator::buildForArray()
  398. ->addAcceptedTypeInArray(
  399. JSONValidator::buildForObject()
  400. ->withRequiredAttribute("item",
  401. dynamic_cast<JSONValidator*>(
  402. outputValidator->getThis()))
  403. ->withRequiredNumber("count")
  404. ->withDefault(1)
  405. ->whichIsGreaterThen(0)
  406. ->finishNumber()
  407. ->finishObject())
  408. ->finishArray())
  409. ->finishObject())
  410. ->finishArray();
  411. typeFilterValidator->release();
  412. operatorFilterValidator->release();
  413. outputValidator->release();
  414. return validator;
  415. }