bettola/common/src/lua_processor.cpp
Ritchie Cunningham 267d2477de [Add] Implemented Copy-on-Write and scriptable rm.
OK, this commit finally implements the Copy-on-Write architecure I spoke
about in previous commits.. It also refactors command execution to be
safer and more extensible.

To enable CoW and centralise state-changing, command scripts no longer
modify the VFS directly. Instead, they return a table describing their
intended action '{ action = "rm", target = "file.txt" }'. The C++
CommandProcessor is then responsible for interpreting this and executing
it safely.
2025-09-21 23:52:36 +01:00

37 lines
1.2 KiB
C++

#include "lua_processor.h"
#include <sol/forward.hpp>
#include "vfs.h"
LuaProcessor::LuaProcessor(void) {
_lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::io);
/* Expose vfs_node struct members to Lua. */
_lua.new_usertype<vfs_node>("vfs_node",
"name", &vfs_node::name,
"type", &vfs_node::type,
"children", &vfs_node::children,
"read_only", &vfs_node::read_only); }
LuaProcessor::~LuaProcessor(void) {}
sol::object LuaProcessor::execute(const std::string& script, vfs_node* current_dir,
const std::vector<std::string>& args) {
try {
/* Pass C++ objects/points into the Lua env. */
_lua["current_dir"] = current_dir;
/* Create and populate the 'arg' table for the script. */
sol::table arg_table = _lua.create_table();
for(size_t i = 0; i < args.size(); ++i) {
arg_table[i+1] = args[i]; /* Lua arrays 1-indexed. */
}
_lua["arg"] = arg_table;
return _lua.script(script);
} catch(const sol::error& e) {
/* Return the error message as a string in a sol::object. */
return sol::make_object(_lua, e.what());
}
}