diff --git a/src/component/io.cpp b/src/component/io.cpp index 346fd25..2c12953 100644 --- a/src/component/io.cpp +++ b/src/component/io.cpp @@ -50,6 +50,20 @@ namespace io gsc::function::add("directoryisempty", utils::io::directory_is_empty); gsc::function::add("listfiles", utils::io::list_files); gsc::function::add("removefile", utils::io::remove_file); + + gsc::function::add("removedirectory", [](const std::filesystem::path& src, const scripting::variadic_args& va) + { + bool recursive = false; + if (va.size() > 0) + { + recursive = va[0]; + } + + utils::io::remove_directory(src, recursive); + }); + + gsc::function::add("copyfolder", utils::io::copy_folder); + gsc::function::add("copydirectory", utils::io::copy_folder); gsc::function::add("readfile", static_cast(utils::io::read_file)); } }; diff --git a/src/game/scripting/script_value.hpp b/src/game/scripting/script_value.hpp index b139768..de7ed1a 100644 --- a/src/game/scripting/script_value.hpp +++ b/src/game/scripting/script_value.hpp @@ -58,7 +58,6 @@ namespace scripting return typenames[type_]; } - printf("UNKNOWN TYPE %i\n", type_); return "unknown"; } @@ -134,8 +133,52 @@ namespace scripting script_value(const vector& value); + private: template - bool is() const; + T get() const + { + if (std::is_constructible::value && this->is()) \ + { + return T(this->as()); + } + + throw std::runtime_error("Invalid type"); + } + public: + +#define ADD_TYPE(type) \ + template <> \ + bool is() const; \ +private: \ + template <> \ + type get<>() const; \ +public: \ + + template + bool is() const + { + if (std::is_constructible::value && this->is()) \ + { + return true; + } + + return false; + } + + ADD_TYPE(bool) + ADD_TYPE(int) + ADD_TYPE(unsigned int) + ADD_TYPE(float) + ADD_TYPE(double) + ADD_TYPE(const char*) + ADD_TYPE(std::string) + + ADD_TYPE(vector) + ADD_TYPE(array) + ADD_TYPE(object) + ADD_TYPE(function) + ADD_TYPE(entity) + ADD_TYPE(script_value) template T as() const @@ -184,10 +227,6 @@ namespace scripting variable_value value_{}; - private: - template - T get() const; - }; class function_argument; diff --git a/src/utils/io.cpp b/src/utils/io.cpp index 038c085..f16a9a6 100644 --- a/src/utils/io.cpp +++ b/src/utils/io.cpp @@ -120,4 +120,16 @@ namespace utils::io { std::filesystem::copy(src, target, std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive); } -} \ No newline at end of file + + size_t remove_directory(const std::filesystem::path& src, bool recursive) + { + if (recursive) + { + return static_cast(std::filesystem::remove_all(src)); + } + else + { + return static_cast(std::filesystem::remove(src)); + } + } +} diff --git a/src/utils/io.hpp b/src/utils/io.hpp index ab4ebaa..cdee176 100644 --- a/src/utils/io.hpp +++ b/src/utils/io.hpp @@ -18,4 +18,5 @@ namespace utils::io bool directory_is_empty(const std::string& directory); std::vector list_files(const std::string& directory); void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target); + size_t remove_directory(const std::filesystem::path& src, bool recursive = false); }