File: te_lua_context.cpp

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (238 lines) | stat: -rw-r--r-- 6,973 bytes parent folder | download
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/debug.h"
#include "common/textconsole.h"
#include "common/lua/lua.h"
#include "common/lua/lualib.h"
#include "common/lua/lauxlib.h"

#include "tetraedge/te/te_lua_context.h"

namespace Tetraedge {

//static lua_State *globalState = nullptr;

static int luaPanicFunction(lua_State *state) {
	const char *msg = lua_tolstring(state, -1, nullptr);
	warning("Lua: %s", msg);
	lua_settop(state, -2);
	return 1;
}

TeLuaContext::TeLuaContext() : _luaState(nullptr) {
	_luaState = lua_open();
	luaL_openlibs(_luaState);
	lua_atpanic(_luaState, luaPanicFunction);
}

TeLuaContext::~TeLuaContext() {
	destroy();
}

void TeLuaContext::addBindings(void(*fn)(lua_State *)) {
	fn(_luaState);
}

void TeLuaContext::create() {
	_luaState = lua_open();
	luaL_openlibs(_luaState);
	lua_atpanic(_luaState, luaPanicFunction);
}

void TeLuaContext::destroy() {
	if (_luaState)
		lua_close(_luaState);
	_luaState = nullptr;
}

TeVariant TeLuaContext::global(const Common::String &name) {
	lua_getglobal(_luaState, name.c_str());
	TeVariant retval;
	int type = lua_type(_luaState, -1);
	if (type == LUA_TBOOLEAN) {
		int result = lua_toboolean(_luaState, -1);
		lua_settop(_luaState, -2);
		return TeVariant(result > 0);
	} else if (type == LUA_TNUMBER) {
		double result = lua_tonumber(_luaState, -1);
		lua_settop(_luaState, -2);
		return TeVariant(result);
	} else if (type == LUA_TSTRING) {
		const char *str = lua_tolstring(_luaState, -1, nullptr);
		lua_settop(_luaState, -2);
		return TeVariant(str);
	}
	if (type != LUA_TNIL)
		warning("TeLuaContext::global: Unexpected type %d for global %s", type, name.c_str());
	else
		debug("TeLuaContext::global: Request for nil global %s", name.c_str());
	return TeVariant();
}

void TeLuaContext::setGlobal(const Common::String &name, int val) {
	lua_pushinteger(_luaState, val);
	lua_setglobal(_luaState, name.c_str());
}

void TeLuaContext::setGlobal(const Common::String &name, bool val) {
	lua_pushboolean(_luaState, val);
	lua_setglobal(_luaState, name.c_str());
}

void TeLuaContext::setGlobal(const Common::String &name, const Common::String &val) {
	lua_pushstring(_luaState, val.c_str());
	lua_setglobal(_luaState, name.c_str());
}

void TeLuaContext::removeGlobal(const Common::String &name) {
	lua_pushnil(_luaState);
	lua_setglobal(_luaState, name.c_str());
}

void TeLuaContext::registerCFunction(const Common::String &name, int(*fn)(lua_State *)) {
	lua_pushcclosure(_luaState, fn, 0);
	lua_setglobal(_luaState, name.c_str());
}

void TeLuaContext::setInRegistry(const Common::String &name, TeLuaGUI *gui) {
	lua_pushstring(_luaState, name.c_str());
	lua_pushlightuserdata(_luaState, gui);
	lua_settable(_luaState, LUA_REGISTRYINDEX);
}

// Types for save file.  Aligned with the Lua types at type of
// writing, but don't save them just in case they could change.
enum TeLuaSaveVarType {
	None = 0,
	Boolean = 1,
	Number = 3,
	String = 4
};

#define TETRAEDGE_LUA_DEBUG_SAVELOAD

Common::Error TeLuaContext::syncState(Common::Serializer &s) {
	// Save/Load globals.  The format of saving is:
	// [type][name][val] [type][name][val]...
	// Vals can be string, number (uint32), or boolean (byte)
	// The type of "None" (0) is the end of the list (and has no name/val).
	if (s.isSaving()) {
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
		debug("TeLuaContext::syncState: --- Saving globals: ---");
#endif
		lua_pushvalue(_luaState, LUA_GLOBALSINDEX);
		lua_pushnil(_luaState);
		int nextresult = lua_next(_luaState, -2);
		while (true) {
			if (nextresult == 0) {
				TeLuaSaveVarType stype = None;
				s.syncAsUint32LE(stype);
				lua_settop(_luaState, -2);
				break;
			}
			uint vtype = lua_type(_luaState, -1);
			Common::String name = lua_tolstring(_luaState, -2, nullptr);
			if (vtype == LUA_TBOOLEAN) {
				TeLuaSaveVarType stype = Boolean;
				s.syncAsUint32LE(stype);
				s.syncString(name);
				bool val = lua_toboolean(_luaState, -1);
				s.syncAsByte(val);
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
				debug("TeLuaContext::syncState: bool %s = %s", name.c_str(), val ? "true" : "false");
#endif
			} else if (vtype == LUA_TNUMBER) {
				TeLuaSaveVarType stype = Number;
				s.syncAsUint32LE(stype);
				s.syncString(name);
				double val = lua_tonumber(_luaState, -1);
				s.syncAsDoubleLE(val);
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
				debug("TeLuaContext::syncState: num %s = %f", name.c_str(), val);
#endif
			} else if (vtype == LUA_TSTRING) {
				TeLuaSaveVarType stype = String;
				s.syncAsUint32LE(stype);
				s.syncString(name);
				Common::String val = lua_tostring(_luaState, -1);
				s.syncString(val);
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
				debug("TeLuaContext::syncState: str %s = '%s'", name.c_str(), val.c_str());
#endif
			}
			lua_settop(_luaState, -2);
			nextresult = lua_next(_luaState, -2);
		}
	} else {
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
		debug("TeLuaContext::syncState: --- Loading globals: --- ");
#endif
		// loading
		TeLuaSaveVarType vtype = None;
		s.syncAsUint32LE(vtype);
		while (vtype != None) {
			Common::String name;
			s.syncString(name);
			switch (vtype) {
				case Boolean: {
					byte b = 0;
					s.syncAsByte(b);
					lua_pushboolean(_luaState, b);
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
					debug("TeLuaContext::syncState: bool %s = %s", name.c_str(), b ? "true" : "false");
#endif
					break;
				}
				case Number: {
					float d = 0;
					s.syncAsDoubleLE(d);
					lua_pushnumber(_luaState, d);
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
					debug("TeLuaContext::syncState: num %s = %f", name.c_str(), d);
#endif
					break;
				}
				case String: {
					Common::String str;
					s.syncString(str);
					lua_pushstring(_luaState, str.c_str());
#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
					debug("TeLuaContext::syncState: str %s = '%s'", name.c_str(), str.c_str());
#endif
					break;
				}
				default:
					error("Unexpected lua type on load %d", (int)vtype);
			}
			lua_setglobal(_luaState, name.c_str());
			s.syncAsUint32LE(vtype);
		}
	}

#ifdef TETRAEDGE_LUA_DEBUG_SAVELOAD
	debug("TeLuaContext::syncState: -------- end --------");
#endif

	return Common::kNoError;
}

} // end namespace Tetraedge