-
Notifications
You must be signed in to change notification settings - Fork 1
/
luaconf.h
166 lines (145 loc) · 3.56 KB
/
luaconf.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <map>
#include <utility>
#include <string>
#include <assert.h>
#include <vector>
class LuaScript {
public:
LuaScript() {}
LuaScript(const std::string path) {
load(path);
}
void load(const std::string path) {
L = luaL_newstate();
luaL_openlibs(L);
if (luaL_loadfile(L, path.c_str()) || lua_pcall(L, 0, 0, 0))
printf("%s\n", lua_tostring(L, -1));
}
~LuaScript() {
lua_close(L);
}
double getNumber(const std::string varName) {
lua_getglobal(L, varName.c_str());
assert(lua_isnumber(L, -1));
double value = lua_tonumber(L, -1);
lua_remove(L, -1);
return value;
}
bool getBoolean(const std::string varName) {
lua_getglobal(L, varName.c_str());
assert(lua_isboolean(L, -1));
int value = lua_toboolean(L, -1);
lua_remove(L, -1);
return value == 1 ? true : false;
}
const std::string getString(const std::string varName) {
lua_getglobal(L, varName.c_str());
assert(lua_isstring(L, -1));
const std::string value = lua_tostring(L, -1);
lua_remove(L, -1);
return value;
}
int getType(const std::string varName) {
lua_getglobal(L, varName.c_str());
int t = lua_type(L, -1);
lua_remove(L, -1);
return t;
}
private:
lua_State* L;
};
class LuaConf {
public:
//LuaConf() {}
LuaConf(const std::string path) {
this->path = path;
script.load(path);
}
void loadArgs(const std::string* varNames, int n) {
for (int i = 0; i < n; ++i) {
luaVar* lv = new luaVar();
int type = script.getType(varNames[i]);
switch (type) {
case LUA_TNUMBER:
lv->type = LUA_TNUMBER;
lv->number = script.getNumber(varNames[i]);
break;
case LUA_TBOOLEAN:
lv->type = LUA_TBOOLEAN;
lv->boolean = script.getBoolean(varNames[i]);
break;
case LUA_TSTRING:
lv->type = LUA_TSTRING;
lv->string = script.getString(varNames[i]);
break;
}
luaVarMap[varNames[i]] = lv;
}
}
void reload() {
script.load(path);
int n = 0;
std::vector<std::string> keys;
for (auto i = luaVarMap.begin(); i != luaVarMap.end(); ++i) {
keys.push_back(i->first.c_str());
++n;
}
loadArgs(keys.data(), n);
}
//TODO: template for int and double
const double* LuaConf::getNumberPtr(const std::string varName) {
if (luaVarMap.find(varName) == luaVarMap.end()) {
luaVarMap[varName] = new luaVar();
luaVarMap[varName]->type = LUA_TNUMBER;
luaVarMap[varName]->number = script.getNumber(varName);
}
else {
assert(luaVarMap[varName]->type == LUA_TNUMBER);
}
return &luaVarMap[varName]->number;
}
const bool* LuaConf::getBoolPtr(const std::string varName) {
if (luaVarMap.find(varName) == luaVarMap.end()) {
luaVarMap[varName] = new luaVar();
luaVarMap[varName]->type = LUA_TBOOLEAN;
luaVarMap[varName]->boolean = script.getBoolean(varName);
}
else {
assert(luaVarMap[varName]->type == LUA_TBOOLEAN);
}
return &luaVarMap[varName]->boolean;
}
const std::string* LuaConf::getStringPtr(const std::string varName) {
if (luaVarMap.find(varName) == luaVarMap.end()) {
luaVarMap[varName] = new luaVar();
luaVarMap[varName]->type = LUA_TSTRING;
luaVarMap[varName]->string = script.getString(varName);
}
else {
assert(luaVarMap[varName]->type == LUA_TSTRING);
}
return &luaVarMap[varName]->string;
}
private:
struct luaVar {
luaVar() {}
~luaVar() {}
luaVar& operator =(const luaVar&) { return *this; }
int type;
union {
double number;
bool boolean;
std::string string;
luaVar* list;
};
};
LuaScript script;
std::string path;
std::map<std::string, luaVar*> luaVarMap;
};