From d768811c5c5bf106a7e056fd37ed7d529f0df39f Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Tue, 10 Dec 2024 23:21:52 +0100 Subject: [PATCH] Refactor PeepAnimationsObject::ReadAnimations into its own method --- src/openrct2/object/PeepAnimationsObject.cpp | 83 +++++++++++--------- src/openrct2/object/PeepAnimationsObject.h | 1 + 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/openrct2/object/PeepAnimationsObject.cpp b/src/openrct2/object/PeepAnimationsObject.cpp index 170d80a927..e30ed8fcbe 100644 --- a/src/openrct2/object/PeepAnimationsObject.cpp +++ b/src/openrct2/object/PeepAnimationsObject.cpp @@ -72,44 +72,7 @@ void PeepAnimationsObject::ReadJson(IReadObjectContext* context, json_t& root) { Guard::Assert(groupJson["animations"].is_object(), "PeepAnimationsObject::ReadJson expects animations to be an array"); - PeepAnimations group{}; - for (auto& [typeStr, typeEnum] : requiredAnimationMap) - { - if (!groupJson["animations"].contains(typeStr)) - { - // Successive animation groups can copy the basic animations from the primary group - if (!_animationGroups.empty()) - { - auto& referenceAnim = _animationGroups[0][typeEnum]; - if (referenceAnim.imageTableOffset != 0) - { - LOG_VERBOSE("Copying animation '%s' from primary group", typeStr); - std::vector sequence = referenceAnim.frame_offsets; - group[typeEnum] = { - .imageTableOffset = referenceAnim.imageTableOffset, - .frame_offsets = sequence, - }; - continue; - } - } - - // No primary animation bail us out -- error here! - LOG_ERROR("Required animation does not exist: %s", typeStr); - continue; - } - - // The `.data()` here is a workaround for older versions of nlohmann-json. - // TODO: remove when we no longer support Ubuntu 22.04 (Jammy). - auto& animJson = animations[typeStr.data()]; - - // Store animation sequence in vector - auto sequence = animJson["sequence"].get>(); - - group[typeEnum] = { - .imageTableOffset = Json::GetNumber(animJson["offset"]), - .frame_offsets = sequence, - }; - } + PeepAnimations group = ReadAnimations(requiredAnimationMap, groupJson["animations"]); // Is this animation group replacing a legacy group? if (groupJson.contains("legacyPosition")) @@ -140,6 +103,50 @@ void PeepAnimationsObject::ReadJson(IReadObjectContext* context, json_t& root) } } +PeepAnimations PeepAnimationsObject::ReadAnimations(const EnumMap& requiredAnimationMap, json_t& animations) +{ + PeepAnimations group{}; + for (auto& [typeStr, typeEnum] : requiredAnimationMap) + { + if (!animations.contains(typeStr)) + { + // Successive animation groups can copy the basic animations from the primary group + if (!_animationGroups.empty()) + { + auto& referenceAnim = _animationGroups[0][typeEnum]; + if (referenceAnim.imageTableOffset != 0) + { + LOG_VERBOSE("Copying animation '%s' from primary group", typeStr); + std::vector sequence = referenceAnim.frame_offsets; + group[typeEnum] = { + .imageTableOffset = referenceAnim.imageTableOffset, + .frame_offsets = sequence, + }; + continue; + } + } + + // No primary animation bail us out -- error here! + LOG_ERROR("Required animation does not exist: %s", typeStr); + continue; + } + + // The `.data()` here is a workaround for older versions of nlohmann-json. + // TODO: remove when we no longer support Ubuntu 22.04 (Jammy). + auto& animJson = animations[typeStr.data()]; + + // Store animation sequence in vector + auto sequence = animJson["sequence"].get>(); + + group[typeEnum] = { + .imageTableOffset = Json::GetNumber(animJson["offset"]), + .frame_offsets = sequence, + }; + } + + return group; +} + std::string PeepAnimationsObject::GetCostumeName() const { return GetStringTable().GetString(ObjectStringID::NAME); diff --git a/src/openrct2/object/PeepAnimationsObject.h b/src/openrct2/object/PeepAnimationsObject.h index 0f63b1ee09..455689d19e 100644 --- a/src/openrct2/object/PeepAnimationsObject.h +++ b/src/openrct2/object/PeepAnimationsObject.h @@ -30,6 +30,7 @@ public: static constexpr ObjectType kObjectType = ObjectType::PeepAnimations; void ReadJson(IReadObjectContext* context, json_t& root) override; + OpenRCT2::PeepAnimations ReadAnimations(const EnumMap& requiredAnimationMap, json_t& animations); void Load() override; void Unload() override;