[Server] Remove usages of get/set env. Add GetModDir function

experimental-mono
Koncord 5 years ago
parent b3456a8841
commit 6af2400752

@ -7,6 +7,7 @@
#include <apps/openmw-mp/Script/ScriptFunctions.hpp>
#include <apps/openmw-mp/Networking.hpp>
#include <apps/openmw-mp/MasterClient.hpp>
#include <Script/Script.hpp>
void ServerFunctions::StopServer(int code) noexcept
@ -162,3 +163,8 @@ void ServerFunctions::AddPluginHash(const char *pluginName, const char *hashStr)
mclient->PushPlugin({pluginName, hash});
}
}
const char* ServerFunctions::GetModDir() noexcept
{
return Script::GetModDir();
}

@ -29,7 +29,8 @@
{"SetScriptErrorIgnoringState", ServerFunctions::SetScriptErrorIgnoringState},\
{"SetRuleString", ServerFunctions::SetRuleString},\
{"SetRuleValue", ServerFunctions::SetRuleValue},\
{"AddPluginHash", ServerFunctions::AddPluginHash}
{"AddPluginHash", ServerFunctions::AddPluginHash},\
{"GetModDir", ServerFunctions::GetModDir}
class ServerFunctions
{
@ -224,6 +225,8 @@ public:
* @param hash Hash string
*/
static void AddPluginHash(const char *pluginName, const char *hash) noexcept;
static const char *GetModDir() noexcept;
};
#endif //OPENMW_SERVERAPI_HPP

@ -9,6 +9,24 @@
using namespace std;
std::set<std::string> LangLua::packagePath;
std::set<std::string> LangLua::packageCPath;
void setLuaPath(lua_State* L, const char* path, bool cpath = false)
{
string field = cpath ? "cpath" : "path";
lua_getglobal(L, "package");
lua_getfield(L, -1, field.c_str());
std::string cur_path = lua_tostring(L, -1);
cur_path.append(";");
cur_path.append(path);
lua_pop(L, 1);
lua_pushstring(L, cur_path.c_str());
lua_setfield(L, -2, field.c_str());
lua_pop(L, 1);
}
lib_t LangLua::GetInterface()
{
return reinterpret_cast<lib_t>(lua);
@ -23,6 +41,17 @@ LangLua::LangLua()
{
lua = luaL_newstate();
luaL_openlibs(lua); // load all lua std libs
std::string p, cp;
for (auto& path : packagePath)
p += path + ';';
for (auto& path : packageCPath)
cp += path + ';';
setLuaPath(lua, p.c_str());
setLuaPath(lua, cp.c_str(), true);
}
LangLua::~LangLua()
@ -234,3 +263,13 @@ boost::any LangLua::Call(const char *name, const char *argl, const std::vector<b
luabridge::LuaException::pcall(lua, n_args, 1);
return boost::any(luabridge::LuaRef::fromStack(lua, -1));
}
void LangLua::AddPackagePath(const std::string& path)
{
packagePath.emplace(path);
}
void LangLua::AddPackageCPath(const std::string& path)
{
packageCPath.emplace(path);
}

@ -9,6 +9,7 @@
#include <extern/LuaBridge/LuaBridge.h>
#include <LuaBridge.h>
#include <set>
#include <boost/any.hpp>
#include "../ScriptFunction.hpp"
@ -41,6 +42,10 @@ public:
LangLua();
LangLua(lua_State *lua);
~LangLua();
static void AddPackagePath(const std::string &path);
static void AddPackageCPath(const std::string &path);
static int MakePublic(lua_State *lua) noexcept;
static int CallPublic(lua_State *lua);
@ -52,6 +57,9 @@ public:
virtual bool IsCallbackPresent(const char *name) override;
virtual boost::any Call(const char *name, const char *argl, int buf, ...) override;
virtual boost::any Call(const char *name, const char *argl, const std::vector<boost::any> &args) override;
private:
static std::set<std::string> packageCPath;
static std::set<std::string> packagePath;
};

@ -12,6 +12,7 @@
using namespace std;
Script::ScriptList Script::scripts;
std::string Script::moddir;
Script::Script(const char *path)
{
@ -94,3 +95,14 @@ void Script::LoadScript(const char *script, const char *base)
snprintf(path, sizeof(path), Utils::convertPath("%s/%s/%s").c_str(), base, "scripts", script);
Script::scripts.emplace_back(new Script(path));
}
void Script::SetModDir(const std::string &moddir)
{
if (Script::moddir.empty()) // do not allow to change in runtime
Script::moddir = moddir;
}
const char* Script::GetModDir()
{
return moddir.c_str();
}

@ -54,12 +54,16 @@ private:
Script(const Script&) = delete;
Script& operator=(const Script&) = delete;
protected:
static std::string moddir;
public:
~Script();
static void LoadScript(const char *script, const char* base);
static void LoadScripts(char* scripts, const char* base);
static void UnloadScripts();
static void SetModDir(const std::string &moddir);
static const char* GetModDir();
static constexpr ScriptCallbackData const& CallBackData(const unsigned int I, const unsigned int N = 0) {
return callbacks[N].index == I ? callbacks[N] : CallBackData(I, N + 1);

@ -221,17 +221,18 @@ int main(int argc, char *argv[])
LOG_APPEND(Log::LOG_FATAL, "- %s", TES3MP_CREDITS_ERROR);
return 1;
}
Script::SetModDir(moddir);
setenv("MOD_DIR", moddir.c_str(), 1); // hack for lua
setenv("LUA_PATH", Utils::convertPath(plugin_home + "/scripts/?.lua" + ";"
+ plugin_home + "/scripts/?.t" + ";"
+ plugin_home + "/lib/lua/?.lua" + ";"
+ plugin_home + "/lib/lua/?.t").c_str(), 1);
#ifdef ENABLE_LUA
LangLua::AddPackagePath(Utils::convertPath(plugin_home + "/scripts/?.lua" + ";"
+ plugin_home + "/lib/lua/?.lua" + ";"));
#ifdef _WIN32
setenv("LUA_CPATH", Utils::convertPath(plugin_home + "/lib/?.dll").c_str(), 1);
LangLua::AddPackageCPath(Utils::convertPath(plugin_home + "/lib/?.dll"));
#else
setenv("LUA_CPATH", Utils::convertPath(plugin_home + "/lib/?.so").c_str(), 1);
LangLua::AddPackageCPath(Utils::convertPath(plugin_home + "/lib/?.so"));
#endif
#endif
int code;

Loading…
Cancel
Save