1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-17 17:08:39 +00:00

Refactor tile.cpp/h parts except texturesource.cpp

This commit is contained in:
cx384 2024-02-27 19:03:46 +01:00 committed by SmallJoker
parent aaf77025b6
commit 879f7e9f03
5 changed files with 112 additions and 130 deletions

View file

@ -23,77 +23,51 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "settings.h"
#include "filesys.h"
#include "porting.h"
#include <vector>
/*
A cache from texture name to texture path
*/
// A cache from texture name to texture path
static MutexedMap<std::string, std::string> g_texturename_to_path_cache;
/*
Replaces the filename extension.
eg:
std::string image = "a/image.png"
replace_ext(image, "jpg")
-> image = "a/image.jpg"
Returns true on success.
*/
static bool replace_ext(std::string &path, const char *ext)
void clearTextureNameCache()
{
if (ext == NULL)
return false;
// Find place of last dot, fail if \ or / found.
s32 last_dot_i = -1;
for (s32 i=path.size()-1; i>=0; i--)
{
if (path[i] == '.')
{
last_dot_i = i;
break;
}
if (path[i] == '\\' || path[i] == '/')
break;
}
// If not found, return an empty string
if (last_dot_i == -1)
return false;
// Else make the new path
path = path.substr(0, last_dot_i+1) + ext;
return true;
g_texturename_to_path_cache.clear();
}
/*
Find out the full path of an image by trying different filename
extensions.
If failed, return "".
*/
std::string getImagePath(std::string path)
// Find out the full path of an image by trying different filename extensions.
// If failed, return "".
std::string getImagePath(std::string_view path)
{
// A NULL-ended list of possible image extensions
const char *extensions[] = { "png", "jpg", "bmp", "tga", NULL };
// If there is no extension, assume PNG
if (removeStringEnd(path, extensions).empty())
path = path + ".png";
// Check paths until something is found to exist
const char **ext = extensions;
do{
bool r = replace_ext(path, *ext);
if (!r)
return "";
if (fs::PathExists(path))
return path;
// (In newer C++ versions a fixed size array and ranges::subrange could be used
// or just modernise removeStringEnd.)
static const char *extensions[] = {".png", ".jpg", ".bmp", ".tga", nullptr};
// Remove present extension
std::string_view stripped_path = removeStringEnd(path, extensions);
// If there is no known extension, assume it has been omitted.
if (stripped_path.empty())
stripped_path = path;
for (const char *ext : extensions) {
if (!ext)
break;
std::string extended_path(stripped_path);
extended_path.append(ext);
if (fs::PathExists(extended_path))
return extended_path;
}
while((++ext) != NULL);
return "";
}
/*
Gets the path to a texture by first checking if the texture exists
in texture_path and if not, using the data path.
Checks all supported extensions by replacing the original extension.
If not found, returns "".
Utilizes a thread-safe cache.
/* Gets the path to a texture by first checking if the texture exists
* in texture_path and if not, using the data path.
*
* Checks all supported extensions by replacing the original extension.
*
* If not found, returns "".
*
* Utilizes a thread-safe cache.
*/
std::string getTexturePath(const std::string &filename, bool *is_base_pack)
{
@ -103,35 +77,26 @@ std::string getTexturePath(const std::string &filename, bool *is_base_pack)
// is_base_pack is only passed when initializing the textures the first time
if (is_base_pack)
*is_base_pack = false;
/*
Check from cache
*/
// Check from cache
bool incache = g_texturename_to_path_cache.get(filename, &fullpath);
if (incache)
return fullpath;
/*
Check from texture_path
*/
// Check from texture_path setting
for (const auto &path : getTextureDirs()) {
std::string testpath = path + DIR_DELIM;
testpath.append(filename);
// Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath);
fullpath = getImagePath(path + DIR_DELIM + filename);
if (!fullpath.empty())
break;
}
/*
Check from default data directory
*/
if (fullpath.empty())
{
// Check from default data directory i.e. .../minetest/textures/base/pack
if (fullpath.empty()) {
std::string base_path = porting::path_share + DIR_DELIM + "textures"
+ DIR_DELIM + "base" + DIR_DELIM + "pack";
std::string testpath = base_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath);
fullpath = getImagePath(base_path + DIR_DELIM + filename);
if (is_base_pack && !fullpath.empty())
*is_base_pack = true;
}
@ -143,11 +108,6 @@ std::string getTexturePath(const std::string &filename, bool *is_base_pack)
return fullpath;
}
void clearTextureNameCache()
{
g_texturename_to_path_cache.clear();
}
std::vector<std::string> getTextureDirs()
{
return fs::GetRecursiveDirs(g_settings->get("texture_path"));