mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-06 15:56:18 +00:00
Lua: Add variadic print, LuaAPI
This commit is contained in:
@@ -125,6 +125,7 @@ void RegisterThread(const std::string str);
|
||||
#define beammp_warn(x) Application::Console().Write(_this_location + std::string("[WARN] ") + (x))
|
||||
#define beammp_info(x) Application::Console().Write(_this_location + std::string("[INFO] ") + (x))
|
||||
#define beammp_error(x) Application::Console().Write(_this_location + std::string("[ERROR] ") + (x))
|
||||
#define beammp_lua_error(x) Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x))
|
||||
#define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x))
|
||||
#define beammp_debug(x) \
|
||||
do { \
|
||||
|
||||
13
include/LuaAPI.h
Normal file
13
include/LuaAPI.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <tuple>
|
||||
#include "TLuaEngine.h"
|
||||
|
||||
namespace LuaAPI {
|
||||
void Print(sol::variadic_args);
|
||||
namespace MP {
|
||||
static inline TLuaEngine* Engine { nullptr };
|
||||
void GetOSName();
|
||||
std::tuple<int, int, int> GetServerVersion();
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,9 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#define SOL_ALL_SAFETIES_ON 1
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
using TLuaStateId = std::string;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@@ -21,7 +24,9 @@ struct TLuaResult {
|
||||
std::atomic_bool Ready;
|
||||
std::atomic_bool Error;
|
||||
std::string ErrorMessage;
|
||||
sol::protected_function_result Result;
|
||||
// TODO: Add condition_variable
|
||||
void WaitUntilReady();
|
||||
};
|
||||
|
||||
struct TLuaPluginConfig {
|
||||
@@ -37,27 +42,34 @@ public:
|
||||
void operator()() override;
|
||||
|
||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const std::shared_ptr<std::string>& Script);
|
||||
void EnsureStateExists(TLuaStateId StateId, const std::string& Name);
|
||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName);
|
||||
void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false);
|
||||
|
||||
static constexpr const char* BeamMPFnNotFoundError = "BEAMMP_FN_NOT_FOUND";
|
||||
|
||||
private:
|
||||
void CollectPlugins();
|
||||
void CollectAndInitPlugins();
|
||||
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 std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId);
|
||||
StateThreadData(const StateThreadData&) = delete;
|
||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(const std::shared_ptr<std::string>& Script);
|
||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(const std::string& FunctionName);
|
||||
void operator()() override;
|
||||
|
||||
private:
|
||||
std::string mName;
|
||||
std::atomic_bool& mShutdown;
|
||||
TLuaStateId mStateId;
|
||||
lua_State* mState;
|
||||
std::thread mThread;
|
||||
std::queue<std::pair<std::shared_ptr<std::string>, std::shared_ptr<TLuaResult>>> mStateExecuteQueue;
|
||||
std::mutex mStateExecuteQueueMutex;
|
||||
std::queue<std::pair<std::string, std::shared_ptr<TLuaResult>>> mStateFunctionQueue;
|
||||
std::mutex mStateFunctionQueueMutex;
|
||||
};
|
||||
|
||||
TNetwork& mNetwork;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
class TLuaPlugin {
|
||||
public:
|
||||
TLuaPlugin(TLuaEngine& Engine, const TLuaPluginConfig& Config);
|
||||
TLuaPlugin(TLuaEngine& Engine, const TLuaPluginConfig& Config, const fs::path& MainFolder);
|
||||
TLuaPlugin(const TLuaPlugin&) = delete;
|
||||
TLuaPlugin& operator=(const TLuaPlugin&) = delete;
|
||||
~TLuaPlugin() noexcept = default;
|
||||
@@ -12,4 +12,7 @@ public:
|
||||
private:
|
||||
TLuaPluginConfig mConfig;
|
||||
TLuaEngine& mEngine;
|
||||
fs::path mFolder;
|
||||
std::string mPluginName;
|
||||
std::unordered_map<std::string, std::shared_ptr<std::string>> mFileContents;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user