WIP code for persistent asset library registrering and separated loading #3

Closed
Julian Eisel wants to merge 1 commits from asset-library-register-and-load into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
9 changed files with 257 additions and 3 deletions
Showing only changes of commit 4cfefd1a5c - Show all commits

View File

@ -23,6 +23,7 @@
struct AssetLibrary;
struct IDRemapper;
struct Main;
struct UserDef;
namespace blender::asset_system {
@ -30,6 +31,19 @@ class AssetIdentifier;
class AssetRepresentation;
class AssetStorage;
/**
* Make an asset library available to the asset system, by letting it take ownership.
*/
void register_library(eAssetLibraryType type, std::unique_ptr<AssetLibrary> library);
/**
* Unregister all asset libraries of \a type, returning the unregistered libraries as owning
* pointers. The return value can be discarded to completely destruct the asset libraries.
*/
Vector<std::unique_ptr<AssetLibrary>> unregister_libraries_of_type(eAssetLibraryType type);
void register_builtin_libraries();
void register_userdef_libraries(const UserDef *userdef);
/**
* AssetLibrary provides access to an asset library's data.
*
@ -76,6 +90,10 @@ class AssetLibrary {
bCallbackFuncStore on_save_callback_store_{};
/* TODO temp, just to set up some settings. */
friend void register_builtin_libraries();
friend void register_userdef_libraries(const UserDef *userdef);
public:
/* Controlled by #ED_asset_catalogs_set_save_catalogs_when_file_is_saved,
* for managing the "Save Catalog Changes" in the quit-confirmation dialog box. */
@ -95,7 +113,7 @@ class AssetLibrary {
* \param root_path: If this is an asset library on disk, the top-level directory path.
*/
AssetLibrary(eAssetLibraryType library_type, StringRef name = "", StringRef root_path = "");
~AssetLibrary();
virtual ~AssetLibrary();
/**
* Execute \a fn for every asset library that is loaded. The asset library is passed to the
@ -107,6 +125,8 @@ class AssetLibrary {
*/
static void foreach_loaded(FunctionRef<void(AssetLibrary &)> fn, bool include_all_library);
virtual void load(const Main &bmain) = 0;
void load_catalogs();
/** Load catalogs that have changed on disk. */

View File

@ -12,6 +12,11 @@
namespace blender::asset_system {
class EssentialsAssetLibrary : public AssetLibrary {
public:
void load() override;
};
StringRefNull essentials_directory_path();
}
} // namespace blender::asset_system

View File

@ -15,13 +15,16 @@ set(SRC
intern/asset_catalog.cc
intern/asset_catalog_path.cc
intern/asset_catalog_tree.cc
intern/asset_essentials_library.cc
intern/asset_identifier.cc
intern/asset_library.cc
intern/asset_library_registry.cc
intern/asset_library_service.cc
intern/asset_representation.cc
intern/asset_storage.cc
intern/utils.cc
intern/asset_libraries/all_library.cc
intern/asset_libraries/essentials_library.cc
intern/asset_libraries/current_file_library.cc
AS_asset_catalog.hh
AS_asset_catalog_path.hh
@ -33,6 +36,8 @@ set(SRC
intern/asset_library_service.hh
intern/asset_storage.hh
intern/utils.hh
intern/asset_libraries/all_library.hh
intern/asset_libraries/current_file_library.hh
AS_asset_library.h
)

View File

@ -0,0 +1,22 @@
/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup asset_system
*/
#include "asset_library_service.hh"
#include "all_library.hh"
namespace blender::asset_system {
AllAssetLibrary::AllAssetLibrary() : AssetLibrary(ASSET_LIBRARY_ALL) {}
void AllAssetLibrary::load(const Main &bmain)
{
AssetLibraryService::get_asset_library_all(&bmain);
}
} // namespace blender::asset_system

View File

@ -0,0 +1,21 @@
/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup asset_system
*/
#pragma once
#include "AS_asset_library.hh"
namespace blender::asset_system {
class AllAssetLibrary : public AssetLibrary {
public:
AllAssetLibrary();
void load(const Main &bmain) override;
};
} // namespace blender::asset_system

View File

@ -0,0 +1,30 @@
/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup asset_system
*/
#include "asset_library_service.hh"
#include "current_file_library.hh"
namespace blender::asset_system {
CurrentFileAssetLibrary::CurrentFileAssetLibrary() : {}
void CurrentFileAssetLibrary::load(const Main &bmain)
{
/* For the "Current File" library we get the asset library root path based on main. */
std::string root_path = bmain ? AS_asset_library_find_suitable_root_path_from_main(bmain) : "";
if (root_path.empty()) {
/* File wasn't saved yet. */
return AssetLibraryService::get_asset_library_current_file();
}
return AssetLibraryService::get_asset_library_on_disk_builtin(ASSET_LIBRARY_LOCAL, root_path);
}
} // namespace blender::asset_system

View File

@ -0,0 +1,22 @@
/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup asset_system
*/
#pragma once
#include "AS_asset_library.hh"
namespace blender::asset_system {
class CurrentFileAssetLibrary : public AssetLibrary {
public:
CurrentFileAssetLibrary();
void load(const Main &bmain) override;
};
} // namespace blender::asset_system

View File

@ -10,10 +10,21 @@
#include "BKE_appdir.h"
#include "AS_asset_library.hh"
#include "asset_library_service.hh"
#include "AS_essentials_library.hh"
namespace blender::asset_system {
void EssentialsAssetLibrary::load(const Main & /*bmain*/) override
{
/* Builtin asset libraries don't need a name, the #eAssetLibraryType is enough to identify them
* (and doesn't change, unlike the name). */
AssetLibraryService::get_asset_library_on_disk(
ASSET_LIBRARY_ESSENTIALS, "", essentials_directory_path());
}
StringRefNull essentials_directory_path()
{
static std::string path = []() {

View File

@ -0,0 +1,118 @@
/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup asset_system
*/
#include <optional>
#include <string>
#include "DNA_userdef_types.h"
#include "BLI_listbase.h"
#include "AS_asset_library.hh"
#include "AS_essentials_library.hh"
#include "asset_libraries/all_library.hh"
#include "asset_libraries/current_file_library.hh"
#include "BLI_map.hh"
#include "BLI_vector.hh"
#include "CLG_log.h"
static CLG_LogRef LOG = {"asset_system"};
namespace blender::asset_system {
class AssetLibraryRegistry {
public:
using LibrariesVec = Vector<std::unique_ptr<AssetLibrary>>;
Map<eAssetLibraryType, LibrariesVec> libraries_by_type;
static AssetLibraryRegistry &get_registry();
};
AssetLibraryRegistry &AssetLibraryRegistry::get_registry()
{
static AssetLibraryRegistry instance;
return instance;
}
void register_library(const eAssetLibraryType type, std::unique_ptr<AssetLibrary> library)
{
AssetLibraryRegistry &registry = AssetLibraryRegistry::get_registry();
registry.libraries_by_type.add_or_modify(
type,
[&](AssetLibraryRegistry::LibrariesVec *libraries) {
*libraries = Vector<std::unique_ptr<AssetLibrary>>{};
libraries->append(std::move(library));
},
[&](AssetLibraryRegistry::LibrariesVec *libraries) {
libraries->append(std::move(library));
});
}
Vector<std::unique_ptr<AssetLibrary>> unregister_libraries_of_type(const eAssetLibraryType type)
{
AssetLibraryRegistry &registry = AssetLibraryRegistry::get_registry();
std::optional<AssetLibraryRegistry::LibrariesVec> libraries = registry.libraries_by_type.pop_try(
type);
if (!libraries) {
CLOG_INFO(&LOG, 2, "No asset libraries registered for this type [%i].", type);
}
return libraries;
}
void register_builtin_libraries()
{
register_library(ASSET_LIBRARY_ALL, std::make_unique<AssetLibrary>(ASSET_LIBRARY_ALL));
register_library(ASSET_LIBRARY_LOCAL, std::make_unique<AssetLibrary>(ASSET_LIBRARY_LOCAL));
std::unique_ptr essentials_library = std::make_unique<AssetLibrary>(
ASSET_LIBRARY_ESSENTIALS, "", essentials_directory_path());
essentials_library->import_method_ = ASSET_IMPORT_APPEND_REUSE;
register_library(ASSET_LIBRARY_ESSENTIALS, std::move(essentials_library));
}
void unregister_all_libraries()
{
AssetLibraryRegistry &registry = AssetLibraryRegistry::get_registry();
registry.libraries_by_type.clear_and_shrink();
}
void register_userdef_libraries(const UserDef *userdef)
{
LISTBASE_FOREACH (bUserAssetLibrary *, user_library, &userdef->asset_libraries) {
std::unique_ptr library = std::make_unique<AssetLibrary>(
ASSET_LIBRARY_CUSTOM, user_library->name, user_library->dirpath);
library->import_method_ = eAssetImportMethod(user_library->import_method);
library->may_override_import_method_ = true;
library->use_relative_path_ = (user_library->flag & ASSET_LIBRARY_RELATIVE_PATH) != 0;
register_library(ASSET_LIBRARY_CUSTOM, std::move(library));
}
}
void update_registered_userdef_libraries(const UserDef *userdef)
{
AssetLibraryRegistry &registry = AssetLibraryRegistry::get_registry();
Map<StringRef, int> registered_libraries_to_index;
AssetLibraryRegistry::LibrariesVec &user_libraries = registry.libraries_by_type.lookup_default(
ASSET_LIBRARY_CUSTOM, {});
for (std::unique_ptr<AssetLibrary> &user_library : user_libraries) {
// registered_libraries_to_index.add(user_library.)
}
LISTBASE_FOREACH (bUserAssetLibrary *, user_library, &userdef->asset_libraries) {
}
}
} // namespace blender::asset_system