Load files from subfolders in texturepacks

Updated and rebased version of a PR by red-001
This commit is contained in:
number Zero 2017-09-13 23:03:18 +03:00 committed by paramat
parent ae9b1aa177
commit 05d93c7fa1
7 changed files with 63 additions and 19 deletions

@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "threading/mutex_auto_lock.h" #include "threading/mutex_auto_lock.h"
#include "client/clientevent.h" #include "client/clientevent.h"
#include "client/renderingengine.h" #include "client/renderingengine.h"
#include "client/tile.h"
#include "util/auth.h" #include "util/auth.h"
#include "util/directiontables.h" #include "util/directiontables.h"
#include "util/pointedthing.h" #include "util/pointedthing.h"
@ -1643,9 +1644,8 @@ void Client::afterContentReceived()
text = wgettext("Initializing nodes..."); text = wgettext("Initializing nodes...");
RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 72); RenderingEngine::draw_load_screen(text, guienv, m_tsrc, 0, 72);
m_nodedef->updateAliases(m_itemdef); m_nodedef->updateAliases(m_itemdef);
std::string texture_path = g_settings->get("texture_path"); for (const auto &path : getTextureDirs())
if (!texture_path.empty() && fs::IsDir(texture_path)) m_nodedef->applyTextureOverrides(path + DIR_DELIM + "override.txt");
m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
m_nodedef->setNodeRegistrationStatus(true); m_nodedef->setNodeRegistrationStatus(true);
m_nodedef->runNodeResolveCallbacks(); m_nodedef->runNodeResolveCallbacks();
delete[] text; delete[] text;

@ -129,11 +129,12 @@ std::string getTexturePath(const std::string &filename)
/* /*
Check from texture_path Check from texture_path
*/ */
const std::string &texture_path = g_settings->get("texture_path"); for (const auto &path : getTextureDirs()) {
if (!texture_path.empty()) { std::string testpath = path + DIR_DELIM + filename;
std::string testpath = texture_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found. // Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath); fullpath = getImagePath(testpath);
if (!fullpath.empty())
break;
} }
/* /*
@ -2388,3 +2389,10 @@ video::ITexture *TextureSource::getShaderFlagsTexture(bool normalmap_present)
return getTexture(tname); return getTexture(tname);
} }
const std::vector<std::string> &getTextureDirs()
{
static thread_local std::vector<std::string> dirs =
fs::GetRecursiveDirs(g_settings->get("texture_path"));
return dirs;
}

@ -337,3 +337,5 @@ struct TileSpec
//! The first is base texture, the second is overlay. //! The first is base texture, the second is overlay.
TileLayer layers[MAX_TILE_LAYERS]; TileLayer layers[MAX_TILE_LAYERS];
}; };
const std::vector<std::string> &getTextureDirs();

@ -380,15 +380,36 @@ std::string TempPath()
#endif #endif
void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst) void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
{
static const std::set<char> chars_to_ignore = { '_', '.' };
if (dir.empty() || !IsDir(dir))
return;
dirs.push_back(dir);
fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
}
std::vector<std::string> GetRecursiveDirs(const std::string &dir)
{
std::vector<std::string> result;
GetRecursiveDirs(result, dir);
return result;
}
void GetRecursiveSubPaths(const std::string &path,
std::vector<std::string> &dst,
bool list_files,
const std::set<char> &ignore)
{ {
std::vector<DirListNode> content = GetDirListing(path); std::vector<DirListNode> content = GetDirListing(path);
for (const auto &n : content) { for (const auto &n : content) {
std::string fullpath = path + DIR_DELIM + n.name; std::string fullpath = path + DIR_DELIM + n.name;
dst.push_back(fullpath); if (ignore.count(n.name[0]))
if (n.dir) { continue;
GetRecursiveSubPaths(fullpath, dst); if (list_files || n.dir)
} dst.push_back(fullpath);
if (n.dir)
GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
} }
} }

@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once #pragma once
#include <set>
#include <string> #include <string>
#include <vector> #include <vector>
#include "exceptions.h" #include "exceptions.h"
@ -66,10 +67,23 @@ bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
// Returns path to temp directory, can return "" on error // Returns path to temp directory, can return "" on error
std::string TempPath(); std::string TempPath();
/* Returns a list of subdirectories, including the path itself, but excluding
hidden directories (whose names start with . or _)
*/
void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
std::vector<std::string> GetRecursiveDirs(const std::string &dir);
/* Multiplatform */ /* Multiplatform */
// The path itself not included /* The path itself not included, returns a list of all subpaths.
void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst); dst - vector that contains all the subpaths.
list files - include files in the list of subpaths.
ignore - paths that start with these charcters will not be listed.
*/
void GetRecursiveSubPaths(const std::string &path,
std::vector<std::string> &dst,
bool list_files,
const std::set<char> &ignore = {});
// Tries to delete all, returns false if any failed // Tries to delete all, returns false if any failed
bool DeletePaths(const std::vector<std::string> &paths); bool DeletePaths(const std::vector<std::string> &paths);

@ -532,7 +532,7 @@ int ModApiMainMenu::l_delete_world(lua_State *L)
std::vector<std::string> paths; std::vector<std::string> paths;
paths.push_back(spec.path); paths.push_back(spec.path);
fs::GetRecursiveSubPaths(spec.path, paths); fs::GetRecursiveSubPaths(spec.path, paths, true);
// Delete files // Delete files
if (!fs::DeletePaths(paths)) { if (!fs::DeletePaths(paths)) {

@ -253,9 +253,8 @@ Server::Server(
m_nodedef->updateAliases(m_itemdef); m_nodedef->updateAliases(m_itemdef);
// Apply texture overrides from texturepack/override.txt // Apply texture overrides from texturepack/override.txt
std::string texture_path = g_settings->get("texture_path"); for (const auto &path : fs::GetRecursiveDirs(g_settings->get("texture_path")))
if (!texture_path.empty() && fs::IsDir(texture_path)) m_nodedef->applyTextureOverrides(path + DIR_DELIM + "override.txt");
m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
m_nodedef->setNodeRegistrationStatus(true); m_nodedef->setNodeRegistrationStatus(true);
@ -2253,8 +2252,8 @@ void Server::fillMediaCache()
paths.push_back(mod.path + DIR_DELIM + "models"); paths.push_back(mod.path + DIR_DELIM + "models");
paths.push_back(mod.path + DIR_DELIM + "locale"); paths.push_back(mod.path + DIR_DELIM + "locale");
} }
paths.push_back(porting::path_user + DIR_DELIM + "textures" + DIR_DELIM + "server"); fs::GetRecursiveDirs(paths, porting::path_user + DIR_DELIM +
"textures" + DIR_DELIM + "server");
// Collect media file information from paths into cache // Collect media file information from paths into cache
for (const std::string &mediapath : paths) { for (const std::string &mediapath : paths) {
std::vector<fs::DirListNode> dirlist = fs::GetDirListing(mediapath); std::vector<fs::DirListNode> dirlist = fs::GetDirListing(mediapath);