~widelands-dev/widelands/trunk

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2006-2024 by the Widelands Development Team
 *
 * 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 2
 * 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 <https://www.gnu.org/licenses/>.
 *
 */

#include "scripting/logic.h"

#include <memory>

#include "base/string.h"
#include "io/filesystem/layered_filesystem.h"
#include "scripting/factory.h"
#include "scripting/globals.h"
#include "scripting/lua_bases.h"
#include "scripting/lua_coroutine.h"
#include "scripting/lua_editor.h"
#include "scripting/lua_game.h"
#include "scripting/lua_globals.h"
#include "scripting/lua_map.h"
#include "scripting/lua_root.h"
#include "scripting/lua_table.h"
#include "scripting/lua_ui.h"
#include "scripting/persistence.h"
#include "scripting/run_script.h"

namespace {

// Setup the basic Widelands functions and pushes egbase into the Lua registry
// so that it is available for all the other Lua functions.
void setup_for_editor_and_game(lua_State* L, Widelands::EditorGameBase* g) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);
	LuaBases::luaopen_wlbases(L);
	LuaMaps::luaopen_wlmap(L);
	LuaUi::luaopen_wlui(L, true);

	// Push the editor game base
	lua_pushlightuserdata(L, static_cast<void*>(g));
	lua_setfield(L, LUA_REGISTRYINDEX, "egbase");
}

// Can run script also from the map.
std::unique_ptr<LuaTable> run_script_maybe_from_map(lua_State* L, const std::string& path) {
	// run_script locks kLua
	if (starts_with(path, "map:")) {
		return run_script(L, path.substr(4), get_egbase(L).map().filesystem());
	}
	return run_script(L, path, g_fs);
}

}  // namespace

LuaEditorInterface::LuaEditorInterface(Widelands::EditorGameBase* g)
   : factory_(new EditorFactory()) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);
	setup_for_editor_and_game(lua_state_, g);
	LuaRoot::luaopen_wlroot(lua_state_, true);
	LuaEditor::luaopen_wleditor(lua_state_);

	// Push the factory class into the registry
	lua_pushlightuserdata(
	   lua_state_, reinterpret_cast<void*>(dynamic_cast<Factory*>(factory_.get())));
	lua_setfield(lua_state_, LUA_REGISTRYINDEX, "factory");
}

std::unique_ptr<LuaTable> LuaEditorInterface::run_script(const std::string& script) {
	return run_script_maybe_from_map(lua_state_, script);
}

LuaFsMenuInterface::LuaFsMenuInterface(FsMenu::MainMenu* menu) : LuaInterface(true) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	lua_pushlightuserdata(lua_state_, static_cast<void*>(menu));
	lua_setfield(lua_state_, LUA_REGISTRYINDEX, "fsmenu");
}

// Special handling of math.random.

// We inject this function to make sure that Lua uses our random number
// generator.  This guarantees that the game stays in sync over the network and
// in replays. Obviously, we only do this for LuaGameInterface, not for
// the others.

// The function was designed to simulate the standard math.random function and
// was therefore copied nearly verbatim from the Lua sources.
static int L_math_random(lua_State* L) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	Widelands::Game& game = get_game(L);
	uint32_t t = game.logic_rand();

	lua_Number r = t / 4294967296.;  // create a double in [0,1)

	switch (lua_gettop(L)) { /* check number of arguments */
	case 0: {                /* no arguments */
		lua_pushdouble(L, r); /* Number between 0 and 1 */
		break;
	}
	case 1: { /* only upper limit */
		int32_t u = luaL_checkint32(L, 1);
		luaL_argcheck(L, 1 <= u, 1, "interval is empty");
		lua_pushuint32(L, floor(r * u) + 1); /* int between 1 and `u' */
		break;
	}
	case 2: { /* lower and upper limits */
		int32_t l = luaL_checkint32(L, 1);
		int32_t u = luaL_checkint32(L, 2);
		luaL_argcheck(L, l <= u, 2, "interval is empty");
		/* int between `l' and `u' */
		lua_pushint32(L, floor(r * (u - l + 1)) + l);
		break;
	}
	default:
		return luaL_error(L, "wrong number of arguments");
	}
	return 1;
}

