~ubuntu-branches/ubuntu/natty/spring/natty

« back to all changes in this revision

Viewing changes to AI/Skirmish/KAIK/LuaParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Ritchie
  • Date: 2010-09-23 18:56:03 UTC
  • mfrom: (3.1.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100923185603-st97s5chplo42y7w
Tags: 0.82.5.1+dfsg1-1ubuntu1
* Latest upstream version for online play
* debian/control: Replace (rather than conflict) spring-engine
  - spring-engine will be a dummy package (LP: #612905)
  - also set maintainer to MOTU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <iostream>
 
2
#include <cassert>
 
3
#include <boost/foreach.hpp>
 
4
 
 
5
#ifdef LUA_LIB_EXT
 
6
#include <lua5.1/lua.hpp>
 
7
#else
 
8
#include "lib/lua/include/lua.h"
 
9
#include "lib/lua/include/lualib.h"
 
10
#include "lib/lua/include/lauxlib.h"
 
11
#endif
 
12
 
 
13
#include "./Util.h"
 
14
#include "./LuaParser.hpp"
 
15
 
 
16
void LuaTable::Print(int depth) const {
 
17
        std::string tabs = "";
 
18
        for (int i = 0; i < depth; i++) {
 
19
                tabs += "\t";
 
20
        }
 
21
 
 
22
        for (std::map<LuaTable*, LuaTable*>::const_iterator it = TblTblPairs.begin(); it != TblTblPairs.end(); it++) {
 
23
                std::cout << tabs << "k<tbl>: ";
 
24
                std::cout << std::endl;
 
25
                        it->first->Print(depth + 1);
 
26
                std::cout << tabs << "v<tbl>: ";
 
27
                std::cout << std::endl;
 
28
                        it->second->Print(depth + 1);
 
29
        }
 
30
        for (std::map<LuaTable*, std::string>::const_iterator it = TblStrPairs.begin(); it != TblStrPairs.end(); it++) {
 
31
                std::cout << tabs << "k<tbl>: ";
 
32
                std::cout << std::endl;
 
33
                        it->first->Print(depth + 1);
 
34
                std::cout << tabs << "v<str>: " << it->second;
 
35
                std::cout << std::endl;
 
36
        }
 
37
        for (std::map<LuaTable*, int>::const_iterator it = TblIntPairs.begin(); it != TblIntPairs.end(); it++) {
 
38
                std::cout << tabs << "k<tbl>: ";
 
39
                std::cout << std::endl;
 
40
                        it->first->Print(depth + 1);
 
41
                std::cout << tabs << "v<int>: " << it->second;
 
42
                std::cout << std::endl;
 
43
        }
 
44
 
 
45
        for (std::map<std::string, LuaTable*>::const_iterator it = StrTblPairs.begin(); it != StrTblPairs.end(); it++) {
 
46
                std::cout << tabs << "k<str>: " << it->first;
 
47
                std::cout << ", v<tbl>: ";
 
48
                std::cout << std::endl;
 
49
                        it->second->Print(depth + 1);
 
50
        }
 
51
        for (std::map<std::string, std::string>::const_iterator it = StrStrPairs.begin(); it != StrStrPairs.end(); it++) {
 
52
                std::cout << tabs << "k<str>: " << it->first;
 
53
                std::cout << ", v<str>: " << it->second;
 
54
                std::cout << std::endl;
 
55
        }
 
56
        for (std::map<std::string, int>::const_iterator it = StrIntPairs.begin(); it != StrIntPairs.end(); it++) {
 
57
                std::cout << tabs << "k<str>: " << it->first;
 
58
                std::cout << ", v<int>: " << it->second;
 
59
                std::cout << std::endl;
 
60
        }
 
61
 
 
62
        for (std::map<int, LuaTable*>::const_iterator it = IntTblPairs.begin(); it != IntTblPairs.end(); it++) {
 
63
                std::cout << tabs << "k<int>: " << it->first;
 
64
                std::cout << ", v<tbl>: ";
 
65
                std::cout << std::endl;
 
66
                        it->second->Print(depth + 1);
 
67
        }
 
68
        for (std::map<int, std::string>::const_iterator it = IntStrPairs.begin(); it != IntStrPairs.end(); it++) {
 
69
                std::cout << tabs << "k<int>: " << it->first;
 
70
                std::cout << ", v<str>: " << it->second;
 
71
                std::cout << std::endl;
 
72
        }
 
73
        for (std::map<int, int>::const_iterator it = IntIntPairs.begin(); it != IntIntPairs.end(); it++) {
 
74
                std::cout << tabs << "k<int>: " << it->first;
 
75
                std::cout << ", v<int>: " << it->second;
 
76
                std::cout << std::endl;
 
77
        }
 
78
}
 
79
 
 
80
void LuaTable::Parse(lua_State* ls, int depth) {
 
81
        assert(lua_istable(ls, -1));
 
82
        lua_pushnil(ls);
 
83
        assert(lua_istable(ls, -2));
 
84
 
 
85
        while (lua_next(ls, -2) != 0) {
 
86
                assert(lua_istable(ls, -3));
 
87
 
 
88
                switch (lua_type(ls, -2)) {
 
89
                        case LUA_TTABLE: {
 
90
                                LuaTable* key = new LuaTable();
 
91
 
 
92
                                switch (lua_type(ls, -1)) {
 
93
                                        case LUA_TTABLE: {
 
94
                                                TblTblPairs[key] = new LuaTable();
 
95
                                                TblTblPairs[key]->Parse(ls, depth + 1);
 
96
 
 
97
                                                lua_pop(ls, 1);
 
98
 
 
99
                                                key->Parse(ls, depth + 1);
 
100
                                        } break;
 
101
                                        case LUA_TSTRING: {
 
102
                                                TblStrPairs[key] = AIUtil::StringToLower(lua_tostring(ls, -1));
 
103
                                                lua_pop(ls, 1);
 
104
 
 
105
                                                key->Parse(ls, depth + 1);
 
106
                                        } break;
 
107
                                        case LUA_TNUMBER: {
 
108
                                                TblIntPairs[key] = lua_tointeger(ls, -1);
 
109
                                                lua_pop(ls, 1);
 
110
 
 
111
                                                key->Parse(ls, depth + 1);
 
112
                                        } break;
 
113
                                }
 
114
 
 
115
                                continue;
 
116
                        } break;
 
117
 
 
118
                        case LUA_TSTRING: {
 
119
                                const std::string key = AIUtil::StringToLower(lua_tostring(ls, -2));
 
120
 
 
121
                                switch (lua_type(ls, -1)) {
 
122
                                        case LUA_TTABLE: {
 
123
                                                StrTblPairs[key] = new LuaTable();
 
124
                                                StrTblPairs[key]->Parse(ls, depth + 1);
 
125
                                        } break;
 
126
                                        case LUA_TSTRING: {
 
127
                                                StrStrPairs[key] = AIUtil::StringToLower(lua_tostring(ls, -1));
 
128
                                        } break;
 
129
                                        case LUA_TNUMBER: {
 
130
                                                StrIntPairs[key] = lua_tointeger(ls, -1);
 
131
                                        } break;
 
132
                                }
 
133
 
 
134
                                lua_pop(ls, 1);
 
135
                                continue;
 
136
                        } break;
 
137
 
 
138
                        case LUA_TNUMBER: {
 
139
                                const int key = lua_tointeger(ls, -2);
 
140
 
 
141
                                switch (lua_type(ls, -1)) {
 
142
                                        case LUA_TTABLE: {
 
143
                                                IntTblPairs[key] = new LuaTable();
 
144
                                                IntTblPairs[key]->Parse(ls, depth + 1);
 
145
                                        } break;
 
146
                                        case LUA_TSTRING: {
 
147
                                                IntStrPairs[key] = AIUtil::StringToLower(lua_tostring(ls, -1));
 
148
                                        } break;
 
149
                                        case LUA_TNUMBER: {
 
150
                                                IntIntPairs[key] = lua_tointeger(ls, -1);
 
151
                                        } break;
 
152
                                }
 
153
 
 
154
                                lua_pop(ls, 1);
 
155
                                continue;
 
156
                        } break;
 
157
                }
 
158
        }
 
159
 
 
160
        assert(lua_istable(ls, -1));
 
161
}
 
162
 
 
163
void LuaTable::GetTblTblKeys(std::list<LuaTable*>*   keys) const { BOOST_FOREACH(TblTblPair p, TblTblPairs) { keys->push_back(p.first); } }
 
164
void LuaTable::GetTblStrKeys(std::list<LuaTable*>*   keys) const { BOOST_FOREACH(TblStrPair p, TblStrPairs) { keys->push_back(p.first); } }
 
165
void LuaTable::GetTblIntKeys(std::list<LuaTable*>*   keys) const { BOOST_FOREACH(TblIntPair p, TblIntPairs) { keys->push_back(p.first); } }
 
166
void LuaTable::GetStrTblKeys(std::list<std::string>* keys) const { BOOST_FOREACH(StrTblPair p, StrTblPairs) { keys->push_back(p.first); } }
 
167
void LuaTable::GetStrStrKeys(std::list<std::string>* keys) const { BOOST_FOREACH(StrStrPair p, StrStrPairs) { keys->push_back(p.first); } }
 
168
void LuaTable::GetStrIntKeys(std::list<std::string>* keys) const { BOOST_FOREACH(StrIntPair p, StrIntPairs) { keys->push_back(p.first); } }
 
169
void LuaTable::GetIntTblKeys(std::list<int>*         keys) const { BOOST_FOREACH(IntTblPair p, IntTblPairs) { keys->push_back(p.first); } }
 
170
void LuaTable::GetIntStrKeys(std::list<int>*         keys) const { BOOST_FOREACH(IntStrPair p, IntStrPairs) { keys->push_back(p.first); } }
 
171
void LuaTable::GetIntIntKeys(std::list<int>*         keys) const { BOOST_FOREACH(IntIntPair p, IntIntPairs) { keys->push_back(p.first); } }
 
172
 
 
173
const LuaTable* LuaTable::GetTblVal(LuaTable* key, LuaTable* defVal) const {
 
174
        const std::map<LuaTable*, LuaTable*>::const_iterator it = TblTblPairs.find(key);
 
175
        return ((it != TblTblPairs.end())? it->second: defVal);
 
176
}
 
177
const LuaTable* LuaTable::GetTblVal(const std::string& key, LuaTable* defVal) const {
 
178
        const std::map<std::string, LuaTable*>::const_iterator it = StrTblPairs.find(key);
 
179
        return ((it != StrTblPairs.end())? it->second: defVal);
 
180
}
 
181
const LuaTable* LuaTable::GetTblVal(int key, LuaTable* defVal) const {
 
182
        const std::map<int, LuaTable*>::const_iterator it = IntTblPairs.find(key);
 
183
        return ((it != IntTblPairs.end())? it->second: defVal);
 
184
}
 
185
 
 
186
const std::string& LuaTable::GetStrVal(LuaTable* key, const std::string& defVal) const {
 
187
        const std::map<LuaTable*, std::string>::const_iterator it = TblStrPairs.find(key);
 
188
        return ((it != TblStrPairs.end())? it->second: defVal);
 
189
}
 
190
const std::string& LuaTable::GetStrVal(const std::string& key, const std::string& defVal) const {
 
191
        const std::map<std::string, std::string>::const_iterator it = StrStrPairs.find(key);
 
192
        return ((it != StrStrPairs.end())? it->second: defVal);
 
193
}
 
194
const std::string& LuaTable::GetStrVal(int key, const std::string& defVal) const {
 
195
        const std::map<int, std::string>::const_iterator it = IntStrPairs.find(key);
 
196
        return ((it != IntStrPairs.end())? it->second: defVal);
 
197
}
 
198
 
 
199
int LuaTable::GetIntVal(LuaTable* key, int defVal) const {
 
200
        const std::map<LuaTable*, int>::const_iterator it = TblIntPairs.find(key);
 
201
        return ((it != TblIntPairs.end())? it->second: defVal);
 
202
}
 
203
int LuaTable::GetIntVal(const std::string& key, int defVal) const {
 
204
        const std::map<std::string, int>::const_iterator it = StrIntPairs.find(key);
 
205
        return ((it != StrIntPairs.end())? it->second: defVal);
 
206
}
 
207
int LuaTable::GetIntVal(int key, int defVal) const {
 
208
        const std::map<int, int>::const_iterator it = IntIntPairs.find(key);
 
209
        return ((it != IntIntPairs.end())? it->second: defVal);
 
210
}
 
211
 
 
212
 
 
213
 
 
214
LuaParser::LuaParser(): luaState(lua_open()), root(NULL) {
 
215
        luaL_openlibs(luaState);
 
216
}
 
217
LuaParser::~LuaParser() {
 
218
        for (std::map<std::string, LuaTable*>::iterator it = tables.begin(); it != tables.end(); it++) {
 
219
                delete it->second;
 
220
        }
 
221
 
 
222
        root = NULL;
 
223
        lua_close(luaState);
 
224
}
 
225
 
 
226
bool LuaParser::Execute(const std::string& file, const std::string& table) {
 
227
        bool ret = false;
 
228
 
 
229
        int loadErr = 0; // 0 | LUA_ERRFILE | LUA_ERRSYNTAX | LUA_ERRMEM
 
230
        int callErr = 0; // 0 | LUA_ERRRUN | LUA_ERRMEM | LUA_ERRERR
 
231
 
 
232
        if ((loadErr = luaL_loadfile(luaState, file.c_str())) != 0 || (callErr = lua_pcall(luaState, 0, 0, 0)) != 0) {
 
233
                error = std::string(lua_tostring(luaState, -1));
 
234
                lua_pop(luaState, 1);
 
235
                return false;
 
236
        }
 
237
 
 
238
        if (tables.find(file) == tables.end()) {
 
239
                tables[file] = new LuaTable();
 
240
                root = tables[file];
 
241
 
 
242
                assert(lua_gettop(luaState) == 0);
 
243
                lua_getglobal(luaState, table.c_str());
 
244
 
 
245
                if (lua_isnil(luaState, -1) == 0) {
 
246
                        assert(lua_istable(luaState, -1));
 
247
                        root->Parse(luaState, 0);
 
248
                        ret = true;
 
249
                } else {
 
250
                        error = "no global table variable \'" + table + "\' declared in chunk \'" + file + "\'";
 
251
                }
 
252
 
 
253
                lua_pop(luaState, 1);
 
254
                assert(lua_gettop(luaState) == 0);
 
255
        } else {
 
256
                root = tables[file];
 
257
                ret = true;
 
258
        }
 
259
 
 
260
        return ret;
 
261
}