1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-28 20:45:33 +00:00
openmw-tes3mp/components/openmw-mp/Master/ProxyMasterPacket.hpp

103 lines
2.8 KiB
C++
Raw Normal View History

2017-04-23 05:36:47 +00:00
//
// Created by koncord on 22.04.17.
//
#ifndef OPENMW_PROXYMASTERPACKET_HPP
#define OPENMW_PROXYMASTERPACKET_HPP
#include <components/openmw-mp/Packets/BasePacket.hpp>
#include "MasterData.hpp"
#include <iostream>
namespace mwmp
{
class ProxyMasterPacket : public BasePacket
{
private:
explicit ProxyMasterPacket(RakNet::RakPeerInterface *peer) : BasePacket(peer)
2017-04-23 05:36:47 +00:00
{
}
public:
template<class Packet>
static void addServer(Packet *packet, QueryData &server, bool send)
2017-04-23 05:36:47 +00:00
{
int32_t rulesSize = server.rules.size();
2017-04-23 05:36:47 +00:00
packet->RW(rulesSize, send);
if (rulesSize > QueryData::maxRules)
rulesSize = 0;
std::map<std::string, ServerRule>::iterator ruleIt;
2017-04-23 05:36:47 +00:00
if (send)
ruleIt = server.rules.begin();
while (rulesSize--)
{
ServerRule *rule = nullptr;
std::string key;
2017-04-23 05:36:47 +00:00
if (send)
{
key = ruleIt->first;
rule = &ruleIt->second;
}
packet->RW(key, send, false, QueryData::maxStringLength);
2017-04-23 05:36:47 +00:00
if (!send)
{
ruleIt = server.rules.insert(std::pair<std::string, ServerRule>(key, ServerRule())).first;
2017-04-23 05:36:47 +00:00
rule = &ruleIt->second;
}
packet->RW(rule->type, send);
if (rule->type == ServerRule::Type::string)
packet->RW(rule->str, send, QueryData::maxStringLength);
2017-04-23 05:36:47 +00:00
else
packet->RW(rule->val, send);
if (send)
ruleIt++;
}
std::vector<std::string>::iterator plIt;
2017-04-23 05:36:47 +00:00
int32_t playersCount = server.players.size();
2017-04-27 03:25:11 +00:00
packet->RW(playersCount, send);
if (playersCount > QueryData::maxPlayers)
playersCount = 0;
if (!send)
2017-04-23 05:36:47 +00:00
{
server.players.clear();
server.players.resize(playersCount);
}
2017-04-23 05:36:47 +00:00
for(auto &&player : server.players)
packet->RW(player, send, false, QueryData::maxStringLength);
2017-04-23 05:36:47 +00:00
int32_t pluginsCount = server.plugins.size();
2017-04-23 05:36:47 +00:00
packet->RW(pluginsCount, send);
if (pluginsCount > QueryData::maxPlugins)
pluginsCount = 0;
if (!send)
{
2017-04-23 05:36:47 +00:00
server.plugins.clear();
server.plugins.resize(pluginsCount);
}
2017-04-23 05:36:47 +00:00
for (auto &&plugin : server.plugins)
2017-04-23 05:36:47 +00:00
{
packet->RW(plugin.name, send, false, QueryData::maxStringLength);
2017-04-23 05:36:47 +00:00
packet->RW(plugin.hash, send);
}
}
};
}
#endif //OPENMW_PROXYMASTERPACKET_HPP