增加serverfunctionhash函数
This commit is contained in:
@@ -1,44 +1,81 @@
|
||||
#include "script_engine.h"
|
||||
namespace ScriptEngine {
|
||||
lua_State* luaVm;
|
||||
std::string luaPath;
|
||||
auto callFunction(const char* funcName) -> int {
|
||||
std::map<std::string, lua_State*> pluginEnvs;
|
||||
std::shared_mutex mutex_pluginEnvs;
|
||||
|
||||
auto callFunction(lua_State* luaVm, const char* funcName) -> int {
|
||||
lua_getglobal(luaVm, funcName);
|
||||
_ASSERT(lua_isfunction(luaVm, -1));
|
||||
if (lua_pcall(luaVm, 0, 1, 0)) {
|
||||
printf("lua_pcall_err:%s\n\n", lua_tostring(luaVm, -1));
|
||||
}
|
||||
const auto result = lua_toboolean(luaVm, 1);
|
||||
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);
|
||||
|
||||
lua_pop(luaVm, 1);
|
||||
return result;
|
||||
}
|
||||
auto initFunciton() -> void {
|
||||
}
|
||||
|
||||
auto initLuaScripts() -> void {
|
||||
std::vector<std::string> files;
|
||||
Tools::GetFiles(luaPath, files);
|
||||
if (files.size() == 0) {
|
||||
auto [dirPaths, dirNames] = Tools::GetDirs(luaPath);
|
||||
if (dirPaths.size() == 0) {
|
||||
LOG("no lua file in %s\n", luaPath.c_str());
|
||||
exit(0);
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < files.size(); i++) {
|
||||
std::string file = files[i];
|
||||
if (file.find(".lua") != std::string::npos) {
|
||||
LOG("excute: %s\n", file.c_str());
|
||||
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];
|
||||
|
||||
if (luaL_dofile(luaVm, file.c_str())) {
|
||||
LOG("dofile_err:%s\n\n", lua_tostring(luaVm, -1));
|
||||
continue;
|
||||
}
|
||||
callFunction("main");
|
||||
lua_State* L = luaL_newstate();
|
||||
ScriptApis::initFunciton(L);
|
||||
|
||||
luaL_openlibs(L);
|
||||
pluginEnvs[dirName] = L;
|
||||
|
||||
std::string file = dirPath + "\\main.lua";
|
||||
if (std::filesystem::exists(file) == false) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
LOG("execute: %s\n", file.c_str());
|
||||
|
||||
if (luaL_dofile(L, file.c_str())) {
|
||||
LOG("dofile_err:%s\n\n", lua_tostring(L, -1));
|
||||
continue;
|
||||
}
|
||||
callFunction(L, "Main");
|
||||
}
|
||||
}
|
||||
auto Init() -> void {
|
||||
luaVm = luaL_newstate();
|
||||
luaPath = Tools::GetExePath() + "\\huoji_scripts\\";
|
||||
initFunciton();
|
||||
|
||||
auto releaseLuaScripts() -> void {
|
||||
std::unique_lock lock(mutex_pluginEnvs);
|
||||
for (auto& pair : pluginEnvs) {
|
||||
lua_close(pair.second);
|
||||
}
|
||||
pluginEnvs.clear();
|
||||
}
|
||||
|
||||
auto reloadLuaScripts() -> void {
|
||||
releaseLuaScripts();
|
||||
initLuaScripts();
|
||||
}
|
||||
auto Init() -> void {
|
||||
// luaPath = Tools::GetExePath() + "\\huoji_scripts\\";
|
||||
luaPath = "F:\\source2\\huoji_scripts\\";
|
||||
initLuaScripts();
|
||||
}
|
||||
} // namespace ScriptEngine
|
||||
|
||||
Reference in New Issue
Block a user