annotate LuaVM.cpp @ 2254:2e02c384c62b

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