From 07a5f5296cb12891030f397f591c050ca7e0c129 Mon Sep 17 00:00:00 2001 From: Koncord Date: Tue, 30 Oct 2018 14:02:27 +0800 Subject: [PATCH] [Server] Rework OnRequestPluginList callback. Add AddPluginHash function --- apps/openmw-mp/Networking.cpp | 43 +++------------------- apps/openmw-mp/Networking.hpp | 3 +- apps/openmw-mp/Script/Functions/Server.cpp | 27 ++++++++++++++ apps/openmw-mp/Script/Functions/Server.hpp | 10 ++++- apps/openmw-mp/Script/ScriptFunctions.hpp | 2 +- 5 files changed, 45 insertions(+), 40 deletions(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 6d2353956..e5979e241 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -446,32 +446,6 @@ RakNet::SystemAddress Networking::getSystemAddress(RakNet::RakNetGUID guid) return peer->GetSystemAddressFromGuid(guid); } -PacketPreInit::PluginContainer Networking::getPluginListSample() -{ - PacketPreInit::PluginContainer pls; - unsigned id = 0; - while (true) - { - unsigned field = 0; - auto name = ""; - Script::Call(name, id, field++); - if (strlen(name) == 0) - break; - PacketPreInit::HashList hashList; - while (true) - { - auto hash = ""; - Script::Call(hash, id, field++); - if (strlen(hash) == 0) - break; - hashList.push_back((unsigned)stoul(hash)); - } - pls.push_back({name, hashList}); - id++; - } - return pls; -} - void Networking::stopServer(int code) { running = false; @@ -595,15 +569,10 @@ void Networking::InitQuery(std::string queryAddr, unsigned short queryPort) void Networking::postInit() { Script::Call(); - samples = getPluginListSample(); - if (mclient) - { - for (auto plugin : samples) - { - if (!plugin.second.empty()) - mclient->PushPlugin({plugin.first, plugin.second[0]}); - else - mclient->PushPlugin({plugin.first, 0}); - } - } + Script::Call(); +} + +PacketPreInit::PluginContainer &Networking::getSamples() +{ + return samples; } diff --git a/apps/openmw-mp/Networking.hpp b/apps/openmw-mp/Networking.hpp index 1814a87ae..f03dd8f08 100644 --- a/apps/openmw-mp/Networking.hpp +++ b/apps/openmw-mp/Networking.hpp @@ -65,9 +65,10 @@ namespace mwmp static Networking *getPtr(); void postInit(); + + PacketPreInit::PluginContainer &getSamples(); private: bool preInit(RakNet::Packet *packet, RakNet::BitStream &bsIn); - PacketPreInit::PluginContainer getPluginListSample(); std::string serverPassword; static Networking *sThis; diff --git a/apps/openmw-mp/Script/Functions/Server.cpp b/apps/openmw-mp/Script/Functions/Server.cpp index 8ac68bf1d..30d1b3d4d 100644 --- a/apps/openmw-mp/Script/Functions/Server.cpp +++ b/apps/openmw-mp/Script/Functions/Server.cpp @@ -105,3 +105,30 @@ void ServerFunctions::SetRuleValue(const char *key, double value) noexcept if (mc) mc->SetRuleValue(key, value); } + +void ServerFunctions::AddPluginHash(const char *pluginName, const char *hashStr) noexcept +{ + auto &samples = mwmp::Networking::getPtr()->getSamples(); + auto it = std::find_if(samples.begin(), samples.end(), [&pluginName](mwmp::PacketPreInit::PluginPair &item) { + return item.first == pluginName; + }); + if (it != samples.end()) + it->second.push_back((unsigned) std::stoul(hashStr)); + else + { + mwmp::PacketPreInit::HashList hashList; + + unsigned hash = 0; + + if (strlen(hashStr) != 0) + { + hash = (unsigned) std::stoul(hashStr); + hashList.push_back(hash); + } + samples.emplace_back(pluginName, hashList); + + auto mclient = mwmp::Networking::getPtr()->getMasterClient(); + if (mclient) + mclient->PushPlugin({pluginName, hash}); + } +} diff --git a/apps/openmw-mp/Script/Functions/Server.hpp b/apps/openmw-mp/Script/Functions/Server.hpp index 21abac154..e75e3d39d 100644 --- a/apps/openmw-mp/Script/Functions/Server.hpp +++ b/apps/openmw-mp/Script/Functions/Server.hpp @@ -22,7 +22,8 @@ {"SetHostname", ServerFunctions::SetHostname},\ {"SetServerPassword", ServerFunctions::SetServerPassword},\ {"SetRuleString", ServerFunctions::SetRuleString},\ - {"SetRuleValue", ServerFunctions::SetRuleValue} + {"SetRuleValue", ServerFunctions::SetRuleValue},\ + {"AddPluginHash", ServerFunctions::AddPluginHash} class ServerFunctions { @@ -152,6 +153,13 @@ public: * \return void */ static void SetRuleValue(const char *key, double value) noexcept; + + /** + * \brief Adds plugins to the internal server structure to validate players. + * @param pluginName Name with extension of the plugin or master file. + * @param hash Hash string + */ + static void AddPluginHash(const char *pluginName, const char *hash) noexcept; }; #endif //OPENMW_SERVERAPI_HPP diff --git a/apps/openmw-mp/Script/ScriptFunctions.hpp b/apps/openmw-mp/Script/ScriptFunctions.hpp index dc32770a3..192aed255 100644 --- a/apps/openmw-mp/Script/ScriptFunctions.hpp +++ b/apps/openmw-mp/Script/ScriptFunctions.hpp @@ -205,7 +205,7 @@ public: {"OnWorldMap", Function()}, {"OnWorldWeather", Function() }, {"OnMpNumIncrement", Function()}, - {"OnRequestPluginList", Function()} + {"OnRequestPluginList", Function()} }; };