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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/**
* @file llluamanager.cpp
* @brief classes and functions for interfacing with LUA.
*
* $LicenseInfo:firstyear=2023&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2023, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*
*/
#include "llviewerprecompiledheaders.h"
#include "llluamanager.h"
#include "llagent.h"
#include "llappearancemgr.h"
#include "llcallbacklist.h"
#include "llfloaterreg.h"
#include "llfloaterimnearbychat.h"
#include "llfloatersidepanelcontainer.h"
#include "llnotificationsutil.h"
#include "llvoavatarself.h"
#include "llviewermenu.h"
#include "llviewermenufile.h"
#include "llviewerwindow.h"
#include "lluilistener.h"
#include <boost/algorithm/string/replace.hpp>
extern "C"
{
#include "lua/lua.h"
#include "lua/lauxlib.h"
#include "lua/lualib.h"
}
#if LL_WINDOWS
#pragma comment(lib, "liblua54.a")
#endif
// FIXME extremely hacky way to get to the UI Listener framework. There's almost certainly a cleaner way.
extern LLUIListener sUIListener;
int lua_printWarning(lua_State *L)
{
std::string msg(lua_tostring(L, 1));
LL_WARNS() << msg << LL_ENDL;
return 1;
}
bool checkLua(lua_State *L, int r, std::string &error_msg)
{
if (r != LUA_OK)
{
error_msg = lua_tostring(L, -1);
LL_WARNS() << error_msg << LL_ENDL;
return false;
}
return true;
}
int lua_avatar_sit(lua_State *L)
{
gAgent.sitDown();
return 1;
}
int lua_avatar_stand(lua_State *L)
{
gAgent.standUp();
return 1;
}
int lua_nearby_chat_send(lua_State *L)
{
std::string msg(lua_tostring(L, 1));
LLFloaterIMNearbyChat *nearby_chat = LLFloaterReg::findTypedInstance<LLFloaterIMNearbyChat>("nearby_chat");
nearby_chat->sendChatFromViewer(msg, CHAT_TYPE_NORMAL, gSavedSettings.getBOOL("PlayChatAnim"));
return 1;
}
int lua_wear_by_name(lua_State *L)
{
std::string folder_name(lua_tostring(L, 1));
LLAppearanceMgr::instance().wearOutfitByName(folder_name);
return 1;
}
int lua_open_floater(lua_State *L)
{
std::string floater_name(lua_tostring(L, 1));
LLSD key;
if (floater_name == "profile")
{
key["id"] = gAgentID;
}
LLFloaterReg::showInstance(floater_name, key);
return 1;
}
int lua_close_floater(lua_State *L)
{
std::string floater_name(lua_tostring(L, 1));
LLSD key;
if (floater_name == "profile")
{
key["id"] = gAgentID;
}
LLFloaterReg::hideInstance(floater_name, key);
return 1;
}
int lua_close_all_floaters(lua_State *L)
{
close_all_windows();
return 1;
}
int lua_click_child(lua_State *L)
{
std::string parent_name(lua_tostring(L, 1));
std::string child_name(lua_tostring(L, 2));
LLFloater *floater = LLFloaterReg::findInstance(parent_name);
LLUICtrl *child = floater->getChild<LLUICtrl>(child_name, true);
child->onCommit();
return 1;
}
int lua_snapshot_to_file(lua_State *L)
{
std::string filename(lua_tostring(L, 1));
//don't take snapshot from coroutine
doOnIdleOneTime([filename]()
{
gViewerWindow->saveSnapshot(filename,
gViewerWindow->getWindowWidthRaw(),
gViewerWindow->getWindowHeightRaw(),
gSavedSettings.getBOOL("RenderUIInSnapshot"),
gSavedSettings.getBOOL("RenderHUDInSnapshot"),
FALSE,
LLSnapshotModel::SNAPSHOT_TYPE_COLOR,
LLSnapshotModel::SNAPSHOT_FORMAT_PNG);
});
return 1;
}
int lua_open_wearing_tab(lua_State *L)
{
LLFloaterSidePanelContainer::showPanel("appearance", LLSD().with("type", "now_wearing"));
return 1;
}
int lua_set_debug_setting_bool(lua_State *L)
{
std::string setting_name(lua_tostring(L, 1));
bool value(lua_toboolean(L, 2));
gSavedSettings.setBOOL(setting_name, value);
return 1;
}
int lua_get_avatar_name(lua_State *L)
{
std::string name = gAgentAvatarp->getFullname();
lua_pushstring(L, name.c_str());
return 1;
}
int lua_is_avatar_flying(lua_State *L)
{
lua_pushboolean(L, gAgent.getFlying());
return 1;
}
int lua_env_setting_event(lua_State *L)
{
handle_env_setting_event(lua_tostring(L, 1));
return 1;
}
void handle_notification_dialog(const LLSD ¬ification, const LLSD &response, lua_State *L, std::string response_cb)
{
if (!response_cb.empty())
{
S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
lua_pushinteger(L, option);
lua_setglobal(L, response_cb.c_str());
}
}
int lua_show_notification(lua_State *L)
{
std::string notification(lua_tostring(L, 1));
if (lua_type(L, 2) == LUA_TTABLE)
{
LLSD args;
// push first key
lua_pushnil(L);
while (lua_next(L, 2) != 0)
{
// right now -2 is key, -1 is value
lua_rawgeti(L, -1, 1);
lua_rawgeti(L, -2, 2);
std::string key = lua_tostring(L, -2);
std::string value = lua_tostring(L, -1);
args[key] = value;
lua_pop(L, 3);
}
std::string response_cb;
if (lua_type(L, 3) == LUA_TSTRING)
{
response_cb = lua_tostring(L, 3);
}
LLNotificationsUtil::add(notification, args, LLSD(), boost::bind(handle_notification_dialog, _1, _2, L, response_cb));
}
else if (lua_type(L, 2) == LUA_TSTRING)
{
std::string response_cb = lua_tostring(L, 2);
LLNotificationsUtil::add(notification, LLSD(), LLSD(), boost::bind(handle_notification_dialog, _1, _2, L, response_cb));
}
else
{
LLNotificationsUtil::add(notification);
}
return 1;
}
int lua_run_ui_command(lua_State *L)
{
int top = lua_gettop(L);
std::string func_name;
if (top >= 1)
{
func_name = lua_tostring(L,1);
}
std::string parameter;
if (top >= 2)
{
parameter = lua_tostring(L,2);
}
LL_WARNS("LUA") << "running ui func " << func_name << " parameter " << parameter << LL_ENDL;
LLSD event;
event["function"] = func_name;
if (!parameter.empty())
{
event["parameter"] = parameter;
}
sUIListener.call(event);
return 1;
}
void initLUA(lua_State *L)
{
lua_register(L, "print_warning", lua_printWarning);
lua_register(L, "avatar_sit", lua_avatar_sit);
lua_register(L, "avatar_stand", lua_avatar_stand);
lua_register(L, "nearby_chat_send", lua_nearby_chat_send);
lua_register(L, "wear_by_name", lua_wear_by_name);
lua_register(L, "open_floater", lua_open_floater);
lua_register(L, "close_floater", lua_close_floater);
lua_register(L, "close_all_floaters", lua_close_all_floaters);
lua_register(L, "click_child", lua_click_child);
lua_register(L, "open_wearing_tab", lua_open_wearing_tab);
lua_register(L, "snapshot_to_file", lua_snapshot_to_file);
lua_register(L, "get_avatar_name", lua_get_avatar_name);
lua_register(L, "is_avatar_flying", lua_is_avatar_flying);
lua_register(L, "env_setting_event", lua_env_setting_event);
lua_register(L, "set_debug_setting_bool", lua_set_debug_setting_bool);
lua_register(L, "show_notification", lua_show_notification);
lua_register(L, "run_ui_command", lua_run_ui_command);
}
void LLLUAmanager::runScriptFile(const std::string &filename, script_finished_fn cb)
{
LLCoros::instance().launch("LUAScriptFileCoro", [filename, cb]()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
initLUA(L);
auto LUA_sleep_func = [](lua_State *L)
{
F32 seconds = lua_tonumber(L, -1);
llcoro::suspendUntilTimeout(seconds);
return 0;
};
lua_register(L, "sleep", LUA_sleep_func);
std::string lua_error;
if (checkLua(L, luaL_dofile(L, filename.c_str()), lua_error))
{
lua_getglobal(L, "call_once_func");
if (lua_isfunction(L, -1))
{
if (checkLua(L, lua_pcall(L, 0, 0, 0), lua_error)) {}
}
}
lua_close(L);
if (cb)
{
cb(lua_error);
}
});
}
void LLLUAmanager::runScriptLine(const std::string &cmd, script_finished_fn cb)
{
LLCoros::instance().launch("LUAScriptFileCoro", [cmd, cb]()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
initLUA(L);
int r = luaL_dostring(L, cmd.c_str());
std::string lua_error;
if (r != LUA_OK)
{
lua_error = lua_tostring(L, -1);
}
lua_close(L);
if (cb)
{
cb(lua_error);
}
});
}
void LLLUAmanager::runScriptOnLogin()
{
std::string filename = gSavedSettings.getString("AutorunLuaScriptName");
if (filename.empty())
{
LL_INFOS() << "Script name wasn't set." << LL_ENDL;
return;
}
filename = gDirUtilp->getExpandedFilename(LL_PATH_USER_SETTINGS, filename);
if (!gDirUtilp->fileExists(filename))
{
LL_INFOS() << filename << " was not found." << LL_ENDL;
return;
}
runScriptFile(filename);
}
|