mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-14 19:56:06 +00:00
Begin Lua Rewrite
The idea is to move everything to TLuaEngine.
This commit is contained in:
@@ -9,30 +9,42 @@
|
||||
#include <optional>
|
||||
#include <set>
|
||||
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
struct TLuaArgs final {
|
||||
std::vector<std::any> Args;
|
||||
};
|
||||
|
||||
class TLuaEngine : public IThreaded {
|
||||
public:
|
||||
explicit TLuaEngine(TServer& Server, TNetwork& Network);
|
||||
|
||||
using TSetOfLuaFile = std::set<std::unique_ptr<TLuaFile>>;
|
||||
using TSetOfLuaFile = std::vector<std::shared_ptr<TLuaFile>>;
|
||||
|
||||
void operator()() override;
|
||||
|
||||
[[nodiscard]] const TSetOfLuaFile& LuaFiles() const { return mLuaFiles; }
|
||||
std::any TriggerLuaEvent(const std::string& Event, bool local, std::weak_ptr<TLuaFile> Caller, std::shared_ptr<TLuaArgs> arg, bool Wait);
|
||||
|
||||
[[nodiscard]] TServer& Server() { return mServer; }
|
||||
[[nodiscard]] const TServer& Server() const { return mServer; }
|
||||
[[nodiscard]] TNetwork& Network() { return mNetwork; }
|
||||
[[nodiscard]] const TNetwork& Network() const { return mNetwork; }
|
||||
|
||||
std::optional<std::reference_wrapper<TLuaFile>> GetScript(lua_State* L);
|
||||
void UnregisterScript(std::shared_ptr<TLuaFile> Script);
|
||||
std::shared_ptr<TLuaFile> GetLuaFileOfScript(lua_State* L);
|
||||
std::shared_ptr<TLuaFile> InsertNewLuaFile(const fs::path& FileName, const std::string& PluginName);
|
||||
void SendError(lua_State* L, const std::string& msg);
|
||||
|
||||
private:
|
||||
void FolderList(const std::string& Path, bool HotSwap);
|
||||
void RegisterFiles(const std::string& Path, bool HotSwap);
|
||||
bool NewFile(const std::string& Path);
|
||||
void RegisterFiles(const fs::path& Path, bool HotSwap);
|
||||
bool IsNewFile(const std::string& Path);
|
||||
|
||||
TNetwork& mNetwork;
|
||||
TServer& mServer;
|
||||
std::string mPath;
|
||||
bool mShutdown { false };
|
||||
mutable std::mutex mLuaFilesMutex;
|
||||
TSetOfLuaFile mLuaFiles;
|
||||
};
|
||||
|
||||
7
include/TLuaEvent.h
Normal file
7
include/TLuaEvent.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class TLuaEvent final {
|
||||
};
|
||||
@@ -11,15 +11,14 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
struct TLuaArg {
|
||||
std::vector<std::any> args;
|
||||
void PushArgs(lua_State* State);
|
||||
};
|
||||
|
||||
class TLuaEngine;
|
||||
class TLuaArgs;
|
||||
|
||||
class TLuaFile {
|
||||
public:
|
||||
explicit TLuaFile(TLuaEngine& Engine, bool Console = false);
|
||||
~TLuaFile();
|
||||
|
||||
void RegisterEvent(const std::string& Event, const std::string& FunctionName);
|
||||
void UnRegisterEvent(const std::string& Event);
|
||||
void SetLastWrite(fs::file_time_type time);
|
||||
@@ -30,11 +29,8 @@ public:
|
||||
fs::file_time_type GetLastWrite();
|
||||
lua_State* GetState();
|
||||
std::string GetOrigin();
|
||||
std::mutex Lock;
|
||||
void Reload();
|
||||
void Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote);
|
||||
explicit TLuaFile(TLuaEngine& Engine, bool Console = false);
|
||||
~TLuaFile();
|
||||
void SetStopThread(bool StopThread) { mStopThread = StopThread; }
|
||||
TLuaEngine& Engine() { return mEngine; }
|
||||
[[nodiscard]] std::string GetPluginName() const;
|
||||
@@ -43,8 +39,14 @@ public:
|
||||
[[nodiscard]] bool GetStopThread() const { return mStopThread; }
|
||||
[[nodiscard]] const TLuaEngine& Engine() const { return mEngine; }
|
||||
[[nodiscard]] std::string GetRegistered(const std::string& Event) const;
|
||||
void PushArgs(const TLuaArgs& args);
|
||||
|
||||
bool operator==(const TLuaFile& Other) const;
|
||||
|
||||
void Load();
|
||||
|
||||
private:
|
||||
std::mutex Lock;
|
||||
TLuaEngine& mEngine;
|
||||
std::set<std::pair<std::string, std::string>> mRegisteredEvents;
|
||||
lua_State* mLuaState { nullptr };
|
||||
@@ -53,9 +55,6 @@ private:
|
||||
std::string mFileName {};
|
||||
bool mStopThread = false;
|
||||
bool mConsole = false;
|
||||
void Load();
|
||||
};
|
||||
|
||||
std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);
|
||||
|
||||
#endif // TLUAFILE_H
|
||||
|
||||
Reference in New Issue
Block a user