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
This commit is contained in:
Julia Pinheiro 2020-10-10 12:21:07 -03:00 committed by GitHub
parent f28907a87d
commit b628bba704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 40 additions and 40 deletions

View File

@ -44,7 +44,7 @@ pushd build
# CMAKE and MAKE opts from environment # 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" 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 else
echo "Unkown target $TARGET" echo "Unknown target $TARGET"
exit 1 exit 1
fi fi
popd popd

View File

@ -36,7 +36,7 @@ void BannerObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* st
// Validate properties // Validate properties
if (_legacyType.large_scenery.price <= 0) 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. // Add banners to 'Signs and items for footpaths' group, rather than lumping them in the Miscellaneous tab.

View File

@ -40,7 +40,7 @@ void FootpathItemObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre
// Validate properties // Validate properties
if (_legacyType.large_scenery.price <= 0) 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. // Add path bits to 'Signs and items for footpaths' group, rather than lumping them in the Miscellaneous tab.

View File

@ -29,7 +29,7 @@ void FootpathObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream*
// Validate properties // Validate properties
if (_legacyType.support_type >= RailingEntrySupportType::Count) if (_legacyType.support_type >= RailingEntrySupportType::Count)
{ {
context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "RailingEntrySupportType not supported."); context->LogError(ObjectError::InvalidProperty, "RailingEntrySupportType not supported.");
} }
} }

View File

@ -142,7 +142,7 @@ std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::ParseImages(
catch (const std::exception& e) catch (const std::exception& e)
{ {
auto msg = String::StdFormat("Unable to load image '%s': %s", s.c_str(), e.what()); 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<RequiredImage>()); result.push_back(std::make_unique<RequiredImage>());
} }
} }
@ -179,7 +179,7 @@ std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::ParseImages(
catch (const std::exception& e) catch (const std::exception& e)
{ {
auto msg = String::StdFormat("Unable to load image '%s': %s", path.c_str(), e.what()); 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<RequiredImage>()); result.push_back(std::make_unique<RequiredImage>());
} }
return result; return result;
@ -216,13 +216,13 @@ std::vector<std::unique_ptr<ImageTable::RequiredImage>> ImageTable::LoadObjectIm
if (placeHoldersAdded > 0) if (placeHoldersAdded > 0)
{ {
std::string msg = "Adding " + std::to_string(placeHoldersAdded) + " placeholders"; 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 else
{ {
std::string msg = "Unable to open '" + objectPath + "'"; 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++) for (size_t i = 0; i < range.size(); i++)
{ {
result.push_back(std::make_unique<RequiredImage>()); result.push_back(std::make_unique<RequiredImage>());
@ -316,7 +316,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream)
uint64_t remainingBytes = stream->GetLength() - stream->GetPosition() - headerTableSize; uint64_t remainingBytes = stream->GetLength() - stream->GetPosition() - headerTableSize;
if (remainingBytes > imageDataSize) 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<uint32_t>(remainingBytes); imageDataSize = static_cast<uint32_t>(remainingBytes);
} }
@ -324,7 +324,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream)
auto data = std::make_unique<uint8_t[]>(dataSize); auto data = std::make_unique<uint8_t[]>(dataSize);
if (data == nullptr) 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."); throw std::runtime_error("Image table too large.");
} }
@ -356,7 +356,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream)
if (unreadBytes > 0) if (unreadBytes > 0)
{ {
std::fill_n(data.get() + readBytes, 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); _data = std::move(data);
@ -364,7 +364,7 @@ void ImageTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream)
} }
catch (const std::exception&) catch (const std::exception&)
{ {
context->LogError(OBJECT_ERROR_BAD_IMAGE_TABLE, "Bad image table."); context->LogError(ObjectError::BadImageTable, "Bad image table.");
throw; throw;
} }
} }

View File

