| 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); |
no test coverage detected