mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
migrate codebase to new network
marked non-implemented stuff with an exception
This commit is contained in:
parent
e0fe6693e0
commit
b06991aaca
@ -14,7 +14,7 @@ namespace MP {
|
||||
std::tuple<int, int, int> GetServerVersion();
|
||||
std::pair<bool, std::string> TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& Data);
|
||||
std::pair<bool, std::string> TriggerClientEventJson(int PlayerID, const std::string& EventName, const sol::table& Data);
|
||||
inline size_t GetPlayerCount() { return Engine->Server().ClientCount(); }
|
||||
inline size_t GetPlayerCount() { return Engine->Network()->authenticated_client_count(); }
|
||||
std::pair<bool, std::string> DropPlayer(int ID, std::optional<std::string> MaybeReason);
|
||||
std::pair<bool, std::string> SendChatMessage(int ID, const std::string& Message);
|
||||
std::pair<bool, std::string> RemoveVehicle(int PlayerID, int VehicleID);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cryptography.h"
|
||||
#include "TScopedTimer.h"
|
||||
#include "commandline.h"
|
||||
#include <atomic>
|
||||
#include <fstream>
|
||||
@ -69,4 +70,5 @@ private:
|
||||
const std::string mDefaultStateId = "BEAMMP_SERVER_CONSOLE";
|
||||
std::ofstream mLogFileStream;
|
||||
std::mutex mLogFileStreamMtx;
|
||||
TScopedTimer mUptimeTimer{};
|
||||
};
|
||||
|
@ -2,10 +2,11 @@
|
||||
|
||||
#include "Common.h"
|
||||
#include "IThreaded.h"
|
||||
#include "Network.h"
|
||||
|
||||
class THeartbeatThread : public IThreaded {
|
||||
public:
|
||||
THeartbeatThread(TResourceManager& ResourceManager, TServer& Server);
|
||||
THeartbeatThread(std::shared_ptr<Network> network);
|
||||
//~THeartbeatThread();
|
||||
void operator()() override;
|
||||
|
||||
@ -13,6 +14,5 @@ private:
|
||||
std::string GenerateCall();
|
||||
std::string GetPlayers();
|
||||
|
||||
TResourceManager& mResourceManager;
|
||||
TServer& mServer;
|
||||
std::shared_ptr<Network> m_network;
|
||||
};
|
||||
|
@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common.h"
|
||||
#include "IThreaded.h"
|
||||
#include "Network.h"
|
||||
#include <any>
|
||||
#include <boost/thread/scoped_thread.hpp>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <initializer_list>
|
||||
@ -67,7 +70,7 @@ struct TLuaChunk {
|
||||
std::string PluginPath;
|
||||
};
|
||||
|
||||
class TLuaEngine : public std::enable_shared_from_this<TLuaEngine>, IThreaded {
|
||||
class TLuaEngine : public std::enable_shared_from_this<TLuaEngine> {
|
||||
public:
|
||||
enum CallStrategy : int {
|
||||
BestEffort,
|
||||
@ -86,13 +89,11 @@ public:
|
||||
beammp_debug("Lua Engine terminated");
|
||||
}
|
||||
|
||||
void operator()() override;
|
||||
void Start();
|
||||
|
||||
TNetwork& Network() { return *mNetwork; }
|
||||
TServer& Server() { return *mServer; }
|
||||
std::shared_ptr<::Network> Network() { return mNetwork; }
|
||||
|
||||
void SetNetwork(TNetwork* Network) { mNetwork = Network; }
|
||||
void SetServer(TServer* Server) { mServer = Server; }
|
||||
void SetNetwork(const std::shared_ptr<::Network>& network) { mNetwork = network; }
|
||||
|
||||
size_t GetResultsToCheckSize() {
|
||||
std::unique_lock Lock(mResultsToCheckMutex);
|
||||
@ -200,7 +201,7 @@ private:
|
||||
void FindAndParseConfig(const fs::path& Folder, TLuaPluginConfig& Config);
|
||||
size_t CalculateMemoryUsage();
|
||||
|
||||
class StateThreadData : IThreaded {
|
||||
class StateThreadData {
|
||||
public:
|
||||
StateThreadData(const std::string& Name, TLuaStateId StateId, TLuaEngine& Engine);
|
||||
StateThreadData(const StateThreadData&) = delete;
|
||||
@ -210,7 +211,7 @@ private:
|
||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args, const std::string& EventName, CallStrategy Strategy);
|
||||
void RegisterEvent(const std::string& EventName, const std::string& FunctionName);
|
||||
void AddPath(const fs::path& Path); // to be added to path and cpath
|
||||
void operator()() override;
|
||||
void Start();
|
||||
sol::state_view State() { return sol::state_view(mState); }
|
||||
|
||||
std::vector<std::string> GetStateGlobalKeys();
|
||||
@ -219,7 +220,7 @@ private:
|
||||
// Debug functions, slow
|
||||
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> Debug_GetStateExecuteQueue();
|
||||
std::vector<TLuaEngine::QueuedFunction> Debug_GetStateFunctionQueue();
|
||||
|
||||
|
||||
sol::table Lua_JsonDecode(const std::string& str);
|
||||
|
||||
private:
|
||||
@ -229,7 +230,7 @@ private:
|
||||
sol::table Lua_GetPlayers();
|
||||
std::string Lua_GetPlayerName(int ID);
|
||||
sol::table Lua_GetPlayerVehicles(int ID);
|
||||
std::pair<sol::table, std::string> Lua_GetPositionRaw(int PID, int VID);
|
||||
std::pair<sol::table, std::string> Lua_GetVehicleStatus(int VID);
|
||||
sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port);
|
||||
int Lua_GetPlayerIDByName(const std::string& Name);
|
||||
sol::table Lua_FS_ListFiles(const std::string& Path);
|
||||
@ -238,7 +239,6 @@ private:
|
||||
std::string mName;
|
||||
TLuaStateId mStateId;
|
||||
lua_State* mState;
|
||||
std::thread mThread;
|
||||
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> mStateExecuteQueue;
|
||||
std::recursive_mutex mStateExecuteQueueMutex;
|
||||
std::vector<QueuedFunction> mStateFunctionQueue;
|
||||
@ -250,6 +250,7 @@ private:
|
||||
std::recursive_mutex mPathsMutex;
|
||||
std::mt19937 mMersenneTwister;
|
||||
std::uniform_real_distribution<double> mUniformRealDistribution01;
|
||||
boost::scoped_thread<> mThread;
|
||||
};
|
||||
|
||||
struct TimedEvent {
|
||||
@ -262,8 +263,7 @@ private:
|
||||
void Reset();
|
||||
};
|
||||
|
||||
TNetwork* mNetwork;
|
||||
TServer* mServer;
|
||||
std::shared_ptr<::Network> mNetwork;
|
||||
const fs::path mResourceServerPath;
|
||||
std::vector<std::shared_ptr<TLuaPlugin>> mLuaPlugins;
|
||||
std::unordered_map<TLuaStateId, std::unique_ptr<StateThreadData>> mLuaStates;
|
||||
@ -275,6 +275,7 @@ private:
|
||||
std::list<std::shared_ptr<TLuaResult>> mResultsToCheck;
|
||||
std::mutex mResultsToCheckMutex;
|
||||
std::condition_variable mResultsToCheckCond;
|
||||
boost::scoped_thread<> mThread;
|
||||
};
|
||||
|
||||
// std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaPlugin* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);
|
||||
|
@ -113,24 +113,26 @@ TEST_CASE("LuaAPI::MP::GetServerVersion") {
|
||||
}
|
||||
|
||||
static inline std::pair<bool, std::string> InternalTriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data) {
|
||||
std::string Packet = "E:" + EventName + ":" + Data;
|
||||
if (PlayerID == -1) {
|
||||
LuaAPI::MP::Engine->Network().SendToAll(nullptr, StringToVector(Packet), true, true);
|
||||
return { true, "" };
|
||||
} else {
|
||||
auto MaybeClient = GetClient(LuaAPI::MP::Engine->Server(), PlayerID);
|
||||
if (!MaybeClient) {
|
||||
beammp_lua_errorf("TriggerClientEvent invalid Player ID '{}'", PlayerID);
|
||||
return { false, "Invalid Player ID" };
|
||||
}
|
||||
auto c = MaybeClient.value();
|
||||
if (!LuaAPI::MP::Engine->Network().Respond(*c, StringToVector(Packet), true)) {
|
||||
beammp_lua_errorf("Respond failed, dropping client {}", PlayerID);
|
||||
LuaAPI::MP::Engine->Network().Disconnect(*c);
|
||||
return { false, "Respond failed, dropping client" };
|
||||
}
|
||||
return { true, "" };
|
||||
}
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*
|
||||
std::string Packet = "E:" + EventName + ":" + Data;
|
||||
if (PlayerID == -1) {
|
||||
LuaAPI::MP::Engine->Network().SendToAll(nullptr, StringToVector(Packet), true, true);
|
||||
return { true, "" };
|
||||
} else {
|
||||
auto MaybeClient = GetClient(LuaAPI::MP::Engine->Server(), PlayerID);
|
||||
if (!MaybeClient) {
|
||||
beammp_lua_errorf("TriggerClientEvent invalid Player ID '{}'", PlayerID);
|
||||
return { false, "Invalid Player ID" };
|
||||
}
|
||||
auto c = MaybeClient.value();
|
||||
if (!LuaAPI::MP::Engine->Network().Respond(*c, StringToVector(Packet), true)) {
|
||||
beammp_lua_errorf("Respond failed, dropping client {}", PlayerID);
|
||||
LuaAPI::MP::Engine->Network().Disconnect(*c);
|
||||
return { false, "Respond failed, dropping client" };
|
||||
}
|
||||
return { true, "" };
|
||||
}*/
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> LuaAPI::MP::TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& DataObj) {
|
||||
@ -139,6 +141,8 @@ std::pair<bool, std::string> LuaAPI::MP::TriggerClientEvent(int PlayerID, const
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> LuaAPI::MP::DropPlayer(int ID, std::optional<std::string> MaybeReason) {
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*
|
||||
auto MaybeClient = GetClient(Engine->Server(), ID);
|
||||
if (!MaybeClient) {
|
||||
beammp_lua_errorf("Tried to drop client with id {}, who doesn't exist", ID);
|
||||
@ -147,9 +151,12 @@ std::pair<bool, std::string> LuaAPI::MP::DropPlayer(int ID, std::optional<std::s
|
||||
auto c = MaybeClient.value();
|
||||
LuaAPI::MP::Engine->Network().ClientKick(*c, MaybeReason.value_or("No reason"));
|
||||
return { true, "" };
|
||||
*/
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> LuaAPI::MP::SendChatMessage(int ID, const std::string& Message) {
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*
|
||||
std::pair<bool, std::string> Result;
|
||||
std::string Packet = "C:Server: " + Message;
|
||||
if (ID == -1) {
|
||||
@ -180,9 +187,12 @@ std::pair<bool, std::string> LuaAPI::MP::SendChatMessage(int ID, const std::stri
|
||||
return Result;
|
||||
}
|
||||
return Result;
|
||||
*/
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> LuaAPI::MP::RemoveVehicle(int PID, int VID) {
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*
|
||||
std::pair<bool, std::string> Result;
|
||||
auto MaybeClient = GetClient(Engine->Server(), PID);
|
||||
if (!MaybeClient) {
|
||||
@ -202,6 +212,7 @@ std::pair<bool, std::string> LuaAPI::MP::RemoveVehicle(int PID, int VID) {
|
||||
Result.second = "Vehicle does not exist";
|
||||
}
|
||||
return Result;
|
||||
*/
|
||||
}
|
||||
|
||||
void LuaAPI::MP::Set(int ConfigID, sol::object NewValue) {
|
||||
@ -273,21 +284,26 @@ void LuaAPI::MP::Sleep(size_t Ms) {
|
||||
}
|
||||
|
||||
bool LuaAPI::MP::IsPlayerConnected(int ID) {
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*
|
||||
auto MaybeClient = GetClient(Engine->Server(), ID);
|
||||
if (MaybeClient) {
|
||||
return MaybeClient.value()->IsConnected.get();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
bool LuaAPI::MP::IsPlayerGuest(int ID) {
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*
|
||||
auto MaybeClient = GetClient(Engine->Server(), ID);
|
||||
if (MaybeClient) {
|
||||
return MaybeClient.value()->IsGuest.get();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void LuaAPI::MP::PrintRaw(sol::variadic_args Args) {
|
||||
|
@ -265,14 +265,15 @@ void TConsole::Command_Kick(const std::string&, const std::vector<std::string>&
|
||||
std::for_each(Name2.begin(), Name2.end(), [](char& c) { c = char(std::tolower(char(c))); });
|
||||
return StringStartsWith(Name1, Name2) || StringStartsWith(Name2, Name1);
|
||||
};
|
||||
mLuaEngine->Server().ForEachClient([&](const std::shared_ptr<TClient>& Client) -> bool {
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*mLuaEngine->Server().ForEachClient([&](const std::shared_ptr<TClient>& Client) -> bool {
|
||||
if (NameCompare(Client->Name.get(), Name)) {
|
||||
mLuaEngine->Network().ClientKick(*Client, Reason);
|
||||
Kicked = true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
});*/
|
||||
if (!Kicked) {
|
||||
Application::Console().WriteRaw("Error: No player with name matching '" + Name + "' was found.");
|
||||
} else {
|
||||
@ -355,6 +356,9 @@ void TConsole::Command_List(const std::string&, const std::vector<std::string>&
|
||||
if (!EnsureArgsCount(args, 0)) {
|
||||
return;
|
||||
}
|
||||
// Implement once running
|
||||
throw std::runtime_error(fmt::format("NOT IMPLEMENTED: {}", __func__));
|
||||
/*
|
||||
if (mLuaEngine->Server().ClientCount() == 0) {
|
||||
Application::Console().WriteRaw("No players online.");
|
||||
} else {
|
||||
@ -369,6 +373,7 @@ void TConsole::Command_List(const std::string&, const std::vector<std::string>&
|
||||
auto Str = ss.str();
|
||||
Application::Console().WriteRaw(Str.substr(0, Str.size() - 1));
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void TConsole::Command_Status(const std::string&, const std::vector<std::string>& args) {
|
||||
@ -377,26 +382,6 @@ void TConsole::Command_Status(const std::string&, const std::vector<std::string>
|
||||
}
|
||||
std::stringstream Status;
|
||||
|
||||
size_t CarCount = 0;
|
||||
size_t ConnectedCount = 0;
|
||||
size_t GuestCount = 0;
|
||||
size_t SyncedCount = 0;
|
||||
size_t SyncingCount = 0;
|
||||
size_t MissedPacketQueueSum = 0;
|
||||
int LargestSecondsSinceLastPing = 0;
|
||||
mLuaEngine->Server().ForEachClient([&](std::shared_ptr<TClient> Client) -> bool {
|
||||
CarCount += size_t(Client->GetCarCount());
|
||||
ConnectedCount += Client->IsConnected ? 1 : 0;
|
||||
GuestCount += Client->IsGuest ? 1 : 0;
|
||||
SyncedCount += Client->IsSynced ? 1 : 0;
|
||||
SyncingCount += Client->IsSyncing ? 1 : 0;
|
||||
MissedPacketQueueSum += Client->MissedPacketsQueue->size();
|
||||
if (Client->SecondsSinceLastPing() < LargestSecondsSinceLastPing) {
|
||||
LargestSecondsSinceLastPing = Client->SecondsSinceLastPing();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
size_t SystemsStarting = 0;
|
||||
size_t SystemsGood = 0;
|
||||
size_t SystemsBad = 0;
|
||||
@ -441,15 +426,21 @@ void TConsole::Command_Status(const std::string&, const std::vector<std::string>
|
||||
SystemsShuttingDownList = SystemsShuttingDownList.substr(0, SystemsShuttingDownList.size() - 2);
|
||||
SystemsShutdownList = SystemsShutdownList.substr(0, SystemsShutdownList.size() - 2);
|
||||
|
||||
auto ElapsedTime = mLuaEngine->Server().UptimeTimer.GetElapsedTime();
|
||||
auto ElapsedTime = mUptimeTimer.GetElapsedTime();
|
||||
|
||||
auto network = mLuaEngine->Network();
|
||||
auto clients = network->all_clients();
|
||||
|
||||
Status << "BeamMP-Server Status:\n"
|
||||
<< "\tTotal Players: " << mLuaEngine->Server().ClientCount() << "\n"
|
||||
<< "\tSyncing Players: " << SyncingCount << "\n"
|
||||
<< "\tSynced Players: " << SyncedCount << "\n"
|
||||
<< "\tConnected Players: " << ConnectedCount << "\n"
|
||||
<< "\tGuests: " << GuestCount << "\n"
|
||||
<< "\tCars: " << CarCount << "\n"
|
||||
<< "\tTotal Players: " << clients.size() << "\n"
|
||||
<< "\tPlayers identifying: " << network->clients_in_state_count(bmp::State::Identification) << "\n"
|
||||
<< "\tPlayers authenticating: " << network->clients_in_state_count(bmp::State::Authentication) << "\n"
|
||||
<< "\tPlayers downloading mods: " << network->clients_in_state_count(bmp::State::ModDownload) << "\n"
|
||||
<< "\tPlayers spawning in: " << network->clients_in_state_count(bmp::State::SessionSetup) << "\n"
|
||||
<< "\tPlayers playing: " << network->clients_in_state_count(bmp::State::Playing) << "\n"
|
||||
<< "\tPlayers leaving: " << network->clients_in_state_count(bmp::State::Leaving) << "\n"
|
||||
<< "\tGuests: " << network->guest_count() << "\n"
|
||||
<< "\tVehicles: " << network->vehicle_count() << "\n"
|
||||
<< "\tUptime: " << ElapsedTime << "ms (~" << size_t(double(ElapsedTime) / 1000.0 / 60.0 / 60.0) << "h) \n"
|
||||
<< "\tLua:\n"
|
||||
<< "\t\tQueued results to check: " << mLuaEngine->GetResultsToCheckSize() << "\n"
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "THeartbeatThread.h"
|
||||
|
||||
#include "Http.h"
|
||||
//#include "SocketIO.h"
|
||||
// #include "SocketIO.h"
|
||||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/rapidjson.h>
|
||||
#include <sstream>
|
||||
@ -120,7 +120,7 @@ std::string THeartbeatThread::GenerateCall() {
|
||||
std::stringstream Ret;
|
||||
|
||||
Ret << "uuid=" << Application::Settings.Key
|
||||
<< "&players=" << mServer.ClientCount()
|
||||
<< "&players=" << m_network->authenticated_client_count()
|
||||
<< "&maxplayers=" << Application::Settings.MaxPlayers
|
||||
<< "&port=" << Application::Settings.Port
|
||||
<< "&map=" << Application::Settings.MapName
|
||||
@ -129,17 +129,16 @@ std::string THeartbeatThread::GenerateCall() {
|
||||
<< "&clientversion=" << std::to_string(Application::ClientMajorVersion()) + ".0" // FIXME: Wtf.
|
||||
<< "&name=" << Application::Settings.ServerName
|
||||
<< "&tags=" << Application::Settings.ServerTags
|
||||
<< "&modlist=" << mResourceManager.TrimmedList()
|
||||
<< "&modstotalsize=" << mResourceManager.MaxModSize()
|
||||
<< "&modstotal=" << mResourceManager.ModsLoaded()
|
||||
<< "&modlist=" << "-"//mResourceManager.TrimmedList()
|
||||
<< "&modstotalsize=" << 0 //mResourceManager.MaxModSize()
|
||||
<< "&modstotal=" << 0 // mResourceManager.ModsLoaded()
|
||||
<< "&playerslist=" << GetPlayers()
|
||||
<< "&desc=" << Application::Settings.ServerDesc
|
||||
<< "&pass=" << (Application::Settings.Password.empty() ? "false" : "true");
|
||||
return Ret.str();
|
||||
}
|
||||
THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& Server)
|
||||
: mResourceManager(ResourceManager)
|
||||
, mServer(Server) {
|
||||
THeartbeatThread::THeartbeatThread(std::shared_ptr<Network> network)
|
||||
: m_network(std::move(network)) {
|
||||
Application::SetSubsystemStatus("Heartbeat", Application::Status::Starting);
|
||||
Application::RegisterShutdownHandler([&] {
|
||||
Application::SetSubsystemStatus("Heartbeat", Application::Status::ShuttingDown);
|
||||
@ -151,12 +150,10 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
|
||||
Start();
|
||||
}
|
||||
std::string THeartbeatThread::GetPlayers() {
|
||||
std::string Return;
|
||||
mServer.ForEachClient([&](const std::shared_ptr<TClient>& ClientPtr) -> bool {
|
||||
Return += ClientPtr->Name.get() + ";";
|
||||
return true;
|
||||
});
|
||||
return Return;
|
||||
std::string players;
|
||||
for (const auto& [id, client] : m_network->authenticated_clients()) {
|
||||
players += client->name.get() + ";";
|
||||
}
|
||||
return players;
|
||||
}
|
||||
/*THeartbeatThread::~THeartbeatThread() {
|
||||
}*/
|
||||
|
||||
|
@ -31,7 +31,7 @@ TLuaEngine::TLuaEngine()
|
||||
}
|
||||
Application::SetSubsystemStatus("LuaEngine", Application::Status::Shutdown);
|
||||
});
|
||||
IThreaded::Start();
|
||||
mThread = boost::scoped_thread<>(&TLuaEngine::Start, this);
|
||||
}
|
||||
|
||||
TEST_CASE("TLuaEngine ctor & dtor") {
|
||||
@ -40,7 +40,7 @@ TEST_CASE("TLuaEngine ctor & dtor") {
|
||||
Application::GracefullyShutdown();
|
||||
}
|
||||
|
||||
void TLuaEngine::operator()() {
|
||||
void TLuaEngine::Start() {
|
||||
RegisterThread("LuaEngine");
|
||||
Application::SetSubsystemStatus("LuaEngine", Application::Status::Good);
|
||||
// lua engine main thread
|
||||
@ -478,9 +478,9 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerLocalEvent(const std::string&
|
||||
}
|
||||
|
||||
sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) {
|
||||
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
||||
auto MaybeClient = mEngine->Network()->get_client(ClientID(ID), bmp::State::Authentication);
|
||||
if (MaybeClient) {
|
||||
auto IDs = MaybeClient.value()->Identifiers.synchronize();
|
||||
auto IDs = MaybeClient.value()->identifiers.synchronize();
|
||||
if (IDs->empty()) {
|
||||
return sol::lua_nil;
|
||||
}
|
||||
@ -495,24 +495,22 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) {
|
||||
}
|
||||
|
||||
sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() {
|
||||
sol::table Result = mStateView.create_table();
|
||||
mEngine->Server().ForEachClient([&](const std::shared_ptr<TClient>& Client) -> bool {
|
||||
Result[Client->ID.get()] = Client->Name.get();
|
||||
return true;
|
||||
});
|
||||
return Result;
|
||||
sol::table result = mStateView.create_table();
|
||||
auto clients = mEngine->Network()->authenticated_clients();
|
||||
for (const auto& [id, client] : clients) {
|
||||
result[client->id] = client->name.get();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name) {
|
||||
int Id = -1;
|
||||
mEngine->mServer->ForEachClient([&Id, &Name](const std::shared_ptr<TClient>& Client) -> bool {
|
||||
if (Client->Name.get() == Name) {
|
||||
Id = Client->ID.get();
|
||||
return false;
|
||||
auto clients = mEngine->Network()->authenticated_clients();
|
||||
for (const auto& [id, client] : clients) {
|
||||
if (client->name.get() == Name) {
|
||||
return int(client->id);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return Id;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
sol::table TLuaEngine::StateThreadData::Lua_FS_ListFiles(const std::string& Path) {
|
||||
@ -542,56 +540,51 @@ sol::table TLuaEngine::StateThreadData::Lua_FS_ListDirectories(const std::string
|
||||
}
|
||||
|
||||
std::string TLuaEngine::StateThreadData::Lua_GetPlayerName(int ID) {
|
||||
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
||||
if (MaybeClient) {
|
||||
return MaybeClient.value()->Name.get();
|
||||
auto maybe_client = mEngine->Network()->get_client(ClientID(ID), bmp::State::Authentication);
|
||||
if (maybe_client) {
|
||||
return maybe_client.value()->name.get();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
sol::table TLuaEngine::StateThreadData::Lua_GetPlayerVehicles(int ID) {
|
||||
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
||||
if (MaybeClient) {
|
||||
auto Client = MaybeClient.value();
|
||||
auto VehicleData = Client->VehicleData.synchronize();
|
||||
if (VehicleData->empty()) {
|
||||
return sol::lua_nil;
|
||||
}
|
||||
sol::state_view StateView(mState);
|
||||
sol::table Result = StateView.create_table();
|
||||
for (const auto& v : *VehicleData) {
|
||||
Result[v.ID()] = v.Data().substr(3);
|
||||
}
|
||||
return Result;
|
||||
} else
|
||||
auto vehicles = mEngine->Network()->get_vehicles_owned_by(ClientID(ID));
|
||||
if (vehicles.empty()) {
|
||||
return sol::lua_nil;
|
||||
}
|
||||
sol::state_view state_view(mState);
|
||||
sol::table result = state_view.create_table();
|
||||
for (const auto& [vid, vehicle] : vehicles) {
|
||||
result[vid] = vehicle->data.get();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::pair<sol::table, std::string> TLuaEngine::StateThreadData::Lua_GetPositionRaw(int PID, int VID) {
|
||||
std::pair<sol::table, std::string> Result;
|
||||
auto MaybeClient = GetClient(mEngine->Server(), PID);
|
||||
if (MaybeClient) {
|
||||
auto Client = MaybeClient.value();
|
||||
std::string VehiclePos = Client->GetCarPositionRaw(VID);
|
||||
|
||||
if (VehiclePos.empty()) {
|
||||
// return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Vehicle not found"));
|
||||
Result.second = "Vehicle not found";
|
||||
return Result;
|
||||
}
|
||||
|
||||
sol::table t = Lua_JsonDecode(VehiclePos);
|
||||
if (t == sol::lua_nil) {
|
||||
Result.second = "Packet decode failed";
|
||||
}
|
||||
// return std::make_tuple(Result, sol::make_object(StateView, sol::lua_nil));
|
||||
Result.first = t;
|
||||
return Result;
|
||||
std::pair<sol::table, std::string> TLuaEngine::StateThreadData::Lua_GetVehicleStatus(int VID) {
|
||||
auto maybe_vehicle = mEngine->Network()->get_vehicle(VehicleID(VID));
|
||||
if (maybe_vehicle) {
|
||||
sol::state_view state_view(mState);
|
||||
sol::table result = state_view.create_table();
|
||||
auto veh = maybe_vehicle.value();
|
||||
auto status = veh->get_status();
|
||||
result["pos"]["x"] = status.pos.x;
|
||||
result["pos"]["y"] = status.pos.y;
|
||||
result["pos"]["z"] = status.pos.z;
|
||||
result["vel"]["x"] = status.vel.x;
|
||||
result["vel"]["y"] = status.vel.y;
|
||||
result["vel"]["z"] = status.vel.z;
|
||||
result["rvel"]["x"] = status.rvel.x;
|
||||
result["rvel"]["y"] = status.rvel.y;
|
||||
result["rvel"]["z"] = status.rvel.z;
|
||||
result["rot"]["x"] = status.rot.x;
|
||||
result["rot"]["y"] = status.rot.y;
|
||||
result["rot"]["z"] = status.rot.z;
|
||||
result["rot"]["w"] = status.rot.w;
|
||||
result["time"] = status.time;
|
||||
return { result, "" };
|
||||
} else {
|
||||
// return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Client expired"));
|
||||
Result.second = "No such player";
|
||||
return Result;
|
||||
return { sol::lua_nil, fmt::format("Vehicle {} not found", VID) };
|
||||
}
|
||||
}
|
||||
|
||||
@ -753,8 +746,8 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
|
||||
MPTable.set_function("GetPlayerVehicles", [&](int ID) -> sol::table {
|
||||
return Lua_GetPlayerVehicles(ID);
|
||||
});
|
||||
MPTable.set_function("GetPositionRaw", [&](int PID, int VID) -> std::pair<sol::table, std::string> {
|
||||
return Lua_GetPositionRaw(PID, VID);
|
||||
MPTable.set_function("GetVehicleStatus", [&](int VID) -> std::pair<sol::table, std::string> {
|
||||
return Lua_GetVehicleStatus(VID);
|
||||
});
|
||||
MPTable.set_function("SendChatMessage", &LuaAPI::MP::SendChatMessage);
|
||||
MPTable.set_function("GetPlayers", [&]() -> sol::table {
|
||||
@ -855,7 +848,7 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
|
||||
FSTable.set_function("ListDirectories", [this](const std::string& Path) {
|
||||
return Lua_FS_ListDirectories(Path);
|
||||
});
|
||||
Start();
|
||||
mThread = boost::scoped_thread<>(&StateThreadData::Start, this);
|
||||
}
|
||||
|
||||
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueScript(const TLuaChunk& Script) {
|
||||
@ -901,7 +894,7 @@ void TLuaEngine::StateThreadData::RegisterEvent(const std::string& EventName, co
|
||||
mEngine->RegisterEvent(EventName, mStateId, FunctionName);
|
||||
}
|
||||
|
||||
void TLuaEngine::StateThreadData::operator()() {
|
||||
void TLuaEngine::StateThreadData::Start() {
|
||||
RegisterThread("Lua:" + mStateId);
|
||||
while (!Application::IsShuttingDown()) {
|
||||
{ // StateExecuteQueue Scope
|
||||
|
12
src/main.cpp
12
src/main.cpp
@ -135,21 +135,15 @@ int BeamMPServerMain(MainArguments Arguments) {
|
||||
TLuaEngine::WaitForAll(Futures, std::chrono::seconds(5));
|
||||
});
|
||||
|
||||
TServer Server(Arguments.List);
|
||||
|
||||
auto LuaEngine = std::make_shared<TLuaEngine>();
|
||||
LuaEngine->SetServer(&Server);
|
||||
Application::Console().InitializeLuaConsole(*LuaEngine);
|
||||
|
||||
RegisterThread("Main");
|
||||
|
||||
beammp_trace("Running in debug mode on a debug build");
|
||||
TResourceManager ResourceManager;
|
||||
TPPSMonitor PPSMonitor(Server);
|
||||
THeartbeatThread Heartbeat(ResourceManager, Server);
|
||||
TNetwork Network(Server, PPSMonitor, ResourceManager);
|
||||
LuaEngine->SetNetwork(&Network);
|
||||
PPSMonitor.SetNetwork(Network);
|
||||
std::shared_ptr<Network> network = std::make_shared<Network>();
|
||||
THeartbeatThread Heartbeat(network);
|
||||
LuaEngine->SetNetwork(network);
|
||||
Application::CheckForUpdates();
|
||||
|
||||
TPluginMonitor PluginMonitor(fs::path(Application::Settings.Resource) / "Server", LuaEngine);
|
||||
|
@ -15,6 +15,7 @@
|
||||
"rapidjson",
|
||||
"sol2",
|
||||
"toml11",
|
||||
"zstd"
|
||||
"zstd",
|
||||
"glm"
|
||||
]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user