2018-07-07 16:40:22 +00:00
|
|
|
#include "Server.hpp"
|
|
|
|
|
2019-03-19 02:25:33 +00:00
|
|
|
#include <components/misc/stringops.hpp>
|
2018-07-07 16:40:22 +00:00
|
|
|
#include <components/openmw-mp/NetworkMessages.hpp>
|
|
|
|
#include <components/openmw-mp/Log.hpp>
|
|
|
|
#include <components/openmw-mp/Version.hpp>
|
|
|
|
|
|
|
|
#include <apps/openmw-mp/Script/ScriptFunctions.hpp>
|
|
|
|
#include <apps/openmw-mp/Networking.hpp>
|
|
|
|
#include <apps/openmw-mp/MasterClient.hpp>
|
2018-12-29 03:40:31 +00:00
|
|
|
#include <Script/Script.hpp>
|
2018-07-07 16:40:22 +00:00
|
|
|
|
2019-03-19 02:25:33 +00:00
|
|
|
static std::string tempFilename;
|
|
|
|
|
|
|
|
void ServerFunctions::LogMessage(unsigned short level, const char *message) noexcept
|
|
|
|
{
|
|
|
|
LOG_MESSAGE_SIMPLE(level, "[Script]: %s", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::LogAppend(unsigned short level, const char *message) noexcept
|
|
|
|
{
|
|
|
|
LOG_APPEND(level, "[Script]: %s", message);
|
|
|
|
}
|
2018-07-07 16:40:22 +00:00
|
|
|
|
|
|
|
void ServerFunctions::StopServer(int code) noexcept
|
|
|
|
{
|
|
|
|
mwmp::Networking::getPtr()->stopServer(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::Kick(unsigned short pid) noexcept
|
|
|
|
{
|
|
|
|
Player *player;
|
|
|
|
GET_PLAYER(pid, player,);
|
2018-10-13 06:34:29 +00:00
|
|
|
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Kicking player %s (%i)", player->npc.mName.c_str(), player->getId());
|
2018-07-07 16:40:22 +00:00
|
|
|
mwmp::Networking::getPtr()->kickPlayer(player->guid);
|
2018-10-30 04:56:50 +00:00
|
|
|
player->setLoadState(Player::KICKED);
|
2018-07-07 16:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::BanAddress(const char *ipAddress) noexcept
|
|
|
|
{
|
|
|
|
mwmp::Networking::getPtr()->banAddress(ipAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::UnbanAddress(const char *ipAddress) noexcept
|
|
|
|
{
|
|
|
|
mwmp::Networking::getPtr()->unbanAddress(ipAddress);
|
|
|
|
}
|
|
|
|
|
2019-03-19 02:52:58 +00:00
|
|
|
bool ServerFunctions::DoesFilePathExist(const char *filePath) noexcept
|
2019-03-19 02:25:33 +00:00
|
|
|
{
|
|
|
|
return boost::filesystem::exists(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ServerFunctions::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";
|
|
|
|
}
|
|
|
|
|
2019-03-19 01:57:16 +00:00
|
|
|
const char* ServerFunctions::GetDataPath() noexcept
|
|
|
|
{
|
|
|
|
return Script::GetModDir();
|
|
|
|
}
|
|
|
|
|
2018-12-17 09:32:31 +00:00
|
|
|
const char *ServerFunctions::GetOperatingSystemType() noexcept
|
|
|
|
{
|
2018-12-17 09:55:50 +00:00
|
|
|
return Utils::getOperatingSystemType().c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ServerFunctions::GetArchitectureType() noexcept
|
|
|
|
{
|
|
|
|
return Utils::getArchitectureType().c_str();
|
2018-12-17 09:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 16:40:22 +00:00
|
|
|
const char *ServerFunctions::GetServerVersion() noexcept
|
|
|
|
{
|
|
|
|
return TES3MP_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ServerFunctions::GetProtocolVersion() noexcept
|
|
|
|
{
|
|
|
|
static std::string version = std::to_string(TES3MP_PROTO_VERSION);
|
|
|
|
return version.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ServerFunctions::GetAvgPing(unsigned short pid) noexcept
|
|
|
|
{
|
|
|
|
Player *player;
|
|
|
|
GET_PLAYER(pid, player, -1);
|
|
|
|
return mwmp::Networking::get().getAvgPing(player->guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ServerFunctions::GetIP(unsigned short pid) noexcept
|
|
|
|
{
|
|
|
|
Player *player;
|
|
|
|
GET_PLAYER(pid, player, "");
|
|
|
|
RakNet::SystemAddress addr = mwmp::Networking::getPtr()->getSystemAddress(player->guid);
|
|
|
|
return addr.ToString(false);
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:19:34 +00:00
|
|
|
unsigned short ServerFunctions::GetPort() noexcept
|
|
|
|
{
|
|
|
|
return mwmp::Networking::get().getPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ServerFunctions::GetMaxPlayers() noexcept
|
|
|
|
{
|
|
|
|
return mwmp::Networking::get().maxConnections();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServerFunctions::HasPassword() noexcept
|
|
|
|
{
|
|
|
|
return mwmp::Networking::get().isPassworded();
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:03:39 +00:00
|
|
|
bool ServerFunctions::GetPluginEnforcementState() noexcept
|
2018-11-30 20:43:10 +00:00
|
|
|
{
|
|
|
|
return mwmp::Networking::getPtr()->getPluginEnforcementState();
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:03:39 +00:00
|
|
|
bool ServerFunctions::GetScriptErrorIgnoringState() noexcept
|
|
|
|
{
|
|
|
|
return mwmp::Networking::getPtr()->getScriptErrorIgnoringState();
|
|
|
|
}
|
|
|
|
|
2018-07-07 16:40:22 +00:00
|
|
|
void ServerFunctions::SetGameMode(const char *gameMode) noexcept
|
|
|
|
{
|
|
|
|
if (mwmp::Networking::getPtr()->getMasterClient())
|
|
|
|
mwmp::Networking::getPtr()->getMasterClient()->SetModname(gameMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::SetHostname(const char *name) noexcept
|
|
|
|
{
|
|
|
|
if (mwmp::Networking::getPtr()->getMasterClient())
|
|
|
|
mwmp::Networking::getPtr()->getMasterClient()->SetHostname(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::SetServerPassword(const char *password) noexcept
|
|
|
|
{
|
|
|
|
mwmp::Networking::getPtr()->setServerPassword(password);
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:43:10 +00:00
|
|
|
void ServerFunctions::SetPluginEnforcementState(bool state) noexcept
|
|
|
|
{
|
|
|
|
mwmp::Networking::getPtr()->setPluginEnforcementState(state);
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:03:39 +00:00
|
|
|
void ServerFunctions::SetScriptErrorIgnoringState(bool state) noexcept
|
|
|
|
{
|
|
|
|
mwmp::Networking::getPtr()->setScriptErrorIgnoringState(state);
|
|
|
|
}
|
|
|
|
|
2018-07-07 16:40:22 +00:00
|
|
|
void ServerFunctions::SetRuleString(const char *key, const char *value) noexcept
|
|
|
|
{
|
|
|
|
auto mc = mwmp::Networking::getPtr()->getMasterClient();
|
|
|
|
if (mc)
|
|
|
|
mc->SetRuleString(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::SetRuleValue(const char *key, double value) noexcept
|
|
|
|
{
|
|
|
|
auto mc = mwmp::Networking::getPtr()->getMasterClient();
|
|
|
|
if (mc)
|
|
|
|
mc->SetRuleValue(key, value);
|
|
|
|
}
|
2018-10-30 06:02:27 +00:00
|
|
|
|
2019-03-19 01:57:16 +00:00
|
|
|
void ServerFunctions::AddDataFileRequirement(const char *dataFilename, const char *checksumString) noexcept
|
2018-10-30 06:02:27 +00:00
|
|
|
{
|
|
|
|
auto &samples = mwmp::Networking::getPtr()->getSamples();
|
2019-03-19 01:57:16 +00:00
|
|
|
auto it = std::find_if(samples.begin(), samples.end(), [&dataFilename](mwmp::PacketPreInit::PluginPair &item) {
|
|
|
|
return item.first == dataFilename;
|
2018-10-30 06:02:27 +00:00
|
|
|
});
|
|
|
|
if (it != samples.end())
|
2019-03-19 01:57:16 +00:00
|
|
|
it->second.push_back((unsigned) std::stoul(checksumString));
|
2018-10-30 06:02:27 +00:00
|
|
|
else
|
|
|
|
{
|
2019-03-19 01:57:16 +00:00
|
|
|
mwmp::PacketPreInit::HashList checksumList;
|
2018-10-30 06:02:27 +00:00
|
|
|
|
2019-03-19 01:57:16 +00:00
|
|
|
unsigned checksum = 0;
|
2018-10-30 06:02:27 +00:00
|
|
|
|
2019-03-19 01:57:16 +00:00
|
|
|
if (strlen(checksumString) != 0)
|
2018-10-30 06:02:27 +00:00
|
|
|
{
|
2019-03-19 01:57:16 +00:00
|
|
|
checksum = (unsigned) std::stoul(checksumString);
|
|
|
|
checksumList.push_back(checksum);
|
2018-10-30 06:02:27 +00:00
|
|
|
}
|
2019-03-19 01:57:16 +00:00
|
|
|
samples.emplace_back(dataFilename, checksumList);
|
2018-10-30 06:02:27 +00:00
|
|
|
|
|
|
|
auto mclient = mwmp::Networking::getPtr()->getMasterClient();
|
|
|
|
if (mclient)
|
2019-03-19 01:57:16 +00:00
|
|
|
mclient->PushPlugin({dataFilename, checksum});
|
2018-10-30 06:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 03:40:31 +00:00
|
|
|
|
2019-03-19 01:57:16 +00:00
|
|
|
// All methods below are deprecated versions of methods from above
|
|
|
|
|
2019-03-19 02:52:58 +00:00
|
|
|
bool ServerFunctions::DoesFileExist(const char *filePath) noexcept
|
|
|
|
{
|
|
|
|
return DoesFilePathExist(filePath);
|
|
|
|
}
|
|
|
|
|
2018-12-29 03:40:31 +00:00
|
|
|
const char* ServerFunctions::GetModDir() noexcept
|
|
|
|
{
|
2019-03-19 01:57:16 +00:00
|
|
|
return GetDataPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerFunctions::AddPluginHash(const char *pluginName, const char *checksumString) noexcept
|
|
|
|
{
|
|
|
|
AddDataFileRequirement(pluginName, checksumString);
|
2018-12-29 03:40:31 +00:00
|
|
|
}
|