annotate LuaVM.cpp @ 2415:f4af3b203f65

LOD.cpp cleaned and search memory corrupt
author Ritor1
date Sun, 20 Jul 2014 22:38:31 +0600
parents aff7a7b072b7
children
rev   line source
2415
f4af3b203f65 LOD.cpp cleaned and search memory corrupt
Ritor1
parents: 2253
diff changeset
1 #define _CRTDBG_MAP_ALLOC
f4af3b203f65 LOD.cpp cleaned and search memory corrupt
Ritor1
parents: 2253
diff changeset
2 #include <stdlib.h>
f4af3b203f65 LOD.cpp cleaned and search memory corrupt
Ritor1
parents: 2253
diff changeset
3 #include <crtdbg.h>
f4af3b203f65 LOD.cpp cleaned and search memory corrupt
Ritor1
parents: 2253
diff changeset
4
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
5 #define _CRT_SECURE_NO_WARNINGS
2052
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
6 #include "LuaVM.h"
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
7 #include "lib/lua/lua.h"
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
8 #include "Log.h"
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
9 #include "OSAPI.h"
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
10
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
11 extern "C" int luaopen_UIControl(lua_State *L); // declare the wrapped module
2057
b7485a6502f2 AgressorAtackInfo
Ritor1
parents: 2052
diff changeset
12 extern "C" int luaopen_Player(lua_State *L); // declare the wrapped module
2052
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
13
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
14 LuaVM *lua = nullptr;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
15
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
16 void LuaVM::Initialize()
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
17 {
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"Overwriting previous Lua state");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
20
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
21 L = luaL_newstate();
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
22 if (!L)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
23 Log::Warning(L"Error creating Lua context.\n");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
24
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
25 // open default lua libs
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
26 luaL_openlibs(L);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
27
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
28 // open each cxx module
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
29 luaopen_UIControl(L);
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("script.lua")))
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
32 // Log::Warning(L"Error opening script.lua\n");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
33 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
34
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
35 bool LuaVM::DoFile(const char *filename)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
36 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
37 if (luaL_dofile(L, GetScriptFileLocation(filename)))
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
38 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
39 Log::Warning(L"Error opening script %s", filename);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
40 return false;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
41 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
42 return true;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
43 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
44
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
45 const char *LuaVM::GetScriptFileLocation(const char *script_name)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
46 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
47 static DWORD tls_index = TlsAlloc();
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
48
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
49 auto buf = (char *)TlsGetValue(tls_index);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
50 if (!buf)
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
51 {
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
52 buf = new char[1024];
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
53 TlsSetValue(tls_index, buf);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
54 }
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
55
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
56 strcpy(buf, "data/scripts/lua/core/");
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
57 strcat(buf, script_name);
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
58 return buf;
71a814f4482a Moved implementation to cpp
Nomad
parents:
diff changeset
59 }