From b628bba704b3b4dd0f5a1b548a4e718504259859 Mon Sep 17 00:00:00 2001 From: Julia Pinheiro <66486603+ju-pinheiro@users.noreply.github.com> Date: Sat, 10 Oct 2020 12:21:07 -0300 Subject: [PATCH] Close #12429: Refactor OBJECT_ERROR to use strong enum and typo fix in build.sh (#13145) * Close #12429: Refactor OBJECT_ERROR to use strong enum * Typo Fix in build.sh: Unknown --- scripts/linux/build.sh | 2 +- src/openrct2/object/BannerObject.cpp | 2 +- src/openrct2/object/FootpathItemObject.cpp | 2 +- src/openrct2/object/FootpathObject.cpp | 2 +- src/openrct2/object/ImageTable.cpp | 16 ++++++------- src/openrct2/object/LargeSceneryObject.cpp | 4 ++-- src/openrct2/object/Object.h | 26 +++++++++++----------- src/openrct2/object/ObjectFactory.cpp | 8 +++---- src/openrct2/object/RideObject.cpp | 10 ++++----- src/openrct2/object/SmallSceneryObject.cpp | 4 ++-- src/openrct2/object/StringTable.cpp | 2 +- src/openrct2/object/WallObject.cpp | 2 +- 12 files changed, 40 insertions(+), 40 deletions(-) diff --git a/scripts/linux/build.sh b/scripts/linux/build.sh index c668f3e03a..bbc323b687 100755 --- a/scripts/linux/build.sh +++ b/scripts/linux/build.sh @@ -44,7 +44,7 @@ pushd build # CMAKE and MAKE opts from environment docker run -v "$PARENT":"$PARENT" -w "$PARENT"/build -i -t openrct2/openrct2:mingw-arch bash -c "cmake ../ $OPENRCT2_CMAKE_OPTS && ninja $OPENRCT2_MAKE_OPTS" else - echo "Unkown target $TARGET" + echo "Unknown target $TARGET" exit 1 fi popd diff --git a/src/openrct2/object/BannerObject.cpp b/src/openrct2/object/BannerObject.cpp index 1f38f470c1..f9be669705 100644 --- a/src/openrct2/object/BannerObject.cpp +++ b/src/openrct2/object/BannerObject.cpp @@ -36,7 +36,7 @@ void BannerObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* st // Validate properties if (_legacyType.large_scenery.price <= 0) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Price can not be free or negative."); + context->LogError(ObjectError::InvalidProperty, "Price can not be free or negative."); } // Add banners to 'Signs and items for footpaths' group, rather than lumping them in the Miscellaneous tab. diff --git a/src/openrct2/object/FootpathItemObject.cpp b/src/openrct2/object/FootpathItemObject.cpp index bc486f0634..1c0ac1feec 100644 --- a/src/openrct2/object/FootpathItemObject.cpp +++ b/src/openrct2/object/FootpathItemObject.cpp @@ -40,7 +40,7 @@ void FootpathItemObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre // Validate properties if (_legacyType.large_scenery.price <= 0) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Price can not be free or negative."); + context->LogError(ObjectError::InvalidProperty, "Price can not be free or negative."); } // Add path bits to 'Signs and items for footpaths' group, rather than lumping them in the Miscellaneous tab. diff --git a/src/openrct2/object/FootpathObject.cpp b/src/openrct2/object/FootpathObject.cpp index 4e3b8bc0a6..6de138b344 100644 --- a/src/openrct2/object/FootpathObject.cpp +++ b/src/openrct2/object/FootpathObject.cpp @@ -29,7 +29,7 @@ void FootpathObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* // Validate properties if (_legacyType.support_type >= RailingEntrySupportType::Count) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "RailingEntrySupportType not supported."); + context->LogError(ObjectError::InvalidProperty, "RailingEntrySupportType not supported."); } } diff --git a/src/openrct2/object/ImageTable.cpp b/src/openrct2/object/ImageTable.cpp index eea338f3f6..f7ee20b276 100644 --- a/src/openrct2/object/ImageTable.cpp +++ b/src/openrct2/object/ImageTable.cpp @@ -142,7 +142,7 @@ std::vector> ImageTable::ParseImages( catch (const std::exception& e) { auto msg = String::StdFormat("Unable to load image '%s': %s", s.c_str(), e.what()); - context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, msg.c_str()); + context->LogWarning(ObjectError::BadImageTable, msg.c_str()); result.push_back(std::make_unique()); } } @@ -179,7 +179,7 @@ std::vector> ImageTable::ParseImages( catch (const std::exception& e) { auto msg = String::StdFormat("Unable to load image '%s': %s", path.c_str(), e.what()); - context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, msg.c_str()); + context->LogWarning(ObjectError::BadImageTable, msg.c_str()); result.push_back(std::make_unique()); } return result; @@ -216,13 +216,13 @@ std::vector> ImageTable::LoadObjectIm if (placeHoldersAdded > 0) { std::string msg = "Adding " + std::to_string(placeHoldersAdded) + " placeholders"; - context->LogWarning(OBJECT_ERROR_INVALID_PROPERTY, msg.c_str()); + context->LogWarning(ObjectError::InvalidProperty, msg.c_str()); } } else { std::string msg = "Unable to open '" + objectPath + "'"; - context->LogWarning(OBJECT_ERROR_INVALID_PROPERTY, msg.c_str()); + context->LogWarning(ObjectError::InvalidProperty, msg.c_str()); for (size_t i = 0; i < range.size(); i++) { result.push_back(std::make_unique()); @@ -316,7 +316,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream) uint64_t remainingBytes = stream->GetLength() - stream->GetPosition() - headerTableSize; if (remainingBytes > imageDataSize) { - context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table size longer than expected."); + context->LogWarning(ObjectError::BadImageTable, "Image table size longer than expected."); imageDataSize = static_cast(remainingBytes); } @@ -324,7 +324,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream) auto data = std::make_unique(dataSize); if (data == nullptr) { - context->LogError(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table too large."); + context->LogError(ObjectError::BadImageTable, "Image table too large."); throw std::runtime_error("Image table too large."); } @@ -356,7 +356,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream) if (unreadBytes > 0) { std::fill_n(data.get() + readBytes, unreadBytes, 0); - context->LogWarning(OBJECT_ERROR_BAD_IMAGE_TABLE, "Image table size shorter than expected."); + context->LogWarning(ObjectError::BadImageTable, "Image table size shorter than expected."); } _data = std::move(data); @@ -364,7 +364,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream) } catch (const std::exception&) { - context->LogError(OBJECT_ERROR_BAD_IMAGE_TABLE, "Bad image table."); + context->LogError(ObjectError::BadImageTable, "Bad image table."); throw; } } diff --git a/src/openrct2/object/LargeSceneryObject.cpp b/src/openrct2/object/LargeSceneryObject.cpp index 3ab667c39d..2bd3a3c991 100644 --- a/src/openrct2/object/LargeSceneryObject.cpp +++ b/src/openrct2/object/LargeSceneryObject.cpp @@ -54,7 +54,7 @@ void LargeSceneryObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre // Validate properties if (_legacyType.large_scenery.price <= 0) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Price can not be free or negative."); + context->LogError(ObjectError::InvalidProperty, "Price can not be free or negative."); } if (_legacyType.large_scenery.removal_price <= 0) { @@ -62,7 +62,7 @@ void LargeSceneryObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre money16 reimbursement = _legacyType.large_scenery.removal_price; if (reimbursement > _legacyType.large_scenery.price) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Sell price can not be more than buy price."); + context->LogError(ObjectError::InvalidProperty, "Sell price can not be more than buy price."); } } } diff --git a/src/openrct2/object/Object.h b/src/openrct2/object/Object.h index b029ffb38c..f2c2831830 100644 --- a/src/openrct2/object/Object.h +++ b/src/openrct2/object/Object.h @@ -143,6 +143,17 @@ namespace OpenRCT2 struct ObjectRepositoryItem; struct rct_drawpixelinfo; +enum class ObjectError : uint32_t +{ + Ok, + Unknown, + BadEncoding, + InvalidProperty, + BadStringTable, + BadImageTable, + UnexpectedEOF, +}; + struct IReadObjectContext { virtual ~IReadObjectContext() = default; @@ -152,8 +163,8 @@ struct IReadObjectContext virtual bool ShouldLoadImages() abstract; virtual std::vector GetData(const std::string_view& path) abstract; - virtual void LogWarning(uint32_t code, const utf8* text) abstract; - virtual void LogError(uint32_t code, const utf8* text) abstract; + virtual void LogWarning(ObjectError code, const utf8* text) abstract; + virtual void LogError(ObjectError code, const utf8* text) abstract; }; #ifdef __WARN_SUGGEST_FINAL_TYPES__ @@ -282,17 +293,6 @@ public: # pragma GCC diagnostic pop #endif -enum OBJECT_ERROR : uint32_t -{ - OBJECT_ERROR_OK, - OBJECT_ERROR_UNKNOWN, - OBJECT_ERROR_BAD_ENCODING, - OBJECT_ERROR_INVALID_PROPERTY, - OBJECT_ERROR_BAD_STRING_TABLE, - OBJECT_ERROR_BAD_IMAGE_TABLE, - OBJECT_ERROR_UNEXPECTED_EOF, -}; - extern int32_t object_entry_group_counts[]; extern int32_t object_entry_group_encoding[]; diff --git a/src/openrct2/object/ObjectFactory.cpp b/src/openrct2/object/ObjectFactory.cpp index 37974b72a4..264c98404f 100644 --- a/src/openrct2/object/ObjectFactory.cpp +++ b/src/openrct2/object/ObjectFactory.cpp @@ -137,7 +137,7 @@ public: return {}; } - void LogWarning(uint32_t code, const utf8* text) override + void LogWarning(ObjectError code, const utf8* text) override { _wasWarning = true; @@ -147,7 +147,7 @@ public: } } - void LogError(uint32_t code, const utf8* text) override + void LogError(ObjectError code, const utf8* text) override { _wasError = true; @@ -192,11 +192,11 @@ namespace ObjectFactory catch (const IOException&) { // TODO check that ex is really EOF and not some other error - context->LogError(OBJECT_ERROR_UNEXPECTED_EOF, "Unexpectedly reached end of file."); + context->LogError(ObjectError::UnexpectedEOF, "Unexpectedly reached end of file."); } catch (const std::exception&) { - context->LogError(OBJECT_ERROR_UNKNOWN, nullptr); + context->LogError(ObjectError::Unknown, nullptr); } } diff --git a/src/openrct2/object/RideObject.cpp b/src/openrct2/object/RideObject.cpp index 33ed519f7d..044e59e1a2 100644 --- a/src/openrct2/object/RideObject.cpp +++ b/src/openrct2/object/RideObject.cpp @@ -170,15 +170,15 @@ void RideObject::ReadLegacy(IReadObjectContext* context, IStream* stream) // Validate properties if (_legacyType.excitement_multiplier > 75) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Excitement multiplier too high."); + context->LogError(ObjectError::InvalidProperty, "Excitement multiplier too high."); } if (_legacyType.intensity_multiplier > 75) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Intensity multiplier too high."); + context->LogError(ObjectError::InvalidProperty, "Intensity multiplier too high."); } if (_legacyType.nausea_multiplier > 75) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Nausea multiplier too high."); + context->LogError(ObjectError::InvalidProperty, "Nausea multiplier too high."); } RideObjectUpdateRideType(&_legacyType); } @@ -545,7 +545,7 @@ void RideObject::ReadJson(IReadObjectContext* context, json_t& root) if (rideType == RIDE_TYPE_NULL) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Unknown ride type"); + context->LogError(ObjectError::InvalidProperty, "Unknown ride type"); } } @@ -586,7 +586,7 @@ void RideObject::ReadJson(IReadObjectContext* context, json_t& root) auto shopItem = ParseShopItem(Json::GetString(rideSells[i])); if (shopItem == SHOP_ITEM_NONE) { - context->LogWarning(OBJECT_ERROR_INVALID_PROPERTY, "Unknown shop item"); + context->LogWarning(ObjectError::InvalidProperty, "Unknown shop item"); } _legacyType.shop_item[i] = shopItem; diff --git a/src/openrct2/object/SmallSceneryObject.cpp b/src/openrct2/object/SmallSceneryObject.cpp index 06f20c08e1..eec4ddef8a 100644 --- a/src/openrct2/object/SmallSceneryObject.cpp +++ b/src/openrct2/object/SmallSceneryObject.cpp @@ -57,7 +57,7 @@ void SmallSceneryObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre // Validate properties if (_legacyType.small_scenery.price <= 0) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Price can not be free or negative."); + context->LogError(ObjectError::InvalidProperty, "Price can not be free or negative."); } if (_legacyType.small_scenery.removal_price <= 0) { @@ -65,7 +65,7 @@ void SmallSceneryObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre money16 reimbursement = _legacyType.small_scenery.removal_price; if (reimbursement > _legacyType.small_scenery.price) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Sell price can not be more than buy price."); + context->LogError(ObjectError::InvalidProperty, "Sell price can not be more than buy price."); } } } diff --git a/src/openrct2/object/StringTable.cpp b/src/openrct2/object/StringTable.cpp index 0293208678..f800719ce4 100644 --- a/src/openrct2/object/StringTable.cpp +++ b/src/openrct2/object/StringTable.cpp @@ -73,7 +73,7 @@ void StringTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream, O } catch (const std::exception&) { - context->LogError(OBJECT_ERROR_BAD_STRING_TABLE, "Bad string table."); + context->LogError(ObjectError::BadStringTable, "Bad string table."); throw; } Sort(); diff --git a/src/openrct2/object/WallObject.cpp b/src/openrct2/object/WallObject.cpp index 989bdf6dc1..b5bffc520d 100644 --- a/src/openrct2/object/WallObject.cpp +++ b/src/openrct2/object/WallObject.cpp @@ -39,7 +39,7 @@ void WallObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* stre // Validate properties if (_legacyType.wall.price <= 0) { - context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Price can not be free or negative."); + context->LogError(ObjectError::InvalidProperty, "Price can not be free or negative."); } // Autofix this object (will be turned into an official object later).