annotate LuaVM.cpp @ 2252:cd80d91b780f

Слияние
author Ritor1
date Wed, 26 Feb 2014 20:10:50 +0600
parents b7485a6502f2
children aff7a7b072b7
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
2057
b7485a6502f2 AgressorAtackInfo
Ritor1
parents: 2052
diff changeset
7 extern "C" int luaopen_Player(lua_State *L); // declare the wrapped module
2052
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
8
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
9 LuaVM *lua = nullptr;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
10
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
11 void LuaVM::Initialize()
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
12 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
13 if (L)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
14 Log::Warning(L"Overwriting previous Lua state");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
15
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
16 L = luaL_newstate();
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
17 if (!L)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
18 Log::Warning(L"Error creating Lua context.\n");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
19
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
20 // open default lua libs
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
21 luaL_openlibs(L);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
22
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
23 // open each cxx module
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
24 luaopen_UIControl(L);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
25
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
26 //if ( luaL_dofile(L,GetScriptFileLocation("script.lua")))
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
27 // Log::Warning(L"Error opening script.lua\n");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
28 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
29
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
30 bool LuaVM::DoFile(const char *filename)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
31 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
32 if (luaL_dofile(L, GetScriptFileLocation(filename)))
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
33 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
34 Log::Warning(L"Error opening script %s", filename);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
35 return false;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
36 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
37 return true;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
38 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
39
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
40 const char *LuaVM::GetScriptFileLocation(const char *script_name)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
41 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
42 static DWORD tls_index = TlsAlloc();
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
43
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
44 auto buf = (char *)TlsGetValue(tls_index);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
45 if (!buf)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
46 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
47 buf = new char[1024];
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
48 TlsSetValue(tls_index, buf);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
49 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
50
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
51 strcpy(buf, "data/scripts/lua/core/");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
52 strcat(buf, script_name);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
53 return buf;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
54 }