From a22c04137920bc20bd158a2ec5edce7ac49d1dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 02:35:44 +0300 Subject: [PATCH 1/9] Enable the use of std::string_view for find in ObjectRepository --- src/openrct2/core/String.hpp | 18 ++++++++++++++++++ src/openrct2/object/ObjectRepository.cpp | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index da52e289d3..113f4977d7 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -19,6 +19,24 @@ namespace OpenRCT2::String { + struct Hash + { + using is_transparent = void; + + size_t operator()(const char* txt) const + { + return std::hash{}(txt); + } + size_t operator()(std::string_view txt) const + { + return std::hash{}(txt); + } + size_t operator()(const std::string& txt) const + { + return std::hash{}(txt); + } + }; + std::string toStd(const utf8* str); std::string toUtf8(std::wstring_view src); std::wstring toWideChar(std::string_view src); diff --git a/src/openrct2/object/ObjectRepository.cpp b/src/openrct2/object/ObjectRepository.cpp index 2fe05c3b04..ad54541eae 100644 --- a/src/openrct2/object/ObjectRepository.cpp +++ b/src/openrct2/object/ObjectRepository.cpp @@ -69,7 +69,7 @@ struct ObjectEntryEqual } }; -using ObjectIdentifierMap = std::unordered_map; +using ObjectIdentifierMap = std::unordered_map>; using ObjectEntryMap = std::unordered_map; class ObjectFileIndex final : public FileIndex @@ -235,7 +235,7 @@ public: const ObjectRepositoryItem* FindObject(std::string_view identifier) const override final { - auto kvp = _newItemMap.find(std::string(identifier)); + auto kvp = _newItemMap.find(identifier); if (kvp != _newItemMap.end()) { return &_items[kvp->second]; From 0b581281c5862c177037d36084b2ad410e7c78fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 02:43:21 +0300 Subject: [PATCH 2/9] Use string_view in ParseSourceGame lookup helper --- src/openrct2/object/ObjectFactory.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openrct2/object/ObjectFactory.cpp b/src/openrct2/object/ObjectFactory.cpp index 9f428069fc..41f20fdbc2 100644 --- a/src/openrct2/object/ObjectFactory.cpp +++ b/src/openrct2/object/ObjectFactory.cpp @@ -227,9 +227,9 @@ namespace OpenRCT2::ObjectFactory static std::unique_ptr CreateObjectFromJson( IObjectRepository& objectRepository, json_t& jRoot, const IFileDataRetriever* fileRetriever, bool loadImageTable); - static ObjectSourceGame ParseSourceGame(const std::string& s) + static ObjectSourceGame ParseSourceGame(const std::string_view s) { - static const std::unordered_map LookupTable{ + static const std::unordered_map LookupTable{ { "rct1", ObjectSourceGame::RCT1 }, { "rct1aa", ObjectSourceGame::AddedAttractions }, { "rct1ll", ObjectSourceGame::LoopyLandscapes }, From b30e1b406be6225135b2ff5ec670c497025390be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 02:48:12 +0300 Subject: [PATCH 3/9] Use unordered_map and constify things in Legacy.cpp --- src/openrct2/park/Legacy.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/openrct2/park/Legacy.cpp b/src/openrct2/park/Legacy.cpp index ed6858ff4e..7bd140f2e6 100644 --- a/src/openrct2/park/Legacy.cpp +++ b/src/openrct2/park/Legacy.cpp @@ -24,11 +24,12 @@ #include "../ride/Track.h" #include "ParkFile.h" -#include +#include +#include using namespace OpenRCT2; -static std::map oldObjectIds = { +static const std::unordered_map oldObjectIds = { { "official.scgpanda", "rct2dlc.scenery_group.scgpanda" }, { "official.wtrpink", "rct2dlc.water.wtrpink" }, { "official.ttrftl07", "toontowner.scenery_small.ttrftl07" }, @@ -2194,7 +2195,7 @@ std::string_view MapToNewObjectIdentifier(std::string_view s) return ""; } -static std::map DATPathNames = { +static const std::unordered_map DATPathNames = { { "rct2.pathash", "PATHASH " }, { "rct2.pathcrzy", "PATHCRZY" }, { "rct2.pathdirt", "PATHDIRT" }, { "rct2.pathspce", "PATHSPCE" }, { "rct2.road", "ROAD " }, { "rct2.tarmacb", "TARMACB " }, { "rct2.tarmacg", "TARMACG " }, { "rct2.tarmac", "TARMAC " }, { "rct2.1920path", "1920PATH" }, @@ -2212,9 +2213,9 @@ std::optional GetDATPathName(std::string_view newPathName) return std::nullopt; } -static RCT2::FootpathMapping _extendedFootpathMappings[] = { +static constexpr auto _extendedFootpathMappings = std::to_array({ { "rct1.path.tarmac", "rct1.footpath_surface.tarmac", "rct1.footpath_surface.queue_blue", "rct2.footpath_railings.wood" }, -}; +}); const RCT2::FootpathMapping* GetFootpathMapping(const ObjectEntryDescriptor& desc) { From 5eabdf8ec962ff68aff8a463c5decf5847132142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 02:54:48 +0300 Subject: [PATCH 4/9] Use std::span for peepAnimObjects, rename to new style --- src/openrct2/park/Legacy.cpp | 8 ++++---- src/openrct2/park/Legacy.h | 4 ++-- src/openrct2/rct12/RCT12.cpp | 2 +- src/openrct2/rct12/RCT12.h | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/openrct2/park/Legacy.cpp b/src/openrct2/park/Legacy.cpp index 7bd140f2e6..3f96764e36 100644 --- a/src/openrct2/park/Legacy.cpp +++ b/src/openrct2/park/Legacy.cpp @@ -2276,7 +2276,7 @@ void UpdateFootpathsFromMapping( pathToRailingsMap[entryIndex] = railingIndex; } -const std::vector peepAnimObjects = { +static constexpr auto kPeepAnimObjects = std::to_array({ "rct2.peep_animations.guest", "rct2.peep_animations.handyman", "rct2.peep_animations.mechanic", @@ -2292,11 +2292,11 @@ const std::vector peepAnimObjects = { "rct2.peep_animations.entertainer_roman", "rct2.peep_animations.entertainer_sheriff", "rct2.peep_animations.entertainer_snowman", -}; +}); -const std::vector& GetLegacyPeepAnimationObjects() +std::span GetLegacyPeepAnimationObjects() { - return peepAnimObjects; + return kPeepAnimObjects; } using AnimObjectConversionTable = std::map>; diff --git a/src/openrct2/park/Legacy.h b/src/openrct2/park/Legacy.h index f07180b96c..54d857e324 100644 --- a/src/openrct2/park/Legacy.h +++ b/src/openrct2/park/Legacy.h @@ -14,8 +14,8 @@ #include #include +#include #include -#include namespace OpenRCT2 { @@ -48,7 +48,7 @@ void UpdateFootpathsFromMapping( ObjectList& requiredObjects, ObjectEntryIndex& surfaceCount, ObjectEntryIndex& railingCount, ObjectEntryIndex entryIndex, const OpenRCT2::RCT2::FootpathMapping* footpathMapping); -const std::vector& GetLegacyPeepAnimationObjects(); +std::span GetLegacyPeepAnimationObjects(); void ConvertPeepAnimationTypeToObjects(OpenRCT2::GameState_t& gameState); std::string_view GetClimateObjectIdFromLegacyClimateType(OpenRCT2::RCT12::ClimateType); diff --git a/src/openrct2/rct12/RCT12.cpp b/src/openrct2/rct12/RCT12.cpp index ba9e66eb5f..fee68c0a12 100644 --- a/src/openrct2/rct12/RCT12.cpp +++ b/src/openrct2/rct12/RCT12.cpp @@ -818,7 +818,7 @@ void RCT12AddDefaultObjects(ObjectList& objectList) } } -void AppendRequiredObjects(ObjectList& objectList, ObjectType objectType, const std::vector& objectNames) +void AppendRequiredObjects(ObjectList& objectList, ObjectType objectType, std::span objectNames) { for (const auto& objectName : objectNames) { diff --git a/src/openrct2/rct12/RCT12.h b/src/openrct2/rct12/RCT12.h index c59b49bd0f..433ffe4af1 100644 --- a/src/openrct2/rct12/RCT12.h +++ b/src/openrct2/rct12/RCT12.h @@ -21,6 +21,7 @@ #include "../world/tile_element/TileElementType.h" #include "Limits.h" +#include #include #include #include @@ -1255,7 +1256,7 @@ std::string_view GetStationIdentifierFromStyle(uint8_t style); uint8_t GetStationStyleFromIdentifier(u8string_view identifier); std::optional GetStyleFromMusicIdentifier(std::string_view identifier); void RCT12AddDefaultObjects(ObjectList& objectList); -void AppendRequiredObjects(ObjectList& objectList, ObjectType objectType, const std::vector& objectNames); +void AppendRequiredObjects(ObjectList& objectList, ObjectType objectType, std::span objectNames); void AppendRequiredObjects(ObjectList& objectList, ObjectType objectType, const OpenRCT2::RCT12::EntryList& entryList); bool IsUserStringID(StringId stringId); From 8fdbf7ac0e1100f3c588e9e3526160748bea74c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 02:55:43 +0300 Subject: [PATCH 5/9] Avoid constructing std::string_view from const char* --- src/openrct2/park/Legacy.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openrct2/park/Legacy.cpp b/src/openrct2/park/Legacy.cpp index 3f96764e36..3856ac3508 100644 --- a/src/openrct2/park/Legacy.cpp +++ b/src/openrct2/park/Legacy.cpp @@ -2379,12 +2379,12 @@ void ConvertPeepAnimationTypeToObjects(OpenRCT2::GameState_t& gameState) LOG_INFO("Converted %d peep entities", numConverted); } -static constexpr std::array kClimateObjectIdsByLegacyClimateType = { +static constexpr auto kClimateObjectIdsByLegacyClimateType = std::to_array({ "rct2.climate.cool_and_wet", "rct2.climate.warm", "rct2.climate.hot_and_dry", "rct2.climate.cold", -}; +}); std::string_view GetClimateObjectIdFromLegacyClimateType(OpenRCT2::RCT12::ClimateType climate) { From 8fe14c5b5c6e929d80676e89537623320bdbe4f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 03:08:01 +0300 Subject: [PATCH 6/9] More code style adjustments --- src/openrct2/park/Legacy.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/openrct2/park/Legacy.cpp b/src/openrct2/park/Legacy.cpp index 3856ac3508..f6c3af88c3 100644 --- a/src/openrct2/park/Legacy.cpp +++ b/src/openrct2/park/Legacy.cpp @@ -29,7 +29,7 @@ using namespace OpenRCT2; -static const std::unordered_map oldObjectIds = { +static const std::unordered_map kOldObjectIds = { { "official.scgpanda", "rct2dlc.scenery_group.scgpanda" }, { "official.wtrpink", "rct2dlc.water.wtrpink" }, { "official.ttrftl07", "toontowner.scenery_small.ttrftl07" }, @@ -2187,15 +2187,15 @@ static const std::unordered_map oldObjectIds std::string_view MapToNewObjectIdentifier(std::string_view s) { - auto it = oldObjectIds.find(s); - if (it != oldObjectIds.end()) + auto it = kOldObjectIds.find(s); + if (it != kOldObjectIds.end()) { return it->second; } return ""; } -static const std::unordered_map DATPathNames = { +static const std::unordered_map kDATPathNames = { { "rct2.pathash", "PATHASH " }, { "rct2.pathcrzy", "PATHCRZY" }, { "rct2.pathdirt", "PATHDIRT" }, { "rct2.pathspce", "PATHSPCE" }, { "rct2.road", "ROAD " }, { "rct2.tarmacb", "TARMACB " }, { "rct2.tarmacg", "TARMACG " }, { "rct2.tarmac", "TARMAC " }, { "rct2.1920path", "1920PATH" }, @@ -2205,21 +2205,21 @@ static const std::unordered_map DATPathNames std::optional GetDATPathName(std::string_view newPathName) { - auto it = DATPathNames.find(newPathName); - if (it != DATPathNames.end()) + auto it = kDATPathNames.find(newPathName); + if (it != kDATPathNames.end()) { return it->second; } return std::nullopt; } -static constexpr auto _extendedFootpathMappings = std::to_array({ +static constexpr auto kExtendedFootpathMappings = std::to_array({ { "rct1.path.tarmac", "rct1.footpath_surface.tarmac", "rct1.footpath_surface.queue_blue", "rct2.footpath_railings.wood" }, }); const RCT2::FootpathMapping* GetFootpathMapping(const ObjectEntryDescriptor& desc) { - for (const auto& mapping : _extendedFootpathMappings) + for (const auto& mapping : kExtendedFootpathMappings) { if (mapping.Original == desc.GetName()) { From 2adbaffbca104339127747afe7621ed22467e450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 03:12:49 +0300 Subject: [PATCH 7/9] Use pair instead of tuple in FileIndex.hpp, reduces compilation time --- src/openrct2/core/FileIndex.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/openrct2/core/FileIndex.hpp b/src/openrct2/core/FileIndex.hpp index ac22a656a5..dec104d2a7 100644 --- a/src/openrct2/core/FileIndex.hpp +++ b/src/openrct2/core/FileIndex.hpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include template @@ -210,7 +210,7 @@ private: return allItems; } - std::tuple> ReadIndexFile(int32_t language, const DirectoryStats& stats) const + std::pair> ReadIndexFile(int32_t language, const DirectoryStats& stats) const { bool loadedItems = false; std::vector items; @@ -251,7 +251,7 @@ private: OpenRCT2::Console::Error::WriteLine("%s", e.what()); } } - return std::make_tuple(loadedItems, std::move(items)); + return { loadedItems, std::move(items) }; } void WriteIndexFile(int32_t language, const DirectoryStats& stats, const std::vector& items) const From 68f6ea8d15ce2ce0b9095ebca228b69e5336b2da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 03:16:45 +0300 Subject: [PATCH 8/9] Use sfl::small_vector for Chunks, only observed a maximum of 16 so far --- src/openrct2/core/OrcaStream.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openrct2/core/OrcaStream.hpp b/src/openrct2/core/OrcaStream.hpp index a6e681f480..5b0ec16ad4 100644 --- a/src/openrct2/core/OrcaStream.hpp +++ b/src/openrct2/core/OrcaStream.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -69,7 +70,7 @@ namespace OpenRCT2 IStream* _stream; Mode _mode; Header _header; - std::vector _chunks; + sfl::small_vector _chunks; MemoryStream _buffer; ChunkEntry _currentChunk; From f6d78375b566fbebd6a61d4d84341ef26dc8b24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 21 Apr 2025 03:41:56 +0300 Subject: [PATCH 9/9] Remove pointless overloads of iequals, implicitly converts to views --- src/openrct2/core/String.cpp | 10 ---------- src/openrct2/core/String.hpp | 2 -- 2 files changed, 12 deletions(-) diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index 1da45aa6d4..c57a787298 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -172,11 +172,6 @@ namespace OpenRCT2::String return equalsImpl(a, b, false); } - bool equals(const u8string& a, const u8string& b) - { - return equalsImpl(a, b, false); - } - bool equals(const utf8* a, const utf8* b, bool ignoreCase) { if (a == b) @@ -197,11 +192,6 @@ namespace OpenRCT2::String return equalsImpl(a, b, true); } - bool iequals(const u8string& a, const u8string& b) - { - return equalsImpl(a, b, true); - } - bool iequals(const utf8* a, const utf8* b) { if (a == b) diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 113f4977d7..a196b6a587 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -52,10 +52,8 @@ namespace OpenRCT2::String int32_t compare(const utf8* a, const utf8* b, bool ignoreCase = false); bool equals(u8string_view a, u8string_view b); - bool equals(const u8string& a, const u8string& b); bool equals(const utf8* a, const utf8* b, bool ignoreCase = false); bool iequals(u8string_view a, u8string_view b); - bool iequals(const u8string& a, const u8string& b); bool iequals(const utf8* a, const utf8* b); bool startsWith(std::string_view str, std::string_view match, bool ignoreCase = false);