RecipieLoader.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. Framework::Text type = zFilter->zValue("itemType")->asString()->getString();
  148. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  149. {
  150. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  151. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  152. ->getName()
  153. .istGleich(type))
  154. return new TypeItemFilter(
  155. StaticRegistry<ItemType>::INSTANCE.zElement(i));
  156. }
  157. return 0;
  158. }
  159. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  160. {
  161. for (RecipieList* l : lists)
  162. {
  163. if (l->getName().istGleich(name)) return l;
  164. }
  165. return 0;
  166. }
  167. void RecipieLoader::registerRecipieList(const char* name)
  168. {
  169. if (zRecipieList(name))
  170. throw new std::invalid_argument("the recipie list already exists");
  171. lists.add(new RecipieList(name));
  172. }
  173. Framework::Text RecipieLoader::getCrafingUIML(ItemType* zTargetType)
  174. {
  175. Framework::Text result = "<dialog id=\"crafting_";
  176. result += zTargetType->getId();
  177. result += "\"><craftingRecipies>";
  178. for (RecipieList* list : lists)
  179. {
  180. Framework::RCArray<Recipie> recipies;
  181. list->findRecipies(zTargetType, recipies);
  182. if (recipies.getEintragAnzahl() > 0)
  183. {
  184. result += "<craftingRecipieGroup name=\"";
  185. result += list->getName();
  186. if (list->getName().istGleich("inventory"))
  187. {
  188. result += "\" itemIcon=\"";
  189. result += ItemTypeEnum::CRAFTING_TABLE;
  190. }
  191. result += "\">";
  192. for (Recipie* recipie : recipies)
  193. {
  194. result += recipie->getRecipieUIML();
  195. }
  196. result += "</craftingRecipieGroup>";
  197. }
  198. }
  199. result += "</craftingRecipies></dialog>";
  200. return result;
  201. }
  202. ItemModifier* RecipieLoader::loadModifier(
  203. Framework::JSON::JSONObject* zModifier)
  204. {
  205. return new AttributeItemModifier(
  206. zModifier->zValue("attribute")->asString()->getString(),
  207. zModifier->zValue("value")->asString()->getString());
  208. }
  209. JSONValidator* RecipieLoader::zRecipieValidator()
  210. {
  211. if (validator) return validator;
  212. Framework::RCArray<Framework::Text> itemTypes;
  213. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  214. {
  215. if (StaticRegistry<ItemType>::INSTANCE.zElement(i))
  216. {
  217. itemTypes.add(new Framework::Text(
  218. StaticRegistry<ItemType>::INSTANCE.zElement(i)
  219. ->getName()
  220. .getText()));
  221. }
  222. }
  223. JSONValidator* filterValidator = JSONValidator::buildForObject()
  224. ->withRequiredString("itemType")
  225. ->whichIsOneOf(itemTypes)
  226. ->finishString()
  227. ->finishObject();
  228. JSONValidator* outputValidator = JSONValidator::buildForObject()
  229. ->withRequiredString("itemType")
  230. ->whichIsOneOf(itemTypes)
  231. ->finishString()
  232. ->finishObject();
  233. validator
  234. = JSONValidator::buildForArray()
  235. ->typeSpecifiedByAttribute("type")
  236. ->removeInvalidEntries()
  237. ->addAcceptedTypeInArray(
  238. JSONValidator::buildForObject()
  239. ->withRequiredString("type")
  240. ->withExactMatch("shaped")
  241. ->finishString()
  242. ->withRequiredString("group")
  243. ->finishString()
  244. ->withRequiredNumber("width")
  245. ->whichIsGreaterThen(0)
  246. ->finishNumber()
  247. ->withRequiredNumber("height")
  248. ->whichIsGreaterThen(0)
  249. ->finishNumber()
  250. ->withRequiredAttribute("inputs",
  251. JSONValidator::buildForArray()
  252. ->withDefault(new JSONArray())
  253. ->addAcceptedTypeInArray(
  254. JSONValidator::buildForObject()
  255. ->withRequiredNumber("x")
  256. ->whichIsGreaterOrEqual(0)
  257. ->finishNumber()
  258. ->withRequiredNumber("y")
  259. ->whichIsGreaterOrEqual(0)
  260. ->finishNumber()
  261. ->withRequiredAttribute("filter",
  262. dynamic_cast<JSONValidator*>(
  263. filterValidator->getThis()))
  264. ->withRequiredAttribute("modifiers",
  265. JSONValidator::buildForArray()
  266. ->withDefault(Parser::getValue(
  267. "[{\"attribute\": \"hp\", "
  268. "\"value\": \"=0\"}]")
  269. ->asArray())
  270. ->addAcceptedTypeInArray(
  271. JSONValidator::
  272. buildForObject()
  273. ->withRequiredString(
  274. "attribute")
  275. ->finishString()
  276. ->withRequiredString(
  277. "value")
  278. ->finishString()
  279. ->finishObject())
  280. ->finishArray())
  281. ->finishObject())
  282. ->finishArray())
  283. ->withRequiredAttribute("output",
  284. dynamic_cast<JSONValidator*>(
  285. outputValidator->getThis()))
  286. ->withRequiredNumber("outputCount")
  287. ->withDefault(1)
  288. ->whichIsGreaterThen(0)
  289. ->finishNumber()
  290. ->finishObject())
  291. ->addAcceptedTypeInArray(
  292. JSONValidator::buildForObject()
  293. ->withRequiredString("type")
  294. ->withExactMatch("unordered")
  295. ->finishString()
  296. ->withRequiredString("group")
  297. ->finishString()
  298. ->withRequiredAttribute("inputs",
  299. JSONValidator::buildForArray()
  300. ->withDefault(new JSONArray())
  301. ->addAcceptedTypeInArray(
  302. JSONValidator::buildForObject()
  303. ->withRequiredNumber("count")
  304. ->withDefault(1)
  305. ->whichIsGreaterThen(0)
  306. ->finishNumber()
  307. ->withRequiredAttribute("filter",
  308. dynamic_cast<JSONValidator*>(
  309. filterValidator->getThis()))
  310. ->withRequiredAttribute("modifiers",
  311. JSONValidator::buildForArray()
  312. ->withDefault(Parser::getValue(
  313. "[{\"attribute\": \"hp\", "
  314. "\"value\": \"=0\"}]")
  315. ->asArray())
  316. ->addAcceptedTypeInArray(
  317. JSONValidator::
  318. buildForObject()
  319. ->withRequiredString(
  320. "attribute")
  321. ->finishString()
  322. ->withRequiredString(
  323. "value")
  324. ->finishString()
  325. ->finishObject())
  326. ->finishArray())
  327. ->finishObject())
  328. ->finishArray())
  329. ->withRequiredAttribute("output",
  330. JSONValidator::buildForArray()
  331. ->addAcceptedTypeInArray(
  332. JSONValidator::buildForObject()
  333. ->withRequiredAttribute("item",
  334. dynamic_cast<JSONValidator*>(
  335. outputValidator->getThis()))
  336. ->withRequiredNumber("count")
  337. ->withDefault(1)
  338. ->whichIsGreaterThen(0)
  339. ->finishNumber()
  340. ->finishObject())
  341. ->finishArray())
  342. ->finishObject())
  343. ->finishArray();
  344. filterValidator->release();
  345. outputValidator->release();
  346. return validator;
  347. }