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