49namespace ed::asset::index {
133 return hasher(file_path_);
145 return file_path_.c_str();
156 char idcode_prefix[2];
158 *(
reinterpret_cast<short *
>(idcode_prefix)) = idcode;
159 std::string name_with_idcode = std::string(idcode_prefix,
sizeof(idcode_prefix)) + name;
175 if (
const char *description = asset_data.
description) {
178 if (
const char *author = asset_data.
author) {
181 if (
const char *copyright = asset_data.
copyright) {
184 if (
const char *license = asset_data.
license) {
188 if (!asset_data.
tags.is_empty()) {
196 const char *identifier =
nullptr;
214 auto entries = std::make_shared<ArrayValue>();
228 if (entries->elements().is_empty()) {
252 if (
const std::optional<StringRefNull> catalog_name = entry.
lookup_str(
258 if (
const std::optional<StringRefNull> catalog_id = entry.
lookup_str(
265 for (
const std::shared_ptr<Value> &item : array_value->elements()) {
270 if (
const std::optional<StringRefNull> import_method_identifier = entry.
lookup_str(
273 int preferred_import_method = 0;
275 import_method_identifier->c_str(),
276 &preferred_import_method))
290 for (
IDProperty *property = properties;
property !=
nullptr;) {
323 if (entries ==
nullptr) {
327 int num_entries_read = 0;
333 num_entries_read += 1;
336 return num_entries_read;
382 return this->library_path;
395 BLI_path_append(index_path,
sizeof(index_path),
"asset-library-indices");
397 std::stringstream ss;
398 ss << std::setfill(
'0') << std::setw(16) << std::hex <<
hash() <<
SEP_STR;
401 this->indices_base_path = std::string(index_path);
411 std::stringstream ss;
412 ss << this->indices_base_path;
413 ss << std::setfill(
'0') << std::setw(16) << std::hex << asset_file.
hash() <<
"_"
424 const char *index_path = this->indices_base_path.c_str();
430 for (
int i = 0; i < dir_entries_num; i++) {
433 this->preexisting_file_indices.add_as(std::string(entry->
path));
456 this->preexisting_file_indices.remove(
filename);
471 int num_files_deleted = 0;
475 for (
auto preexisting_index : this->preexisting_file_indices.items()) {
476 if (preexisting_index.value.is_used) {
480 const std::string &file_path = preexisting_index.key;
481 CLOG_DEBUG(&
LOG,
"Remove unused index file \"%s\".", file_path.c_str());
482 files_to_remove.
add(preexisting_index.key);
485 for (
StringRef file_to_remove : files_to_remove) {
491 return num_files_deleted;
535 std::unique_ptr<DictionaryValue> root = std::make_unique<DictionaryValue>();
539 this->contents = std::move(root);
551 if (root ==
nullptr) {
572 return num_entries_read;
627 is.open(this->filename);
630 std::unique_ptr<Value> read_data = formatter.
deserialize(is);
635 return std::make_unique<AssetIndex>(read_data);
652 os.open(this->filename, std::ios::out | std::ios::trunc);
678 tm_from.tm_year = 2022 - 1900;
679 tm_from.tm_mon = 11 - 1;
682 tm_from.tm_year = 2022 - 1900;
683 tm_from.tm_mon = 12 - 1;
685 std::time_t timestamp_from = std::mktime(&tm_from);
686 std::time_t timestamp_to = std::mktime(&tm_to);
691 if (
IN_RANGE(stat.st_mtime, timestamp_from, timestamp_to)) {
692 CLOG_DEBUG(&
LOG,
"Remove potentially broken index file \"%s\".", index_path.c_str());
693 files_to_remove.
add(index_path);
697 int num_files_deleted = 0;
698 for (
StringRef filepath : files_to_remove) {
704 return num_files_deleted;
709 int *r_read_entries_len,
716 if (!asset_index_file.
exists()) {
729 "Asset index file \"%s\" needs to be refreshed as it is older than the asset file \"%s\".",
737 "Asset file index is to small to contain any entries. \"%s\"",
739 *r_read_entries_len = 0;
743 std::unique_ptr<AssetIndex> contents = asset_index_file.
read_contents();
745 CLOG_DEBUG(&
LOG,
"Asset file index is ignored; failed to read contents.");
749 if (!contents->is_latest_version()) {
751 "Asset file index is ignored; expected version %d but file is version %d \"%s\".",
753 contents->get_version(),
759 const int read_entries_len = contents->extract_into(*entries);
761 *r_read_entries_len = read_entries_len;
773 "Update for \"%s\" store index in \"%s\".",
781static void *
init_user_data(
const char *root_directory,
size_t root_directory_maxncpy)
787 return library_index;
799 if (num_indices_removed > 0) {
800 CLOG_INFO(&
LOG,
"Removed %d unused indices.", num_indices_removed);
File and directory operations.
#define BLI_SCOPED_DEFER(function_to_defer)
#define STRNCPY_UTF8(dst, src)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_DEBUG(clg_ref,...)
#define CLOG_INFO(clg_ref,...)
ID and Library types, which are fundamental for SDNA.
#define IN_RANGE(a, b, c)
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
virtual ~AbstractFile()=default
size_t get_file_size() const
virtual const char * get_file_path() const =0
AssetLibraryIndex & library_index
bool ensure_parent_path_exists() const
bool constains_entries() const
const char * get_file_path() const override
AssetIndexFile(AssetLibraryIndex &library_index, BlendFile &asset_filename)
std::unique_ptr< AssetIndex > read_contents() const
bool is_older_than(const BlendFile &asset_file) const
void write_contents(AssetIndex &content)
const size_t MIN_FILE_SIZE_WITH_ENTRIES
AssetIndexFile(AssetLibraryIndex &library_index, StringRef index_file_path)
Reference to a blend file that can be indexed.
BlendFile(StringRefNull file_path)
const char * get_file_path() const override
std::string get_filename() const
Span< std::shared_ptr< Value > > elements() const
void append_str(std::string value)
const std::shared_ptr< Value > * lookup(StringRef key) const
std::optional< StringRefNull > lookup_str(StringRef key) const
std::optional< int64_t > lookup_int(StringRef key) const
const ArrayValue * lookup_array(StringRef key) const
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
IDProperty * convert_from_serialize_value(const io::serialize::Value &value)
Convert the given value (which must be an array) to an IDProperty.
std::unique_ptr< io::serialize::ArrayValue > convert_to_serialize_values(const IDProperty *properties)
Convert the given properties to Value objects for serialization.
static void free_user_data(void *user_data)
constexpr StringRef ATTRIBUTE_ENTRIES_CATALOG_NAME("catalog_name")
constexpr StringRef ATTRIBUTE_ENTRIES_TAGS("tags")
constexpr StringRef ATTRIBUTE_ENTRIES_NAME("name")
static void * init_user_data(const char *root_directory, size_t root_directory_maxncpy)
constexpr StringRef ATTRIBUTE_ENTRIES_CATALOG_ID("catalog_id")
static int init_indexer_entries_from_value(FileIndexerEntries &indexer_entries, const DictionaryValue &value)
constexpr StringRef ATTRIBUTE_ENTRIES_DESCRIPTION("description")
constexpr StringRef ATTRIBUTE_ENTRIES_PROPERTIES("properties")
const FileIndexerType file_indexer_asset
constexpr StringRef ATTRIBUTE_ENTRIES_PREFERRED_IMPORT_METHOD("preferred_import_method")
AssetMetaData * asset_metadata_from_dictionary(const io::serialize::DictionaryValue &entry)
static void filelist_finished(void *user_data)
static eFileIndexerResult read_index(const char *filename, FileIndexerEntries *entries, int *r_read_entries_len, void *user_data)
constexpr StringRef ATTRIBUTE_ENTRIES_LICENSE("license")
constexpr StringRef ATTRIBUTE_VERSION("version")
Indexer for asset libraries.
static void init_indexer_entry_from_value(FileIndexerEntry &indexer_entry, const DictionaryValue &entry)
static void init_value_from_file_indexer_entry(DictionaryValue &result, const FileIndexerEntry *indexer_entry)
constexpr StringRef ATTRIBUTE_ENTRIES("entries")
static void add_id_name(DictionaryValue &result, const short idcode, const StringRefNull name)
add id + name to the attributes.
static void init_value_from_file_indexer_entries(DictionaryValue &result, const FileIndexerEntries &indexer_entries)
constexpr StringRef ATTRIBUTE_ENTRIES_COPYRIGHT("copyright")
constexpr StringRef ATTRIBUTE_ENTRIES_AUTHOR("author")
constexpr FileIndexerType asset_indexer()
static void update_index(const char *filename, FileIndexerEntries *entries, void *user_data)
std::string filename(const std::string_view &str, size_t pos)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
const EnumPropertyItem rna_enum_asset_import_method_items[]
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
int bool bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL(1
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
size_t BLI_path_append(char *__restrict dst, size_t dst_maxncpy, const char *__restrict file) ATTR_NONNULL(1
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void void BLI_linklist_prepend(LinkNode **listp, void *ptr) ATTR_NONNULL(1)
AssetTag * BKE_asset_metadata_tag_add(AssetMetaData *asset_data, const char *name) ATTR_NONNULL(1
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
int char char int int int int size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
@ ASSETDATA_USE_OWN_IMPORT_METHOD
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool RNA_enum_value_from_identifier(const EnumPropertyItem *item, const char *identifier, int *r_value)
AssetMetaData * BKE_asset_metadata_create()
@ FILE_INDEXER_ENTRIES_LOADED
@ FILE_INDEXER_NEEDS_UPDATE
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
void BKE_appdir_folder_caches(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
ID_Type GS(const char *name)
bool BLI_file_older(const char *file1, const char *file2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool RNA_enum_identifier(const EnumPropertyItem *item, const int value, const char **r_identifier)
ATTR_WARN_UNUSED_RESULT const void * element
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
User defined tag. Currently only used by assets, could be used more often at some point....
AssetMetaData * asset_data
BLODataBlockInfo datablock_info
FileIndexerReadIndexFunc read_index
FileIndexerFinishedFunc filelist_finished
FileIndexerUpdateIndexFunc update_index
FileIndexerFreeUserDataFunc free_user_data
FileIndexerInitUserDataFunc init_user_data
AssetIndex(std::unique_ptr< Value > &value)
bool is_latest_version() const
int extract_into(FileIndexerEntries &indexer_entries) const
AssetIndex(const FileIndexerEntries &indexer_entries)
const int UNKNOWN_VERSION
static const int CURRENT_VERSION
Version to store in new index files.
std::unique_ptr< Value > contents
References the asset library directory.
void mark_as_used(const std::string &filename)
int remove_unused_index_files()
int remove_broken_index_files()
AssetLibraryIndex(const StringRef library_path)
std::string index_file_path(const BlendFile &asset_file) const
void collect_preexisting_file_indices()
Map< std::string, PreexistingFileIndexInfo > preexisting_file_indices
bool delete_file_index(const std::string &filename)
void init_indices_base_path()
Initializes AssetLibraryIndex.indices_base_path.
std::string indices_base_path
Absolute path where the indices of library are stored.
StringRefNull get_library_file_path() const