mirror of https://github.com/OpenRCT2/OpenRCT2.git
Close #12420: Refactor NETWORK_PERMISSION to use strong enum
First approach, instead of a bigger refactor to avoid the need of "static_cast".
This commit is contained in:
parent
6345fe64d5
commit
20a8bbc322
|
@ -972,7 +972,7 @@ static void window_multiplayer_groups_scrollpaint(rct_window* w, rct_drawpixelin
|
|||
int32_t groupindex = network_get_group_index(_selectedGroup);
|
||||
if (groupindex != -1)
|
||||
{
|
||||
if (network_can_perform_action(groupindex, i))
|
||||
if (network_can_perform_action(groupindex, static_cast<NetworkPermission>(i)))
|
||||
{
|
||||
char* lineCh = buffer;
|
||||
lineCh = utf8_write_codepoint(lineCh, FORMAT_WINDOW_COLOUR_2);
|
||||
|
|
|
@ -468,7 +468,7 @@ void window_player_overview_invalidate(rct_window* w)
|
|||
}
|
||||
|
||||
// Only enable kick button for other players
|
||||
const bool canKick = network_can_perform_action(network_get_current_player_group_index(), NETWORK_PERMISSION_KICK_PLAYER);
|
||||
const bool canKick = network_can_perform_action(network_get_current_player_group_index(), NetworkPermission::KickPlayer);
|
||||
const bool isServer = network_get_player_flags(playerIndex) & NETWORK_PLAYER_FLAG_ISSERVER;
|
||||
const bool isOwnWindow = (network_get_current_player_id() == w->number);
|
||||
widget_set_enabled(w, WIDX_KICK, canKick && !isOwnWindow && !isServer);
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
# include <algorithm>
|
||||
|
||||
int32_t NetworkActions::FindCommand(int32_t command)
|
||||
NetworkPermission NetworkActions::FindCommand(int32_t command)
|
||||
{
|
||||
auto it = std::find_if(Actions.begin(), Actions.end(), [&command](NetworkAction const& action) {
|
||||
for (int currentCommand : action.Commands)
|
||||
|
@ -30,24 +30,24 @@ int32_t NetworkActions::FindCommand(int32_t command)
|
|||
});
|
||||
if (it != Actions.end())
|
||||
{
|
||||
return static_cast<int32_t>(it - Actions.begin());
|
||||
return static_cast<NetworkPermission>(it - Actions.begin());
|
||||
}
|
||||
return -1;
|
||||
return NetworkPermission::Count;
|
||||
}
|
||||
|
||||
int32_t NetworkActions::FindCommandByPermissionName(const std::string& permission_name)
|
||||
NetworkPermission NetworkActions::FindCommandByPermissionName(const std::string& permission_name)
|
||||
{
|
||||
auto it = std::find_if(Actions.begin(), Actions.end(), [&permission_name](NetworkAction const& action) {
|
||||
return action.PermissionName == permission_name;
|
||||
});
|
||||
if (it != Actions.end())
|
||||
{
|
||||
return static_cast<int32_t>(it - Actions.begin());
|
||||
return static_cast<NetworkPermission>(it - Actions.begin());
|
||||
}
|
||||
return -1;
|
||||
return NetworkPermission::Count;
|
||||
}
|
||||
|
||||
const std::array<NetworkAction, NETWORK_PERMISSION_COUNT> NetworkActions::Actions = {
|
||||
const std::array<NetworkAction, static_cast<size_t>(NetworkPermission::Count)> NetworkActions::Actions = {
|
||||
NetworkAction{
|
||||
STR_ACTION_CHAT,
|
||||
"PERMISSION_CHAT",
|
||||
|
|
|
@ -22,33 +22,33 @@ enum MISC_COMMAND
|
|||
MISC_COMMAND_PASSWORDLESS_LOGIN = -3,
|
||||
};
|
||||
|
||||
enum NETWORK_PERMISSION
|
||||
enum class NetworkPermission : uint32_t
|
||||
{
|
||||
NETWORK_PERMISSION_CHAT,
|
||||
NETWORK_PERMISSION_TERRAFORM,
|
||||
NETWORK_PERMISSION_SET_WATER_LEVEL,
|
||||
NETWORK_PERMISSION_TOGGLE_PAUSE,
|
||||
NETWORK_PERMISSION_CREATE_RIDE,
|
||||
NETWORK_PERMISSION_REMOVE_RIDE,
|
||||
NETWORK_PERMISSION_BUILD_RIDE,
|
||||
NETWORK_PERMISSION_RIDE_PROPERTIES,
|
||||
NETWORK_PERMISSION_SCENERY,
|
||||
NETWORK_PERMISSION_PATH,
|
||||
NETWORK_PERMISSION_CLEAR_LANDSCAPE,
|
||||
NETWORK_PERMISSION_GUEST,
|
||||
NETWORK_PERMISSION_STAFF,
|
||||
NETWORK_PERMISSION_PARK_PROPERTIES,
|
||||
NETWORK_PERMISSION_PARK_FUNDING,
|
||||
NETWORK_PERMISSION_KICK_PLAYER,
|
||||
NETWORK_PERMISSION_MODIFY_GROUPS,
|
||||
NETWORK_PERMISSION_SET_PLAYER_GROUP,
|
||||
NETWORK_PERMISSION_CHEAT,
|
||||
NETWORK_PERMISSION_TOGGLE_SCENERY_CLUSTER,
|
||||
NETWORK_PERMISSION_PASSWORDLESS_LOGIN,
|
||||
NETWORK_PERMISSION_MODIFY_TILE,
|
||||
NETWORK_PERMISSION_EDIT_SCENARIO_OPTIONS,
|
||||
Chat,
|
||||
Terraform,
|
||||
SetWaterLevel,
|
||||
TogglePause,
|
||||
CreateRide,
|
||||
RemoveRide,
|
||||
BuildRide,
|
||||
RideProperties,
|
||||
Scenery,
|
||||
Path,
|
||||
ClearLandscape,
|
||||
Guest,
|
||||
Staff,
|
||||
ParkProperties,
|
||||
ParkFunding,
|
||||
KickPlayer,
|
||||
ModifyGroups,
|
||||
SetPlayerGroup,
|
||||
Cheat,
|
||||
ToggleSceneryCluster,
|
||||
PasswordlessLogin,
|
||||
ModifyTile,
|
||||
EditScenarioOptions,
|
||||
|
||||
NETWORK_PERMISSION_COUNT,
|
||||
Count
|
||||
};
|
||||
|
||||
class NetworkAction final
|
||||
|
@ -62,8 +62,8 @@ public:
|
|||
class NetworkActions final
|
||||
{
|
||||
public:
|
||||
static const std::array<NetworkAction, NETWORK_PERMISSION_COUNT> Actions;
|
||||
static const std::array<NetworkAction, static_cast<size_t>(NetworkPermission::Count)> Actions;
|
||||
|
||||
static int32_t FindCommand(int32_t command);
|
||||
static int32_t FindCommandByPermissionName(const std::string& permission_name);
|
||||
static NetworkPermission FindCommand(int32_t command);
|
||||
static NetworkPermission FindCommandByPermissionName(const std::string& permission_name);
|
||||
};
|
||||
|
|
|
@ -993,7 +993,7 @@ void NetworkBase::SetupDefaultGroups()
|
|||
// Spectator group
|
||||
auto spectator = std::make_unique<NetworkGroup>();
|
||||
spectator->SetName("Spectator");
|
||||
spectator->ToggleActionPermission(NETWORK_PERMISSION_CHAT);
|
||||
spectator->ToggleActionPermission(NetworkPermission::Chat);
|
||||
spectator->Id = 1;
|
||||
group_list.push_back(std::move(spectator));
|
||||
|
||||
|
@ -1001,13 +1001,13 @@ void NetworkBase::SetupDefaultGroups()
|
|||
auto user = std::make_unique<NetworkGroup>();
|
||||
user->SetName("User");
|
||||
user->ActionsAllowed.fill(0xFF);
|
||||
user->ToggleActionPermission(NETWORK_PERMISSION_KICK_PLAYER);
|
||||
user->ToggleActionPermission(NETWORK_PERMISSION_MODIFY_GROUPS);
|
||||
user->ToggleActionPermission(NETWORK_PERMISSION_SET_PLAYER_GROUP);
|
||||
user->ToggleActionPermission(NETWORK_PERMISSION_CHEAT);
|
||||
user->ToggleActionPermission(NETWORK_PERMISSION_PASSWORDLESS_LOGIN);
|
||||
user->ToggleActionPermission(NETWORK_PERMISSION_MODIFY_TILE);
|
||||
user->ToggleActionPermission(NETWORK_PERMISSION_EDIT_SCENARIO_OPTIONS);
|
||||
user->ToggleActionPermission(NetworkPermission::KickPlayer);
|
||||
user->ToggleActionPermission(NetworkPermission::ModifyGroups);
|
||||
user->ToggleActionPermission(NetworkPermission::SetPlayerGroup);
|
||||
user->ToggleActionPermission(NetworkPermission::Cheat);
|
||||
user->ToggleActionPermission(NetworkPermission::PasswordlessLogin);
|
||||
user->ToggleActionPermission(NetworkPermission::ModifyTile);
|
||||
user->ToggleActionPermission(NetworkPermission::EditScenarioOptions);
|
||||
user->Id = 2;
|
||||
group_list.push_back(std::move(user));
|
||||
|
||||
|
@ -3380,7 +3380,7 @@ void network_set_player_last_action(uint32_t index, int32_t command)
|
|||
{
|
||||
Guard::IndexInRange(index, gNetwork.player_list);
|
||||
|
||||
gNetwork.player_list[index]->LastAction = NetworkActions::FindCommand(command);
|
||||
gNetwork.player_list[index]->LastAction = static_cast<int32_t>(NetworkActions::FindCommand(command));
|
||||
gNetwork.player_list[index]->LastActionTime = platform_get_ticks();
|
||||
}
|
||||
|
||||
|
@ -3596,10 +3596,11 @@ GameActionResult::Ptr network_modify_groups(
|
|||
}
|
||||
NetworkGroup* mygroup = nullptr;
|
||||
NetworkPlayer* player = gNetwork.GetPlayerByID(actionPlayerId);
|
||||
auto networkPermission = static_cast<NetworkPermission>(permissionIndex);
|
||||
if (player != nullptr && permissionState == PermissionState::Toggle)
|
||||
{
|
||||
mygroup = gNetwork.GetGroupByID(player->Group);
|
||||
if (mygroup == nullptr || !mygroup->CanPerformAction(permissionIndex))
|
||||
if (mygroup == nullptr || !mygroup->CanPerformAction(networkPermission))
|
||||
{
|
||||
return std::make_unique<GameActionResult>(
|
||||
GA_ERROR::DISALLOWED, STR_CANT_MODIFY_PERMISSION_THAT_YOU_DO_NOT_HAVE_YOURSELF);
|
||||
|
@ -3626,7 +3627,7 @@ GameActionResult::Ptr network_modify_groups(
|
|||
}
|
||||
else
|
||||
{
|
||||
group->ToggleActionPermission(permissionIndex);
|
||||
group->ToggleActionPermission(networkPermission);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3731,7 +3732,7 @@ rct_string_id network_get_action_name_string_id(uint32_t index)
|
|||
}
|
||||
}
|
||||
|
||||
int32_t network_can_perform_action(uint32_t groupindex, uint32_t index)
|
||||
int32_t network_can_perform_action(uint32_t groupindex, NetworkPermission index)
|
||||
{
|
||||
Guard::IndexInRange(groupindex, gNetwork.group_list);
|
||||
|
||||
|
@ -4146,7 +4147,7 @@ rct_string_id network_get_action_name_string_id(uint32_t index)
|
|||
{
|
||||
return -1;
|
||||
}
|
||||
int32_t network_can_perform_action(uint32_t groupindex, uint32_t index)
|
||||
int32_t network_can_perform_action(uint32_t groupindex, NetworkPermission index)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -38,8 +38,8 @@ NetworkGroup NetworkGroup::FromJson(const json_t* json)
|
|||
{
|
||||
continue;
|
||||
}
|
||||
int32_t action_id = NetworkActions::FindCommandByPermissionName(perm_name);
|
||||
if (action_id != -1)
|
||||
NetworkPermission action_id = NetworkActions::FindCommandByPermissionName(perm_name);
|
||||
if (action_id != NetworkPermission::Count)
|
||||
{
|
||||
group.ToggleActionPermission(action_id);
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ json_t* NetworkGroup::ToJson() const
|
|||
json_t* actionsArray = json_array();
|
||||
for (size_t i = 0; i < NetworkActions::Actions.size(); i++)
|
||||
{
|
||||
if (CanPerformAction(i))
|
||||
if (CanPerformAction(static_cast<NetworkPermission>(i)))
|
||||
{
|
||||
const char* perm_name = NetworkActions::Actions[i].PermissionName.c_str();
|
||||
json_array_append_new(actionsArray, json_string(perm_name));
|
||||
|
@ -95,10 +95,11 @@ void NetworkGroup::Write(NetworkPacket& packet)
|
|||
}
|
||||
}
|
||||
|
||||
void NetworkGroup::ToggleActionPermission(size_t index)
|
||||
void NetworkGroup::ToggleActionPermission(NetworkPermission index)
|
||||
{
|
||||
size_t byte = index / 8;
|
||||
size_t bit = index % 8;
|
||||
size_t index_st = static_cast<size_t>(index);
|
||||
size_t byte = index_st / 8;
|
||||
size_t bit = index_st % 8;
|
||||
if (byte >= ActionsAllowed.size())
|
||||
{
|
||||
return;
|
||||
|
@ -106,10 +107,11 @@ void NetworkGroup::ToggleActionPermission(size_t index)
|
|||
ActionsAllowed[byte] ^= (1 << bit);
|
||||
}
|
||||
|
||||
bool NetworkGroup::CanPerformAction(size_t index) const
|
||||
bool NetworkGroup::CanPerformAction(NetworkPermission index) const
|
||||
{
|
||||
size_t byte = index / 8;
|
||||
size_t bit = index % 8;
|
||||
size_t index_st = static_cast<size_t>(index);
|
||||
size_t byte = index_st / 8;
|
||||
size_t bit = index_st % 8;
|
||||
if (byte >= ActionsAllowed.size())
|
||||
{
|
||||
return false;
|
||||
|
@ -119,8 +121,8 @@ bool NetworkGroup::CanPerformAction(size_t index) const
|
|||
|
||||
bool NetworkGroup::CanPerformCommand(int32_t command) const
|
||||
{
|
||||
int32_t action = NetworkActions::FindCommand(command);
|
||||
if (action != -1)
|
||||
NetworkPermission action = NetworkActions::FindCommand(command);
|
||||
if (action != NetworkPermission::Count)
|
||||
{
|
||||
return CanPerformAction(action);
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
#include <jansson.h>
|
||||
#include <string>
|
||||
|
||||
enum class NetworkPermission : uint32_t;
|
||||
|
||||
class NetworkGroup final
|
||||
{
|
||||
public:
|
||||
|
@ -29,8 +31,8 @@ public:
|
|||
|
||||
void Read(NetworkPacket& packet);
|
||||
void Write(NetworkPacket& packet);
|
||||
void ToggleActionPermission(size_t index);
|
||||
bool CanPerformAction(size_t index) const;
|
||||
void ToggleActionPermission(NetworkPermission index);
|
||||
bool CanPerformAction(NetworkPermission index) const;
|
||||
bool CanPerformCommand(int32_t command) const;
|
||||
|
||||
json_t* ToJson() const;
|
||||
|
|
|
@ -29,6 +29,7 @@ struct CoordsXYZ;
|
|||
class GameActionResult;
|
||||
enum class ModifyGroupType : uint8_t;
|
||||
enum class PermissionState : uint8_t;
|
||||
enum class NetworkPermission : uint32_t;
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
|
@ -87,7 +88,7 @@ std::unique_ptr<GameActionResult> network_kick_player(NetworkPlayerId_t playerId
|
|||
uint8_t network_get_default_group();
|
||||
int32_t network_get_num_actions();
|
||||
rct_string_id network_get_action_name_string_id(uint32_t index);
|
||||
int32_t network_can_perform_action(uint32_t groupindex, uint32_t index);
|
||||
int32_t network_can_perform_action(uint32_t groupindex, NetworkPermission index);
|
||||
int32_t network_can_perform_command(uint32_t groupindex, int32_t index);
|
||||
void network_set_pickup_peep(uint8_t playerid, Peep* peep);
|
||||
Peep* network_get_pickup_peep(uint8_t playerid);
|
||||
|
|
|
@ -69,7 +69,7 @@ namespace OpenRCT2::Scripting
|
|||
auto permissionIndex = 0;
|
||||
for (const auto& action : NetworkActions::Actions)
|
||||
{
|
||||
if (network_can_perform_action(index, permissionIndex))
|
||||
if (network_can_perform_action(index, static_cast<NetworkPermission>(permissionIndex)))
|
||||
{
|
||||
auto p = TransformPermissionKeyToJS(action.PermissionName);
|
||||
result.push_back(p);
|
||||
|
@ -113,7 +113,8 @@ namespace OpenRCT2::Scripting
|
|||
for (size_t i = 0; i < enabledPermissions.size(); i++)
|
||||
{
|
||||
auto toggle
|
||||
= (enabledPermissions[i] != (network_can_perform_action(groupIndex, static_cast<uint32_t>(i)) != 0));
|
||||
= (enabledPermissions[i]
|
||||
!= (network_can_perform_action(groupIndex, static_cast<NetworkPermission>(i)) != 0));
|
||||
if (toggle)
|
||||
{
|
||||
auto networkAction2 = NetworkModifyGroupAction(
|
||||
|
|
Loading…
Reference in New Issue