From b46767de6e70193bc15311b1d4d8f45554dfe04f Mon Sep 17 00:00:00 2001 From: David Cernat Date: Tue, 19 Mar 2019 03:57:16 +0200 Subject: [PATCH 1/6] [Server] Clean up recent additions to ServerFunctions --- apps/openmw-mp/Script/Functions/Server.cpp | 36 ++++++++++++++-------- apps/openmw-mp/Script/Functions/Server.hpp | 35 ++++++++++++++++----- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Server.cpp b/apps/openmw-mp/Script/Functions/Server.cpp index 95cad4ddd..3df37109b 100644 --- a/apps/openmw-mp/Script/Functions/Server.cpp +++ b/apps/openmw-mp/Script/Functions/Server.cpp @@ -35,6 +35,11 @@ void ServerFunctions::UnbanAddress(const char *ipAddress) noexcept mwmp::Networking::getPtr()->unbanAddress(ipAddress); } +const char* ServerFunctions::GetDataPath() noexcept +{ + return Script::GetModDir(); +} + const char *ServerFunctions::GetOperatingSystemType() noexcept { return Utils::getOperatingSystemType().c_str(); @@ -137,34 +142,41 @@ void ServerFunctions::SetRuleValue(const char *key, double value) noexcept mc->SetRuleValue(key, value); } -void ServerFunctions::AddPluginHash(const char *pluginName, const char *hashStr) noexcept +void ServerFunctions::AddDataFileRequirement(const char *dataFilename, const char *checksumString) 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; + auto it = std::find_if(samples.begin(), samples.end(), [&dataFilename](mwmp::PacketPreInit::PluginPair &item) { + return item.first == dataFilename; }); if (it != samples.end()) - it->second.push_back((unsigned) std::stoul(hashStr)); + it->second.push_back((unsigned) std::stoul(checksumString)); else { - mwmp::PacketPreInit::HashList hashList; + mwmp::PacketPreInit::HashList checksumList; - unsigned hash = 0; + unsigned checksum = 0; - if (strlen(hashStr) != 0) + if (strlen(checksumString) != 0) { - hash = (unsigned) std::stoul(hashStr); - hashList.push_back(hash); + checksum = (unsigned) std::stoul(checksumString); + checksumList.push_back(checksum); } - samples.emplace_back(pluginName, hashList); + samples.emplace_back(dataFilename, checksumList); auto mclient = mwmp::Networking::getPtr()->getMasterClient(); if (mclient) - mclient->PushPlugin({pluginName, hash}); + mclient->PushPlugin({dataFilename, checksum}); } } +// All methods below are deprecated versions of methods from above + const char* ServerFunctions::GetModDir() noexcept { - return Script::GetModDir(); + return GetDataPath(); +} + +void ServerFunctions::AddPluginHash(const char *pluginName, const char *checksumString) noexcept +{ + AddDataFileRequirement(pluginName, checksumString); } diff --git a/apps/openmw-mp/Script/Functions/Server.hpp b/apps/openmw-mp/Script/Functions/Server.hpp index fb698d20c..695b60a77 100644 --- a/apps/openmw-mp/Script/Functions/Server.hpp +++ b/apps/openmw-mp/Script/Functions/Server.hpp @@ -10,6 +10,7 @@ {"BanAddress", ServerFunctions::BanAddress},\ {"UnbanAddress", ServerFunctions::UnbanAddress},\ \ + {"GetDataPath", ServerFunctions::GetDataPath},\ {"GetOperatingSystemType", ServerFunctions::GetOperatingSystemType},\ {"GetArchitectureType", ServerFunctions::GetArchitectureType},\ {"GetServerVersion", ServerFunctions::GetServerVersion},\ @@ -29,8 +30,11 @@ {"SetScriptErrorIgnoringState", ServerFunctions::SetScriptErrorIgnoringState},\ {"SetRuleString", ServerFunctions::SetRuleString},\ {"SetRuleValue", ServerFunctions::SetRuleValue},\ - {"AddPluginHash", ServerFunctions::AddPluginHash},\ - {"GetModDir", ServerFunctions::GetModDir} + \ + {"AddDataFileRequirement", ServerFunctions::AddDataFileRequirement},\ + \ + {"GetModDir", ServerFunctions::GetModDir},\ + {"AddPluginHash", ServerFunctions::AddPluginHash} class ServerFunctions { @@ -68,6 +72,13 @@ public: */ static void UnbanAddress(const char *ipAddress) noexcept; + /** + * \brief Get the path of the server's data folder. + * + * \return The data path. + */ + static const char *GetDataPath() noexcept; + /** * \brief Get the type of the operating system used by the server. * @@ -119,7 +130,7 @@ public: /** * \brief Get the port used by the server. * - * \return Port + * \return The port. */ static unsigned short GetPort() noexcept; @@ -220,13 +231,23 @@ public: 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 + * \brief Add a data file and a corresponding CRC32 checksum to the data file loadout + * that connecting clients need to match. + * + * It can be used multiple times to set multiple checksums for the same data file. + * + * Note: If an empty string is provided for the checksum, a checksum will not be + * required for that data file. + * + * @param dataFilename The filename of the data file. + * @param checksumString A string with the CRC32 checksum required. */ - static void AddPluginHash(const char *pluginName, const char *hash) noexcept; + static void AddDataFileRequirement(const char *dataFilename, const char *checksumString) noexcept; + + // All methods below are deprecated versions of methods from above static const char *GetModDir() noexcept; + static void AddPluginHash(const char *pluginName, const char *checksumString) noexcept; }; #endif //OPENMW_SERVERAPI_HPP From 2cdabddc0e53d700230e79ebd0fcd2c1b8472801 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Tue, 19 Mar 2019 04:25:33 +0200 Subject: [PATCH 2/6] [Server] Move most MiscellaneousFunctions to ServerFunctions --- .../Script/Functions/Miscellaneous.cpp | 36 ------------- .../Script/Functions/Miscellaneous.hpp | 53 +------------------ apps/openmw-mp/Script/Functions/Server.cpp | 34 ++++++++++++ apps/openmw-mp/Script/Functions/Server.hpp | 50 +++++++++++++++++ 4 files changed, 85 insertions(+), 88 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Miscellaneous.cpp b/apps/openmw-mp/Script/Functions/Miscellaneous.cpp index d9e55e4c8..1a0aff916 100644 --- a/apps/openmw-mp/Script/Functions/Miscellaneous.cpp +++ b/apps/openmw-mp/Script/Functions/Miscellaneous.cpp @@ -1,38 +1,12 @@ #include "Miscellaneous.hpp" -#include #include -#include #include #include using namespace std; -static std::string tempFilename; - -bool MiscellaneousFunctions::DoesFileExist(const char *filePath) noexcept -{ - return boost::filesystem::exists(filePath); -} - -const char *MiscellaneousFunctions::GetCaseInsensitiveFilename(const char *folderPath, const char *filename) noexcept -{ - if (!boost::filesystem::exists(folderPath)) return "invalid"; - - boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end - - for (boost::filesystem::directory_iterator itr(folderPath); itr != end_itr; ++itr) - { - if (Misc::StringUtils::ciEqual(itr->path().filename().string(), filename)) - { - tempFilename = itr->path().filename().string(); - return tempFilename.c_str(); - } - } - return "invalid"; -} - unsigned int MiscellaneousFunctions::GetLastPlayerId() noexcept { return Players::getLastPlayerId(); @@ -47,13 +21,3 @@ void MiscellaneousFunctions::SetCurrentMpNum(int mpNum) noexcept { mwmp::Networking::getPtr()->setCurrentMpNum(mpNum); } - -void MiscellaneousFunctions::LogMessage(unsigned short level, const char *message) noexcept -{ - LOG_MESSAGE_SIMPLE(level, "[Script]: %s", message); -} - -void MiscellaneousFunctions::LogAppend(unsigned short level, const char *message) noexcept -{ - LOG_APPEND(level, "[Script]: %s", message); -} diff --git a/apps/openmw-mp/Script/Functions/Miscellaneous.hpp b/apps/openmw-mp/Script/Functions/Miscellaneous.hpp index bd3f211c7..96b646579 100644 --- a/apps/openmw-mp/Script/Functions/Miscellaneous.hpp +++ b/apps/openmw-mp/Script/Functions/Miscellaneous.hpp @@ -4,42 +4,15 @@ #include "../Types.hpp" #define MISCELLANEOUSAPI \ - {"DoesFileExist", MiscellaneousFunctions::DoesFileExist},\ - {"GetCaseInsensitiveFilename", MiscellaneousFunctions::GetCaseInsensitiveFilename},\ - \ {"GetLastPlayerId", MiscellaneousFunctions::GetLastPlayerId},\ \ {"GetCurrentMpNum", MiscellaneousFunctions::GetCurrentMpNum},\ - {"SetCurrentMpNum", MiscellaneousFunctions::SetCurrentMpNum},\ - \ - {"LogMessage", MiscellaneousFunctions::LogMessage},\ - {"LogAppend", MiscellaneousFunctions::LogAppend} + {"SetCurrentMpNum", MiscellaneousFunctions::SetCurrentMpNum} class MiscellaneousFunctions { public: - /** - * \brief Check whether a certain file exists. - * - * This will be a case sensitive check on case sensitive filesystems. - * - * Whenever you want to enforce case insensitivity, use GetCaseInsensitiveFilename() instead. - * - * \return Whether the file exists or not. - */ - static bool DoesFileExist(const char *filePath) noexcept; - - /** - * \brief Get the first filename in a folder that has a case insensitive match with the filename - * argument. - * - * This is used to retain case insensitivity when opening data files on Linux. - * - * \return The filename that matches. - */ - static const char *GetCaseInsensitiveFilename(const char *folderPath, const char *filename) noexcept; - /** * \brief Get the last player ID currently connected to the server. * @@ -75,30 +48,6 @@ public: * \return void */ static void SetCurrentMpNum(int mpNum) noexcept; - - /** - * \brief Write a log message with its own timestamp. - * - * It will have "[Script]:" prepended to it so as to mark it as a script-generated log message. - * - * \param level The logging level used (0 for LOG_VERBOSE, 1 for LOG_INFO, 2 for LOG_WARN, - * 3 for LOG_ERROR, 4 for LOG_FATAL). - * \param message The message logged. - * \return void - */ - static void LogMessage(unsigned short level, const char *message) noexcept; - - /** - * \brief Write a log message without its own timestamp. - * - * It will have "[Script]:" prepended to it so as to mark it as a script-generated log message. - * - * \param level The logging level used (0 for LOG_VERBOSE, 1 for LOG_INFO, 2 for LOG_WARN, - * 3 for LOG_ERROR, 4 for LOG_FATAL). - * \param message The message logged. - * \return void - */ - static void LogAppend(unsigned short level, const char *message) noexcept; }; #endif //OPENMW_MISCELLANEOUSAPI_HPP diff --git a/apps/openmw-mp/Script/Functions/Server.cpp b/apps/openmw-mp/Script/Functions/Server.cpp index 3df37109b..e4f1e86f7 100644 --- a/apps/openmw-mp/Script/Functions/Server.cpp +++ b/apps/openmw-mp/Script/Functions/Server.cpp @@ -1,5 +1,6 @@ #include "Server.hpp" +#include #include #include #include @@ -9,6 +10,17 @@ #include #include