[Refactor] Simulate executable files in VFS.

This commit is contained in:
Ritchie Cunningham 2025-10-21 20:51:49 +01:00
parent 651f7c415e
commit 7b58ecc4d5
5 changed files with 24 additions and 23 deletions

View File

@ -74,7 +74,7 @@ bool DatabaseManager::create_player(const std::string& username, const std::stri
/* Copy scripts from template into new machine's /bin */
vfs_node* template_bin = vfs_template->children["bin"];
for(auto const& [name, node] : template_bin->children) {
_vfs_repository->create_node(machine_id, &bin_id, name, FILE_NODE, node->content);
_vfs_repository->create_node(machine_id, &bin_id, name, node->type, node->content);
}
/* Add default SSH service. */

View File

@ -39,11 +39,12 @@ MachineManager::MachineManager(DatabaseManager* db_manager) :
std::ifstream t(entry.path());
std::stringstream buffer;
buffer << t.rdbuf();
std::string filename = entry.path().filename().string();
vfs_node* script_node = new_node(filename, FILE_NODE, bin);
std::string filename_with_ext = entry.path().filename().string();
std::string filename = filename_with_ext.substr(0, filename_with_ext.find_last_of('.'));
vfs_node* script_node = new_node(filename, EXEC_NODE, bin);
script_node->content = buffer.str();
bin->children[filename] = script_node;
fprintf(stderr, "Loaded script: /bin/%s\n", filename.c_str());
fprintf(stderr, "Loaded executable: /bin/%s\n", filename.c_str());
}
}
}

View File

@ -7,6 +7,7 @@
#include "lua_api.h"
#include "lua_processor.h"
#include "machine.h"
#include "vfs.h"
vfs_node* find_node_by_id(vfs_node* root, long long id) {
@ -92,24 +93,22 @@ std::string Session::process_command(const std::string& command) {
args.push_back(arg);
}
/* Search for script in the /bin directory of the current session machine. */
std::string script_filename = command_name + ".lua";
long long script_id = 0;
std::string script_content;
_db_manager->_db << "SELECT T2.id, T2.content FROM vfs_nodes AS T1 JOIN vfs_nodes AS T2 "
"ON T1.id = T2.parent_id WHERE T1.name = 'bin' AND T2.name = ? "
"AND t1.machine_id = ?;"
<< script_filename << _session_machine->id
>> [&](long long id, std::string content) {
script_id = id;
script_content = content;
};
if(script_id > 0) {
/* Find the /bin directory in the current machine's VFS. */
vfs_node* bin_dir = find_node_by_path(_session_machine->vfs_root, "/bin");
if(bin_dir && bin_dir->type == DIR_NODE) {
auto it = bin_dir->children.find(command_name);
if(it != bin_dir->children.end()) {
vfs_node* command_node = it->second;
if(command_node->type == EXEC_NODE) {
/* Execute the script. */
bool is_remote = (_session_machine != _home_machine);
sol::object result = lua.execute(script_content, *this, args, is_remote);
return result.is<std::string>() ? result.as<std::string>() : "[Script returned an unexpected type]";
sol::object result = lua.execute(command_node->content, *this, args, is_remote);
return result.is<std::string>() ? result.as<std::string>()
: "[Script returned an unexpected type]";
}
}
}
return "Unknown command: " + command_name + "\n";
}

View File

@ -10,7 +10,8 @@ typedef std::map<std::string, vfs_node*> vfs_child_map;
enum vfs_node_type {
FILE_NODE,
DIR_NODE
DIR_NODE,
EXEC_NODE
};
struct vfs_node {

View File

@ -333,7 +333,7 @@ void NetworkManager::_seed_npc_machines(void) {
vfs_node* template_bin = _machine_manager.get_vfs_template()->children["bin"];
for(auto const& [name, node] : template_bin->children) {
_db_manager->vfs().create_node(machine_id, &bin_id, name, FILE_NODE, node->content);
_db_manager->vfs().create_node(machine_id, &bin_id, name, node->type, node->content);
}
for(const auto& service : def.services) {