Files

109 lines
3.2 KiB
C++
Raw Permalink Normal View History

2023-10-02 18:04:54 +08:00
#include "script_engine.h"
2023-10-09 16:56:33 +08:00
extern "C" int luaopen_cjson(lua_State * L);
2023-10-02 18:04:54 +08:00
namespace ScriptEngine {
std::string luaPath;
2023-10-03 00:25:23 +08:00
std::map<std::string, lua_State*> pluginEnvs;
std::shared_mutex mutex_pluginEnvs;
auto callFunction(lua_State* luaVm, const char* funcName) -> int {
2023-10-02 18:04:54 +08:00
lua_getglobal(luaVm, funcName);
2023-10-03 00:25:23 +08:00
auto result = 0;
do {
if (lua_type(luaVm, -1) == LUA_TNIL) {
printf("lua_getglobal :%s\n\n", lua_tostring(luaVm, -1));
result = 0;
break;
}
if (!lua_isfunction(luaVm, -1)) {
printf("lua_isfunction_err:%s\n\n", lua_tostring(luaVm, -1));
result = 0;
break;
}
if (lua_pcall(luaVm, 0, 1, 0)) {
printf("lua_pcall_err:%s\n\n", lua_tostring(luaVm, -1));
result = 0;
break;
}
const auto result = lua_toboolean(luaVm, 1);
} while (false);
2023-10-02 18:04:54 +08:00
lua_pop(luaVm, 1);
return result;
}
2023-10-03 00:25:23 +08:00
2023-10-02 18:04:54 +08:00
auto initLuaScripts() -> void {
2023-10-03 00:25:23 +08:00
auto [dirPaths, dirNames] = Tools::GetDirs(luaPath);
if (dirPaths.size() == 0) {
2023-10-02 18:04:54 +08:00
LOG("no lua file in %s\n", luaPath.c_str());
2023-10-03 00:25:23 +08:00
return;
2023-10-02 18:04:54 +08:00
}
2023-10-03 00:25:23 +08:00
std::unique_lock lock(mutex_pluginEnvs);
for (size_t i = 0; i < dirPaths.size(); i++) {
std::string dirPath = dirPaths[i];
std::string dirName = dirNames[i];
lua_State* L = luaL_newstate();
luaL_openlibs(L);
2023-10-09 16:41:40 +08:00
luaL_requiref(L, "cjson", luaopen_cjson, 1);
lua_pop(L, 1);
2023-10-03 04:07:50 +08:00
ScriptApis::initFunciton(L);
2023-10-03 00:25:23 +08:00
pluginEnvs[dirName] = L;
2023-10-08 05:28:41 +08:00
if (dirPath.find("disable_") != std::string::npos) {
2023-10-08 01:56:49 +08:00
continue;
}
2023-10-03 00:25:23 +08:00
std::string file = dirPath + "\\main.lua";
if (std::filesystem::exists(file) == false) {
continue;
}
2023-10-08 01:56:49 +08:00
2023-10-03 00:25:23 +08:00
LOG("execute: %s\n", file.c_str());
2023-10-03 04:50:01 +08:00
std::string scriptDir = dirPath;
lua_getglobal(L, "package");
2023-10-09 16:41:40 +08:00
lua_getfield(
L, -1, "path"); // get field "path" from table at top of stack (-1)
std::string cur_path =
lua_tostring(L, -1); // grab path string from top of stack
cur_path.append(";"); // do your path magic here
2023-10-03 04:50:01 +08:00
cur_path.append(scriptDir);
cur_path.append("\\?.lua");
2023-10-09 16:41:40 +08:00
lua_pop(
L,
1); // get rid of the string on the stack we just pushed on line 5
lua_pushstring(L, cur_path.c_str()); // push the new one
lua_setfield(L, -2, "path"); // set the field "path" in table at -2
// with value at top of stack
lua_pop(L, 1); // get rid of package table from top of stack
2023-10-03 04:50:01 +08:00
2023-10-03 00:25:23 +08:00
if (luaL_dofile(L, file.c_str())) {
LOG("dofile_err:%s\n\n", lua_tostring(L, -1));
continue;
2023-10-02 18:04:54 +08:00
}
2023-10-03 00:25:23 +08:00
callFunction(L, "Main");
2023-10-02 18:04:54 +08:00
}
2023-10-03 00:25:23 +08:00
}
2023-10-02 18:04:54 +08:00
2023-10-03 00:25:23 +08:00
auto releaseLuaScripts() -> void {
std::unique_lock lock(mutex_pluginEnvs);
for (auto& pair : pluginEnvs) {
lua_close(pair.second);
}
pluginEnvs.clear();
2023-10-02 18:04:54 +08:00
}
2023-10-03 00:25:23 +08:00
auto reloadLuaScripts() -> void {
releaseLuaScripts();
2023-10-02 18:04:54 +08:00
initLuaScripts();
}
2023-10-03 00:25:23 +08:00
auto Init() -> void {
2023-10-08 01:56:49 +08:00
luaPath = Tools::GetExePath() + "\\huoji_scripts\\";
// luaPath = "F:\\source2\\huoji_scripts\\";
2023-10-03 00:25:23 +08:00
initLuaScripts();
2023-10-02 18:04:54 +08:00
}
2023-10-03 00:25:23 +08:00
} // namespace ScriptEngine