mirror of https://github.com/OpenRCT2/OpenRCT2.git
Use EnumMap for lookup tables in scripting
This commit is contained in:
parent
ce8040b858
commit
f44687b6b2
|
@ -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<typename T> class DukEnumMap
|
||||
{
|
||||
private:
|
||||
std::unordered_map<std::string_view, T> _s2n;
|
||||
std::unordered_map<T, std::string_view> _n2s;
|
||||
|
||||
public:
|
||||
DukEnumMap(const std::initializer_list<std::pair<std::string_view, T>>& items)
|
||||
{
|
||||
_s2n = std::unordered_map<std::string_view, T>(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<T>(0);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
};
|
||||
template<typename T> using DukEnumMap = EnumMap<T>;
|
||||
|
||||
inline duk_ret_t duk_json_decode_wrapper(duk_context* ctx, void*)
|
||||
{
|
||||
|
|
|
@ -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<std::string, GameCommand> ActionNameToType = {
|
||||
// clang-format off
|
||||
const static EnumMap<GameCommand> ActionNameToType = {
|
||||
{ "balloonpress", GameCommand::BalloonPress },
|
||||
{ "bannerplace", GameCommand::PlaceBanner },
|
||||
{ "bannerremove", GameCommand::RemoveBanner },
|
||||
|
@ -1067,21 +1069,21 @@ const static std::unordered_map<std::string, GameCommand> 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<GameAction> CreateGameActionFromActionId(const std::string& actionid)
|
||||
static std::unique_ptr<GameAction> CreateGameActionFromActionId(const std::string& name)
|
||||
{
|
||||
auto result = ActionNameToType.find(actionid);
|
||||
auto result = ActionNameToType.find(name);
|
||||
if (result != ActionNameToType.end())
|
||||
{
|
||||
return GameActions::Create(result->second);
|
||||
|
|
Loading…
Reference in New Issue