LuaGameInterface::LuaGameInterface(Widelands::Game* g) : factory_(new GameFactory()) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	setup_for_editor_and_game(lua_state_, g);

	// Overwrite math.random
	lua_getglobal(lua_state_, "math");
	lua_pushcfunction(lua_state_, L_math_random);
	lua_setfield(lua_state_, -2, "random");
	lua_pop(lua_state_, 1);  // pop "math"

	LuaRoot::luaopen_wlroot(lua_state_, false);
	LuaGame::luaopen_wlgame(lua_state_);

	// Push the game into the registry
	lua_pushlightuserdata(lua_state_, static_cast<void*>(g));
	lua_setfield(lua_state_, LUA_REGISTRYINDEX, "game");

	// Push the factory class into the registry
	lua_pushlightuserdata(
	   lua_state_, reinterpret_cast<void*>(dynamic_cast<Factory*>(factory_.get())));
	lua_setfield(lua_state_, LUA_REGISTRYINDEX, "factory");
}

std::unique_ptr<LuaCoroutine> LuaGameInterface::read_coroutine(FileRead& fr) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	std::unique_ptr<LuaCoroutine> rv(new LuaCoroutine(nullptr));
	rv->read(lua_state_, fr);
	return rv;
}

void LuaGameInterface::write_coroutine(FileWrite& fw, const LuaCoroutine& cr) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	cr.write(fw);
}

void LuaGameInterface::read_global_env(FileRead& fr,
                                       Widelands::MapObjectLoader& mol,
                                       uint32_t size) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	// Clean out the garbage before loading.
	lua_gc(lua_state_, LUA_GCCOLLECT, 0);

	assert(lua_gettop(lua_state_) == 0);  // S:
	unpersist_object(lua_state_, fr, mol, size);
	assert(lua_gettop(lua_state_) == 1);  // S: unpersisted_object
	luaL_checktype(lua_state_, -1, LUA_TTABLE);

	// Now, we have to merge all keys from the loaded table
	// into the global table
	lua_pushnil(lua_state_);  // S: table nil
	while (lua_next(lua_state_, 1) != 0) {
		// S: table key value
		lua_rawgeti(
		   lua_state_, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);  // S: table key value globals_table
		lua_pushvalue(lua_state_, -3);                        // S: table key value globals_table key
		lua_gettable(lua_state_, -2);  // S: table key value globals_table value_in_globals
		if (lua_compare(lua_state_, -1, -3, LUA_OPEQ) != 0) {
			lua_pop(lua_state_, 3);  // S: table key
			continue;
		}  // Make this a global value
		lua_pop(lua_state_, 1);         // S: table key value globals_table
		lua_pushvalue(lua_state_, -3);  // S: table key value globals_table key
		lua_pushvalue(lua_state_, -3);  // S: table key value globals_table key value
		lua_settable(lua_state_, -3);   // S: table key value globals_table
		lua_pop(lua_state_, 2);         // S: table key
	}

	lua_pop(lua_state_, 1);  // pop the table returned by unpersist_object

	// Clean out the garbage before returning.
	lua_gc(lua_state_, LUA_GCCOLLECT, 0);
}

void LuaGameInterface::read_textdomain_stack(FileRead& fr) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	LuaGlobals::read_textdomain_stack(fr, lua_state_);
}
void LuaGameInterface::write_textdomain_stack(FileWrite& fw) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	LuaGlobals::write_textdomain_stack(fw, lua_state_);
}

uint32_t LuaGameInterface::write_global_env(FileWrite& fw, Widelands::MapObjectSaver& mos) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	// Clean out the garbage before writing.
	lua_gc(lua_state_, LUA_GCCOLLECT, 0);

	// Empty table + object to persist on the stack Stack
	lua_newtable(lua_state_);
	lua_rawgeti(lua_state_, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);

	uint32_t nwritten = persist_object(lua_state_, fw, mos);

	// Garbage collect once more, so we do not return unnecessary stuff.
	lua_gc(lua_state_, LUA_GCCOLLECT, 0);

	return nwritten;
}

std::unique_ptr<LuaTable> LuaGameInterface::get_hook(const std::string& name) {
	// TODO(tothxa): kObjects before kLua is needed because of Panel::do_run() and plugin actions
	MutexLock o(MutexLock::ID::kObjects);
	MutexLock m(MutexLock::ID::kLua);

	lua_getglobal(lua_state_, "hooks");
	if (lua_isnil(lua_state_, -1)) {
		lua_pop(lua_state_, 1);
		return std::unique_ptr<LuaTable>();
	}

	lua_getfield(lua_state_, -1, name.c_str());
	if (lua_isnil(lua_state_, -1)) {
		lua_pop(lua_state_, 2);
		return std::unique_ptr<LuaTable>();
	}
	lua_remove(lua_state_, -2);

	std::unique_ptr<LuaTable> return_value(new LuaTable(lua_state_));
	lua_pop(lua_state_, 1);
	return return_value;
}

std::unique_ptr<LuaTable> LuaGameInterface::run_script(const std::string& script) {
	return run_script_maybe_from_map(lua_state_, script);
}