123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include "RecipieLoader.h"
- #include <Datei.h>
- #include <iostream>
- #include <stdexcept>
- #include "Game.h"
- #include "ItemType.h"
- #include "JsonUtils.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)
- {
- loadAllJsonsFromDirectory(
- path, [this](JSONValue* zJson, Framework::Text path) {
- std::cout << "loading recipies from '" << path << "'" << std::endl;
- JSONValidator* validator
- = Framework::JSON::Validator::JSONValidator::buildForArray()
- ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
- ->getValidator<Recipie>())
- ->removeInvalidEntries()
- ->finishArray();
- Framework::RCArray<JSONValidationResult> invalidParts;
- JSONValue* valid = validator->getValidParts(zJson, &invalidParts);
- for (JSONValidationResult* invalidPart : invalidParts)
- {
- invalidPart->printInvalidInfo();
- }
- int count = 0;
- if (valid)
- {
- for (JSONValue* value : *valid->asArray())
- {
- Recipie* recipie
- = Game::INSTANCE->zTypeRegistry()->fromJson<Recipie>(
- value);
- if (recipie)
- {
- Framework::Text group = recipie->getGroupName();
- RecipieList* list = zRecipieList(group);
- if (!list)
- {
- registerRecipieList(group);
- list = zRecipieList(group);
- }
- list->addRecipie(recipie);
- count++;
- }
- }
- }
- std::cout << count << " recipies were loaded.\n";
- });
- }
- 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(int itemTypeId)
- {
- Framework::Text result = "<dialog id=\"crafting_";
- result.append() << itemTypeId << "\"><craftingRecipies>";
- for (RecipieList* list : lists)
- {
- Framework::RCArray<Recipie> recipies;
- list->findRecipies(itemTypeId, recipies);
- if (recipies.getEintragAnzahl() > 0)
- {
- result.append()
- << "<craftingRecipieGroup name=\"" << list->getName();
- if (list->getName().istGleich("inventory"))
- {
- result.append()
- << "\" itemIcon=\""
- << Game::INSTANCE->getItemTypeId("Crafting Table");
- }
- result += "\">";
- for (Recipie* recipie : recipies)
- {
- result += recipie->getRecipieUIML();
- }
- result += "</craftingRecipieGroup>";
- }
- }
- result += "</craftingRecipies></dialog>";
- return result;
- }
|