annotate LuaVM.cpp @ 2052:71a814f4482a

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