Files
test2/source/blender/blenkernel/intern/asset_weak_reference.cc
Julian Eisel d90795bc3c Asset System: New "weak" asset reference for storing in .blend files
No user visible changes expected.

For brush assets, we need a way to store a reference to a brush in .blend files, so that the last active brush can be restored from the file. See #101908. It seems like a generally useful thing to have.

Adds a new DNA struct to store a "weak" asset reference, that is, a reference that can break under a number of circumstances, but should work reliably enough under normal usage. There's no way to reliably reference an asset currently, so this works on a "best effort" basis. It can break when assets are moved inside the asset library, asset libraries are unregistered from the Preferences, or a file is opened on a different machine with different Preferences, for example. It can also break currently if an asset library is renamed.
It contains:
- Information to identify the asset library the asset can be found in.
- A relative "identifier" (currently a relative path) for the asset within the asset library.

There's further code to resolve a weak reference to file paths and Blender library paths.

Part of #101908.

Co-authored-by: Bastien Montagne <bastien@blender.org>

Pull Request: https://projects.blender.org/blender/blender/pulls/105603
2023-03-30 12:25:42 +02:00

96 lines
2.8 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup bke
*/
#include <memory>
#include "AS_asset_identifier.hh"
#include "AS_asset_library.hh"
#include "BKE_asset.h"
#include "BLO_read_write.h"
#include "DNA_asset_types.h"
#include "MEM_guardedalloc.h"
using namespace blender;
/* #AssetWeakReference -------------------------------------------- */
AssetWeakReference::AssetWeakReference()
: asset_library_type(0), asset_library_identifier(nullptr), relative_asset_identifier(nullptr)
{
}
AssetWeakReference::AssetWeakReference(AssetWeakReference &&other)
: asset_library_type(other.asset_library_type),
asset_library_identifier(other.asset_library_identifier),
relative_asset_identifier(other.relative_asset_identifier)
{
other.asset_library_type = 0; /* Not a valid type. */
other.asset_library_identifier = nullptr;
other.relative_asset_identifier = nullptr;
}
AssetWeakReference::~AssetWeakReference()
{
MEM_delete(asset_library_identifier);
MEM_delete(relative_asset_identifier);
}
void BKE_asset_weak_reference_free(AssetWeakReference **weak_ref)
{
MEM_delete(*weak_ref);
*weak_ref = nullptr;
}
AssetWeakReference *BKE_asset_weak_reference_copy(AssetWeakReference *weak_ref)
{
if (weak_ref == nullptr) {
return nullptr;
}
AssetWeakReference *weak_ref_copy = MEM_new<AssetWeakReference>(__func__);
weak_ref_copy->asset_library_type = weak_ref->asset_library_type;
weak_ref_copy->asset_library_identifier = BLI_strdup(weak_ref->asset_library_identifier);
weak_ref_copy->relative_asset_identifier = BLI_strdup(weak_ref->relative_asset_identifier);
return weak_ref_copy;
}
std::unique_ptr<AssetWeakReference> AssetWeakReference::make_reference(
const asset_system::AssetLibrary &library,
const asset_system::AssetIdentifier &asset_identifier)
{
std::unique_ptr weak_ref = std::make_unique<AssetWeakReference>();
weak_ref->asset_library_type = library.library_type();
StringRefNull name = library.name();
if (!name.is_empty()) {
weak_ref->asset_library_identifier = BLI_strdupn(name.c_str(), name.size());
}
StringRefNull relative_identifier = asset_identifier.library_relative_identifier();
weak_ref->relative_asset_identifier = BLI_strdupn(relative_identifier.c_str(),
relative_identifier.size());
return weak_ref;
}
void BKE_asset_weak_reference_write(BlendWriter *writer, const AssetWeakReference *weak_ref)
{
BLO_write_struct(writer, AssetWeakReference, weak_ref);
BLO_write_string(writer, weak_ref->asset_library_identifier);
BLO_write_string(writer, weak_ref->relative_asset_identifier);
}
void BKE_asset_weak_reference_read(BlendDataReader *reader, AssetWeakReference *weak_ref)
{
BLO_read_data_address(reader, &weak_ref->asset_library_identifier);
BLO_read_data_address(reader, &weak_ref->relative_asset_identifier);
}