[Add] Load command scripts from filesystem.
This commit is contained in:
parent
cb6022116a
commit
a502fa64cf
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,5 @@
|
||||
# Build output.
|
||||
bin/
|
||||
/bin/
|
||||
build/
|
||||
|
||||
# Editor/IDE, whatever people use.
|
||||
|
||||
14
assets/scripts/bin/ls.lua
Normal file
14
assets/scripts/bin/ls.lua
Normal file
@ -0,0 +1,14 @@
|
||||
-- /bin/ls - Lists files in a directory.
|
||||
local dir = current_dir -- Get directory object from C++.
|
||||
local output = ""
|
||||
|
||||
-- Iterate over the 'children' map exposed via C++.
|
||||
for name, node in pairs(dir.children) do
|
||||
output = output .. name
|
||||
if node.type == 1 then -- 1 is DIR_NODE enum from C++.
|
||||
output = output .. "/"
|
||||
end
|
||||
output = output .. " "
|
||||
end
|
||||
|
||||
return output
|
||||
3
assets/scripts/bin/rm.lua
Normal file
3
assets/scripts/bin/rm.lua
Normal file
@ -0,0 +1,3 @@
|
||||
local file_to_remove = arg[1]
|
||||
if not file_to_remove then return "rm: missing operand" end
|
||||
return { action = "rm", target = file_to_remove }
|
||||
@ -1,3 +1,7 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include "vfs_manager.h"
|
||||
#include "vfs.h"
|
||||
|
||||
@ -38,35 +42,20 @@ VFSManager::VFSManager(void) {
|
||||
_vfs_root->read_only = true;
|
||||
bin->read_only = true;
|
||||
|
||||
/* TODO:
|
||||
* Load all scripts from assets/scripts/bin into the bind node.
|
||||
* We'll create ls.lua manually for now.
|
||||
*/
|
||||
vfs_node* ls_script = new_node("ls.lua", FILE_NODE, bin);
|
||||
ls_script->content = R"lua(-- /bin/ls.lua - Lists files in a directory.
|
||||
local dir = current_dir -- Get directory object from C++.
|
||||
local output = ""
|
||||
|
||||
-- Iterate over the 'children' map exposed from c++.
|
||||
for name, node in pairs(dir.children) do
|
||||
output = output .. name
|
||||
if node.type == 1 then
|
||||
output = output .. "/"
|
||||
end
|
||||
output = output .. " "
|
||||
end
|
||||
return output
|
||||
)lua";
|
||||
|
||||
bin->children["ls.lua"] = ls_script;
|
||||
|
||||
vfs_node* rm_script = new_node("rm.lua", FILE_NODE, bin);
|
||||
rm_script->content = R"lua(-- /bin/rm.lua - Removes a file.
|
||||
local file_to_remove = arg[1]
|
||||
if not file_to_remove then return "rm: missing operand" end
|
||||
return { action = "rm", target = file_to_remove }
|
||||
)lua";
|
||||
bin->children["rm.lua"] = rm_script;
|
||||
/* Load all scripts from assets/scripts/bin into the VFS. */
|
||||
const std::string path = "assets/scripts/bin";
|
||||
for(const auto & entry : std::filesystem::directory_iterator(path)) {
|
||||
if(entry.is_regular_file() && entry.path().extension() == ".lua") {
|
||||
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);
|
||||
script_node->content = buffer.str();
|
||||
bin->children[filename] = script_node;
|
||||
fprintf(stderr, "Loaded script: /bin/%s\n", filename.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VFSManager::~VFSManager(void) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user