added file.list function
This commit is contained in:
parent
548c55fced
commit
b15d234a88
@ -259,7 +259,7 @@ void Engine::loadContent() {
|
|||||||
names = manager.assembly(names);
|
names = manager.assembly(names);
|
||||||
contentPacks = manager.getAll(names);
|
contentPacks = manager.getAll(names);
|
||||||
|
|
||||||
std::vector<std::pair<std::string, fs::path>> resRoots;
|
std::vector<PathsRoot> resRoots;
|
||||||
for (auto& pack : contentPacks) {
|
for (auto& pack : contentPacks) {
|
||||||
resRoots.push_back({pack.id, pack.folder});
|
resRoots.push_back({pack.id, pack.folder});
|
||||||
|
|
||||||
|
|||||||
@ -164,14 +164,14 @@ fs::path EnginePaths::resolve(std::string path) {
|
|||||||
throw files_access_error("unknown entry point '"+prefix+"'");
|
throw files_access_error("unknown entry point '"+prefix+"'");
|
||||||
}
|
}
|
||||||
|
|
||||||
ResPaths::ResPaths(fs::path mainRoot, std::vector<std::pair<std::string, fs::path>> roots)
|
ResPaths::ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots)
|
||||||
: mainRoot(mainRoot), roots(roots) {
|
: mainRoot(mainRoot), roots(roots) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::path ResPaths::find(const std::string& filename) const {
|
fs::path ResPaths::find(const std::string& filename) const {
|
||||||
for (int i = roots.size()-1; i >= 0; i--) {
|
for (int i = roots.size()-1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
fs::path file = root.second / fs::u8path(filename);
|
fs::path file = root.path / fs::u8path(filename);
|
||||||
if (fs::exists(file)) {
|
if (fs::exists(file)) {
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
@ -182,8 +182,8 @@ fs::path ResPaths::find(const std::string& filename) const {
|
|||||||
std::string ResPaths::findRaw(const std::string& filename) const {
|
std::string ResPaths::findRaw(const std::string& filename) const {
|
||||||
for (int i = roots.size()-1; i >= 0; i--) {
|
for (int i = roots.size()-1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
if (fs::exists(root.second / fs::path(filename))) {
|
if (fs::exists(root.path / fs::path(filename))) {
|
||||||
return root.first+":"+filename;
|
return root.name + ":" + filename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto resDir = mainRoot;
|
auto resDir = mainRoot;
|
||||||
@ -193,11 +193,35 @@ std::string ResPaths::findRaw(const std::string& filename) const {
|
|||||||
throw std::runtime_error("could not to find file "+util::quote(filename));
|
throw std::runtime_error("could not to find file "+util::quote(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) const {
|
||||||
|
std::vector<std::string> entries;
|
||||||
|
for (int i = roots.size()-1; i >= 0; i--) {
|
||||||
|
auto& root = roots[i];
|
||||||
|
fs::path folder = root.path / fs::u8path(folderName);
|
||||||
|
if (!fs::is_directory(folder))
|
||||||
|
continue;
|
||||||
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
|
auto name = entry.path().filename().u8string();
|
||||||
|
entries.push_back(root.name+":"+folderName+"/"+name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
fs::path folder = mainRoot / fs::u8path(folderName);
|
||||||
|
if (!fs::is_directory(folder))
|
||||||
|
return entries;
|
||||||
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
|
auto name = entry.path().filename().u8string();
|
||||||
|
entries.push_back("core:"+folderName+"/"+name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<fs::path> ResPaths::listdir(const std::string& folderName) const {
|
std::vector<fs::path> ResPaths::listdir(const std::string& folderName) const {
|
||||||
std::vector<fs::path> entries;
|
std::vector<fs::path> entries;
|
||||||
for (int i = roots.size()-1; i >= 0; i--) {
|
for (int i = roots.size()-1; i >= 0; i--) {
|
||||||
auto& root = roots[i];
|
auto& root = roots[i];
|
||||||
fs::path folder = root.second / fs::u8path(folderName);
|
fs::path folder = root.path / fs::u8path(folderName);
|
||||||
if (!fs::is_directory(folder))
|
if (!fs::is_directory(folder))
|
||||||
continue;
|
continue;
|
||||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||||
|
|||||||
@ -42,18 +42,24 @@ public:
|
|||||||
fs::path resolve(std::string path);
|
fs::path resolve(std::string path);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PathsRoot {
|
||||||
|
std::string name;
|
||||||
|
fs::path path;
|
||||||
|
};
|
||||||
|
|
||||||
class ResPaths {
|
class ResPaths {
|
||||||
fs::path mainRoot;
|
fs::path mainRoot;
|
||||||
std::vector<std::pair<std::string, fs::path>> roots;
|
std::vector<PathsRoot> roots;
|
||||||
public:
|
public:
|
||||||
ResPaths(
|
ResPaths(
|
||||||
fs::path mainRoot,
|
fs::path mainRoot,
|
||||||
std::vector<std::pair<std::string, fs::path>> roots
|
std::vector<PathsRoot> roots
|
||||||
);
|
);
|
||||||
|
|
||||||
fs::path find(const std::string& filename) const;
|
fs::path find(const std::string& filename) const;
|
||||||
std::string findRaw(const std::string& filename) const;
|
std::string findRaw(const std::string& filename) const;
|
||||||
std::vector<fs::path> listdir(const std::string& folder) const;
|
std::vector<fs::path> listdir(const std::string& folder) const;
|
||||||
|
std::vector<std::string> listdirRaw(const std::string& folder) const;
|
||||||
|
|
||||||
const fs::path& getMainRoot() const;
|
const fs::path& getMainRoot() const;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -140,6 +140,37 @@ static int l_file_write_bytes(lua_State* L) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_file_list_all_res(lua_State* L, const std::string& path) {
|
||||||
|
auto files = scripting::engine->getResPaths()->listdirRaw(path);
|
||||||
|
lua_createtable(L, files.size(), 0);
|
||||||
|
for (size_t i = 0; i < files.size(); i++) {
|
||||||
|
lua_pushstring(L, files[i].c_str());
|
||||||
|
lua_rawseti(L, -2, i+1);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_file_list(lua_State* L) {
|
||||||
|
std::string dirname = lua_tostring(L, 1);
|
||||||
|
if (dirname.find(':') == std::string::npos) {
|
||||||
|
return l_file_list_all_res(L, dirname);
|
||||||
|
}
|
||||||
|
fs::path path = resolve_path(L, dirname);
|
||||||
|
if (!fs::is_directory(path)) {
|
||||||
|
throw std::runtime_error(util::quote(path.u8string())+" is not a directory");
|
||||||
|
}
|
||||||
|
lua_createtable(L, 0, 0);
|
||||||
|
size_t index = 1;
|
||||||
|
for (auto& entry : fs::directory_iterator(path)) {
|
||||||
|
auto name = entry.path().filename().u8string();
|
||||||
|
auto file = dirname + "/" + name;
|
||||||
|
lua_pushstring(L, file.c_str());
|
||||||
|
lua_rawseti(L, -2, index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg filelib [] = {
|
const luaL_Reg filelib [] = {
|
||||||
{"resolve", lua_wrap_errors<l_file_resolve>},
|
{"resolve", lua_wrap_errors<l_file_resolve>},
|
||||||
{"find", lua_wrap_errors<l_file_find>},
|
{"find", lua_wrap_errors<l_file_find>},
|
||||||
@ -153,5 +184,6 @@ const luaL_Reg filelib [] = {
|
|||||||
{"mkdirs", lua_wrap_errors<l_file_mkdirs>},
|
{"mkdirs", lua_wrap_errors<l_file_mkdirs>},
|
||||||
{"read_bytes", lua_wrap_errors<l_file_read_bytes>},
|
{"read_bytes", lua_wrap_errors<l_file_read_bytes>},
|
||||||
{"write_bytes", lua_wrap_errors<l_file_write_bytes>},
|
{"write_bytes", lua_wrap_errors<l_file_write_bytes>},
|
||||||
|
{"list", lua_wrap_errors<l_file_list>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user