comparison Engine/LuaVM.cpp @ 2499:68cdef6879a0

engine folder
author Ritor1
date Fri, 19 Sep 2014 02:57:42 +0600
parents
children
comparison
equal deleted inserted replaced
2498:92eeeb5200f2 2499:68cdef6879a0
1 #define _CRTDBG_MAP_ALLOC
2 #include <stdlib.h>
3 #include <crtdbg.h>
4
5 #define _CRT_SECURE_NO_WARNINGS
6 #include "LuaVM.h"
7 #include "lib/lua/lua.h"
8 #include "Log.h"
9 #include "OSAPI.h"
10
11 extern "C" int luaopen_UIControl(lua_State *L); // declare the wrapped module
12 extern "C" int luaopen_Player(lua_State *L); // declare the wrapped module
13
14 LuaVM *lua = nullptr;
15
16 void LuaVM::Initialize()
17 {
18 if (L)
19 Log::Warning(L"Overwriting previous Lua state");
20
21 L = luaL_newstate();
22 if (!L)
23 Log::Warning(L"Error creating Lua context.\n");
24
25 // open default lua libs
26 luaL_openlibs(L);
27
28 // open each cxx module
29 luaopen_UIControl(L);
30
31 //if ( luaL_dofile(L,GetScriptFileLocation("script.lua")))
32 // Log::Warning(L"Error opening script.lua\n");
33 }
34
35 bool LuaVM::DoFile(const char *filename)
36 {
37 if (luaL_dofile(L, GetScriptFileLocation(filename)))
38 {
39 Log::Warning(L"Error opening script %s", filename);
40 return false;
41 }
42 return true;
43 }
44
45 const char *LuaVM::GetScriptFileLocation(const char *script_name)
46 {
47 static DWORD tls_index = TlsAlloc();
48
49 auto buf = (char *)TlsGetValue(tls_index);
50 if (!buf)
51 {
52 buf = new char[1024];
53 TlsSetValue(tls_index, buf);
54 }
55
56 strcpy(buf, "data/scripts/lua/core/");
57 strcat(buf, script_name);
58 return buf;
59 }