RecipieLoader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. shapedLists.leeren();
  19. lists.leeren();
  20. std::cout << "loading recipies from '" << path << "'" << std::endl;
  21. Framework::Datei d;
  22. d.setDatei(path);
  23. if (d.istOrdner())
  24. {
  25. Framework::RCArray<Framework::Text>* files = d.getDateiListe();
  26. for (Framework::Text* f : *files)
  27. loadRecipies(Framework::Text(path) + "/" + *f);
  28. files->release();
  29. }
  30. else
  31. {
  32. JSONValue* json = loadJSONFromFile(path);
  33. JSONValidator* validator = zRecipieValidator();
  34. JSONValue* valid = validator->getValidParts(json);
  35. json->release();
  36. if (valid)
  37. {
  38. for (JSONValue* recipie : *valid->asArray())
  39. loadRecipie(recipie->asObject());
  40. valid->release();
  41. }
  42. }
  43. }
  44. void RecipieLoader::loadRecipie(JSONObject* zRecipie)
  45. {
  46. Framework::Text group = zRecipie->zValue("group")->asString()->getString();
  47. if (zRecipie->zValue("type")->asString()->getString().istGleich("shaped"))
  48. {
  49. int width = (int)zRecipie->zValue("width")->asNumber()->getNumber();
  50. int height = (int)zRecipie->zValue("height")->asNumber()->getNumber();
  51. ShapedRecipie* recipie = new ShapedRecipie(width, height);
  52. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  53. {
  54. int x
  55. = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
  56. int y
  57. = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
  58. ItemFilter* filter
  59. = loadFilter(input->asObject()->zValue("filter")->asObject());
  60. CombinedItemModifier* resultingModifier
  61. = new CombinedItemModifier();
  62. for (JSONValue* modifier :
  63. *input->asObject()->zValue("modifiers")->asArray())
  64. resultingModifier->addModifier(
  65. loadModifier(modifier->asObject()));
  66. recipie->setIngredient(x, y, filter, resultingModifier);
  67. }
  68. int outputCount = (int)zRecipie->asObject()
  69. ->zValue("outputCount")
  70. ->asNumber()
  71. ->getNumber();
  72. Framework::Text outputType = zRecipie->asObject()
  73. ->zValue("output")
  74. ->asObject()
  75. ->zValue("itemType")
  76. ->asString()
  77. ->getString();
  78. Item* item = 0;
  79. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  80. {
  81. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  82. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  83. ->getName()
  84. .istGleich(outputType))
  85. {
  86. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
  87. ->createItem();
  88. break;
  89. }
  90. }
  91. recipie->setOutput(item, outputCount);
  92. if (!zShapedRecipieList(group)) registerShapedRecipieList(group);
  93. zShapedRecipieList(group)->addRecipie(recipie);
  94. }
  95. else if (zRecipie->zValue("type")->asString()->getString().istGleich(
  96. "unordered"))
  97. {
  98. Recipie* recipie = new Recipie();
  99. for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
  100. {
  101. int count = (int)input->asObject()
  102. ->zValue("count")
  103. ->asNumber()
  104. ->getNumber();
  105. ItemFilter* filter
  106. = loadFilter(input->asObject()->zValue("filter")->asObject());
  107. recipie->addIngredient(filter, count);
  108. }
  109. for (JSONValue* output : *zRecipie->zValue("outputs")->asArray())
  110. {
  111. int count = (int)output->asObject()
  112. ->zValue("count")
  113. ->asNumber()
  114. ->getNumber();
  115. Framework::Text outputType = output->asObject()
  116. ->zValue("item")
  117. ->asObject()
  118. ->zValue("itemType")
  119. ->asString()
  120. ->getString();
  121. Item* item = 0;
  122. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount();
  123. i++)
  124. {
  125. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  126. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  127. ->getName()
  128. .istGleich(outputType))
  129. {
  130. item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
  131. ->createItem();
  132. break;
  133. }
  134. }
  135. recipie->addOutput(item, count);
  136. }
  137. if (!zRecipieList(group)) registerRecipieList(group);
  138. zRecipieList(group)->addRecipie(recipie);
  139. }
  140. }
  141. ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
  142. {
  143. Framework::Text type = zFilter->zValue("itemType")->asString()->getString();
  144. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  145. {
  146. if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
  147. && StaticRegistry<ItemType>::INSTANCE.zElement(i)
  148. ->getName()
  149. .istGleich(type))
  150. return new TypeItemFilter(
  151. StaticRegistry<ItemType>::INSTANCE.zElement(i));
  152. }
  153. return 0;
  154. }
  155. RecipieList* RecipieLoader::zRecipieList(const char* name) const
  156. {
  157. for (RecipieList* l : lists)
  158. {
  159. if (l->getName().istGleich(name)) return l;
  160. }
  161. return 0;
  162. }
  163. ShapedRecipieList* RecipieLoader::zShapedRecipieList(const char* name) const
  164. {
  165. for (ShapedRecipieList* l : shapedLists)
  166. {
  167. if (l->getName().istGleich(name)) return l;
  168. }
  169. return 0;
  170. }
  171. void RecipieLoader::registerRecipieList(const char* name)
  172. {
  173. if (zRecipieList(name))
  174. throw new std::invalid_argument("the recipie list already exists");
  175. lists.add(new RecipieList(name));
  176. }
  177. void RecipieLoader::registerShapedRecipieList(const char* name)
  178. {
  179. if (zShapedRecipieList(name))
  180. throw new std::invalid_argument("the recipie list already exists");
  181. shapedLists.add(new ShapedRecipieList(name));
  182. }
  183. ItemModifier* RecipieLoader::loadModifier(
  184. Framework::JSON::JSONObject* zModifier)
  185. {
  186. return new AttributeItemModifier(
  187. zModifier->zValue("attribute")->asString()->getString(),
  188. zModifier->zValue("value")->asString()->getString());
  189. }
  190. JSONValidator* RecipieLoader::zRecipieValidator()
  191. {
  192. if (validator) return validator;
  193. Framework::RCArray<Framework::Text> itemTypes;
  194. for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
  195. {
  196. if (StaticRegistry<ItemType>::INSTANCE.zElement(i))
  197. {
  198. itemTypes.add(new Framework::Text(
  199. StaticRegistry<ItemType>::INSTANCE.zElement(i)
  200. ->getName()
  201. .getText()));
  202. }
  203. }
  204. JSONValidator* filterValidator = JSONValidator::buildForObject()
  205. ->withRequiredString("itemType")
  206. ->whichIsOneOf(itemTypes)
  207. ->finishString()
  208. ->finishObject();
  209. JSONValidator* outputValidator = JSONValidator::buildForObject()
  210. ->withRequiredString("itemType")
  211. ->whichIsOneOf(itemTypes)
  212. ->finishString()
  213. ->finishObject();
  214. validator
  215. = JSONValidator::buildForArray()
  216. ->typeSpecifiedByAttribute("type")
  217. ->removeInvalidEntries()
  218. ->addAcceptedTypeInArray(
  219. JSONValidator::buildForObject()
  220. ->withRequiredString("type")
  221. ->withExactMatch("shaped")
  222. ->finishString()
  223. ->withRequiredString("group")
  224. ->finishString()
  225. ->withRequiredNumber("width")
  226. ->whichIsGreaterThen(0)
  227. ->finishNumber()
  228. ->withRequiredNumber("height")
  229. ->whichIsGreaterThen(0)
  230. ->finishNumber()
  231. ->withRequiredAttribute("inputs",
  232. JSONValidator::buildForArray()
  233. ->withDefault(new JSONArray())
  234. ->addAcceptedTypeInArray(
  235. JSONValidator::buildForObject()
  236. ->withRequiredNumber("x")
  237. ->whichIsGreaterOrEqual(0)
  238. ->finishNumber()
  239. ->withRequiredNumber("y")
  240. ->whichIsGreaterOrEqual(0)
  241. ->finishNumber()
  242. ->withRequiredAttribute("filter",
  243. dynamic_cast<JSONValidator*>(
  244. filterValidator->getThis()))
  245. ->withRequiredAttribute("modifiers",
  246. JSONValidator::buildForArray()
  247. ->withDefault(Parser::getValue(
  248. "[{\"attribute\": \"hp\", "
  249. "\"value\": \"=0\"}]")
  250. ->asArray())
  251. ->addAcceptedTypeInArray(
  252. JSONValidator::
  253. buildForObject()
  254. ->withRequiredString(
  255. "attribute")
  256. ->finishString()
  257. ->withRequiredString(
  258. "value")
  259. ->finishString()
  260. ->finishObject())
  261. ->finishArray())
  262. ->finishObject())
  263. ->finishArray())
  264. ->withRequiredAttribute("output",
  265. dynamic_cast<JSONValidator*>(
  266. outputValidator->getThis()))
  267. ->withRequiredNumber("outputCount")
  268. ->withDefault(1)
  269. ->whichIsGreaterThen(0)
  270. ->finishNumber()
  271. ->finishObject())
  272. ->addAcceptedTypeInArray(
  273. JSONValidator::buildForObject()
  274. ->withRequiredString("type")
  275. ->withExactMatch("unordered")
  276. ->finishString()
  277. ->withRequiredString("group")
  278. ->finishString()
  279. ->withRequiredAttribute("inputs",
  280. JSONValidator::buildForArray()
  281. ->withDefault(new JSONArray())
  282. ->addAcceptedTypeInArray(
  283. JSONValidator::buildForObject()
  284. ->withRequiredNumber("count")
  285. ->withDefault(1)
  286. ->whichIsGreaterThen(0)
  287. ->finishNumber()
  288. ->withRequiredAttribute("filter",
  289. dynamic_cast<JSONValidator*>(
  290. filterValidator->getThis()))
  291. ->finishObject())
  292. ->finishArray())
  293. ->withRequiredAttribute("output",
  294. JSONValidator::buildForArray()
  295. ->addAcceptedTypeInArray(
  296. JSONValidator::buildForObject()
  297. ->withRequiredAttribute("item",
  298. dynamic_cast<JSONValidator*>(
  299. outputValidator->getThis()))
  300. ->withRequiredNumber("count")
  301. ->withDefault(1)
  302. ->whichIsGreaterThen(0)
  303. ->finishNumber()
  304. ->finishObject())
  305. ->finishArray())
  306. ->finishObject())
  307. ->finishArray();
  308. filterValidator->release();
  309. outputValidator->release();
  310. return validator;
  311. }