forked from spring/RapidTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lua.cpp
70 lines (53 loc) · 1.3 KB
/
Lua.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Lua.hpp"
#include <stdexcept>
#include <string.h>
namespace Rapid {
LuaT::LuaT()
:
mState{luaL_newstate()}
{
luaL_openlibs(mState);
}
LuaT::~LuaT()
{
lua_close(mState);
}
ModinfoT LuaT::getModinfo(std::string const & Buffer)
{
auto Error = luaL_dostring(mState, Buffer.c_str());
if (Error != 0) throw std::runtime_error{"Lua error"};
std::string Name;
std::string Version;
std::vector<std::string> Depends;
lua_pushnil(mState);
while (lua_next(mState, -2) != 0)
{
char const * Symbol = lua_tostring(mState, -2);
// Get name string
if (strcasecmp(Symbol, "name") == 0) Name = lua_tostring(mState, -1);
// Get version string
else if (strcasecmp(Symbol, "version") == 0) Version = lua_tostring(mState, -1);
// Get depends array
else if (strcasecmp(Symbol, "depend") == 0)
{
lua_pushstring(mState, "table");
lua_gettable(mState, LUA_GLOBALSINDEX);
lua_pushstring(mState, "getn");
lua_gettable(mState, -2);
lua_pushvalue(mState, -3);
lua_call(mState, 1, 1);
auto Number = lua_tonumber(mState, -1);
Depends.reserve(Number);
lua_pop(mState, 2);
lua_pushnil(mState);
while (lua_next(mState, -2) != 0)
{
Depends.push_back(lua_tostring(mState, -1));
lua_pop(mState, 1);
}
}
lua_pop(mState, 1);
}
return {Name, Version, Depends};
}
}