RecipieLoader.cpp 15 KB

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