[Refactor] Simulate executable files in VFS.
This commit is contained in:
parent
651f7c415e
commit
7b58ecc4d5
@ -74,7 +74,7 @@ bool DatabaseManager::create_player(const std::string& username, const std::stri
|
|||||||
/* Copy scripts from template into new machine's /bin */
|
/* Copy scripts from template into new machine's /bin */
|
||||||
vfs_node* template_bin = vfs_template->children["bin"];
|
vfs_node* template_bin = vfs_template->children["bin"];
|
||||||
for(auto const& [name, node] : template_bin->children) {
|
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. */
|
/* Add default SSH service. */
|
||||||
|
|||||||
@ -39,11 +39,12 @@ MachineManager::MachineManager(DatabaseManager* db_manager) :
|
|||||||
std::ifstream t(entry.path());
|
std::ifstream t(entry.path());
|
||||||
std::stringstream buffer;
|
std::stringstream buffer;
|
||||||
buffer << t.rdbuf();
|
buffer << t.rdbuf();
|
||||||
std::string filename = entry.path().filename().string();
|
std::string filename_with_ext = entry.path().filename().string();
|
||||||
vfs_node* script_node = new_node(filename, FILE_NODE, bin);
|
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();
|
script_node->content = buffer.str();
|
||||||
bin->children[filename] = script_node;
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include "lua_api.h"
|
#include "lua_api.h"
|
||||||
#include "lua_processor.h"
|
#include "lua_processor.h"
|
||||||
#include "machine.h"
|
#include "machine.h"
|
||||||
|
#include "vfs.h"
|
||||||
|
|
||||||
|
|
||||||
vfs_node* find_node_by_id(vfs_node* root, long long id) {
|
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);
|
args.push_back(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Search for script in the /bin directory of the current session machine. */
|
/* Find the /bin directory in the current machine's VFS. */
|
||||||
std::string script_filename = command_name + ".lua";
|
vfs_node* bin_dir = find_node_by_path(_session_machine->vfs_root, "/bin");
|
||||||
long long script_id = 0;
|
if(bin_dir && bin_dir->type == DIR_NODE) {
|
||||||
std::string script_content;
|
auto it = bin_dir->children.find(command_name);
|
||||||
_db_manager->_db << "SELECT T2.id, T2.content FROM vfs_nodes AS T1 JOIN vfs_nodes AS T2 "
|
if(it != bin_dir->children.end()) {
|
||||||
"ON T1.id = T2.parent_id WHERE T1.name = 'bin' AND T2.name = ? "
|
vfs_node* command_node = it->second;
|
||||||
"AND t1.machine_id = ?;"
|
if(command_node->type == EXEC_NODE) {
|
||||||
<< script_filename << _session_machine->id
|
/* Execute the script. */
|
||||||
>> [&](long long id, std::string content) {
|
bool is_remote = (_session_machine != _home_machine);
|
||||||
script_id = id;
|
sol::object result = lua.execute(command_node->content, *this, args, is_remote);
|
||||||
script_content = content;
|
return result.is<std::string>() ? result.as<std::string>()
|
||||||
};
|
: "[Script returned an unexpected type]";
|
||||||
|
}
|
||||||
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<std::string>() ? result.as<std::string>() : "[Script returned an unexpected type]";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Unknown command: " + command_name + "\n";
|
return "Unknown command: " + command_name + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,8 @@ typedef std::map<std::string, vfs_node*> vfs_child_map;
|
|||||||
|
|
||||||
enum vfs_node_type {
|
enum vfs_node_type {
|
||||||
FILE_NODE,
|
FILE_NODE,
|
||||||
DIR_NODE
|
DIR_NODE,
|
||||||
|
EXEC_NODE
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vfs_node {
|
struct vfs_node {
|
||||||
|
|||||||
@ -333,7 +333,7 @@ void NetworkManager::_seed_npc_machines(void) {
|
|||||||
|
|
||||||
vfs_node* template_bin = _machine_manager.get_vfs_template()->children["bin"];
|
vfs_node* template_bin = _machine_manager.get_vfs_template()->children["bin"];
|
||||||
for(auto const& [name, node] : template_bin->children) {
|
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) {
|
for(const auto& service : def.services) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user