From e3a922d4e9e76ee9fc9ed9c0b5807babcf2f95b3 Mon Sep 17 00:00:00 2001 From: faraphel Date: Sat, 24 Aug 2024 19:22:39 +0200 Subject: [PATCH] bound all the file system methods to AngelScript --- source/main.cpp | 22 ++++++---------------- source/script/module/debug/_load.cpp | 18 +++++------------- source/script/module/filesystem/File.cpp | 2 +- source/script/module/filesystem/File.hpp | 6 +++--- source/script/module/filesystem/Path.cpp | 11 +++++------ source/script/module/filesystem/Path.hpp | 4 ++-- source/script/module/filesystem/_load.cpp | 21 ++++++++++++++++++++- 7 files changed, 42 insertions(+), 42 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index b1e3b92..50e5b98 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -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; diff --git a/source/script/module/debug/_load.cpp b/source/script/module/debug/_load.cpp index c71bb09..874dfba 100644 --- a/source/script/module/debug/_load.cpp +++ b/source/script/module/debug/_load.cpp @@ -1,5 +1,6 @@ #include "_load.hpp" +#include #include #include @@ -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); } } diff --git a/source/script/module/filesystem/File.cpp b/source/script/module/filesystem/File.cpp index 31fef29..94af417 100644 --- a/source/script/module/filesystem/File.cpp +++ b/source/script/module/filesystem/File.cpp @@ -13,7 +13,7 @@ void File::open(const std::string& mode) { this->stream = std::fstream(this->getRealPath(), atlas::utils::file::getOpenMode(mode)); } -std::vector File::read(const std::size_t size) { +std::vector File::read(const std::uint64_t size) { // create a buffer std::vector buffer(size); diff --git a/source/script/module/filesystem/File.hpp b/source/script/module/filesystem/File.hpp index b96d075..513fdb1 100644 --- a/source/script/module/filesystem/File.hpp +++ b/source/script/module/filesystem/File.hpp @@ -28,7 +28,7 @@ public: * @param size the size of the content to read. * @return the content of the file */ - std::vector read(size_t size = std::numeric_limits::max()); + std::vector read(std::uint64_t size = std::numeric_limits::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. diff --git a/source/script/module/filesystem/Path.cpp b/source/script/module/filesystem/Path.cpp index 5102090..6622a7f 100644 --- a/source/script/module/filesystem/Path.cpp +++ b/source/script/module/filesystem/Path.cpp @@ -18,7 +18,7 @@ Path Path::getParent() const { ); } -std::vector> Path::getChildrens(const bool recursive, const std::string& pattern) const { +std::vector 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> Path::getChildrens(const bool recursive, cons const std::filesystem::path realPath = this->getRealPath(); // prepare the list of children paths - auto childsPath = std::vector>(); + auto childsPath = std::vector(); // iterate through the childrens of the directory for (const auto& childRealEntry : std::filesystem::directory_iterator(realPath)) { @@ -39,11 +39,10 @@ std::vector> 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( + 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 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( + auto file = new File( this->fileSystem, this->virtualPath ); diff --git a/source/script/module/filesystem/Path.hpp b/source/script/module/filesystem/Path.hpp index 7b17b4c..427f285 100644 --- a/source/script/module/filesystem/Path.hpp +++ b/source/script/module/filesystem/Path.hpp @@ -29,7 +29,7 @@ public: * Get the child paths * @return the child paths */ - [[nodiscard]] std::vector> getChildrens(bool recursive = false, const std::string& pattern = "*") const; + [[nodiscard]] std::vector 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 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; diff --git a/source/script/module/filesystem/_load.cpp b/source/script/module/filesystem/_load.cpp index fa5d527..d5375ae 100644 --- a/source/script/module/filesystem/_load.cpp +++ b/source/script/module/filesystem/_load.cpp @@ -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@ read(uint64 size)", asMETHOD(File, read), asCALL_THISCALL); assert(error >= 0); + error = asEngine->RegisterObjectMethod("File", "void write(const array &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@ 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@ getSuffixes() const", asMETHOD(Path, getSuffixes), asCALL_THISCALL); assert(error >= 0); + error = asEngine->RegisterObjectMethod("Path", "array@ 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);