forked from TupoyeMenu/YimLuaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_module.cpp
More file actions
448 lines (382 loc) · 13.6 KB
/
lua_module.cpp
File metadata and controls
448 lines (382 loc) · 13.6 KB
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "lua_module.hpp"
#include "bindings/command.hpp"
#include "bindings/entities.hpp"
#include "bindings/event.hpp"
#include "bindings/global_table.hpp"
#include "bindings/globals.hpp"
#include "bindings/gui.hpp"
#include "bindings/imgui.hpp"
#include "bindings/locals.hpp"
#include "bindings/log.hpp"
#include "bindings/memory.hpp"
#include "bindings/native.hpp"
#include "bindings/network.hpp"
#include "bindings/script.hpp"
#include "bindings/scr_function.hpp"
#include "bindings/scr_patch.hpp"
#include "bindings/self.hpp"
#include "bindings/stats.hpp"
#include "bindings/tunables.hpp"
#include "bindings/vector.hpp"
#include "bindings/vehicles.hpp"
#include "bindings/weapons.hpp"
#include "file_manager.hpp"
#include "lua/lua_manager.hpp"
#include "script_mgr.hpp"
namespace big
{
// https://sol2.readthedocs.io/en/latest/exceptions.html
int exception_handler(lua_State* L, sol::optional<const std::exception&> maybe_exception, sol::string_view description)
{
// L is the lua state, which you can wrap in a state_view if necessary
// maybe_exception will contain exception, if it exists
// description will either be the what() of the exception or a description saying that we hit the general-case catch(...)
if (maybe_exception)
{
const std::exception& ex = *maybe_exception;
LOG(FATAL) << ex.what();
}
else
{
LOG(FATAL) << description;
}
Logger::FlushQueue();
// you must push 1 element onto the stack to be
// transported through as the error object in Lua
// note that Lua -- and 99.5% of all Lua users and libraries -- expects a string
// so we push a single string (in our case, the description of the error)
return sol::stack::push(L, description);
}
inline void panic_handler(sol::optional<std::string> maybe_msg)
{
LOG(FATAL) << "Lua is in a panic state and will now abort() the application";
if (maybe_msg)
{
const std::string& msg = maybe_msg.value();
LOG(FATAL) << "error message: " << msg;
}
Logger::FlushQueue();
// When this function exits, Lua will exhibit default behavior and abort()
}
static int traceback_error_handler(lua_State* L)
{
std::string msg = "An unknown error has triggered the error handler";
sol::optional<sol::string_view> maybetopmsg = sol::stack::unqualified_check_get<sol::string_view>(L, 1, &sol::no_panic);
if (maybetopmsg)
{
const sol::string_view& topmsg = maybetopmsg.value();
msg.assign(topmsg.data(), topmsg.size());
}
luaL_traceback(L, L, msg.c_str(), 1);
sol::optional<sol::string_view> maybetraceback = sol::stack::unqualified_check_get<sol::string_view>(L, -1, &sol::no_panic);
if (maybetraceback)
{
const sol::string_view& traceback = maybetraceback.value();
msg.assign(traceback.data(), traceback.size());
}
LOG(FATAL) << msg;
return sol::stack::push(L, msg);
}
lua_module::lua_module(const std::filesystem::path& module_path, folder& scripts_folder, bool disabled) :
m_state(),
m_module_path(module_path),
m_module_name(module_path.filename().string()),
m_module_id(rage::joaat(m_module_name)),
m_disabled(disabled)
{
if (!m_disabled)
{
// clang-format off
m_state.open_libraries(
sol::lib::base,
sol::lib::package,
sol::lib::coroutine,
sol::lib::string,
sol::lib::os,
sol::lib::math,
sol::lib::table,
sol::lib::bit32,
sol::lib::io,
#if SOL_IS_ON(SOL_USE_LUAJIT)
sol::lib::ffi,
sol::lib::jit,
#endif
sol::lib::utf8
);
// clang-format on
init_lua_api(scripts_folder);
m_state["!module_name"] = m_module_name;
m_state["!this"] = this;
m_state.set_exception_handler(exception_handler);
m_state.set_panic(sol::c_call<decltype(&panic_handler), &panic_handler>);
lua_CFunction traceback_function = sol::c_call<decltype(&traceback_error_handler), &traceback_error_handler>;
sol::protected_function::set_default_handler(sol::object(m_state.lua_state(), sol::in_place, traceback_function));
m_last_write_time = std::filesystem::last_write_time(m_module_path);
}
}
lua_module::~lua_module()
{
{
std::lock_guard guard(m_registered_scripts_mutex);
m_registered_scripts.clear();
m_registered_script_patches.clear();
}
for (auto memory : m_allocated_memory)
delete[] memory;
}
rage::joaat_t lua_module::module_id() const
{
return m_module_id;
}
const std::string& lua_module::module_name() const
{
return m_module_name;
}
const std::filesystem::path& lua_module::module_path() const
{
return m_module_path;
}
const std::chrono::time_point<std::chrono::file_clock> lua_module::last_write_time() const
{
return m_last_write_time;
}
const bool lua_module::is_disabled() const
{
return m_disabled;
}
const std::filesystem::path lua_module::get_config_folder() const
{
const auto config_path = g_lua_manager->get_scripts_config_folder().get_path() / m_module_name;
if (!std::filesystem::exists(config_path))
std::filesystem::create_directory(config_path);
return config_path;
}
void lua_module::set_folder_for_lua_require(folder& scripts_folder)
{
std::string scripts_search_path = scripts_folder.get_path().string() + "/?.lua;";
for (const auto& entry : std::filesystem::recursive_directory_iterator(scripts_folder.get_path(), std::filesystem::directory_options::skip_permission_denied))
{
if (!entry.is_directory())
continue;
if(std::filesystem::relative(entry, scripts_folder.get_path()).wstring().contains(L"disabled"))
continue;
scripts_search_path += entry.path().string() + "/?.lua;";
}
// Remove final ';'
scripts_search_path.pop_back();
m_state["package"]["path"] = scripts_search_path;
}
static std::optional<std::filesystem::path> make_absolute(const std::filesystem::path& root, const std::filesystem::path& user_path)
{
if (user_path.is_absolute())
return std::nullopt;
auto canon_root = std::filesystem::weakly_canonical(root);
auto final_path = std::filesystem::weakly_canonical(canon_root / user_path);
auto [root_end, nothing] = std::mismatch(canon_root.begin(), canon_root.end(), final_path.begin());
if (root_end != canon_root.end())
return std::nullopt;
return final_path;
};
void lua_module::sandbox_lua_os_library()
{
const auto& os = m_state["os"];
sol::table sandbox_os(m_state, sol::create);
sandbox_os["clock"] = os["clock"];
sandbox_os["date"] = os["date"];
sandbox_os["difftime"] = os["difftime"];
sandbox_os["time"] = os["time"];
// Lua API: Function
// Table: os
// Name: rename
// Param: oldname: string
// Param: newname: string
// Returns: boolean, string?: True if the file was successfully renamed, false and an error message otherwise.
sandbox_os["rename"] = [this](const std::string& oldname, const std::string& newname) -> sol::object {
const auto old_path = make_absolute(get_config_folder(), oldname);
const auto new_path = make_absolute(get_config_folder(), newname);
if (!old_path)
{
LOG(WARNING) << "os.rename is restricted to the script's config folder, and the filename provided (" << oldname << ") seems to be outside of it.";
return sol::make_object(m_state, std::make_tuple(false, "File not found."));
}
if (!new_path)
{
LOG(WARNING) << "os.rename is restricted to the script's config folder, and the filename provided (" << newname << ") seems to be outside of it.";
return sol::make_object(m_state, std::make_tuple(false, "New file name is invalid."));
}
try
{
std::filesystem::rename(old_path.value(), new_path.value());
return sol::make_object(m_state, true);
}
catch (const std::exception& e)
{
return sol::make_object(m_state, std::make_tuple(false, e.what()));
}
};
m_state["os"] = sandbox_os;
}
void lua_module::sandbox_lua_io_library()
{
auto io = m_state["io"];
sol::table sandbox_io(m_state, sol::create);
m_io_open = io["open"];
// Lua API: Table
// Name: io
// Table for file manipulation. Modified for security purposes.
sandbox_io["open"] = [this](const std::string& filename, const std::string& mode) {
const auto scripts_config_sub_path = make_absolute(get_config_folder(), filename);
if (!scripts_config_sub_path)
{
LOG(WARNING) << "io.open is restricted to the scripts_config folder, and the filename provided (" << filename << ") is outside of it.";
return sol::reference(sol::lua_nil);
}
const auto res = m_io_open(scripts_config_sub_path.value().u8string().c_str(), mode).get<sol::reference>();
if (res.get_type() == sol::type::lua_nil)
{
LOG(WARNING) << "Couldn't io.open a file called " << filename << " mode (" << mode << "). Note that io.open is restricted to the scripts_config folder. If you want to check if a file exists, use io.exists";
}
return res;
};
// Lua API: Function
// Table: io
// Name: exists
// Param: filename: string
// Returns: boolean: exists: True if the passed file path exists
sandbox_io["exists"] = [this](const std::string& filename) -> bool {
const auto scripts_config_sub_path = make_absolute(get_config_folder(), filename);
if (!scripts_config_sub_path)
{
LOG(WARNING) << "io.open is restricted to the scripts_config folder, and the filename provided (" << filename << ") is outside of it.";
return false;
}
return std::filesystem::exists(*scripts_config_sub_path);
};
m_state["io"] = sandbox_io;
}
template<size_t N>
static constexpr auto not_supported_lua_function(const char (&function_name)[N])
{
return [function_name](sol::this_state state, sol::variadic_args args) {
big::lua_module* module = sol::state_view(state)["!this"];
LOG(FATAL) << module->module_name() << " tried calling a currently not supported lua function: " << function_name;
Logger::FlushQueue();
};
}
void lua_module::sandbox_lua_loads(folder& scripts_folder)
{
// That's from lua base lib, luaB
m_state["load"] = not_supported_lua_function("load");
m_state["loadstring"] = not_supported_lua_function("loadstring");
m_state["loadfile"] = not_supported_lua_function("loadfile");
m_state["dofile"] = not_supported_lua_function("dofile");
// That's from lua package lib.
// We only allow dependencies between .lua files, no DLLs.
m_state["package"]["loadlib"] = not_supported_lua_function("package.loadlib");
m_state["package"]["cpath"] = "";
// 1 2 3 4
// {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
#if SOL_LUA_VERSION_I_ < 502
m_state["package"]["loaders"][3] = not_supported_lua_function("package.loaders C");
m_state["package"]["loaders"][4] = not_supported_lua_function("package.loaders Croot");
#else
m_state["package"]["searchers"][3] = not_supported_lua_function("package.searcher C");
m_state["package"]["searchers"][4] = not_supported_lua_function("package.searcher Croot");
#endif
set_folder_for_lua_require(scripts_folder);
}
void lua_module::init_lua_api(folder& scripts_folder)
{
// https://blog.rubenwardy.com/2020/07/26/sol3-script-sandbox/
// https://www.lua.org/manual/5.4/manual.html#pdf-require
sandbox_lua_os_library();
sandbox_lua_io_library();
sandbox_lua_loads(scripts_folder);
lua::log::bind(m_state);
lua::globals::bind(m_state);
lua::script::bind(m_state);
lua::scr_function::bind(m_state);
lua::scr_patch::bind(m_state);
lua::native::bind(m_state);
lua::memory::bind(m_state);
lua::gui::bind(m_state);
lua::network::bind(m_state);
lua::command::bind(m_state);
lua::tunables::bind(m_state);
lua::locals::bind(m_state);
lua::event::bind(m_state);
lua::vector::bind(m_state);
lua::global_table::bind(m_state);
lua::imgui::bind(m_state, m_state.globals());
lua::entities::bind(m_state);
lua::self::bind(m_state);
lua::stats::bind(m_state);
lua::weapons::bind(m_state);
lua::vehicles::bind(m_state);
}
void lua_module::load_and_call_script()
{
auto result = m_state.safe_script_file(m_module_path.string(), &sol::script_pass_on_error, sol::load_mode::text);
if (!result.valid())
{
LOG(FATAL) << m_module_name << " failed to load: " << result.get<sol::error>().what();
Logger::FlushQueue();
}
else
{
LOG(INFO) << "Loaded " << m_module_name;
}
}
void lua_module::tick_scripts()
{
std::lock_guard guard(m_registered_scripts_mutex);
const auto script_count = m_registered_scripts.size();
for (size_t i = 0; i < script_count; i++)
{
const auto script = m_registered_scripts[i].get();
if (script->is_enabled())
{
script->tick();
}
}
}
void lua_module::cleanup_done_scripts()
{
std::lock_guard guard(m_registered_scripts_mutex);
std::erase_if(m_registered_scripts, [](auto& script) {
return script->is_done();
});
}
sol::object lua_module::to_lua(const lua::memory::runtime_func_t::parameters_t* params, const uint8_t i, const std::vector<lua::memory::type_info_t>& param_types)
{
if (param_types[i] == lua::memory::type_info_t::none_)
{
return sol::nil;
}
else if (param_types[i] == lua::memory::type_info_t::ptr_)
{
return sol::make_object(m_state, lua::memory::pointer(params->get<uintptr_t>(i)));
}
else
{
return sol::make_object(m_state, lua::memory::value_wrapper_t(params->get_arg_ptr(i), param_types[i]));
}
return sol::nil;
}
sol::object lua_module::to_lua(lua::memory::runtime_func_t::return_value_t* return_value, const lua::memory::type_info_t return_value_type)
{
if (return_value_type == lua::memory::type_info_t::none_)
{
return sol::nil;
}
else if (return_value_type == lua::memory::type_info_t::ptr_)
{
return sol::make_object(m_state, lua::memory::pointer((uintptr_t)return_value->get()));
}
else
{
return sol::make_object(m_state, lua::memory::value_wrapper_t((char*)return_value->get(), return_value_type));
}
return sol::nil;
}
}