From de46b91a524c5f2c8e72b379f2900afe34ccb815 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 11 Nov 2021 10:29:17 +0800 Subject: *misc --- Runtime/Lua/LuaHelper.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'Runtime/Lua/LuaHelper.cpp') diff --git a/Runtime/Lua/LuaHelper.cpp b/Runtime/Lua/LuaHelper.cpp index 71fe782..084d4f6 100644 --- a/Runtime/Lua/LuaHelper.cpp +++ b/Runtime/Lua/LuaHelper.cpp @@ -236,4 +236,32 @@ bool LuaHelper::GetLuaClass(LuaBind::State& state, const char* fullName) lua_replace(state, top + 1); lua_settop(state, top + 1); return true; +} + +bool LuaHelper::GetGlobalField(LuaBind::State& state, const char* fullName) +{ + if (fullName == NULL || strlen(fullName) == 0) + return false; + int top = state.GetTop(); + lua_pushvalue(state, LUA_GLOBALSINDEX); + string name = fullName; + while (name.size() > 0) + { + int dot = name.find('.'); + dot = dot != string::npos ? dot : name.size(); + string pkg = name.substr(0, dot); + if (dot < name.size()) + name = name.substr(dot + 1, name.size() - dot - 1); + else + name = ""; + lua_getfield(state, -1, pkg.c_str()); + if (lua_isnil(state, -1)) + { + lua_settop(state, top); + return false; + } + } + lua_replace(state, top + 1); + lua_settop(state, top + 1); + return true; } \ No newline at end of file -- cgit v1.1-26-g67d0