mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 15:26:59 +00:00
Lua: Add FS.Remove, FS.Rename (move), FS.Copy, FS.Exists
This commit is contained in:
parent
4de80e0c7a
commit
6f9f790c5b
@ -23,4 +23,11 @@ namespace MP {
|
||||
void Sleep(size_t Ms);
|
||||
void PrintRaw(sol::variadic_args);
|
||||
}
|
||||
namespace FS {
|
||||
std::pair<bool, std::string> CreateDirectory(const std::string& Path);
|
||||
std::pair<bool, std::string> Remove(const std::string& Path);
|
||||
std::pair<bool, std::string> Rename(const std::string& Path, const std::string& NewPath);
|
||||
std::pair<bool, std::string> Copy(const std::string& Path, const std::string& NewPath);
|
||||
bool Exists(const std::string& Path);
|
||||
}
|
||||
}
|
||||
|
@ -251,3 +251,63 @@ int LuaAPI::PanicHandler(lua_State* State) {
|
||||
beammp_lua_error("PANIC: " + sol::stack::get<std::string>(State, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename FnT, typename... ArgsT>
|
||||
static std::pair<bool, std::string> FSWrapper(FnT Fn, ArgsT&&... Args) {
|
||||
std::error_code errc;
|
||||
std::pair<bool, std::string> Result;
|
||||
Fn(std::forward<ArgsT>(Args)..., errc);
|
||||
Result.first = errc == std::error_code {};
|
||||
if (!Result.first) {
|
||||
Result.second = errc.message();
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> LuaAPI::FS::CreateDirectory(const std::string& Path) {
|
||||
std::error_code errc;
|
||||
std::pair<bool, std::string> 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<bool, std::string> LuaAPI::FS::Remove(const std::string& Path) {
|
||||
std::error_code errc;
|
||||
std::pair<bool, std::string> Result;
|
||||
fs::remove(fs::relative(Path), errc);
|
||||
Result.first = errc == std::error_code {};
|
||||
if (!Result.first) {
|
||||
Result.second = errc.message();
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> LuaAPI::FS::Rename(const std::string& Path, const std::string& NewPath) {
|
||||
std::error_code errc;
|
||||
std::pair<bool, std::string> 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<bool, std::string> LuaAPI::FS::Copy(const std::string& Path, const std::string& NewPath) {
|
||||
std::error_code errc;
|
||||
std::pair<bool, std::string> 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));
|
||||
}
|
||||
|
@ -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<bool, std::string> {
|
||||
std::error_code errc;
|
||||
std::pair<bool, std::string> 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();
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,6 @@ TLuaPlugin::TLuaPlugin(TLuaEngine& Engine, const TLuaPluginConfig& Config, const
|
||||
std::vector<fs::path> 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()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user