[Server] Move server administration functions to ServerFunctions class

0.6.3
David Cernat 7 years ago
parent c0fde5ae97
commit 141e404ed9

@ -64,9 +64,9 @@ set(SERVER
Script/Functions/Books.cpp Script/Functions/Cells.cpp Script/Functions/CharClass.cpp Script/Functions/Books.cpp Script/Functions/Cells.cpp Script/Functions/CharClass.cpp
Script/Functions/Chat.cpp Script/Functions/Dialogue.cpp Script/Functions/Factions.cpp Script/Functions/Chat.cpp Script/Functions/Dialogue.cpp Script/Functions/Factions.cpp
Script/Functions/GUI.cpp Script/Functions/Items.cpp Script/Functions/Mechanics.cpp Script/Functions/GUI.cpp Script/Functions/Items.cpp Script/Functions/Mechanics.cpp
Script/Functions/Positions.cpp Script/Functions/Quests.cpp Script/Functions/Settings.cpp Script/Functions/Positions.cpp Script/Functions/Quests.cpp Script/Functions/Server.cpp
Script/Functions/Shapeshift.cpp Script/Functions/Spells.cpp Script/Functions/Stats.cpp Script/Functions/Settings.cpp Script/Functions/Shapeshift.cpp Script/Functions/Spells.cpp
Script/Functions/Timer.cpp Script/Functions/Stats.cpp Script/Functions/Timer.cpp
Script/API/TimerAPI.cpp Script/API/PublicFnAPI.cpp Script/API/TimerAPI.cpp Script/API/PublicFnAPI.cpp
${LuaScript_Sources} ${LuaScript_Sources}

@ -0,0 +1,89 @@
#include "Server.hpp"
#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>
void ServerFunctions::StopServer(int code) noexcept
{
mwmp::Networking::getPtr()->stopServer(code);
}
void ServerFunctions::Kick(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player,);
mwmp::Networking::getPtr()->kickPlayer(player->guid);
}
void ServerFunctions::BanAddress(const char *ipAddress) noexcept
{
mwmp::Networking::getPtr()->banAddress(ipAddress);
}
void ServerFunctions::UnbanAddress(const char *ipAddress) noexcept
{
mwmp::Networking::getPtr()->unbanAddress(ipAddress);
}
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);
}
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);
}
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);
}

@ -0,0 +1,133 @@
#ifndef OPENMW_SERVERAPI_HPP
#define OPENMW_SERVERAPI_HPP
#include "../Types.hpp"
#define SERVERAPI \
{"StopServer", ServerFunctions::StopServer},\
\
{"Kick", ServerFunctions::Kick},\
{"BanAddress", ServerFunctions::BanAddress},\
{"UnbanAddress", ServerFunctions::UnbanAddress},\
\
{"GetServerVersion", ServerFunctions::GetServerVersion},\
{"GetProtocolVersion", ServerFunctions::GetProtocolVersion},\
{"GetAvgPing", ServerFunctions::GetAvgPing},\
{"GetIP", ServerFunctions::GetIP},\
\
{"SetGameMode", ServerFunctions::SetGameMode},\
{"SetHostname", ServerFunctions::SetHostname},\
{"SetServerPassword", ServerFunctions::SetServerPassword},\
{"SetRuleString", ServerFunctions::SetRuleString},\
{"SetRuleValue", ServerFunctions::SetRuleValue}
class ServerFunctions
{
public:
/**
* \brief Shut down the server.
*
* \param code The shutdown code.
* \return void
*/
static void StopServer(int code) noexcept;
/**
* \brief Kick a certain player from the server.
*
* \param pid The player ID.
* \return void
*/
static void Kick(unsigned short pid) noexcept;
/**
* \brief Ban a certain IP address from the server.
*
* \param ipAddress The IP address.
* \return void
*/
static void BanAddress(const char *ipAddress) noexcept;
/**
* \brief Unban a certain IP address from the server.
*
* \param ipAddress The IP address.
* \return void
*/
static void UnbanAddress(const char *ipAddress) noexcept;
/**
* \brief Get the TES3MP version of the server.
*
* \return The server version.
*/
static const char *GetServerVersion() noexcept;
/**
* \brief Get the protocol version of the server.
*
* \return The protocol version.
*/
static const char *GetProtocolVersion() noexcept;
/**
* \brief Get the average ping of a certain player.
*
* \param pid The player ID.
* \return The average ping.
*/
static int GetAvgPing(unsigned short pid) noexcept;
/**
* \brief Get the IP address of a certain player.
*
* \param pid The player ID.
* \return The IP address.
*/
static const char* GetIP(unsigned short pid) noexcept;
/**
* \brief Set the game mode of the server, as displayed in the server browser.
*
* \param name The new game mode.
* \return void
*/
static void SetGameMode(const char* gameMode) noexcept;
/**
* \brief Set the name of the server, as displayed in the server browser.
*
* \param name The new name.
* \return void
*/
static void SetHostname(const char* name) noexcept;
/**
* \brief Set the password required to join the server.
*
* \param password The password.
* \return void
*/
static void SetServerPassword(const char *passw) noexcept;
/**
* \brief Set a rule string for the server details displayed in the server browser.
*
* \param key The name of the rule.
* \param value The string value of the rule.
* \return void
*/
static void SetRuleString(const char *key, const char *value) noexcept;
/**
* \brief Set a rule value for the server details displayed in the server browser.
*
* \param key The name of the rule.
* \param value The numerical value of the rule.
* \return void
*/
static void SetRuleValue(const char *key, double value) noexcept;
};
#endif //OPENMW_SERVERAPI_HPP

