mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
add sol2
This commit is contained in:
parent
8b69127cdd
commit
be90a8a2c0
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -13,3 +13,6 @@
|
||||
[submodule "include/toml11"]
|
||||
path = deps/toml11
|
||||
url = https://github.com/ToruNiina/toml11
|
||||
[submodule "include/sol2"]
|
||||
path = deps/sol2
|
||||
url = https://github.com/ThePhD/sol2
|
||||
|
@ -1,40 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Common.h"
|
||||
#include "IThreaded.h"
|
||||
#include "TLuaFile.h"
|
||||
#include "TServer.h"
|
||||
#include <optional>
|
||||
#include <lua.hpp>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
|
||||
class TLuaEngine : public IThreaded {
|
||||
public:
|
||||
explicit TLuaEngine(TServer& Server, TNetwork& Network);
|
||||
|
||||
using TSetOfLuaFile = std::set<std::unique_ptr<TLuaFile>>;
|
||||
|
||||
void operator()() override;
|
||||
|
||||
[[nodiscard]] const TSetOfLuaFile& LuaFiles() const { return mLuaFiles; }
|
||||
[[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);
|
||||
|
||||
static std::unordered_map<std::string, lua_State*> mGlobals;
|
||||
private:
|
||||
void FolderList(const std::string& Path, bool HotSwap);
|
||||
void RegisterFiles(const std::string& Path, bool HotSwap);
|
||||
bool NewFile(const std::string& Path);
|
||||
|
||||
TNetwork& mNetwork;
|
||||
TServer& mServer;
|
||||
std::string mPath;
|
||||
bool mShutdown { false };
|
||||
TSetOfLuaFile mLuaFiles;
|
||||
};
|
@ -1,61 +0,0 @@
|
||||
#ifndef TLUAFILE_H
|
||||
#define TLUAFILE_H
|
||||
|
||||
#include <any>
|
||||
#include <filesystem>
|
||||
#include <lua.hpp>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
struct TLuaArg {
|
||||
std::vector<std::any> args;
|
||||
void PushArgs(lua_State* State);
|
||||
};
|
||||
|
||||
class TLuaEngine;
|
||||
|
||||
class TLuaFile {
|
||||
public:
|
||||
void RegisterEvent(const std::string& Event, const std::string& FunctionName);
|
||||
void UnRegisterEvent(const std::string& Event);
|
||||
void SetLastWrite(fs::file_time_type time);
|
||||
bool IsRegistered(const std::string& Event);
|
||||
void SetPluginName(const std::string& Name);
|
||||
void Execute(const std::string& Command);
|
||||
void SetFileName(const std::string& Name);
|
||||
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;
|
||||
[[nodiscard]] std::string GetFileName() const;
|
||||
[[nodiscard]] const lua_State* GetState() const;
|
||||
[[nodiscard]] bool GetStopThread() const { return mStopThread; }
|
||||
[[nodiscard]] const TLuaEngine& Engine() const { return mEngine; }
|
||||
[[nodiscard]] std::string GetRegistered(const std::string& Event) const;
|
||||
|
||||
private:
|
||||
TLuaEngine& mEngine;
|
||||
std::set<std::pair<std::string, std::string>> mRegisteredEvents;
|
||||
lua_State* mLuaState { nullptr };
|
||||
fs::file_time_type mLastWrote;
|
||||
std::string mPluginName {};
|
||||
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
|
@ -1,109 +0,0 @@
|
||||
#include "TLuaEngine.h"
|
||||
#include "TLuaFile.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
std::unordered_map<std::string, lua_State*> TLuaEngine::mGlobals;
|
||||
|
||||
// necessary as lua relies on global state
|
||||
TLuaEngine* TheEngine;
|
||||
|
||||
TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network)
|
||||
: mNetwork(Network)
|
||||
, mServer(Server) {
|
||||
TheEngine = this;
|
||||
if (!fs::exists(Application::Settings.Resource)) {
|
||||
fs::create_directory(Application::Settings.Resource);
|
||||
}
|
||||
std::string Path = Application::Settings.Resource + ("/Server");
|
||||
if (!fs::exists(Path)) {
|
||||
fs::create_directory(Path);
|
||||
}
|
||||
FolderList(Path, false);
|
||||
mPath = Path;
|
||||
Application::RegisterShutdownHandler([&] {if (mThread.joinable()) {
|
||||
debug("shutting down LuaEngine");
|
||||
mShutdown = true;
|
||||
mThread.join();
|
||||
debug("shut down LuaEngine");
|
||||
} });
|
||||
Start();
|
||||
}
|
||||
|
||||
void TLuaEngine::operator()() {
|
||||
RegisterThread("LuaEngine");
|
||||
info("Lua system online");
|
||||
while (!mShutdown) {
|
||||
if (!mLuaFiles.empty()) {
|
||||
for (auto& Script : mLuaFiles) {
|
||||
struct stat Info { };
|
||||
if (stat(Script->GetFileName().c_str(), &Info) != 0) {
|
||||
Script->SetStopThread(true);
|
||||
mLuaFiles.erase(Script);
|
||||
info(("[HOTSWAP] Removed removed script due to delete"));
|
||||
break;
|
||||
}
|
||||
if (Script->GetLastWrite() != fs::last_write_time(Script->GetFileName())) {
|
||||
Script->SetStopThread(true);
|
||||
info(("[HOTSWAP] Updated Scripts due to edit"));
|
||||
Script->SetLastWrite(fs::last_write_time(Script->GetFileName()));
|
||||
Script->Reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
FolderList(mPath, true);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::reference_wrapper<TLuaFile>> TLuaEngine::GetScript(lua_State* L) {
|
||||
for (auto& Script : mLuaFiles) {
|
||||
if (Script->GetState() == L)
|
||||
return *Script;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) {
|
||||
for (const auto& entry : fs::directory_iterator(Path)) {
|
||||
auto pos = entry.path().filename().string().find('.');
|
||||
if (pos == std::string::npos) {
|
||||
RegisterFiles(entry.path().string(), HotSwap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TLuaEngine::RegisterFiles(const std::string& Path, bool HotSwap) {
|
||||
#if defined(__linux) || defined(__linux__)
|
||||
std::string Name = Path.substr(Path.find_last_of('/') + 1);
|
||||
#else
|
||||
std::string Name = Path.substr(Path.find_last_of('\\') + 1);
|
||||
#endif
|
||||
if (!HotSwap)
|
||||
info(("Loading plugin : ") + Name);
|
||||
for (const auto& entry : fs::directory_iterator(Path)) {
|
||||
auto pos = entry.path().string().find((".lua"));
|
||||
if (pos != std::string::npos && entry.path().string().length() - pos == 4) {
|
||||
if (!HotSwap || NewFile(entry.path().string())) {
|
||||
auto FileName = entry.path().string();
|
||||
std::unique_ptr<TLuaFile> ScriptToInsert(new TLuaFile(*this));
|
||||
auto& Script = *ScriptToInsert;
|
||||
mLuaFiles.insert(std::move(ScriptToInsert));
|
||||
Script.Init(Name, FileName, fs::last_write_time(FileName));
|
||||
if (HotSwap)
|
||||
info(("[HOTSWAP] Added : ") + Script.GetFileName().substr(Script.GetFileName().find('\\')));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TLuaEngine::NewFile(const std::string& Path) {
|
||||
for (auto& Script : mLuaFiles) {
|
||||
if (Path == Script->GetFileName())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
1045
src/TLuaFile.cpp
1045
src/TLuaFile.cpp
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user