source: network-game/server/LuaLoader.cpp@ a9a1295

Last change on this file since a9a1295 was 53643ca, checked in by Dmitry Portnoy <dmp1488@…>, 10 years ago

Server loads user profile and game history info from the database, saves game history to the db after every game, and uses a lua settings file to load db settings

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[53643ca]1#include "LuaLoader.h"
2
3#include <iostream>
4
5using namespace std;
6
7LuaLoader::LuaLoader()
8{
9 // new Lua state
10 std::cout << "[C++] Starting Lua state" << std::endl;
11 this->lua_state = luaL_newstate();
12
13 // load Lua libraries
14 std::cout << "[C++] Loading Lua libraries" << std::endl;
15 static const luaL_Reg lualibs[] =
16 {
17 {"base", luaopen_base},
18 {"io", luaopen_io},
19 {NULL, NULL}
20 };
21 const luaL_Reg *lib = lualibs;
22 for(; lib->func != NULL; lib++)
23 {
24 std::cout << " loading '" << lib->name << "'" << std::endl;
25 luaL_requiref(this->lua_state, lib->name, lib->func, 1);
26 lua_settop(this->lua_state, 0);
27 }
28}
29
30LuaLoader::~LuaLoader()
31{
32}
33
34bool LuaLoader::runScript(string filename)
35{
36 // load the script
37 int status = luaL_loadfile(this->lua_state, filename.c_str());
38 cout << " return: " << status << std::endl;
39
40 // run the script with the given arguments
41 cout << "[C++] Running script" << std::endl;
42 if (status == LUA_OK) {
43 lua_pcall(lua_state, 0, LUA_MULTRET, 0);
44 return true;
45 } else {
46 return false;
47 }
48}
49
50string LuaLoader::getValue(string key)
51{
52 lua_getfield(lua_state, 1, key.c_str());
53 string str(luaL_checkstring(lua_state, -1));
54
55 return str;
56}
Note: See TracBrowser for help on using the repository browser.