@ -1,7 +1,3 @@
//
// Created by koncord on 24.01.16.
//
#include "ScriptFunctions.hpp" #include "ScriptFunctions.hpp"
#include "API/PublicFnAPI.hpp" #include "API/PublicFnAPI.hpp"
#include <cstdarg> #include <cstdarg>
@ -10,7 +6,6 @@
#include <apps/openmw-mp/Networking.hpp> #include <apps/openmw-mp/Networking.hpp>
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include <components/openmw-mp/Version.hpp> #include <components/openmw-mp/Version.hpp>
#include "MasterClient.hpp"
template<typename... Types> template<typename... Types>
constexpr char TypeString<Types...>::value[]; constexpr char TypeString<Types...>::value[];
@ -95,82 +90,3 @@ boost::any ScriptFunctions::CallPublic(const char *name, va_list args) noexcept
return 0; return 0;
} }
void ScriptFunctions::StopServer(int code) noexcept
{
mwmp::Networking::getPtr()->stopServer(code);
}
void ScriptFunctions::Kick(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player,);
mwmp::Networking::getPtr()->kickPlayer(player->guid);
}
void ScriptFunctions::BanAddress(const char *ipAddress) noexcept
{
mwmp::Networking::getPtr()->banAddress(ipAddress);
}
void ScriptFunctions::UnbanAddress(const char *ipAddress) noexcept
{
mwmp::Networking::getPtr()->unbanAddress(ipAddress);
}
const char *ScriptFunctions::GetServerVersion() noexcept
{
return TES3MP_VERSION;
}
const char *ScriptFunctions::GetProtocolVersion() noexcept
{
static string version = to_string(TES3MP_PROTO_VERSION);
return version.c_str();
}
int ScriptFunctions::GetAvgPing(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, -1);
return mwmp::Networking::get().getAvgPing(player->guid);
}
const char *ScriptFunctions::GetIP(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, "");
RakNet::SystemAddress addr = mwmp::Networking::getPtr()->getSystemAddress(player->guid);
return addr.ToString(false);
}
void ScriptFunctions::SetGameMode(const char *gameMode) noexcept
{
if (mwmp::Networking::getPtr()->getMasterClient())
mwmp::Networking::getPtr()->getMasterClient()->SetModname(gameMode);
}
void ScriptFunctions::SetHostname(const char *name) noexcept
{
if (mwmp::Networking::getPtr()->getMasterClient())
mwmp::Networking::getPtr()->getMasterClient()->SetHostname(name);
}
void ScriptFunctions::SetServerPassword(const char *password) noexcept
{
mwmp::Networking::getPtr()->setServerPassword(password);
}
void ScriptFunctions::SetRuleString(const char *key, const char *value) noexcept
{
auto mc = mwmp::Networking::getPtr()->getMasterClient();
if (mc)
mc->SetRuleString(key, value);
}
void ScriptFunctions::SetRuleValue(const char *key, double value) noexcept
{
auto mc = mwmp::Networking::getPtr()->getMasterClient();
if (mc)
mc->SetRuleValue(key, value);
}

