#include "RecipieLoader.h" #include #include #include #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* 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::INSTANCE.getCount(); i++) { if (StaticRegistry::INSTANCE.zElement(i) && StaticRegistry::INSTANCE.zElement(i) ->getName() .istGleich(outputType)) { item = StaticRegistry::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::INSTANCE.getCount(); i++) { if (StaticRegistry::INSTANCE.zElement(i) && StaticRegistry::INSTANCE.zElement(i) ->getName() .istGleich(outputType)) { item = StaticRegistry::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::INSTANCE.getCount(); i++) { if (StaticRegistry::INSTANCE.zElement(i) && StaticRegistry::INSTANCE.zElement(i) ->getName() .istGleich(type)) return new TypeItemFilter( StaticRegistry::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; } 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 = ""; for (RecipieList* list : lists) { Framework::RCArray recipies; list->findRecipies(zTargetType, recipies); if (recipies.getEintragAnzahl() > 0) { result += "getName(); result += "\">"; for (Recipie* recipie : recipies) { result += recipie->getRecipieUIML(); } result += ""; } } result += ""; 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 itemTypes; for (int i = 0; i < StaticRegistry::INSTANCE.getCount(); i++) { if (StaticRegistry::INSTANCE.zElement(i)) { itemTypes.add(new Framework::Text( StaticRegistry::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( 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( 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( 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", JSONValidator::buildForArray() ->addAcceptedTypeInArray( JSONValidator::buildForObject() ->withRequiredAttribute("item", dynamic_cast( outputValidator->getThis())) ->withRequiredNumber("count") ->withDefault(1) ->whichIsGreaterThen(0) ->finishNumber() ->finishObject()) ->finishArray()) ->finishObject()) ->finishArray(); filterValidator->release(); outputValidator->release(); return validator; }