@ -54,7 +54,7 @@ void LargeSceneryObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre
// Validate properties // Validate properties
if (_legacyType.large_scenery.price <= 0) 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) 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; money16 reimbursement = _legacyType.large_scenery.removal_price;
if (reimbursement > _legacyType.large_scenery.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.");
} }
} }
} }

View File

@ -143,6 +143,17 @@ namespace OpenRCT2
struct ObjectRepositoryItem; struct ObjectRepositoryItem;
struct rct_drawpixelinfo; struct rct_drawpixelinfo;
enum class ObjectError : uint32_t
{
Ok,
Unknown,
BadEncoding,
InvalidProperty,
BadStringTable,
BadImageTable,
UnexpectedEOF,
};
struct IReadObjectContext struct IReadObjectContext
{ {
virtual ~IReadObjectContext() = default; virtual ~IReadObjectContext() = default;
@ -152,8 +163,8 @@ struct IReadObjectContext
virtual bool ShouldLoadImages() abstract; virtual bool ShouldLoadImages() abstract;
virtual std::vector<uint8_t> GetData(const std::string_view& path) abstract; virtual std::vector<uint8_t> GetData(const std::string_view& path) abstract;
virtual void LogWarning(uint32_t code, const utf8* text) abstract; virtual void LogWarning(ObjectError code, const utf8* text) abstract;
virtual void LogError(uint32_t code, const utf8* text) abstract; virtual void LogError(ObjectError code, const utf8* text) abstract;
}; };
#ifdef __WARN_SUGGEST_FINAL_TYPES__ #ifdef __WARN_SUGGEST_FINAL_TYPES__
@ -282,17 +293,6 @@ public:
# pragma GCC diagnostic pop # pragma GCC diagnostic pop
#endif #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_counts[];
extern int32_t object_entry_group_encoding[]; extern int32_t object_entry_group_encoding[];

View File

@ -137,7 +137,7 @@ public:
return {}; return {};
} }
void LogWarning(uint32_t code, const utf8* text) override void LogWarning(ObjectError code, const utf8* text) override
{ {
_wasWarning = true; _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; _wasError = true;
@ -192,11 +192,11 @@ namespace ObjectFactory
catch (const IOException&) catch (const IOException&)
{ {
// TODO check that ex is really EOF and not some other error // 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&) catch (const std::exception&)
{ {
context->LogError(OBJECT_ERROR_UNKNOWN, nullptr); context->LogError(ObjectError::Unknown, nullptr);
} }
} }

View File

@ -170,15 +170,15 @@ void RideObject::ReadLegacy(IReadObjectContext* context, IStream* stream)
// Validate properties // Validate properties
if (_legacyType.excitement_multiplier > 75) 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) 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) 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); RideObjectUpdateRideType(&_legacyType);
} }
@ -545,7 +545,7 @@ void RideObject::ReadJson(IReadObjectContext* context, json_t& root)
if (rideType == RIDE_TYPE_NULL) 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])); auto shopItem = ParseShopItem(Json::GetString(rideSells[i]));
if (shopItem == SHOP_ITEM_NONE) 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; _legacyType.shop_item[i] = shopItem;

View File

@ -57,7 +57,7 @@ void SmallSceneryObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStre
// Validate properties // Validate properties
if (_legacyType.small_scenery.price <= 0) 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) 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; money16 reimbursement = _legacyType.small_scenery.removal_price;
if (reimbursement > _legacyType.small_scenery.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.");
} }
} }
} }

View File

@ -73,7 +73,7 @@ void StringTable::Read(IReadObjectContext* context, OpenRCT2::IStream* stream, O
} }
catch (const std::exception&) catch (const std::exception&)
{ {
context->LogError(OBJECT_ERROR_BAD_STRING_TABLE, "Bad string table."); context->LogError(ObjectError::BadStringTable, "Bad string table.");
throw; throw;
} }
Sort(); Sort();

View File

@ -39,7 +39,7 @@ void WallObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* stre
// Validate properties // Validate properties
if (_legacyType.wall.price <= 0) 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). // Autofix this object (will be turned into an official object later).