123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #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)
- {
- 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();
- int count = 0;
- if (valid)
- {
- for (JSONValue* recipie : *valid->asArray())
- loadRecipie(recipie->asObject());
- count = valid->asArray()->getLength();
- valid->release();
- }
- std::cout << count << " recipies were loaded.\n";
- }
- }
- 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->addOutput(item, outputCount);
- if (!zRecipieList(group)) registerRecipieList(group);
- zRecipieList(group)->addRecipie(recipie);
- }
- else if (zRecipie->zValue("type")->asString()->getString().istGleich(
- "unordered"))
- {
- UnshapedRecipie* recipie = new UnshapedRecipie();
- for (JSONValue* input : *zRecipie->zValue("inputs")->asArray())
- {
- int count = (int)input->asObject()
- ->zValue("count")
- ->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->addIngredient(filter, count, resultingModifier);
- }
- 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)
- {
- if (zFilter->hasValue("itemType"))
- {
- 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));
- }
- }
- else if (zFilter->hasValue("operator"))
- {
- Framework::Text op = zFilter->zValue("operator")
- ->asString()
- ->getString();
- ItemFilter* left = loadFilter(zFilter->zValue("left")->asObject());
- ItemFilter* right = loadFilter(zFilter->zValue("right")->asObject());
- if (op.istGleich("&&"))
- {
- return new CombinedItemFilter(
- left, right, [](bool a, bool b) { return a && b; });
- }
- else if (op.istGleich("||"))
- {
- return new CombinedItemFilter(
- left, right, [](bool a, bool b) { return a || b; });
- }
- else if (op.istGleich("=="))
- {
- return new CombinedItemFilter(
- left, right, [](bool a, bool b) { return a == b; });
- }
- else if (op.istGleich("!="))
- {
- return new CombinedItemFilter(
- left, right, [](bool a, bool b) { return a != b; });
- }
- else
- {
- left->release();
- right->release();
- }
- }
- return 0;
- }
- RecipieList* RecipieLoader::zRecipieList(const char* name) const
- {
- for (RecipieList* l : lists)
- {
- 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));
- }
- Framework::Text RecipieLoader::getCrafingUIML(ItemType* zTargetType)
- {
- Framework::Text result = "<dialog id=\"crafting_";
- result.append() << zTargetType->getId() << "\"><craftingRecipies>";
- for (RecipieList* list : lists)
- {
- Framework::RCArray<Recipie> recipies;
- list->findRecipies(zTargetType, recipies);
- if (recipies.getEintragAnzahl() > 0)
- {
- result.append()
- << "<craftingRecipieGroup name=\"" << list->getName();
- if (list->getName().istGleich("inventory"))
- {
- result.append()
- << "\" itemIcon=\"" << ItemTypeEnum::CRAFTING_TABLE;
- }
- result += "\">";
- for (Recipie* recipie : recipies)
- {
- result += recipie->getRecipieUIML();
- }
- result += "</craftingRecipieGroup>";
- }
- }
- result += "</craftingRecipies></dialog>";
- return result;
- }
- 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* typeFilterValidator
- = JSONValidator::buildForObject()
- ->setObjectReferenceId("typeFilter")
- ->withRequiredString("itemType")
- ->whichIsOneOf(itemTypes)
- ->finishString()
- ->finishObject();
- Framework::RCArray<Framework::Text> operators;
- operators.add(new Framework::Text("=="));
- operators.add(new Framework::Text("!="));
- operators.add(new Framework::Text("&&"));
- operators.add(new Framework::Text("||"));
- JSONValidator* operatorFilterValidator
- = JSONValidator::buildForObject()
- ->setObjectReferenceId("operatorFilter")
- ->withRequiredString("operator")
- ->whichIsOneOf(operators)
- ->finishString()
- ->withRequiredAttribute(
- "left", JSONValidator::buildForObjectReference("typeFilter"))
- ->withRequiredAttribute("left",
- JSONValidator::buildForObjectReference("operatorFilter"))
- ->withRequiredAttribute(
- "right", JSONValidator::buildForObjectReference("typeFilter"))
- ->withRequiredAttribute("right",
- JSONValidator::buildForObjectReference("operatorFilter"))
- ->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*>(
- typeFilterValidator->getThis()))
- ->withRequiredAttribute("filter",
- dynamic_cast<JSONValidator*>(
- operatorFilterValidator
- ->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*>(
- typeFilterValidator->getThis()))
- ->withRequiredAttribute("filter",
- dynamic_cast<JSONValidator*>(
- operatorFilterValidator
- ->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",
- JSONValidator::buildForArray()
- ->addAcceptedTypeInArray(
- JSONValidator::buildForObject()
- ->withRequiredAttribute("item",
- dynamic_cast<JSONValidator*>(
- outputValidator->getThis()))
- ->withRequiredNumber("count")
- ->withDefault(1)
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject())
- ->finishArray())
- ->finishObject())
- ->finishArray();
- typeFilterValidator->release();
- operatorFilterValidator->release();
- outputValidator->release();
- return validator;
- }
|