mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-02-16 18:50:44 +00:00
Continue Lua Rewrite
This commit is contained in:
@@ -56,13 +56,13 @@ inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const ch
|
||||
}
|
||||
|
||||
#define beammp_assert(cond) _assert(__FILE__, __func__, __LINE__, #cond, (cond))
|
||||
#define AssertNotReachable() _assert(__FILE__, __func__, __LINE__, "reached unreachable code", false)
|
||||
#define beammp_assert_not_reachable() _assert(__FILE__, __func__, __LINE__, "reached unreachable code", false)
|
||||
#else
|
||||
// In release build, these macros turn into NOPs. The compiler will optimize these out.
|
||||
#define Assert(x) \
|
||||
#define beammp_assert(x) \
|
||||
do { \
|
||||
} while (false)
|
||||
#define AssertNotReachable() \
|
||||
#define beammp_assert_not_reachable() \
|
||||
do { \
|
||||
} while (false)
|
||||
#endif // DEBUG
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class TConfig {
|
||||
public:
|
||||
explicit TConfig();
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "commandline.h"
|
||||
#include <atomic>
|
||||
#include <fstream>
|
||||
#include "commandline.h"
|
||||
|
||||
class TLuaEngine;
|
||||
|
||||
class TConsole {
|
||||
public:
|
||||
@@ -10,9 +12,10 @@ public:
|
||||
|
||||
void Write(const std::string& str);
|
||||
void WriteRaw(const std::string& str);
|
||||
// BROKEN void InitializeLuaConsole(TLuaEngine& Engine);
|
||||
void InitializeLuaConsole(TLuaEngine& Engine);
|
||||
|
||||
private:
|
||||
// BROKEN std::unique_ptr<TLuaFile> mLuaConsole { nullptr };
|
||||
Commandline mCommandline;
|
||||
TLuaEngine& mLuaEngine;
|
||||
const std::string mStateId = "BEAMMP_SERVER_CONSOLE";
|
||||
};
|
||||
|
||||
@@ -3,30 +3,77 @@
|
||||
#include "TNetwork.h"
|
||||
#include "TServer.h"
|
||||
#include <filesystem>
|
||||
#include <sol/sol.hpp>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <toml11/toml.hpp>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
using TLuaStateId = std::string;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class TLuaPlugin;
|
||||
|
||||
struct TLuaResult {
|
||||
std::atomic_bool Ready;
|
||||
// TODO: Add condition_variable
|
||||
sol::protected_function_result Result;
|
||||
};
|
||||
|
||||
struct TLuaPluginConfig {
|
||||
static inline const std::string FileName = "PluginConfig.toml";
|
||||
TLuaStateId StateId;
|
||||
// TODO: Execute list
|
||||
};
|
||||
|
||||
class TLuaEngine : IThreaded {
|
||||
public:
|
||||
TLuaEngine(TServer& Server, TNetwork& Network);
|
||||
|
||||
void operator()() override;
|
||||
|
||||
TLuaResult EnqueueScript(TLuaStateId StateID, const std::shared_ptr<std::string>& Script);
|
||||
void EnsureStateExists(TLuaStateId StateId, const std::string& Name);
|
||||
|
||||
private:
|
||||
void CollectPlugins();
|
||||
void InitializePlugin(const fs::path& folder);
|
||||
void InitializePlugin(const fs::path& Folder, const TLuaPluginConfig& Config);
|
||||
void FindAndParseConfig(const fs::path& Folder, TLuaPluginConfig& Config);
|
||||
|
||||
class StateThreadData : IThreaded {
|
||||
public:
|
||||
StateThreadData(const std::string& Name, std::atomic_bool& Shutdown);
|
||||
StateThreadData(const StateThreadData&) = delete;
|
||||
void EnqueueScript(const std::shared_ptr<std::string>& Script);
|
||||
void operator()() override;
|
||||
~StateThreadData();
|
||||
|
||||
private:
|
||||
std::string mName;
|
||||
std::atomic_bool& mShutdown;
|
||||
sol::state mState;
|
||||
std::thread mThread;
|
||||
std::queue<std::shared_ptr<std::string>> mStateExecuteQueue;
|
||||
std::mutex mStateExecuteQueueMutex;
|
||||
};
|
||||
|
||||
TNetwork& mNetwork;
|
||||
TServer& mServer;
|
||||
sol::state mL;
|
||||
std::atomic_bool mShutdown { false };
|
||||
fs::path mResourceServerPath;
|
||||
std::vector<TLuaPlugin> mLuaPlugins;
|
||||
std::unordered_map<std::string, sol::state> mLuaStates;
|
||||
std::vector<TLuaPlugin*> mLuaPlugins;
|
||||
std::unordered_map<TLuaStateId, std::unique_ptr<StateThreadData>> mLuaStates;
|
||||
std::mutex mLuaStatesMutex;
|
||||
};
|
||||
|
||||
#include <any>
|
||||
// DEAD CODE
|
||||
struct TLuaArg {
|
||||
std::vector<std::any> args;
|
||||
void PushArgs(lua_State* State);
|
||||
};
|
||||
std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaPlugin* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "TLuaEngine.h"
|
||||
|
||||
class TLuaPlugin {
|
||||
public:
|
||||
TLuaPlugin(TLuaEngine& Engine, const TLuaPluginConfig& Config);
|
||||
TLuaPlugin(const TLuaPlugin&) = delete;
|
||||
TLuaPlugin& operator=(const TLuaPlugin&) = delete;
|
||||
~TLuaPlugin() noexcept = default;
|
||||
|
||||
const TLuaPluginConfig& GetConfig() const { return mConfig; }
|
||||
|
||||
private:
|
||||
TLuaPluginConfig mConfig;
|
||||
TLuaEngine& mEngine;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user