Improve modules and sandbox system

This commit is contained in:
Eduardo Bart
2012-07-23 21:22:38 -03:00
parent 61d64c7417
commit 1c3e630237
12 changed files with 238 additions and 145 deletions

View File

@@ -30,8 +30,7 @@
Module::Module(const std::string& name)
{
m_name = name;
g_lua.newEnvironment();
m_sandboxEnv = g_lua.ref();
m_sandboxEnv = g_lua.newSandboxEnv();
}
bool Module::load()
@@ -49,12 +48,11 @@ bool Module::load()
stdext::throw_exception(stdext::format("dependency '%s' has failed to load", m_name, depName));
}
if(m_sandboxed)
g_lua.setGlobalEnvironment(m_sandboxEnv);
for(const std::string& script : m_scripts) {
g_lua.loadScript(script);
if(m_sandboxed) {
g_lua.getRef(m_sandboxEnv);
g_lua.setEnv();
}
g_lua.safeCall(0, 0);
}
@@ -69,8 +67,13 @@ bool Module::load()
g_lua.safeCall(0, 0);
}
if(m_sandboxed)
g_lua.resetGlobalEnvironment();
g_logger.debug(stdext::format("Loaded module '%s'", m_name));
} catch(stdext::exception& e) {
if(m_sandboxed)
g_lua.resetGlobalEnvironment();
g_logger.error(stdext::format("Unable to load module '%s': %s", m_name, e.what()));
return false;
}
@@ -93,20 +96,29 @@ void Module::unload()
{
if(m_loaded) {
try {
if(m_sandboxed)
g_lua.setGlobalEnvironment(m_sandboxEnv);
const std::string& onUnloadBuffer = std::get<0>(m_onUnloadFunc);
const std::string& onUnloadSource = std::get<1>(m_onUnloadFunc);
if(!onUnloadBuffer.empty()) {
g_lua.loadBuffer(onUnloadBuffer, onUnloadSource);
if(m_sandboxed) {
g_lua.getRef(m_sandboxEnv);
g_lua.setEnv();
}
g_lua.safeCall(0, 0);
}
if(m_sandboxed)
g_lua.resetGlobalEnvironment();
} catch(stdext::exception& e) {
if(m_sandboxed)
g_lua.resetGlobalEnvironment();
g_logger.error(stdext::format("Unable to unload module '%s': %s", m_name, e.what()));
}
// clear all env references
g_lua.getRef(m_sandboxEnv);
g_lua.clearTable();
g_lua.pop();
m_loaded = false;
//g_logger.info(stdext::format("Unloaded module '%s'", m_name));
g_modules.updateModuleLoadOrder(asModule());
@@ -135,6 +147,12 @@ bool Module::hasDependency(const std::string& name)
return false;
}
int Module::getSandbox(LuaInterface* lua)
{
lua->getRef(m_sandboxEnv);
return 1;
}
void Module::discover(const OTMLNodePtr& moduleNode)
{
const static std::string none = "none";

View File

@@ -43,7 +43,9 @@ public:
bool isLoaded() { return m_loaded; }
bool isReloadable() { return m_reloadable; }
bool isDependent();
bool isSandboxed() { return m_sandboxed; }
bool hasDependency(const std::string& name);
int getSandbox(LuaInterface *lua);
std::string getDescription() { return m_description; }
std::string getName() { return m_name; }

View File

@@ -151,7 +151,7 @@ void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
if(!file) {
out.clear(std::ios::failbit);
stdext::throw_exception(stdext::format("failed to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError()));
stdext::throw_exception(stdext::format("unable to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError()));
} else {
int fileSize = PHYSFS_fileLength(file);
if(fileSize > 0) {
@@ -167,7 +167,7 @@ void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
std::ifstream fin(fileName);
if(!fin) {
out.clear(std::ios::failbit);
stdext::throw_exception(stdext::format("failed to load file '%s': %s", fileName.c_str(), PHYSFS_getLastError()));
stdext::throw_exception(stdext::format("unable to file '%s': %s", fileName.c_str(), PHYSFS_getLastError()));
} else {
out << fin.rdbuf();
}