| 38 | } |
| 39 | |
| 40 | static void load_lua_script(const char *script_name, const char *lua_script_data, size_t lua_script_data_size, bool sandbox, bool global) noexcept { |
| 41 | // Create a new state for this script |
| 42 | auto *state = luaL_newstate(); |
| 43 | |
| 44 | luaL_openlibs(state); |
| 45 | |
| 46 | // Update Lua path |
| 47 | auto new_lua_path = get_chimera().get_path() / "lua" / "modules"; |
| 48 | lua_getglobal(state, "package"); |
| 49 | lua_pushstring(state, (new_lua_path.string() + "\\?.lua").c_str()); |
| 50 | lua_setfield(state, -2, "path"); |
| 51 | lua_pop(state, 1); |
| 52 | |
| 53 | if(sandbox) { |
| 54 | const char *sandbox_script = "io = nil\ |
| 55 | dofile = nil\ |
| 56 | getfenv = nil\ |
| 57 | load = nil\ |
| 58 | loadfile = nil\ |
| 59 | loadstring = nil\ |
| 60 | require = nil\ |
| 61 | os.execute = nil\ |
| 62 | os.exit = nil\ |
| 63 | os.remove = nil\ |
| 64 | os.rename = nil\ |
| 65 | os.tmpname = nil"; |
| 66 | |
| 67 | auto load_result = luaL_loadbuffer(state, sandbox_script, std::strlen(sandbox_script), script_name); |
| 68 | |
| 69 | if(load_result != LUA_OK || lua_pcall(state, 0, 0, 0) != LUA_OK) { |
| 70 | print_error(state); |
| 71 | lua_close(state); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Set up functions |
| 77 | set_fs_functions(state); |
| 78 | set_io_functions(state); |
| 79 | set_game_functions(state); |
| 80 | |
| 81 | // Refresh variables |
| 82 | refresh_variables(state); |
| 83 | |
| 84 | // Set up script globals |
| 85 | lua_pushstring(state, script_name); |
| 86 | lua_setglobal(state, "script_name"); |
| 87 | |
| 88 | lua_pushstring(state, global ? "global" : "map"); |
| 89 | lua_setglobal(state, "script_type"); |
| 90 | |
| 91 | lua_pushboolean(state, sandbox); |
| 92 | lua_setglobal(state, "sandboxed"); |
| 93 | |
| 94 | lua_pushinteger(state, CHIMERA_GIT_COMMIT_COUNT); |
| 95 | lua_setglobal(state, "build"); |
| 96 | |
| 97 | lua_pushstring(state, CHIMERA_VERSION_STRING); |
no test coverage detected