123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- #include "RecipieLoader.h"
- #include <Datei.h>
- #include <iostream>
- #include <stdexcept>
- #include "ItemType.h"
- using namespace Framework::JSON;
- using namespace Validator;
- RecipieLoader::RecipieLoader()
- : Framework::ReferenceCounter(),
- validator(0)
- {}
- RecipieLoader::~RecipieLoader()
- {
- if (validator) validator->release();
- }
- void RecipieLoader::loadRecipies(const char* path)
- {
- shapedLists.leeren();
- lists.leeren();
- std::cout << "loading recipies from '" << path << "'" << std::endl;
- Framework::Datei d;
- d.setDatei(path);
- if (d.istOrdner())
- {
- Framework::RCArray<Framework::Text>* files = d.getDateiListe();
- for (Framework::Text* f : *files)
- loadRecipies(Framework::Text(path) + "/" + *f);
- files->release();
- }
- else
- {
- JSONValue* json = loadJSONFromFile(path);
- JSONValidator* validator = zRecipieValidator();
- JSONValue* valid = validator->getValidParts(json);
- json->release();
- if (valid)
- {
- for (JSONValue* recipie : *valid->asArray())
- loadRecipie(recipie->asObject());
- valid->release();
- }
- }
- }
- void RecipieLoader::loadRecipie(JSONObject* zRecipie)
- {
- Framework::Text group = zRecipie->zValue("group")->asString()->getString();
- if (zRecipie->zValue("type")->asString()->getString().istGleich("shaped"))
- {
- int width = (int)zRecipie->zValue("width")->asNumber()->getNumber();
- int height = (int)zRecipie->zValue("height")->asNumber()->getNumber();
- ShapedRecipie* recipie = new ShapedRecipie(width, height);
- for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
- {
- int x
- = (int)input->asObject()->zValue("x")->asNumber()->getNumber();
- int y
- = (int)input->asObject()->zValue("y")->asNumber()->getNumber();
- ItemFilter* filter
- = loadFilter(input->asObject()->zValue("filter")->asObject());
- CombinedItemModifier* resultingModifier
- = new CombinedItemModifier();
- for (JSONValue* modifier :
- *input->asObject()->zValue("modifiers")->asArray())
- resultingModifier->addModifier(
- loadModifier(modifier->asObject()));
- recipie->setIngredient(x, y, filter, resultingModifier);
- }
- int outputCount = (int)zRecipie->asObject()
- ->zValue("outputCount")
- ->asNumber()
- ->getNumber();
- Framework::Text outputType = zRecipie->asObject()
- ->zValue("output")
- ->asObject()
- ->zValue("itemType")
- ->asString()
- ->getString();
- Item* item = 0;
- for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
- {
- if (StaticRegistry<ItemType>::INSTANCE
- .zElement(i) && StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->getName()
- .istGleich(outputType))
- {
- item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->createItem();
- break;
- }
- }
- recipie->setOutput(item, outputCount);
- if (!zShapedRecipieList(group)) registerShapedRecipieList(group);
- zShapedRecipieList(group)->addRecipie(recipie);
- }
- else if (zRecipie->zValue("type")->asString()->getString().istGleich(
- "unordered"))
- {
- Recipie* recipie = new Recipie();
- for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
- {
- int count = (int)input->asObject()
- ->zValue("count")
- ->asNumber()
- ->getNumber();
- ItemFilter* filter
- = loadFilter(input->asObject()->zValue("filter")->asObject());
- recipie->addIngredient(filter, count);
- }
- for (JSONValue* output : *zRecipie->zValue("outputs")->asArray())
- {
- int count = (int)output->asObject()
- ->zValue("count")
- ->asNumber()
- ->getNumber();
- Framework::Text outputType = output->asObject()
- ->zValue("item")
- ->asObject()
- ->zValue("itemType")
- ->asString()
- ->getString();
- Item* item = 0;
- for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount();
- i++)
- {
- if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
- && StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->getName()
- .istGleich(outputType))
- {
- item = StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->createItem();
- break;
- }
- }
- recipie->addOutput(item, count);
- }
- if (!zRecipieList(group)) registerRecipieList(group);
- zRecipieList(group)->addRecipie(recipie);
- }
- }
- ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
- {
- Framework::Text type = zFilter->zValue("itemType")->asString()->getString();
- for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
- {
- if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
- && StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->getName()
- .istGleich(type))
- return new TypeItemFilter(
- StaticRegistry<ItemType>::INSTANCE.zElement(i));
- }
- return 0;
- }
- RecipieList* RecipieLoader::zRecipieList(const char* name) const
- {
- for (RecipieList* l : lists)
- {
- if (l->getName().istGleich(name)) return l;
- }
- return 0;
- }
- ShapedRecipieList* RecipieLoader::zShapedRecipieList(const char* name) const
- {
- for (ShapedRecipieList* l : shapedLists)
- {
- if (l->getName().istGleich(name)) return l;
- }
- return 0;
- }
- void RecipieLoader::registerRecipieList(const char* name)
- {
- if (zRecipieList(name))
- throw new std::invalid_argument("the recipie list already exists");
- lists.add(new RecipieList(name));
- }
- void RecipieLoader::registerShapedRecipieList(const char* name)
- {
- if (zShapedRecipieList(name))
- throw new std::invalid_argument("the recipie list already exists");
- shapedLists.add(new ShapedRecipieList(name));
- }
- ItemModifier* RecipieLoader::loadModifier(
- Framework::JSON::JSONObject* zModifier)
- {
- return new AttributeItemModifier(
- zModifier->zValue("attribute")->asString()->getString(),
- zModifier->zValue("value")->asString()->getString());
- }
- JSONValidator* RecipieLoader::zRecipieValidator()
- {
- if (validator) return validator;
- Framework::RCArray<Framework::Text> itemTypes;
- for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
- {
- if (StaticRegistry<ItemType>::INSTANCE.zElement(i))
- {
- itemTypes.add(new Framework::Text(
- StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->getName()
- .getText()));
- }
- }
- JSONValidator* filterValidator = JSONValidator::buildForObject()
- ->withRequiredString("itemType")
- ->whichIsOneOf(itemTypes)
- ->finishString()
- ->finishObject();
- JSONValidator* outputValidator = JSONValidator::buildForObject()
- ->withRequiredString("itemType")
- ->whichIsOneOf(itemTypes)
- ->finishString()
- ->finishObject();
- validator
- = JSONValidator::buildForArray()
- ->typeSpecifiedByAttribute("type")
- ->removeInvalidEntries()
- ->addAcceptedTypeInArray(
- JSONValidator::buildForObject()
- ->withRequiredString("type")
- ->withExactMatch("shaped")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredNumber("width")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredNumber("height")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredAttribute("inputs",
- JSONValidator::buildForArray()
- ->withDefault(new JSONArray())
- ->addAcceptedTypeInArray(
- JSONValidator::buildForObject()
- ->withRequiredNumber("x")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredNumber("y")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredAttribute("filter",
- dynamic_cast<JSONValidator*>(
- filterValidator->getThis()))
- ->withRequiredAttribute("modifiers",
- JSONValidator::buildForArray()
- ->withDefault(Parser::getValue(
- "[{\"attribute\": \"hp\", "
- "\"value\": \"=0\"}]")
- ->asArray())
- ->addAcceptedTypeInArray(
- JSONValidator::
- buildForObject()
- ->withRequiredString(
- "attribute")
- ->finishString()
- ->withRequiredString(
- "value")
- ->finishString()
- ->finishObject())
- ->finishArray())
- ->finishObject())
- ->finishArray())
- ->withRequiredAttribute("output",
- dynamic_cast<JSONValidator*>(
- outputValidator->getThis()))
- ->withRequiredNumber("outputCount")
- ->withDefault(1)
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject())
- ->addAcceptedTypeInArray(
- JSONValidator::buildForObject()
- ->withRequiredString("type")
- ->withExactMatch("unordered")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredAttribute("inputs",
- JSONValidator::buildForArray()
- ->withDefault(new JSONArray())
- ->addAcceptedTypeInArray(
- JSONValidator::buildForObject()
- ->withRequiredNumber("count")
- ->withDefault(1)
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredAttribute("filter",
- dynamic_cast<JSONValidator*>(
- filterValidator->getThis()))
- ->finishObject())
- ->finishArray())
- ->withRequiredAttribute("output",
- JSONValidator::buildForArray()
- ->addAcceptedTypeInArray(
- JSONValidator::buildForObject()
- ->withRequiredAttribute("item",
- dynamic_cast<JSONValidator*>(
- outputValidator->getThis()))
- ->withRequiredNumber("count")
- ->withDefault(1)
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject())
- ->finishArray())
- ->finishObject())
- ->finishArray();
- filterValidator->release();
- outputValidator->release();
- return validator;
- }
|