From 7b58ecc4d5b33edec6136d31c277b3b99b211d6a Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Tue, 21 Oct 2025 20:51:49 +0100 Subject: [PATCH] [Refactor] Simulate executable files in VFS. --- common/src/db/database_manager.cpp | 2 +- common/src/machine_manager.cpp | 7 ++++--- common/src/session.cpp | 33 +++++++++++++++--------------- common/src/vfs.h | 3 ++- server/src/network_manager.cpp | 2 +- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/common/src/db/database_manager.cpp b/common/src/db/database_manager.cpp index a1db1c5..bdc74b1 100644 --- a/common/src/db/database_manager.cpp +++ b/common/src/db/database_manager.cpp @@ -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. */ diff --git a/common/src/machine_manager.cpp b/common/src/machine_manager.cpp index b7df0ed..c4267ce 100644 --- a/common/src/machine_manager.cpp +++ b/common/src/machine_manager.cpp @@ -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()); } } } diff --git a/common/src/session.cpp b/common/src/session.cpp index 306cfbb..bc1643b 100644 --- a/common/src/session.cpp +++ b/common/src/session.cpp @@ -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) { - bool is_remote = (_session_machine != _home_machine); - sol::object result = lua.execute(script_content, *this, args, is_remote); - return result.is() ? result.as() : "[Script returned an unexpected type]"; + /* 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(command_node->content, *this, args, is_remote); + return result.is() ? result.as() + : "[Script returned an unexpected type]"; + } + } } + return "Unknown command: " + command_name + "\n"; } diff --git a/common/src/vfs.h b/common/src/vfs.h index 558061c..fcc9d86 100644 --- a/common/src/vfs.h +++ b/common/src/vfs.h @@ -10,7 +10,8 @@ typedef std::map vfs_child_map; enum vfs_node_type { FILE_NODE, - DIR_NODE + DIR_NODE, + EXEC_NODE }; struct vfs_node { diff --git a/server/src/network_manager.cpp b/server/src/network_manager.cpp index 996039a..acf06b5 100644 --- a/server/src/network_manager.cpp +++ b/server/src/network_manager.cpp @@ -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) {