@ -16,6 +16,7 @@
#include <Script/Functions/Positions.hpp> #include <Script/Functions/Positions.hpp>
#include <Script/Functions/Quests.hpp> #include <Script/Functions/Quests.hpp>
#include <Script/Functions/Shapeshift.hpp> #include <Script/Functions/Shapeshift.hpp>
#include <Script/Functions/Server.hpp>
#include <Script/Functions/Settings.hpp> #include <Script/Functions/Settings.hpp>
#include <Script/Functions/Spells.hpp> #include <Script/Functions/Spells.hpp>
#include <Script/Functions/Stats.hpp> #include <Script/Functions/Stats.hpp>
@ -45,14 +46,6 @@ public:
static void MakePublic(ScriptFunc _public, const char *name, char ret_type, const char *def) noexcept; static void MakePublic(ScriptFunc _public, const char *name, char ret_type, const char *def) noexcept;
static boost::any CallPublic(const char *name, va_list args) noexcept; static boost::any CallPublic(const char *name, va_list args) noexcept;
/**
* \brief Shut down the server.
*
* \param code The shutdown code.
* \return void
*/
static void StopServer(int code) noexcept;
/** /**
* \brief Create a timer that will run a script function after a certain interval. * \brief Create a timer that will run a script function after a certain interval.
* *
@ -119,101 +112,6 @@ public:
*/ */
static bool IsTimerElapsed(int timerId) noexcept; static bool IsTimerElapsed(int timerId) noexcept;
/**
* \brief Kick a certain player from the server.
*
* \param pid The player ID.
* \return void
*/
static void Kick(unsigned short pid) noexcept;
/**
* \brief Ban a certain IP address from the server.
*
* \param ipAddress The IP address.
* \return void
*/
static void BanAddress(const char *ipAddress) noexcept;
/**
* \brief Unban a certain IP address from the server.
*
* \param ipAddress The IP address.
* \return void
*/
static void UnbanAddress(const char *ipAddress) noexcept;
/**
* \brief Get the TES3MP version of the server.
*
* \return The server version.
*/
static const char *GetServerVersion() noexcept;
/**
* \brief Get the protocol version of the server.
*
* \return The protocol version.
*/
static const char *GetProtocolVersion() noexcept;
/**
* \brief Get the average ping of a certain player.
*
* \param pid The player ID.
* \return The average ping.
*/
static int GetAvgPing(unsigned short pid) noexcept;
/**
* \brief Get the IP address of a certain player.
*
* \param pid The player ID.
* \return The IP address.
*/
static const char* GetIP(unsigned short pid) noexcept;
/**
* \brief Set the game mode of the server, as displayed in the server browser.
*
* \param name The new game mode.
* \return void
*/
static void SetGameMode(const char* gameMode) noexcept;
/**
* \brief Set the name of the server, as displayed in the server browser.
*
* \param name The new name.
* \return void
*/
static void SetHostname(const char* name) noexcept;
/**
* \brief Set the password required to join the server.
*
* \param password The password.
* \return void
*/
static void SetServerPassword(const char *passw) noexcept;
/**
* \brief Set a rule string for the server details displayed in the server browser.
*
* \param key The name of the rule.
* \param value The string value of the rule.
* \return void
*/
static void SetRuleString(const char *key, const char *value) noexcept;
/**
* \brief Set a rule value for the server details displayed in the server browser.
*
* \param key The name of the rule.
* \param value The numerical value of the rule.
* \return void
*/
static void SetRuleValue(const char *key, double value) noexcept;
static constexpr ScriptFunctionData functions[]{ static constexpr ScriptFunctionData functions[]{
{"CreateTimer", ScriptFunctions::CreateTimer}, {"CreateTimer", ScriptFunctions::CreateTimer},
@ -227,22 +125,6 @@ public:
{"FreeTimer", ScriptFunctions::FreeTimer}, {"FreeTimer", ScriptFunctions::FreeTimer},
{"IsTimerElapsed", ScriptFunctions::IsTimerElapsed}, {"IsTimerElapsed", ScriptFunctions::IsTimerElapsed},
{"StopServer", ScriptFunctions::StopServer},
{"Kick", ScriptFunctions::Kick},
{"BanAddress", ScriptFunctions::BanAddress},
{"UnbanAddress", ScriptFunctions::UnbanAddress},
{"GetServerVersion", ScriptFunctions::GetServerVersion},
{"GetProtocolVersion", ScriptFunctions::GetProtocolVersion},
{"GetAvgPing", ScriptFunctions::GetAvgPing},
{"SetGameMode", ScriptFunctions::SetGameMode},
{"SetHostname", ScriptFunctions::SetHostname},
{"SetServerPassword", ScriptFunctions::SetServerPassword},
{"SetRuleString", ScriptFunctions::SetRuleString},
{"SetRuleValue", ScriptFunctions::SetRuleValue},
{"GetIP", ScriptFunctions::GetIP},
ACTORAPI, ACTORAPI,
BOOKAPI, BOOKAPI,
CELLAPI, CELLAPI,
@ -257,6 +139,7 @@ public:
POSITIONAPI, POSITIONAPI,
QUESTAPI, QUESTAPI,
SHAPESHIFTAPI, SHAPESHIFTAPI,
SERVERAPI,
SETTINGSAPI, SETTINGSAPI,
SPELLAPI, SPELLAPI,
STATAPI, STATAPI,

Loading…
Cancel
Save