Asset Library Service: make insensitive to trailing slashes
Make `AssetLibraryService::get_asset_library_on_disk(path)` insensitive to trailing slashes; i.e. `get_asset_library_on_disk("/path")` and `get_asset_library_on_disk("/path/¨)` will now return the same `AssetLibrary*`.
This commit is contained in:
parent
a7075a30e2
commit
9a1d75e0b9
|
@ -24,6 +24,7 @@
|
|||
#include "BKE_blender.h"
|
||||
#include "BKE_callbacks.h"
|
||||
|
||||
#include "BLI_path_util.h"
|
||||
#include "BLI_string_ref.hh"
|
||||
|
||||
#include "MEM_guardedalloc.h"
|
||||
|
@ -54,14 +55,27 @@ void AssetLibraryService::destroy()
|
|||
instance_.reset();
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::string normalize_directory_path(StringRefNull directory)
|
||||
{
|
||||
|
||||
char dir_normalized[PATH_MAX];
|
||||
STRNCPY(dir_normalized, directory.c_str());
|
||||
BLI_path_normalize_dir(NULL, dir_normalized);
|
||||
return std::string(dir_normalized);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
AssetLibrary *AssetLibraryService::get_asset_library_on_disk(StringRefNull top_level_directory)
|
||||
{
|
||||
BLI_assert_msg(!top_level_directory.is_empty(),
|
||||
"top level directory must be given for on-disk asset library");
|
||||
|
||||
AssetLibraryPtr *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(top_level_directory);
|
||||
std::string top_dir_trailing_slash = normalize_directory_path(top_level_directory);
|
||||
|
||||
AssetLibraryPtr *lib_uptr_ptr = on_disk_libraries_.lookup_ptr(top_dir_trailing_slash);
|
||||
if (lib_uptr_ptr != nullptr) {
|
||||
CLOG_INFO(&LOG, 2, "get \"%s\" (cached)", top_level_directory.c_str());
|
||||
CLOG_INFO(&LOG, 2, "get \"%s\" (cached)", top_dir_trailing_slash.c_str());
|
||||
return lib_uptr_ptr->get();
|
||||
}
|
||||
|
||||
|
@ -69,10 +83,10 @@ AssetLibrary *AssetLibraryService::get_asset_library_on_disk(StringRefNull top_l
|
|||
AssetLibrary *lib = lib_uptr.get();
|
||||
|
||||
lib->on_save_handler_register();
|
||||
lib->load(top_level_directory);
|
||||
lib->load(top_dir_trailing_slash);
|
||||
|
||||
on_disk_libraries_.add_new(top_level_directory, std::move(lib_uptr));
|
||||
CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", top_level_directory.c_str());
|
||||
on_disk_libraries_.add_new(top_dir_trailing_slash, std::move(lib_uptr));
|
||||
CLOG_INFO(&LOG, 2, "get \"%s\" (loaded)", top_dir_trailing_slash.c_str());
|
||||
return lib;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
#include "asset_library_service.hh"
|
||||
|
||||
#include "BLI_path_util.h"
|
||||
|
||||
#include "CLG_log.h"
|
||||
|
||||
#include "testing/testing.h"
|
||||
|
@ -84,6 +86,30 @@ TEST_F(AssetLibraryServiceTest, library_pointers)
|
|||
* cannot be reliably tested by just pointer comparison, though. */
|
||||
}
|
||||
|
||||
TEST_F(AssetLibraryServiceTest, library_path_trailing_slashes)
|
||||
{
|
||||
AssetLibraryService *service = AssetLibraryService::get();
|
||||
|
||||
char asset_lib_no_slash[PATH_MAX];
|
||||
char asset_lib_with_slash[PATH_MAX];
|
||||
STRNCPY(asset_lib_no_slash, asset_library_root_.c_str());
|
||||
STRNCPY(asset_lib_with_slash, asset_library_root_.c_str());
|
||||
|
||||
/* Ensure #asset_lib_no_slash has no trailing slash, regardless of what was passed on the CLI to
|
||||
* the unit test. */
|
||||
while (strlen(asset_lib_no_slash) &&
|
||||
ELEM(asset_lib_no_slash[strlen(asset_lib_no_slash) - 1], SEP, ALTSEP)) {
|
||||
asset_lib_no_slash[strlen(asset_lib_no_slash) - 1] = '\0';
|
||||
}
|
||||
|
||||
BLI_path_slash_ensure(asset_lib_with_slash);
|
||||
|
||||
AssetLibrary *const lib_no_slash = service->get_asset_library_on_disk(asset_lib_no_slash);
|
||||
|
||||
EXPECT_EQ(lib_no_slash, service->get_asset_library_on_disk(asset_lib_with_slash))
|
||||
<< "With or without trailing slash shouldn't matter.";
|
||||
}
|
||||
|
||||
TEST_F(AssetLibraryServiceTest, catalogs_loaded)
|
||||
{
|
||||
AssetLibraryService *const service = AssetLibraryService::get();
|
||||
|
|
Loading…
Reference in New Issue