From 70bdddcf3621db7ca4bf9b270a3c00058a5e6da6 Mon Sep 17 00:00:00 2001 From: Michael Steenbeek <1478678+Gymnasiast@users.noreply.github.com> Date: Tue, 25 Mar 2025 08:51:44 +0100 Subject: [PATCH] Reduce object casting (#24072) --- src/openrct2-ui/windows/Footpath.cpp | 11 ++++------- src/openrct2-ui/windows/Land.cpp | 9 +++------ src/openrct2-ui/windows/MapGen.cpp | 6 ++---- src/openrct2/EditorObjectSelectionSession.cpp | 3 ++- src/openrct2/actions/RideSetSettingAction.cpp | 2 +- .../actions/SurfaceSetStyleAction.cpp | 8 +++----- src/openrct2/audio/Audio.cpp | 8 +++----- src/openrct2/object/TerrainEdgeObject.cpp | 3 +-- src/openrct2/object/TerrainSurfaceObject.cpp | 3 +-- .../paint/tile_element/Paint.Entrance.cpp | 3 +-- src/openrct2/rct1/S4Importer.cpp | 4 ++-- src/openrct2/rct2/S6Importer.cpp | 2 +- src/openrct2/ride/Ride.cpp | 2 +- src/openrct2/ride/TrackDesignSave.cpp | 4 ++-- src/openrct2/scenario/Scenario.cpp | 4 +--- .../scripting/bindings/ride/ScRide.cpp | 2 +- .../scripting/bindings/world/ScResearch.cpp | 2 +- src/openrct2/world/Footpath.cpp | 19 +++---------------- .../world/map_generator/SurfaceSelection.cpp | 2 +- .../world/tile_element/SurfaceElement.cpp | 5 ++--- 20 files changed, 36 insertions(+), 66 deletions(-) diff --git a/src/openrct2-ui/windows/Footpath.cpp b/src/openrct2-ui/windows/Footpath.cpp index bcb8942639..dbf4bb6ea2 100644 --- a/src/openrct2-ui/windows/Footpath.cpp +++ b/src/openrct2-ui/windows/Footpath.cpp @@ -499,8 +499,7 @@ namespace OpenRCT2::Ui::Windows else { auto& objManager = OpenRCT2::GetContext()->GetObjectManager(); - auto* pathObj = static_cast( - objManager.GetLoadedObject(ObjectType::paths, gFootpathSelection.LegacyPath)); + const auto* pathObj = objManager.GetLoadedObject(gFootpathSelection.LegacyPath); if (pathObj != nullptr) { auto pathEntry = reinterpret_cast(pathObj->GetLegacyData()); @@ -640,11 +639,10 @@ namespace OpenRCT2::Ui::Windows // Set footpath and queue type button images auto pathImage = kSpriteIdNull; auto queueImage = kSpriteIdNull; - auto pathObj = static_cast( - objManager.GetLoadedObject(ObjectType::paths, gFootpathSelection.LegacyPath)); + const auto* pathObj = objManager.GetLoadedObject(gFootpathSelection.LegacyPath); if (pathObj != nullptr) { - auto pathEntry = reinterpret_cast(pathObj->GetLegacyData()); + auto pathEntry = reinterpret_cast(pathObj->GetLegacyData()); pathImage = pathEntry->GetPreviewImage(); queueImage = pathEntry->GetQueuePreviewImage(); } @@ -676,8 +674,7 @@ namespace OpenRCT2::Ui::Windows std::optional defaultIndex; for (ObjectEntryIndex i = 0; i < kMaxFootpathSurfaceObjects; i++) { - const auto* pathType = static_cast( - objManager.GetLoadedObject(ObjectType::footpathSurface, i)); + const auto* pathType = objManager.GetLoadedObject(i); if (pathType == nullptr) { continue; diff --git a/src/openrct2-ui/windows/Land.cpp b/src/openrct2-ui/windows/Land.cpp index eb84dbd551..1168440eb4 100644 --- a/src/openrct2-ui/windows/Land.cpp +++ b/src/openrct2-ui/windows/Land.cpp @@ -306,8 +306,7 @@ namespace OpenRCT2::Ui::Windows if (gLandToolTerrainSurface != kObjectEntryIndexNull) { auto& objManager = GetContext()->GetObjectManager(); - const auto surfaceObj = static_cast( - objManager.GetLoadedObject(ObjectType::terrainSurface, gLandToolTerrainSurface)); + const auto* surfaceObj = objManager.GetLoadedObject(gLandToolTerrainSurface); if (surfaceObj != nullptr) { price += numTiles * static_cast(surfaceObj->Price); @@ -843,8 +842,7 @@ namespace OpenRCT2::Ui::Windows void DrawDropdownButtons(DrawPixelInfo& dpi) { auto& objManager = GetContext()->GetObjectManager(); - const auto surfaceObj = static_cast( - objManager.GetLoadedObject(ObjectType::terrainSurface, _selectedFloorTexture)); + const auto* surfaceObj = objManager.GetLoadedObject(_selectedFloorTexture); ImageId surfaceImage; if (surfaceObj != nullptr) { @@ -853,8 +851,7 @@ namespace OpenRCT2::Ui::Windows surfaceImage = surfaceImage.WithPrimary(surfaceObj->Colour); } - const auto edgeObj = static_cast( - objManager.GetLoadedObject(ObjectType::terrainEdge, _selectedWallTexture)); + const auto edgeObj = objManager.GetLoadedObject(_selectedWallTexture); ImageId edgeImage; if (edgeObj != nullptr) { diff --git a/src/openrct2-ui/windows/MapGen.cpp b/src/openrct2-ui/windows/MapGen.cpp index 58635c8c16..393876b65d 100644 --- a/src/openrct2-ui/windows/MapGen.cpp +++ b/src/openrct2-ui/windows/MapGen.cpp @@ -1185,8 +1185,7 @@ namespace OpenRCT2::Ui::Windows void DrawDropdownButtons(DrawPixelInfo& dpi, WidgetIndex floorWidgetIndex, WidgetIndex edgeWidgetIndex) { auto& objManager = GetContext()->GetObjectManager(); - const auto surfaceObj = static_cast( - objManager.GetLoadedObject(ObjectType::terrainSurface, _settings.landTexture)); + const auto* surfaceObj = objManager.GetLoadedObject(_settings.landTexture); ImageId surfaceImage; if (surfaceObj != nullptr) { @@ -1198,8 +1197,7 @@ namespace OpenRCT2::Ui::Windows } ImageId edgeImage; - const auto edgeObj = static_cast( - objManager.GetLoadedObject(ObjectType::terrainEdge, _settings.edgeTexture)); + const auto* edgeObj = objManager.GetLoadedObject(_settings.edgeTexture); if (edgeObj != nullptr) { edgeImage = ImageId(edgeObj->IconImageId); diff --git a/src/openrct2/EditorObjectSelectionSession.cpp b/src/openrct2/EditorObjectSelectionSession.cpp index 7ef7dd8b5b..7684ac84bc 100644 --- a/src/openrct2/EditorObjectSelectionSession.cpp +++ b/src/openrct2/EditorObjectSelectionSession.cpp @@ -26,6 +26,7 @@ #include "object/ObjectList.h" #include "object/ObjectManager.h" #include "object/ObjectRepository.h" +#include "object/WaterObject.h" #include "ride/RideData.h" #include "ride/RideManager.hpp" #include "ride/TrainManager.h" @@ -466,7 +467,7 @@ static void SelectDesignerObjects() static void ReplaceSelectedWaterPalette(const ObjectRepositoryItem* item) { auto& objectManager = OpenRCT2::GetContext()->GetObjectManager(); - auto* oldPalette = objectManager.GetLoadedObject(ObjectType::water, 0); + auto* oldPalette = objectManager.GetLoadedObject(0); if (oldPalette != nullptr) { diff --git a/src/openrct2/actions/RideSetSettingAction.cpp b/src/openrct2/actions/RideSetSettingAction.cpp index fec30a0ee7..842982b3ac 100644 --- a/src/openrct2/actions/RideSetSettingAction.cpp +++ b/src/openrct2/actions/RideSetSettingAction.cpp @@ -116,7 +116,7 @@ GameActions::Result RideSetSettingAction::Query() const case RideSetSetting::MusicType: { auto& objManager = GetContext()->GetObjectManager(); - auto musicObj = objManager.GetLoadedObject(ObjectType::music, _value); + auto musicObj = objManager.GetLoadedObject(_value); if (musicObj == nullptr) { LOG_ERROR("Invalid music style: %u", _value); diff --git a/src/openrct2/actions/SurfaceSetStyleAction.cpp b/src/openrct2/actions/SurfaceSetStyleAction.cpp index 8138daf0e2..51a7a03dd2 100644 --- a/src/openrct2/actions/SurfaceSetStyleAction.cpp +++ b/src/openrct2/actions/SurfaceSetStyleAction.cpp @@ -55,7 +55,7 @@ GameActions::Result SurfaceSetStyleAction::Query() const if (_surfaceStyle != kObjectEntryIndexNull) { const auto surfaceObj = static_cast( - objManager.GetLoadedObject(ObjectType::terrainSurface, _surfaceStyle)); + objManager.GetLoadedObject(_surfaceStyle)); if (surfaceObj == nullptr) { @@ -123,8 +123,7 @@ GameActions::Result SurfaceSetStyleAction::Query() const if (_surfaceStyle != curSurfaceStyle) { - const auto surfaceObject = static_cast( - objManager.GetLoadedObject(ObjectType::terrainSurface, _surfaceStyle)); + const auto* surfaceObject = objManager.GetLoadedObject(_surfaceStyle); if (surfaceObject != nullptr) { surfaceCost += surfaceObject->Price; @@ -192,8 +191,7 @@ GameActions::Result SurfaceSetStyleAction::Execute() const if (_surfaceStyle != curSurfaceStyle) { auto& objManager = OpenRCT2::GetContext()->GetObjectManager(); - const auto surfaceObject = static_cast( - objManager.GetLoadedObject(ObjectType::terrainSurface, _surfaceStyle)); + const auto* surfaceObject = objManager.GetLoadedObject(_surfaceStyle); if (surfaceObject != nullptr) { surfaceCost += surfaceObject->Price; diff --git a/src/openrct2/audio/Audio.cpp b/src/openrct2/audio/Audio.cpp index f82412d4bc..a206735cc4 100644 --- a/src/openrct2/audio/Audio.cpp +++ b/src/openrct2/audio/Audio.cpp @@ -189,14 +189,12 @@ namespace OpenRCT2::Audio uint32_t sampleIndex = EnumValue(id); if (id >= SoundId::LiftRMC) { - audioObject = static_cast( - objManager.GetLoadedObject(ObjectType::audio, _soundsAdditionalAudioObjectEntryIndex)); + audioObject = objManager.GetLoadedObject(_soundsAdditionalAudioObjectEntryIndex); sampleIndex -= EnumValue(SoundId::LiftRMC); } else { - audioObject = static_cast( - objManager.GetLoadedObject(ObjectType::audio, _soundsAudioObjectEntryIndex)); + audioObject = objManager.GetLoadedObject(_soundsAudioObjectEntryIndex); } return std::make_tuple(audioObject, sampleIndex); } @@ -368,7 +366,7 @@ namespace OpenRCT2::Audio if (_titleAudioObjectEntryIndex != kObjectEntryIndexNull) { auto& objManager = GetContext()->GetObjectManager(); - auto* obj = objManager.GetLoadedObject(ObjectType::audio, _titleAudioObjectEntryIndex); + auto* obj = objManager.GetLoadedObject(_titleAudioObjectEntryIndex); if (obj != nullptr) { objManager.UnloadObjects({ obj->GetDescriptor() }); diff --git a/src/openrct2/object/TerrainEdgeObject.cpp b/src/openrct2/object/TerrainEdgeObject.cpp index 38670accea..b28db34207 100644 --- a/src/openrct2/object/TerrainEdgeObject.cpp +++ b/src/openrct2/object/TerrainEdgeObject.cpp @@ -65,6 +65,5 @@ void TerrainEdgeObject::ReadJson(IReadObjectContext* context, json_t& root) TerrainEdgeObject* TerrainEdgeObject::GetById(ObjectEntryIndex entryIndex) { auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto* obj = objMgr.GetLoadedObject(ObjectType::terrainEdge, entryIndex); - return static_cast(obj); + return objMgr.GetLoadedObject(entryIndex); } diff --git a/src/openrct2/object/TerrainSurfaceObject.cpp b/src/openrct2/object/TerrainSurfaceObject.cpp index 5e1c33cb01..13dbdd2a44 100644 --- a/src/openrct2/object/TerrainSurfaceObject.cpp +++ b/src/openrct2/object/TerrainSurfaceObject.cpp @@ -182,6 +182,5 @@ ImageId TerrainSurfaceObject::GetImageId( TerrainSurfaceObject* TerrainSurfaceObject::GetById(ObjectEntryIndex entryIndex) { auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto* obj = objMgr.GetLoadedObject(ObjectType::terrainSurface, entryIndex); - return static_cast(obj); + return objMgr.GetLoadedObject(entryIndex); } diff --git a/src/openrct2/paint/tile_element/Paint.Entrance.cpp b/src/openrct2/paint/tile_element/Paint.Entrance.cpp index beadb0aef7..3fafbd3c18 100644 --- a/src/openrct2/paint/tile_element/Paint.Entrance.cpp +++ b/src/openrct2/paint/tile_element/Paint.Entrance.cpp @@ -287,8 +287,7 @@ static void PaintParkEntrance(PaintSession& session, uint8_t direction, int32_t } auto& objManager = GetContext()->GetObjectManager(); - auto entrance = reinterpret_cast( - objManager.GetLoadedObject(ObjectType::parkEntrance, entranceEl.getEntryIndex())); + const auto* entrance = objManager.GetLoadedObject(entranceEl.getEntryIndex()); auto sequence = entranceEl.GetSequenceIndex(); switch (sequence) { diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 0b8248a24c..a79f68d147 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -272,7 +272,7 @@ namespace OpenRCT2::RCT1 std::lock_guard lock(mtx); // Unload loaded scenario text object, if any. - if (auto* obj = objManager.GetLoadedObject(ObjectType::scenarioText, 0); obj != nullptr) + if (auto* obj = objManager.GetLoadedObject(0); obj != nullptr) objManager.UnloadObjects({ obj->GetDescriptor() }); // Load the one specified @@ -2411,7 +2411,7 @@ namespace OpenRCT2::RCT1 std::lock_guard lock(mtx); // Unload loaded scenario text object, if any. - if (auto* obj = objManager.GetLoadedObject(ObjectType::scenarioText, 0); obj != nullptr) + if (auto* obj = objManager.GetLoadedObject(0); obj != nullptr) objManager.UnloadObjects({ obj->GetDescriptor() }); // Load the one specified diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index 61d09e6467..4c731abe09 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -300,7 +300,7 @@ namespace OpenRCT2::RCT2 std::lock_guard lock(mtx); // Unload loaded scenario text object, if any. - if (auto* obj = objManager.GetLoadedObject(ObjectType::scenarioText, 0); obj != nullptr) + if (auto* obj = objManager.GetLoadedObject(0); obj != nullptr) objManager.UnloadObjects({ obj->GetDescriptor() }); // Load the one specified diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 588ddccea9..b35610b390 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -224,7 +224,7 @@ const RideObjectEntry* GetRideEntryByIndex(ObjectEntryIndex index) { auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objMgr.GetLoadedObject(ObjectType::ride, index); + auto obj = objMgr.GetLoadedObject(index); if (obj == nullptr) { return nullptr; diff --git a/src/openrct2/ride/TrackDesignSave.cpp b/src/openrct2/ride/TrackDesignSave.cpp index c537eb4dd0..d19c61e983 100644 --- a/src/openrct2/ride/TrackDesignSave.cpp +++ b/src/openrct2/ride/TrackDesignSave.cpp @@ -272,7 +272,7 @@ static TrackDesignAddStatus TrackDesignSaveAddLargeScenery(const CoordsXY& loc, { auto entryIndex = tileElement->GetEntryIndex(); auto& objectMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objectMgr.GetLoadedObject(ObjectType::largeScenery, entryIndex); + auto obj = objectMgr.GetLoadedObject(entryIndex); if (obj != nullptr && TrackDesignSaveIsSupportedObject(obj)) { auto sceneryEntry = reinterpret_cast(obj->GetLegacyData()); @@ -488,7 +488,7 @@ static void TrackDesignSaveRemoveLargeScenery(const CoordsXY& loc, LargeSceneryE auto entryIndex = tileElement->GetEntryIndex(); auto& objectMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objectMgr.GetLoadedObject(ObjectType::largeScenery, entryIndex); + auto obj = objectMgr.GetLoadedObject(entryIndex); if (obj != nullptr) { auto sceneryEntry = reinterpret_cast(obj->GetLegacyData()); diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index 81411b0bf3..cc34ca5c0c 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -112,10 +112,8 @@ void ScenarioReset(GameState_t& gameState) gameState.cash = gameState.initialCash; auto& objManager = GetContext()->GetObjectManager(); - if (auto* object = objManager.GetLoadedObject(ObjectType::scenarioText, 0); object != nullptr) + if (auto* textObject = objManager.GetLoadedObject(0); textObject != nullptr) { - auto* textObject = reinterpret_cast(object); - gameState.scenarioName = textObject->GetScenarioName(); gameState.park.Name = textObject->GetParkName(); gameState.scenarioDetails = textObject->GetScenarioDetails(); diff --git a/src/openrct2/scripting/bindings/ride/ScRide.cpp b/src/openrct2/scripting/bindings/ride/ScRide.cpp index f0cdd80e01..21210b678a 100644 --- a/src/openrct2/scripting/bindings/ride/ScRide.cpp +++ b/src/openrct2/scripting/bindings/ride/ScRide.cpp @@ -36,7 +36,7 @@ namespace OpenRCT2::Scripting auto ride = GetRide(); if (ride != nullptr) { - auto rideObject = GetContext()->GetObjectManager().GetLoadedObject(ObjectType::ride, ride->subtype); + auto rideObject = GetContext()->GetObjectManager().GetLoadedObject(ride->subtype); if (rideObject != nullptr) { return std::make_shared(ObjectType::ride, ride->subtype); diff --git a/src/openrct2/scripting/bindings/world/ScResearch.cpp b/src/openrct2/scripting/bindings/world/ScResearch.cpp index 4d1901b7c4..08b273f2a2 100644 --- a/src/openrct2/scripting/bindings/world/ScResearch.cpp +++ b/src/openrct2/scripting/bindings/world/ScResearch.cpp @@ -279,7 +279,7 @@ namespace OpenRCT2::Scripting } else { - auto sceneryGroup = objManager.GetLoadedObject(ObjectType::sceneryGroup, researchItem.entryIndex); + auto sceneryGroup = objManager.GetLoadedObject(researchItem.entryIndex); if (sceneryGroup != nullptr) { researchItem.baseRideType = 0; diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index 390dcfcf37..6cd92279f3 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -1943,32 +1943,19 @@ bool FootpathSelectDefault() const FootpathObject* GetLegacyFootpathEntry(ObjectEntryIndex entryIndex) { auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objMgr.GetLoadedObject(ObjectType::paths, entryIndex); - if (obj == nullptr) - return nullptr; - - const FootpathObject* footpathObject = (static_cast(obj)); - return footpathObject; + return objMgr.GetLoadedObject(entryIndex); } const FootpathSurfaceObject* GetPathSurfaceEntry(ObjectEntryIndex entryIndex) { auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objMgr.GetLoadedObject(ObjectType::footpathSurface, entryIndex); - if (obj == nullptr) - return nullptr; - - return static_cast(obj); + return objMgr.GetLoadedObject(entryIndex); } const FootpathRailingsObject* GetPathRailingsEntry(ObjectEntryIndex entryIndex) { auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objMgr.GetLoadedObject(ObjectType::footpathRailings, entryIndex); - if (obj == nullptr) - return nullptr; - - return static_cast(obj); + return objMgr.GetLoadedObject(entryIndex); } RideId PathElement::GetRideIndex() const diff --git a/src/openrct2/world/map_generator/SurfaceSelection.cpp b/src/openrct2/world/map_generator/SurfaceSelection.cpp index 723b62f696..109b770524 100644 --- a/src/openrct2/world/map_generator/SurfaceSelection.cpp +++ b/src/openrct2/world/map_generator/SurfaceSelection.cpp @@ -63,7 +63,7 @@ namespace OpenRCT2::World::MapGenerator if (edgeTexture.empty()) { - auto surfaceObject = objectManager.GetLoadedObject(ObjectType::terrainSurface, surfaceTextureId); + auto surfaceObject = objectManager.GetLoadedObject(surfaceTextureId); auto surfaceTexture = surfaceObject->GetIdentifier(); // Base edge type on surface type diff --git a/src/openrct2/world/tile_element/SurfaceElement.cpp b/src/openrct2/world/tile_element/SurfaceElement.cpp index b15ab422e8..f8045ad5bc 100644 --- a/src/openrct2/world/tile_element/SurfaceElement.cpp +++ b/src/openrct2/world/tile_element/SurfaceElement.cpp @@ -64,10 +64,9 @@ bool SurfaceElement::CanGrassGrow() const { auto surfaceStyle = GetSurfaceObjectIndex(); auto& objMgr = OpenRCT2::GetContext()->GetObjectManager(); - auto obj = objMgr.GetLoadedObject(ObjectType::terrainSurface, surfaceStyle); - if (obj != nullptr) + const auto* surfaceObject = objMgr.GetLoadedObject(surfaceStyle); + if (surfaceObject != nullptr) { - const auto* surfaceObject = static_cast(obj); if (surfaceObject->Flags & TERRAIN_SURFACE_FLAGS::CAN_GROW) { return true;