diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 115d7ec..2ec90c4 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -23,4 +23,11 @@ namespace MP { void Sleep(size_t Ms); void PrintRaw(sol::variadic_args); } +namespace FS { + std::pair CreateDirectory(const std::string& Path); + std::pair Remove(const std::string& Path); + std::pair Rename(const std::string& Path, const std::string& NewPath); + std::pair Copy(const std::string& Path, const std::string& NewPath); + bool Exists(const std::string& Path); +} } diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index 14cf22c..af02a59 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -251,3 +251,63 @@ int LuaAPI::PanicHandler(lua_State* State) { beammp_lua_error("PANIC: " + sol::stack::get(State, 1)); return 0; } + +template +static std::pair FSWrapper(FnT Fn, ArgsT&&... Args) { + std::error_code errc; + std::pair Result; + Fn(std::forward(Args)..., errc); + Result.first = errc == std::error_code {}; + if (!Result.first) { + Result.second = errc.message(); + } + return Result; +} + +std::pair LuaAPI::FS::CreateDirectory(const std::string& Path) { + std::error_code errc; + std::pair Result; + fs::create_directories(fs::relative(Path), errc); + Result.first = errc == std::error_code {}; + if (!Result.first) { + Result.second = errc.message(); + } + return Result; +} + +std::pair LuaAPI::FS::Remove(const std::string& Path) { + std::error_code errc; + std::pair Result; + fs::remove(fs::relative(Path), errc); + Result.first = errc == std::error_code {}; + if (!Result.first) { + Result.second = errc.message(); + } + return Result; +} + +std::pair LuaAPI::FS::Rename(const std::string& Path, const std::string& NewPath) { + std::error_code errc; + std::pair Result; + fs::rename(fs::relative(Path), fs::relative(NewPath), errc); + Result.first = errc == std::error_code {}; + if (!Result.first) { + Result.second = errc.message(); + } + return Result; +} + +std::pair LuaAPI::FS::Copy(const std::string& Path, const std::string& NewPath) { + std::error_code errc; + std::pair Result; + fs::copy(fs::relative(Path), fs::relative(NewPath), fs::copy_options::recursive, errc); + Result.first = errc == std::error_code {}; + if (!Result.first) { + Result.second = errc.message(); + } + return Result; +} + +bool LuaAPI::FS::Exists(const std::string& Path) { + return fs::exists(fs::relative(Path)); +} diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 73b7694..cae6563 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -44,7 +44,7 @@ void TLuaEngine::operator()() { } // this thread handles timers while (!mShutdown) { - std::this_thread::sleep_for(std::chrono::mi)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } @@ -363,16 +363,11 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi "Description", 6); auto FSTable = StateView.create_named_table("FS"); - FSTable.set_function("CreateDirectory", [&](const std::string& Path) -> std::pair { - std::error_code errc; - std::pair Result; - fs::create_directories(Path, errc); - Result.first = errc == std::error_code {}; - if (!Result.first) { - Result.second = errc.message(); - } - return Result; - }); + FSTable.set_function("CreateDirectory", &LuaAPI::FS::CreateDirectory); + FSTable.set_function("Exists", &LuaAPI::FS::Exists); + FSTable.set_function("Remove", &LuaAPI::FS::Remove); + FSTable.set_function("Rename", &LuaAPI::FS::Rename); + FSTable.set_function("Copy", &LuaAPI::FS::Copy); Start(); } diff --git a/src/TLuaPlugin.cpp b/src/TLuaPlugin.cpp index d0631a9..ec5e176 100644 --- a/src/TLuaPlugin.cpp +++ b/src/TLuaPlugin.cpp @@ -14,7 +14,6 @@ TLuaPlugin::TLuaPlugin(TLuaEngine& Engine, const TLuaPluginConfig& Config, const std::vector Entries; for (const auto& Entry : fs::directory_iterator(mFolder)) { if (Entry.is_regular_file() && Entry.path().extension() == ".lua") { - beammp_debug("Found script \"" + Entry.path().string() + "\" in \"" + mFolder.string() + "\""); Entries.push_back(Entry); } } @@ -37,7 +36,6 @@ TLuaPlugin::TLuaPlugin(TLuaEngine& Engine, const TLuaPluginConfig& Config, const Contents->resize(Size); auto NRead = std::fread(Contents->data(), 1, Contents->size(), File); if (NRead == Contents->size()) { - beammp_debug("Successfully read \"" + Entry.string() + "\" (" + std::to_string(NRead) + " Bytes)"); mFileContents[fs::relative(Entry).string()] = Contents; // Execute first time auto Result = mEngine.EnqueueScript(mConfig.StateId, TLuaChunk(Contents, Entry.string(), MainFolder.string()));