1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
|
/*
* JsonNode.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "JsonNode.h"
#include "ScopeGuard.h"
#include "HeroBonus.h"
#include "filesystem/Filesystem.h"
#include "VCMI_Lib.h" //for identifier resolution
#include "CModHandler.h"
#include "CGeneralTextHandler.h"
#include "JsonDetail.h"
using namespace JsonDetail;
class LibClasses;
class CModHandler;
static const JsonNode nullNode;
std::ostream & operator<<(std::ostream &out, const JsonNode &node)
{
JsonWriter writer(out, node);
return out << "\n";
}
JsonNode::JsonNode(JsonType Type):
type(DATA_NULL)
{
setType(Type);
}
JsonNode::JsonNode(const char *data, size_t datasize):
type(DATA_NULL)
{
JsonParser parser(data, datasize);
*this = parser.parse("<unknown>");
}
JsonNode::JsonNode(ResourceID && fileURI):
type(DATA_NULL)
{
auto file = CResourceHandler::get()->load(fileURI)->readAll();
JsonParser parser(reinterpret_cast<char*>(file.first.get()), file.second);
*this = parser.parse(fileURI.getName());
}
JsonNode::JsonNode(const ResourceID & fileURI):
type(DATA_NULL)
{
auto file = CResourceHandler::get()->load(fileURI)->readAll();
JsonParser parser(reinterpret_cast<char*>(file.first.get()), file.second);
*this = parser.parse(fileURI.getName());
}
JsonNode::JsonNode(ResourceID && fileURI, bool &isValidSyntax):
type(DATA_NULL)
{
auto file = CResourceHandler::get()->load(fileURI)->readAll();
JsonParser parser(reinterpret_cast<char*>(file.first.get()), file.second);
*this = parser.parse(fileURI.getName());
isValidSyntax = parser.isValid();
}
JsonNode::JsonNode(const JsonNode ©):
type(DATA_NULL),
meta(copy.meta)
{
setType(copy.getType());
switch(type)
{
break; case DATA_NULL:
break; case DATA_BOOL: Bool() = copy.Bool();
break; case DATA_FLOAT: Float() = copy.Float();
break; case DATA_STRING: String() = copy.String();
break; case DATA_VECTOR: Vector() = copy.Vector();
break; case DATA_STRUCT: Struct() = copy.Struct();
}
}
JsonNode::~JsonNode()
{
setType(DATA_NULL);
}
void JsonNode::swap(JsonNode &b)
{
using std::swap;
swap(meta, b.meta);
swap(data, b.data);
swap(type, b.type);
}
JsonNode & JsonNode::operator =(JsonNode node)
{
swap(node);
return *this;
}
bool JsonNode::operator == (const JsonNode &other) const
{
if (getType() == other.getType())
{
switch(type)
{
case DATA_NULL: return true;
case DATA_BOOL: return Bool() == other.Bool();
case DATA_FLOAT: return Float() == other.Float();
case DATA_STRING: return String() == other.String();
case DATA_VECTOR: return Vector() == other.Vector();
case DATA_STRUCT: return Struct() == other.Struct();
}
}
return false;
}
bool JsonNode::operator != (const JsonNode &other) const
{
return !(*this == other);
}
JsonNode::JsonType JsonNode::getType() const
{
return type;
}
void JsonNode::setMeta(std::string metadata, bool recursive)
{
meta = metadata;
if (recursive)
{
switch (type)
{
break; case DATA_VECTOR:
{
for(auto & node : Vector())
{
node.setMeta(metadata);
}
}
break; case DATA_STRUCT:
{
for(auto & node : Struct())
{
node.second.setMeta(metadata);
}
}
}
}
}
void JsonNode::setType(JsonType Type)
{
if (type == Type)
return;
//Reset node to nullptr
if (Type != DATA_NULL)
setType(DATA_NULL);
switch (type)
{
break; case DATA_STRING: delete data.String;
break; case DATA_VECTOR: delete data.Vector;
break; case DATA_STRUCT: delete data.Struct;
break; default:
break;
}
//Set new node type
type = Type;
switch(type)
{
break; case DATA_NULL:
break; case DATA_BOOL: data.Bool = false;
break; case DATA_FLOAT: data.Float = 0;
break; case DATA_STRING: data.String = new std::string();
break; case DATA_VECTOR: data.Vector = new JsonVector();
break; case DATA_STRUCT: data.Struct = new JsonMap();
}
}
bool JsonNode::isNull() const
{
return type == DATA_NULL;
}
void JsonNode::clear()
{
setType(DATA_NULL);
}
bool & JsonNode::Bool()
{
setType(DATA_BOOL);
return data.Bool;
}
double & JsonNode::Float()
{
setType(DATA_FLOAT);
return data.Float;
}
std::string & JsonNode::String()
{
setType(DATA_STRING);
return *data.String;
}
JsonVector & JsonNode::Vector()
{
setType(DATA_VECTOR);
return *data.Vector;
}
JsonMap & JsonNode::Struct()
{
setType(DATA_STRUCT);
return *data.Struct;
}
const bool boolDefault = false;
const bool & JsonNode::Bool() const
{
if (type == DATA_NULL)
return boolDefault;
assert(type == DATA_BOOL);
return data.Bool;
}
const double floatDefault = 0;
const double & JsonNode::Float() const
{
if (type == DATA_NULL)
return floatDefault;
assert(type == DATA_FLOAT);
return data.Float;
}
const std::string stringDefault = std::string();
const std::string & JsonNode::String() const
{
if (type == DATA_NULL)
return stringDefault;
assert(type == DATA_STRING);
return *data.String;
}
const JsonVector vectorDefault = JsonVector();
const JsonVector & JsonNode::Vector() const
{
if (type == DATA_NULL)
return vectorDefault;
assert(type == DATA_VECTOR);
return *data.Vector;
}
const JsonMap mapDefault = JsonMap();
const JsonMap & JsonNode::Struct() const
{
if (type == DATA_NULL)
return mapDefault;
assert(type == DATA_STRUCT);
return *data.Struct;
}
JsonNode & JsonNode::operator[](std::string child)
{
return Struct()[child];
}
const JsonNode & JsonNode::operator[](std::string child) const
{
auto it = Struct().find(child);
if (it != Struct().end())
return it->second;
return nullNode;
}
// to avoid duplicating const and non-const code
template<typename Node>
Node & resolvePointer(Node & in, const std::string & pointer)
{
if (pointer.empty())
return in;
assert(pointer[0] == '/');
size_t splitPos = pointer.find('/', 1);
std::string entry = pointer.substr(1, splitPos -1);
std::string remainer = splitPos == std::string::npos ? "" : pointer.substr(splitPos);
if (in.getType() == JsonNode::DATA_VECTOR)
{
if (entry.find_first_not_of("0123456789") != std::string::npos) // non-numbers in string
throw std::runtime_error("Invalid Json pointer");
if (entry.size() > 1 && entry[0] == '0') // leading zeros are not allowed
throw std::runtime_error("Invalid Json pointer");
size_t index = boost::lexical_cast<size_t>(entry);
if (in.Vector().size() > index)
return in.Vector()[index].resolvePointer(remainer);
}
return in[entry].resolvePointer(remainer);
}
const JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer) const
{
return ::resolvePointer(*this, jsonPointer);
}
JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer)
{
return ::resolvePointer(*this, jsonPointer);
}
///JsonUtils
void JsonUtils::parseTypedBonusShort(const JsonVector& source, std::shared_ptr<Bonus> dest)
{
dest->val = source[1].Float();
resolveIdentifier(source[2],dest->subtype);
dest->additionalInfo = source[3].Float();
dest->duration = Bonus::PERMANENT; //TODO: handle flags (as integer)
dest->turnsRemain = 0;
}
std::shared_ptr<Bonus> JsonUtils::parseBonus (const JsonVector &ability_vec) //TODO: merge with AddAbility, create universal parser for all bonus properties
{
auto b = std::make_shared<Bonus>();
std::string type = ability_vec[0].String();
auto it = bonusNameMap.find(type);
if (it == bonusNameMap.end())
{
logGlobal->errorStream() << "Error: invalid ability type " << type;
return b;
}
b->type = it->second;
parseTypedBonusShort(ability_vec, b);
return b;
}
template <typename T>
const T & parseByMap(const std::map<std::string, T> & map, const JsonNode * val, std::string err)
{
static T defaultValue = T();
if (!val->isNull())
{
std::string type = val->String();
auto it = map.find(type);
if (it == map.end())
{
logGlobal->errorStream() << "Error: invalid " << err << type;
return defaultValue;
}
else
{
return it->second;
}
}
else
return defaultValue;
}
void JsonUtils::resolveIdentifier(si32 &var, const JsonNode &node, std::string name)
{
const JsonNode &value = node[name];
if (!value.isNull())
{
switch (value.getType())
{
case JsonNode::DATA_FLOAT:
var = value.Float();
break;
case JsonNode::DATA_STRING:
VLC->modh->identifiers.requestIdentifier(value, [&](si32 identifier)
{
var = identifier;
});
break;
default:
logGlobal->errorStream() << "Error! Wrong identifier used for value of " << name;
}
}
}
void JsonUtils::resolveIdentifier(const JsonNode &node, si32 &var)
{
switch (node.getType())
{
case JsonNode::DATA_FLOAT:
var = node.Float();
break;
case JsonNode::DATA_STRING:
VLC->modh->identifiers.requestIdentifier(node, [&](si32 identifier)
{
var = identifier;
});
break;
default:
logGlobal->errorStream() << "Error! Wrong identifier used for identifier!";
}
}
std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonNode &ability)
{
auto b = std::make_shared<Bonus>();
if (!parseBonus(ability, b.get()))
{
return nullptr;
}
return b;
}
bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b)
{
const JsonNode *value;
std::string type = ability["type"].String();
auto it = bonusNameMap.find(type);
if (it == bonusNameMap.end())
{
logGlobal->errorStream() << "Error: invalid ability type " << type;
return false;
}
b->type = it->second;
resolveIdentifier(b->subtype, ability, "subtype");
b->val = ability["val"].Float();
value = &ability["valueType"];
if (!value->isNull())
b->valType = static_cast<Bonus::ValueType>(parseByMap(bonusValueMap, value, "value type "));
resolveIdentifier(b->additionalInfo, ability, "addInfo");
b->turnsRemain = ability["turns"].Float();
b->sid = ability["sourceID"].Float();
b->description = ability["description"].String();
value = &ability["effectRange"];
if (!value->isNull())
b->effectRange = static_cast<Bonus::LimitEffect>(parseByMap(bonusLimitEffect, value, "effect range "));
value = &ability["duration"];
if (!value->isNull())
{
switch (value->getType())
{
case JsonNode::DATA_STRING:
b->duration = parseByMap(bonusDurationMap, value, "duration type ");
break;
case JsonNode::DATA_VECTOR:
{
ui16 dur = 0;
for (const JsonNode & d : value->Vector())
{
dur |= parseByMap(bonusDurationMap, &d, "duration type ");
}
b->duration = dur;
}
break;
default:
logGlobal->errorStream() << "Error! Wrong bonus duration format.";
}
}
value = &ability["source"];
if (!value->isNull())
b->source = static_cast<Bonus::BonusSource>(parseByMap(bonusSourceMap, value, "source type "));
value = &ability["limiters"];
if (!value->isNull())
{
for (const JsonNode & limiter : value->Vector())
{
switch (limiter.getType())
{
case JsonNode::DATA_STRING: //pre-defined limiters
b->limiter = parseByMap(bonusLimiterMap, &limiter, "limiter type ");
break;
case JsonNode::DATA_STRUCT: //customizable limiters
{
std::shared_ptr<ILimiter> l;
if (limiter["type"].String() == "CREATURE_TYPE_LIMITER")
{
std::shared_ptr<CCreatureTypeLimiter> l2 = std::make_shared<CCreatureTypeLimiter>(); //TODO: How the hell resolve pointer to creature?
const JsonVector vec = limiter["parameters"].Vector();
VLC->modh->identifiers.requestIdentifier("creature", vec[0], [=](si32 creature)
{
l2->setCreature(CreatureID(creature));
});
if (vec.size() > 1)
{
l2->includeUpgrades = vec[1].Bool();
}
else
l2->includeUpgrades = false;
l = l2;
}
if (limiter["type"].String() == "HAS_ANOTHER_BONUS_LIMITER")
{
std::shared_ptr<HasAnotherBonusLimiter> l2 = std::make_shared<HasAnotherBonusLimiter>();
const JsonVector vec = limiter["parameters"].Vector();
std::string anotherBonusType = vec[0].String();
auto it = bonusNameMap.find(anotherBonusType);
if (it == bonusNameMap.end())
{
logGlobal->errorStream() << "Error: invalid ability type " << anotherBonusType;
continue;
}
l2->type = it->second;
if (vec.size() > 1 )
{
resolveIdentifier(vec[1], l2->subtype);
l2->isSubtypeRelevant = true;
}
l = l2;
}
b->addLimiter(l);
}
break;
}
}
}
value = &ability["propagator"];
if (!value->isNull())
b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type ");
return true;
}
//returns first Key with value equal to given one
template<class Key, class Val>
Key reverseMapFirst(const Val & val, const std::map<Key, Val> & map)
{
for(auto it : map)
{
if(it.second == val)
{
return it.first;
}
}
assert(0);
return "";
}
void JsonUtils::unparseBonus( JsonNode &node, const std::shared_ptr<Bonus>& bonus )
{
node["type"].String() = reverseMapFirst<std::string, Bonus::BonusType>(bonus->type, bonusNameMap);
node["subtype"].Float() = bonus->subtype;
node["val"].Float() = bonus->val;
node["valueType"].String() = reverseMapFirst<std::string, Bonus::ValueType>(bonus->valType, bonusValueMap);
node["additionalInfo"].Float() = bonus->additionalInfo;
node["turns"].Float() = bonus->turnsRemain;
node["sourceID"].Float() = bonus->source;
node["description"].String() = bonus->description;
node["effectRange"].String() = reverseMapFirst<std::string, Bonus::LimitEffect>(bonus->effectRange, bonusLimitEffect);
node["duration"].String() = reverseMapFirst<std::string, ui16>(bonus->duration, bonusDurationMap);
node["source"].String() = reverseMapFirst<std::string, Bonus::BonusSource>(bonus->source, bonusSourceMap);
if(bonus->limiter)
{
node["limiter"].String() = reverseMapFirst<std::string, TLimiterPtr>(bonus->limiter, bonusLimiterMap);
}
if(bonus->propagator)
{
node["propagator"].String() = reverseMapFirst<std::string, TPropagatorPtr>(bonus->propagator, bonusPropagatorMap);
}
}
void minimizeNode(JsonNode & node, const JsonNode & schema)
{
if (schema["type"].String() == "object")
{
std::set<std::string> foundEntries;
for(auto & entry : schema["required"].Vector())
{
std::string name = entry.String();
foundEntries.insert(name);
minimizeNode(node[name], schema["properties"][name]);
if (vstd::contains(node.Struct(), name) &&
node[name] == schema["properties"][name]["default"])
{
node.Struct().erase(name);
}
}
// erase all unhandled entries
for (auto it = node.Struct().begin(); it != node.Struct().end();)
{
if (!vstd::contains(foundEntries, it->first))
it = node.Struct().erase(it);
else
it++;
}
}
}
void JsonUtils::minimize(JsonNode & node, std::string schemaName)
{
minimizeNode(node, getSchema(schemaName));
}
// FIXME: except for several lines function is identical to minimizeNode. Some way to reduce duplication?
void maximizeNode(JsonNode & node, const JsonNode & schema)
{
// "required" entry can only be found in object/struct
if (schema["type"].String() == "object")
{
std::set<std::string> foundEntries;
// check all required entries that have default version
for(auto & entry : schema["required"].Vector())
{
std::string name = entry.String();
foundEntries.insert(name);
if (node[name].isNull() &&
!schema["properties"][name]["default"].isNull())
{
node[name] = schema["properties"][name]["default"];
}
maximizeNode(node[name], schema["properties"][name]);
}
// erase all unhandled entries
for (auto it = node.Struct().begin(); it != node.Struct().end();)
{
if (!vstd::contains(foundEntries, it->first))
it = node.Struct().erase(it);
else
it++;
}
}
}
void JsonUtils::maximize(JsonNode & node, std::string schemaName)
{
maximizeNode(node, getSchema(schemaName));
}
bool JsonUtils::validate(const JsonNode &node, std::string schemaName, std::string dataName)
{
std::string log = Validation::check(schemaName, node);
if (!log.empty())
{
logGlobal->warnStream() << "Data in " << dataName << " is invalid!";
logGlobal->warnStream() << log;
}
return log.empty();
}
const JsonNode & getSchemaByName(std::string name)
{
// cached schemas to avoid loading json data multiple times
static std::map<std::string, JsonNode> loadedSchemas;
if (vstd::contains(loadedSchemas, name))
return loadedSchemas[name];
std::string filename = "config/schemas/" + name + ".json";
if (CResourceHandler::get()->existsResource(ResourceID(filename)))
{
loadedSchemas[name] = JsonNode(ResourceID(filename));
return loadedSchemas[name];
}
logGlobal->errorStream() << "Error: missing schema with name " << name << "!";
assert(0);
return nullNode;
}
const JsonNode & JsonUtils::getSchema(std::string URI)
{
std::vector<std::string> segments;
size_t posColon = URI.find(':');
size_t posHash = URI.find('#');
assert(posColon != std::string::npos);
std::string protocolName = URI.substr(0, posColon);
std::string filename = URI.substr(posColon + 1, posHash - posColon - 1);
if (protocolName != "vcmi")
{
logGlobal->errorStream() << "Error: unsupported URI protocol for schema: " << segments[0];
return nullNode;
}
// check if json pointer if present (section after hash in string)
if (posHash == std::string::npos || posHash == URI.size() - 1)
return getSchemaByName(filename);
else
return getSchemaByName(filename).resolvePointer(URI.substr(posHash + 1));
}
void JsonUtils::merge(JsonNode & dest, JsonNode & source)
{
if (dest.getType() == JsonNode::DATA_NULL)
{
std::swap(dest, source);
return;
}
switch (source.getType())
{
case JsonNode::DATA_NULL:
{
dest.clear();
break;
}
case JsonNode::DATA_BOOL:
case JsonNode::DATA_FLOAT:
case JsonNode::DATA_STRING:
case JsonNode::DATA_VECTOR:
{
std::swap(dest, source);
break;
}
case JsonNode::DATA_STRUCT:
{
//recursively merge all entries from struct
for(auto & node : source.Struct())
merge(dest[node.first], node.second);
}
}
}
void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source)
{
// uses copy created in stack to safely merge two nodes
merge(dest, source);
}
void JsonUtils::inherit(JsonNode & descendant, const JsonNode & base)
{
JsonNode inheritedNode(base);
merge(inheritedNode,descendant);
descendant.swap(inheritedNode);
}
JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files)
{
bool isValid;
return assembleFromFiles(files, isValid);
}
JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files, bool &isValid)
{
isValid = true;
JsonNode result;
for(std::string file : files)
{
bool isValidFile;
JsonNode section(ResourceID(file, EResType::TEXT), isValidFile);
merge(result, section);
isValid |= isValidFile;
}
return result;
}
JsonNode JsonUtils::assembleFromFiles(std::string filename)
{
JsonNode result;
ResourceID resID(filename, EResType::TEXT);
for(auto & loader : CResourceHandler::get()->getResourcesWithName(resID))
{
// FIXME: some way to make this code more readable
auto stream = loader->load(resID);
std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
stream->read(textData.get(), stream->getSize());
JsonNode section((char*)textData.get(), stream->getSize());
merge(result, section);
}
return result;
}
|