diff --git a/src/openrct2/scripting/Duktape.hpp b/src/openrct2/scripting/Duktape.hpp index fdd7f0f327..95de254bb3 100644 --- a/src/openrct2/scripting/Duktape.hpp +++ b/src/openrct2/scripting/Duktape.hpp @@ -12,6 +12,7 @@ #ifdef ENABLE_SCRIPTING # include "../core/Console.hpp" +# include "../core/EnumMap.hpp" # include "../ride/Vehicle.h" # include "../world/Map.h" @@ -221,42 +222,7 @@ namespace OpenRCT2::Scripting /** * Bi-directional map for converting between strings and enums / numbers. */ - template class DukEnumMap - { - private: - std::unordered_map _s2n; - std::unordered_map _n2s; - - public: - DukEnumMap(const std::initializer_list>& items) - { - _s2n = std::unordered_map(items.begin(), items.end()); - for (const auto& kvp : items) - { - _n2s.emplace(std::get<1>(kvp), std::get<0>(kvp)); - } - } - - std::string_view operator[](T k) const - { - auto it = _n2s.find(k); - if (it == _n2s.end()) - { - return ""; - } - return it->second; - } - - T operator[](std::string_view k) const - { - auto it = _s2n.find(k); - if (it == _s2n.end()) - { - return static_cast(0); - } - return it->second; - } - }; + template using DukEnumMap = EnumMap; inline duk_ret_t duk_json_decode_wrapper(duk_context* ctx, void*) { diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index db2f1a1801..d7fb754e89 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -17,6 +17,7 @@ # include "../actions/RideCreateAction.h" # include "../actions/StaffHireNewAction.h" # include "../config/Config.h" +# include "../core/EnumMap.hpp" # include "../core/File.h" # include "../core/FileScanner.h" # include "../core/Path.hpp" @@ -987,7 +988,8 @@ public: } }; -const static std::unordered_map ActionNameToType = { +// clang-format off +const static EnumMap ActionNameToType = { { "balloonpress", GameCommand::BalloonPress }, { "bannerplace", GameCommand::PlaceBanner }, { "bannerremove", GameCommand::RemoveBanner }, @@ -1067,21 +1069,21 @@ const static std::unordered_map ActionNameToType = { { "waterraise", GameCommand::RaiseWater }, { "watersetheight", GameCommand::SetWaterHeight } }; +// clang-format on static std::string GetActionName(GameCommand commandId) { - auto it = std::find_if( - ActionNameToType.begin(), ActionNameToType.end(), [commandId](const auto& kvp) { return kvp.second == commandId; }); + auto it = ActionNameToType.find(commandId); if (it != ActionNameToType.end()) { - return it->first; + return std::string{ it->first }; } return {}; } -static std::unique_ptr CreateGameActionFromActionId(const std::string& actionid) +static std::unique_ptr CreateGameActionFromActionId(const std::string& name) { - auto result = ActionNameToType.find(actionid); + auto result = ActionNameToType.find(name); if (result != ActionNameToType.end()) { return GameActions::Create(result->second);