2052
|
1 #include "LuaVM.h"
|
|
2 #include "lib/lua/lua.h"
|
|
3 #include "Log.h"
|
|
4 #include "OSAPI.h"
|
|
5
|
|
6 extern "C" int luaopen_UIControl(lua_State *L); // declare the wrapped module
|
2057
|
7 extern "C" int luaopen_Player(lua_State *L); // declare the wrapped module
|
2052
|
8
|
|
9 LuaVM *lua = nullptr;
|
|
10
|
|
11 void LuaVM::Initialize()
|
|
12 {
|
|
13 if (L)
|
|
14 Log::Warning(L"Overwriting previous Lua state");
|
|
15
|
|
16 L = luaL_newstate();
|
|
17 if (!L)
|
|
18 Log::Warning(L"Error creating Lua context.\n");
|
|
19
|
|
20 // open default lua libs
|
|
21 luaL_openlibs(L);
|
|
22
|
|
23 // open each cxx module
|
|
24 luaopen_UIControl(L);
|
|
25
|
|
26 //if ( luaL_dofile(L,GetScriptFileLocation("script.lua")))
|
|
27 // Log::Warning(L"Error opening script.lua\n");
|
|
28 }
|
|
29
|
|
30 bool LuaVM::DoFile(const char *filename)
|
|
31 {
|
|
32 if (luaL_dofile(L, GetScriptFileLocation(filename)))
|
|
33 {
|
|
34 Log::Warning(L"Error opening script %s", filename);
|
|
35 return false;
|
|
36 }
|
|
37 return true;
|
|
38 }
|
|
39
|
|
40 const char *LuaVM::GetScriptFileLocation(const char *script_name)
|
|
41 {
|
|
42 static DWORD tls_index = TlsAlloc();
|
|
43
|
|
44 auto buf = (char *)TlsGetValue(tls_index);
|
|
45 if (!buf)
|
|
46 {
|
|
47 buf = new char[1024];
|
|
48 TlsSetValue(tls_index, buf);
|
|
49 }
|
|
50
|
|
51 strcpy(buf, "data/scripts/lua/core/");
|
|
52 strcat(buf, script_name);
|
|
53 return buf;
|
|
54 } |