1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-18 20:43:04 +01:00

Move network units to OpenRCT2::Network namespace

This commit is contained in:
Aaron van Geffen
2025-08-31 15:26:10 +02:00
parent d6e9f3d205
commit 6ea5959b2b
28 changed files with 7396 additions and 7311 deletions

View File

@@ -15,119 +15,120 @@
#include "NetworkAction.h"
#include "NetworkTypes.h"
using namespace OpenRCT2;
NetworkGroup NetworkGroup::FromJson(const json_t& jsonData)
namespace OpenRCT2::Network
{
Guard::Assert(jsonData.is_object(), "NetworkGroup::FromJson expects parameter jsonData to be object");
NetworkGroup group;
json_t jsonId = jsonData["id"];
json_t jsonName = jsonData["name"];
json_t jsonPermissions = jsonData["permissions"];
if (jsonId.is_null() || jsonName.is_null() || jsonPermissions.is_null())
NetworkGroup NetworkGroup::FromJson(const json_t& jsonData)
{
throw std::runtime_error("Missing group data");
Guard::Assert(jsonData.is_object(), "NetworkGroup::FromJson expects parameter jsonData to be object");
NetworkGroup group;
json_t jsonId = jsonData["id"];
json_t jsonName = jsonData["name"];
json_t jsonPermissions = jsonData["permissions"];
if (jsonId.is_null() || jsonName.is_null() || jsonPermissions.is_null())
{
throw std::runtime_error("Missing group data");
}
group.Id = Json::GetNumber<uint8_t>(jsonId);
group._name = Json::GetString(jsonName);
std::fill(group.ActionsAllowed.begin(), group.ActionsAllowed.end(), 0);
for (const auto& jsonValue : jsonPermissions)
{
const std::string permission = Json::GetString(jsonValue);
NetworkPermission action_id = NetworkActions::FindCommandByPermissionName(permission);
if (action_id != NetworkPermission::Count)
{
group.ToggleActionPermission(action_id);
}
}
return group;
}
group.Id = Json::GetNumber<uint8_t>(jsonId);
group._name = Json::GetString(jsonName);
std::fill(group.ActionsAllowed.begin(), group.ActionsAllowed.end(), 0);
for (const auto& jsonValue : jsonPermissions)
json_t NetworkGroup::ToJson() const
{
const std::string permission = Json::GetString(jsonValue);
NetworkPermission action_id = NetworkActions::FindCommandByPermissionName(permission);
if (action_id != NetworkPermission::Count)
json_t jsonGroup = {
{ "id", Id },
{ "name", GetName() },
};
json_t actionsArray = json_t::array();
for (size_t i = 0; i < NetworkActions::Actions.size(); i++)
{
group.ToggleActionPermission(action_id);
if (CanPerformAction(static_cast<NetworkPermission>(i)))
{
actionsArray.emplace_back(NetworkActions::Actions[i].PermissionName);
}
}
jsonGroup["permissions"] = actionsArray;
return jsonGroup;
}
const std::string& NetworkGroup::GetName() const noexcept
{
return _name;
}
void NetworkGroup::SetName(std::string_view name)
{
_name = name;
}
void NetworkGroup::Read(NetworkPacket& packet)
{
packet >> Id;
SetName(packet.ReadString());
for (auto& action : ActionsAllowed)
{
packet >> action;
}
}
return group;
}
json_t NetworkGroup::ToJson() const
{
json_t jsonGroup = {
{ "id", Id },
{ "name", GetName() },
};
json_t actionsArray = json_t::array();
for (size_t i = 0; i < NetworkActions::Actions.size(); i++)
void NetworkGroup::Write(NetworkPacket& packet) const
{
if (CanPerformAction(static_cast<NetworkPermission>(i)))
packet << Id;
packet.WriteString(GetName().c_str());
for (const auto& action : ActionsAllowed)
{
actionsArray.emplace_back(NetworkActions::Actions[i].PermissionName);
packet << action;
}
}
jsonGroup["permissions"] = actionsArray;
return jsonGroup;
}
const std::string& NetworkGroup::GetName() const noexcept
{
return _name;
}
void NetworkGroup::SetName(std::string_view name)
{
_name = name;
}
void NetworkGroup::Read(NetworkPacket& packet)
{
packet >> Id;
SetName(packet.ReadString());
for (auto& action : ActionsAllowed)
void NetworkGroup::ToggleActionPermission(NetworkPermission index)
{
packet >> action;
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;
}
ActionsAllowed[byte] ^= (1 << bit);
}
}
void NetworkGroup::Write(NetworkPacket& packet) const
{
packet << Id;
packet.WriteString(GetName().c_str());
for (const auto& action : ActionsAllowed)
bool NetworkGroup::CanPerformAction(NetworkPermission index) const noexcept
{
packet << action;
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;
}
return (ActionsAllowed[byte] & (1 << bit)) != 0;
}
}
void NetworkGroup::ToggleActionPermission(NetworkPermission index)
{
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;
}
ActionsAllowed[byte] ^= (1 << bit);
}
bool NetworkGroup::CanPerformAction(NetworkPermission index) const noexcept
{
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())
bool NetworkGroup::CanPerformCommand(GameCommand command) const
{
NetworkPermission action = NetworkActions::FindCommand(command);
if (action != NetworkPermission::Count)
{
return CanPerformAction(action);
}
return false;
}
return (ActionsAllowed[byte] & (1 << bit)) != 0;
}
bool NetworkGroup::CanPerformCommand(GameCommand command) const
{
NetworkPermission action = NetworkActions::FindCommand(command);
if (action != NetworkPermission::Count)
{
return CanPerformAction(action);
}
return false;
}
} // namespace OpenRCT2::Network
#endif