1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-05 19:31:04 +00:00

actually load the code, and make it not crash

This commit is contained in:
Desour 2025-02-21 13:31:49 +01:00
parent e4a3b631cf
commit 2a75ffa38a
14 changed files with 82 additions and 37 deletions

View file

@ -145,7 +145,8 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
// Finally, put the table into the global environment:
lua_setglobal(m_luastack, "core");
if (m_type == ScriptingType::Client)
if (m_type == ScriptingType::Client
|| m_type == ScriptingType::SSCSM)
lua_pushstring(m_luastack, "/");
else
lua_pushstring(m_luastack, DIR_DELIM);
@ -267,17 +268,18 @@ void ScriptApiBase::loadScript(const std::string &script_path)
}
#if CHECK_CLIENT_BUILD()
void ScriptApiBase::loadModFromMemory(const std::string &mod_name)
void ScriptApiBase::loadModFromMemory(const std::string &mod_name, std::string init_path)
{
ModNameStorer mod_name_storer(getStack(), mod_name);
sanity_check(m_type == ScriptingType::Client
|| m_type == ScriptingType::SSCSM);
const std::string init_filename = mod_name + ":init.lua";
const std::string chunk_name = "@" + init_filename;
if (init_path.empty())
init_path = mod_name + ":init.lua";
const std::string chunk_name = "@" + init_path;
const std::string *contents = getModVFS()->getModFile(init_filename);
const std::string *contents = getModVFS()->getModFile(init_path);
if (!contents)
throw ModError("Mod \"" + mod_name + "\" lacks init.lua");

View file

@ -80,7 +80,7 @@ public:
void loadScript(const std::string &script_path);
#if CHECK_CLIENT_BUILD()
void loadModFromMemory(const std::string &mod_name);
void loadModFromMemory(const std::string &mod_name, std::string init_path = "");
#endif
void runCallbacksRaw(int nargs,

View file

@ -385,7 +385,7 @@ void ScriptApiSecurity::initializeSecuritySSCSM()
"assert",
"core",
"collectgarbage",
"DIR_DELIM", //TODO: useless?
"DIR_DELIM",
"error",
"getfenv",
"ipairs",
@ -419,6 +419,10 @@ void ScriptApiSecurity::initializeSecuritySSCSM()
"difftime",
"time"
};
static const char *debug_whitelist[] = {
"getinfo", // used by builtin and unset before mods load //TODO
"traceback" //TODO
};
#if USE_LUAJIT
static const char *jit_whitelist[] = {
@ -465,6 +469,14 @@ void ScriptApiSecurity::initializeSecuritySSCSM()
lua_pop(L, 1); // Pop old OS
// Copy safe debug functions //TODO
lua_getglobal(L, "debug");
lua_newtable(L);
copy_safe(L, debug_whitelist, sizeof(debug_whitelist));
lua_setfield(L, -3, "debug");
lua_pop(L, 1); // Pop old debug
#if USE_LUAJIT
// Copy safe jit functions, if they exist
lua_getglobal(L, "jit");

View file

@ -7,19 +7,12 @@
#include "s_internal.h"
#include "script/sscsm/sscsm_environment.h"
void ScriptApiSSCSM::load_mods(const std::vector<std::string> &init_paths)
void ScriptApiSSCSM::load_mods(const std::vector<std::pair<std::string, std::string>> &mods)
{
//TODO
SSCSMEnvironment *env = getSSCSMEnv();
actionstream << "load_mods:\n";
for (const auto &p : init_paths) {
actionstream << " " << p << ":\n";
auto f = env->readVFSFile(p);
if (!f.has_value()) {
throw ModError("load_mods(): File doesn't exist: " + p);
}
actionstream << *f << "\n";
infostream << "Loading SSCSMs:" << std::endl;
for (const auto &m : mods) {
infostream << "Loading SSCSM " << m.first << std::endl;
loadModFromMemory(m.first, m.second);
}
}

View file

@ -9,7 +9,7 @@
class ScriptApiSSCSM : virtual public ScriptApiBase
{
public:
void load_mods(const std::vector<std::string> &init_paths);
void load_mods(const std::vector<std::pair<std::string, std::string>> &mods);
void environment_step(float dtime);
};