Lua: Add GetPluginName, GetPluginPath

This commit is contained in:
Lion Kortlepel 2021-08-18 09:00:23 +02:00
parent 6462636b29
commit 2a96546c8c
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 42 additions and 6 deletions

View File

@ -37,12 +37,13 @@ public:
~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;
std::string GetPluginPath() const;
std::string GetPluginName() const;
std::string GetFileName() const;
const lua_State* GetState() const;
bool GetStopThread() const { return mStopThread; }
const TLuaEngine& Engine() const { return mEngine; }
std::string GetRegistered(const std::string& Event) const;
private:
TLuaEngine& mEngine;

View File

@ -922,6 +922,31 @@ int lua_HttpsPOST(lua_State* L) {
return 2;
}
int lua_GetPluginName(lua_State* L) {
auto MaybeFile = Engine().GetScript(L);
if (MaybeFile) {
lua_pushstring(L, MaybeFile.value().get().GetPluginName().c_str());
return 1;
} else {
warn("no plugin associated with this state");
return 0;
}
}
int lua_GetPluginPath(lua_State* L) {
auto MaybeFile = Engine().GetScript(L);
if (MaybeFile) {
lua_pushstring(L, MaybeFile.value().get().GetPluginPath().c_str());
return 1;
} else {
warn("no plugin associated with this state");
return 0;
}
}
int lua_Dump(lua_State* L) {
}
void TLuaFile::Load() {
Assert(mLuaState);
luaL_openlibs(mLuaState);
@ -965,11 +990,14 @@ void TLuaFile::Load() {
LuaTable::InsertFunction(mLuaState, "HttpsGET", lua_HttpsGET);
LuaTable::InsertFunction(mLuaState, "HttpsPOST", lua_HttpsPOST);
LuaTable::InsertFunction(mLuaState, "GetServerVersion", lua_GetServerVersion);
LuaTable::InsertFunction(mLuaState, "GetPluginName", lua_GetPluginName);
LuaTable::InsertFunction(mLuaState, "GetPluginPath", lua_GetPluginPath);
LuaTable::End(mLuaState, "MP");
lua_register(mLuaState, "print", lua_Print);
lua_register(mLuaState, "printRaw", lua_PrintRaw);
lua_register(mLuaState, "dump", lua_Dump);
lua_register(mLuaState, "exit", lua_ServerExit);
if (!mConsole)
Reload();
@ -1032,6 +1060,13 @@ TLuaFile::~TLuaFile() {
lua_close(mLuaState);
}
std::string TLuaFile::GetPluginPath() const {
auto path = fs::path(Application::Settings.Resource);
path /= "Server";
path /= mPluginName;
return path.string();
}
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
Assert(L);
auto MaybeS = Engine.GetScript(L);