MCPcopy Create free account
hub / github.com/OpenRCT2/OpenRCT2 / CreateObjectFromJson

Function CreateObjectFromJson

src/openrct2/object/ObjectFactory.cpp:530–614  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

528 }
529
530 std::unique_ptr<Object> CreateObjectFromJson(
531 json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable, const std::string_view path)
532 {
533 if (!jRoot.is_object())
534 {
535 throw std::runtime_error("Object JSON root was not an object");
536 }
537
538 LOG_VERBOSE("CreateObjectFromJson(...)");
539
540 std::unique_ptr<Object> result;
541
542 auto lookup = kObjectTypeMap.find(Json::GetString(jRoot["objectType"]));
543 if (lookup != kObjectTypeMap.end())
544 {
545 auto objectType = lookup->second;
546 auto id = Json::GetString(jRoot["id"]);
547
548 // Base audio files are renamed to a common, virtual name so asset packs can override it correctly.
549 const bool isRCT2BaseAudio = id == Audio::AudioObjectIdentifiers::kRCT2Base && !isUsingClassic();
550 const bool isRCTCBaseAudio = id == Audio::AudioObjectIdentifiers::kRCTCBase && isUsingClassic();
551 if (isRCT2BaseAudio || isRCTCBaseAudio)
552 {
553 id = Audio::AudioObjectIdentifiers::kRCT2;
554 }
555
556 auto version = VersionTuple(Json::GetString(jRoot["version"]));
557 ObjectEntryDescriptor descriptor;
558 auto originalId = Json::GetString(jRoot["originalId"]);
559 if (originalId.length() == 8 + 1 + 8 + 1 + 8)
560 {
561 auto originalName = originalId.substr(9, 8);
562
563 RCTObjectEntry entry = {};
564 entry.flags = std::stoul(originalId.substr(0, 8), nullptr, 16);
565 entry.checksum = std::stoul(originalId.substr(18, 8), nullptr, 16);
566 auto minLength = std::min<size_t>(8, originalName.length());
567 std::memcpy(entry.name, originalName.c_str(), minLength);
568
569 // Some bad objects try to override different types
570 if (entry.GetType() != objectType)
571 {
572 LOG_ERROR(
573 "Object \"%s\" has invalid originalId set \"%s\". Ignoring override.", id.c_str(), originalId.c_str());
574 descriptor = ObjectEntryDescriptor(objectType, id);
575 }
576 else
577 {
578 descriptor = ObjectEntryDescriptor(entry);
579 }
580 }
581 else
582 {
583 descriptor = ObjectEntryDescriptor(objectType, id);
584 }
585 descriptor.Version = version;
586 result = CreateObject(objectType);
587 result->SetVersion(version);

Callers 2

CreateObjectFromZipFileFunction · 0.85
CreateObjectFromJsonFileFunction · 0.85

Calls 15

GetStringFunction · 0.85
isUsingClassicFunction · 0.85
VersionTupleFunction · 0.85
CreateObjectFunction · 0.85
ReadObjectContextClass · 0.85
GetBooleanFunction · 0.85
ExtractSourceGamesFunction · 0.85
findMethod · 0.80
SetVersionMethod · 0.80
SetIdentifierMethod · 0.80
SetDescriptorMethod · 0.80

Tested by

no test coverage detected