Lua: Add various FS functions

This commit is contained in:
Lion Kortlepel 2021-09-21 00:27:09 +02:00
parent 23ffa25d78
commit fe3ccafc1d
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
3 changed files with 32 additions and 1 deletions

View File

@ -32,5 +32,8 @@ namespace FS {
std::string GetExtension(const std::string& Path);
std::string GetParentFolder(const std::string& Path);
bool Exists(const std::string& Path);
bool IsDirectory(const std::string& Path);
bool IsFile(const std::string& Path);
std::string ConcatPaths(sol::variadic_args Args);
}
}

View File

@ -321,5 +321,30 @@ std::string LuaAPI::FS::GetExtension(const std::string& Path) {
}
std::string LuaAPI::FS::GetParentFolder(const std::string& Path) {
return fs::relative(Path).parent_path().string();
return fs::path(Path).parent_path().string();
}
bool LuaAPI::FS::IsDirectory(const std::string& Path) {
return fs::is_directory(Path);
}
bool LuaAPI::FS::IsFile(const std::string& Path) {
return fs::is_regular_file(Path);
}
std::string LuaAPI::FS::ConcatPaths(sol::variadic_args Args) {
fs::path Path;
for (size_t i = 0; i < Args.size(); ++i) {
auto Obj = Args[i];
if (!Obj.is<std::string>()) {
beammp_lua_error("FS.Concat called with non-string argument");
return "";
}
Path += Obj.as<std::string>();
if (i < Args.size() - 1 && !Path.empty()) {
Path += fs::path::preferred_separator;
}
}
auto Result = Path.lexically_normal().string();
return Result;
}

View File

@ -458,6 +458,9 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
FSTable.set_function("GetFilename", &LuaAPI::FS::GetFilename);
FSTable.set_function("GetExtension", &LuaAPI::FS::GetExtension);
FSTable.set_function("GetParentFolder", &LuaAPI::FS::GetParentFolder);
FSTable.set_function("IsDirectory", &LuaAPI::FS::IsDirectory);
FSTable.set_function("IsFile", &LuaAPI::FS::IsFile);
FSTable.set_function("ConcatPaths", &LuaAPI::FS::ConcatPaths);
Start();
}