bound all the file system methods to AngelScript

This commit is contained in:
faraphel 2024-08-24 19:22:39 +02:00
parent 747c60aa22
commit e3a922d4e9
7 changed files with 42 additions and 42 deletions

View file

@ -3,6 +3,7 @@
#include <memory>
#include <angelscript.h>
#include <cassert>
#include <VirtualFileSystem.hpp>
#include <scriptbuilder/scriptbuilder.h>
@ -27,40 +28,29 @@ int main_AngelScript(int argc, char* argv[]) {
// script
CScriptBuilder builder;
error = builder.StartNewModule(asEngine, "script");
if (error < 0)
throw std::runtime_error("Could not start a new module.");
error = builder.StartNewModule(asEngine, "script"); assert(error >= 0);
error = builder.AddSectionFromFile(".mods/Atlas/scripts/hello.as");
if (error < 0)
throw std::runtime_error("Could not add a section to the module.");
error = builder.BuildModule();
if (error < 0)
throw std::runtime_error("Could not build the module.");
error = builder.BuildModule(); assert(error >= 0);
// execution
asIScriptModule *module = asEngine->GetModule("script");
if (module == nullptr)
throw std::runtime_error("Could not get the module.");
asIScriptModule *module = asEngine->GetModule("script"); assert(error >= 0);
asIScriptFunction *function = module->GetFunctionByDecl("void main()");
if (function == nullptr)
throw std::runtime_error("Could not get the main function.");
asIScriptContext *context = asEngine->CreateContext();
if (context == nullptr)
throw std::runtime_error("Could not create the context.");
asIScriptContext *context = asEngine->CreateContext(); assert(error >= 0);
error = context->Prepare(function);
if (error < 0)
throw std::runtime_error("Could not register the function in the context.");
error = context->Prepare(function); assert(error >= 0);
error = context->Execute();
// clean up
context->Release();
delete atlasEngine;
return error;

View file

@ -1,5 +1,6 @@
#include "_load.hpp"
#include <cassert>
#include <stdexcept>
#include <string>
@ -10,26 +11,17 @@ namespace atlas::script::module::debug {
void load(const engine::Engine* atlasEngine) {
asIScriptEngine* asEngine = atlasEngine->getAsEngine();
int error;
// start namespace
error = asEngine->SetDefaultNamespace("atlas::debug");
if (error < 0)
throw std::runtime_error("Could not enter the \"atlas::debug\" namespace.");
error = asEngine->SetDefaultNamespace("atlas::debug"); assert(error >= 0);
// functions
error = asEngine->RegisterGlobalFunction(
"void log(const string& in, const string& in)",
asFUNCTION(log),
asCALL_CDECL
);
if (error < 0)
throw std::runtime_error("Could not register the \"load\" function.");
error = asEngine->RegisterGlobalFunction("void log(const string &in category, const string &in message)", asFUNCTION(log), asCALL_CDECL); assert(error >= 0);
// end namespace
error = asEngine->SetDefaultNamespace("");
if (error < 0)
throw std::runtime_error("Could not reset the namespace.");
error = asEngine->SetDefaultNamespace(""); assert(error >= 0);
}
}

View file

@ -13,7 +13,7 @@ void File::open(const std::string& mode) {
this->stream = std::fstream(this->getRealPath(), atlas::utils::file::getOpenMode(mode));
}
std::vector<std::uint8_t> File::read(const std::size_t size) {
std::vector<std::uint8_t> File::read(const std::uint64_t size) {
// create a buffer
std::vector<std::uint8_t> buffer(size);

View file

@ -28,7 +28,7 @@ public:
* @param size the size of the content to read.
* @return the content of the file
*/
std::vector<std::uint8_t> read(size_t size = std::numeric_limits<std::size_t>::max());
std::vector<std::uint8_t> read(std::uint64_t size = std::numeric_limits<std::uint64_t>::max());
/**
* Write data into the file
* @param buffer the data to write into the file
@ -40,12 +40,12 @@ public:
* Get the current cursor position
* @return the current cursor position
*/
std::size_t tell();
std::uint64_t tell();
/**
* Set the cursor position
* @param position the new cursor position
*/
void seek(std::size_t position);
void seek(std::uint64_t position);
/**
* Close the file.

View file

@ -18,7 +18,7 @@ Path Path::getParent() const {
);
}
std::vector<std::shared_ptr<Path>> Path::getChildrens(const bool recursive, const std::string& pattern) const {
std::vector<Path> Path::getChildrens(const bool recursive, const std::string& pattern) const {
// TODO(Faraphel): implement recursive and pattern
// check if the path is iterable
@ -29,7 +29,7 @@ std::vector<std::shared_ptr<Path>> Path::getChildrens(const bool recursive, cons
const std::filesystem::path realPath = this->getRealPath();
// prepare the list of children paths
auto childsPath = std::vector<std::shared_ptr<Path>>();
auto childsPath = std::vector<Path>();
// iterate through the childrens of the directory
for (const auto& childRealEntry : std::filesystem::directory_iterator(realPath)) {
@ -39,11 +39,10 @@ std::vector<std::shared_ptr<Path>> Path::getChildrens(const bool recursive, cons
// get the virtual path
std::filesystem::path childVirtualPath = this->virtualPath / childRealPath.filename();
// add it to the list of childrens
auto path = std::make_shared<Path>(
childsPath.emplace_back(
this->fileSystem,
childVirtualPath
);
childsPath.push_back(path);
}
return childsPath;
@ -61,9 +60,9 @@ bool Path::isDirectory() const {
return is_directory(this->getRealPath());
}
std::shared_ptr<File> Path::open(const std::string& mode, const std::string& encoding) {
File* Path::open(const std::string& mode, const std::string& encoding) const {
// create a new file object
auto file = std::make_shared<File>(
auto file = new File(
this->fileSystem,
this->virtualPath
);

View file

@ -29,7 +29,7 @@ public:
* Get the child paths
* @return the child paths
*/
[[nodiscard]] std::vector<std::shared_ptr<Path>> getChildrens(bool recursive = false, const std::string& pattern = "*") const;
[[nodiscard]] std::vector<Path> getChildrens(bool recursive = false, const std::string& pattern = "*") const;
// type
@ -56,7 +56,7 @@ public:
* open the path as a file
* @return the opened file
*/
std::shared_ptr<File> open(const std::string& mode, const std::string& encoding = "utf-8");
[[nodiscard]] File* open(const std::string& mode, const std::string& encoding = "utf-8") const;
[[nodiscard]] bool mkdir(bool parents) const;

View file

@ -21,19 +21,38 @@ void load(const engine::Engine* atlasEngine) {
error = asEngine->RegisterObjectType("File", 0, asOBJ_REF); assert(error >= 0);
error = asEngine->RegisterObjectBehaviour("File", asBEHAVE_ADDREF, "void f()", asMETHOD(File, asAddReference), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectBehaviour("File", asBEHAVE_RELEASE, "void f()", asMETHOD(File, asRelease), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("File", "void open()", asMETHOD(File, open), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("File", "array<uint8>@ read(uint64 size)", asMETHOD(File, read), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("File", "void write(const array<uint8> &in data)", asMETHOD(File, write), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("File", "uint64 tell()", asMETHOD(File, tell), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("File", "void seek(uint64 position)", asMETHOD(File, seek), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("File", "void close()", asMETHOD(File, close), asCALL_THISCALL); assert(error >= 0);
// path type
error = asEngine->RegisterObjectType("Path", 0, asOBJ_REF); assert(error >= 0);
error = asEngine->RegisterObjectBehaviour("Path", asBEHAVE_ADDREF, "void f()", asMETHOD(Path, asAddReference), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectBehaviour("Path", asBEHAVE_RELEASE, "void f()", asMETHOD(Path, asRelease), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "Path@ getParent() const", asMETHOD(Path, getParent), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "array<Path@>@ getChildrens(bool recursive, const string &in pattern) const", asMETHOD(Path, getChildrens), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "bool exists() const", asMETHOD(Path, exists), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "bool isFile() const", asMETHOD(Path, isFile), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "bool isDirectory() const", asMETHOD(Path, isDirectory), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "File@ open(const string &in mode) const", asMETHOD(Path, open), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "bool mkdir(bool parents) const", asMETHOD(Path, mkdir), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "void remove(bool recursive) const", asMETHOD(Path, remove), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "string getPath() const", asMETHOD(Path, getPath), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "string getDirname() const", asMETHOD(Path, getDirname), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "string getFilename() const", asMETHOD(Path, getFilename), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "string getBasename() const", asMETHOD(Path, getBasename), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "string getSuffix() const", asMETHOD(Path, getSuffix), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "array<string>@ getSuffixes() const", asMETHOD(Path, getSuffixes), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("Path", "array<string>@ getComponents() const", asMETHOD(Path, getComponents), asCALL_THISCALL); assert(error >= 0);
// file system type
error = asEngine->RegisterObjectType("FileSystem", 0, asOBJ_REF); assert(error >= 0);
error = asEngine->RegisterObjectBehaviour("FileSystem", asBEHAVE_ADDREF, "void f()", asMETHOD(FileSystem, asAddReference), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectBehaviour("FileSystem", asBEHAVE_RELEASE, "void f()", asMETHOD(FileSystem, asRelease), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("FileSystem", "Path@ getPath(const string& in) const", asMETHOD(FileSystem, getPath), asCALL_THISCALL); assert(error >= 0);
error = asEngine->RegisterObjectMethod("FileSystem", "Path@ getPath(const string &in path) const", asMETHOD(FileSystem, getPath), asCALL_THISCALL); assert(error >= 0);
// file system singleton
error = asEngine->RegisterGlobalProperty("const FileSystem fileSystem", fileSystem); assert(error >= 0);