annotate Engine/LuaVM.cpp @ 2542:bacf9809126a

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