Small refactor of some of the RNA diffing API.

Propagate `eRNAOverrideMatchResult` 'return' flags at higher level into
BKE API, instead of just returning a boolean true when new override
rules have been created.

NOTE: This is an intermediary step towards fixing T102766.

Differential Revision: https://developer.blender.org/D16761
This commit is contained in:
Bastien Montagne 2022-12-20 23:36:29 +09:00 committed by Bastien Montagne
parent 4c295276f0
commit fcddb7cda7
Notes: blender-bot 2023-02-14 02:22:13 +01:00
Referenced by commit 09ba00974f, Fix recent liboverride diff report refactor.
8 changed files with 48 additions and 32 deletions

View File

@ -401,6 +401,8 @@ bool BKE_lib_override_library_status_check_reference(struct Main *bmain, struct
* Compare local and reference data-blocks and create new override operations as needed,
* or reset to reference values if overriding is not allowed.
*
* \param r_report_flags #eRNAOverrideMatchResult flags giving info about the result of this call.
*
* \note Defining override operations is only mandatory before saving a `.blend` file on disk
* (not for undo!).
* Knowing that info at runtime is only useful for UI/UX feedback.
@ -411,11 +413,17 @@ bool BKE_lib_override_library_status_check_reference(struct Main *bmain, struct
*
* \return true if any library operation was created.
*/
bool BKE_lib_override_library_operations_create(struct Main *bmain, struct ID *local);
void BKE_lib_override_library_operations_create(struct Main *bmain,
struct ID *local,
int *r_report_flags);
/**
* Check all overrides from given \a bmain and create/update overriding operations as needed.
*
* \param r_report_flags #eRNAOverrideMatchResult flags giving info about the result of this call.
*/
bool BKE_lib_override_library_main_operations_create(struct Main *bmain, bool force_auto);
void BKE_lib_override_library_main_operations_create(struct Main *bmain,
bool force_auto,
int *r_report_flags);
/**
* Reset all overrides in given \a id_root, while preserving ID relations.

View File

@ -435,7 +435,7 @@ static void setup_app_data(bContext *C,
reports->duration.lib_overrides_resync;
/* We need to rebuild some of the deleted override rules (for UI feedback purpose). */
BKE_lib_override_library_main_operations_create(bmain, true);
BKE_lib_override_library_main_operations_create(bmain, true, NULL);
}
}

View File

@ -1424,7 +1424,7 @@ void BKE_blendfile_library_relocate(BlendfileLinkAppendContext *lapp_context,
/* All override rules need to be up to date, since there will be no do_version here, otherwise
* older, now-invalid rules might be applied and likely fail, or some changes might be missing,
* etc. See T93353. */
BKE_lib_override_library_main_operations_create(bmain, true);
BKE_lib_override_library_main_operations_create(bmain, true, NULL);
/* Remove all IDs to be reloaded from Main. */
lba_idx = set_listbasepointers(bmain, lbarray);
@ -1634,7 +1634,7 @@ void BKE_blendfile_library_relocate(BlendfileLinkAppendContext *lapp_context,
.reports = reports,
});
/* We need to rebuild some of the deleted override rules (for UI feedback purpose). */
BKE_lib_override_library_main_operations_create(bmain, true);
BKE_lib_override_library_main_operations_create(bmain, true, NULL);
}
BKE_main_collection_sync(bmain);

View File

@ -1427,7 +1427,7 @@ bool BKE_lib_override_library_create(Main *bmain,
BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
/* We need to rebuild some of the deleted override rules (for UI feedback purpose). */
BKE_lib_override_library_main_operations_create(bmain, true);
BKE_lib_override_library_main_operations_create(bmain, true, nullptr);
return success;
}
@ -3297,19 +3297,18 @@ bool BKE_lib_override_library_status_check_reference(Main *bmain, ID *local)
return true;
}
bool BKE_lib_override_library_operations_create(Main *bmain, ID *local)
void BKE_lib_override_library_operations_create(Main *bmain, ID *local, int *r_report_flags)
{
BLI_assert(!ID_IS_LINKED(local));
BLI_assert(local->override_library != nullptr);
const bool is_template = (local->override_library->reference == nullptr);
bool created = false;
if (!is_template) {
/* Do not attempt to generate overriding rules from an empty place-holder generated by link
* code when it cannot find the actual library/ID. Much better to keep the local data-block as
* is in the file in that case, until broken lib is fixed. */
if (ID_MISSING(local->override_library->reference)) {
return created;
return;
}
if (GS(local->name) == ID_OB) {
@ -3330,7 +3329,7 @@ bool BKE_lib_override_library_operations_create(Main *bmain, ID *local)
RNA_id_pointer_create(local, &rnaptr_local);
RNA_id_pointer_create(local->override_library->reference, &rnaptr_reference);
eRNAOverrideMatchResult report_flags = (eRNAOverrideMatchResult)0;
eRNAOverrideMatchResult local_report_flags = RNA_OVERRIDE_MATCH_RESULT_INIT;
RNA_struct_override_matches(
bmain,
&rnaptr_local,
@ -3339,28 +3338,27 @@ bool BKE_lib_override_library_operations_create(Main *bmain, ID *local)
0,
local->override_library,
(eRNAOverrideMatch)(RNA_OVERRIDE_COMPARE_CREATE | RNA_OVERRIDE_COMPARE_RESTORE),
&report_flags);
&local_report_flags);
if (report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
created = true;
}
if (report_flags & RNA_OVERRIDE_MATCH_RESULT_RESTORED) {
if (local_report_flags & RNA_OVERRIDE_MATCH_RESULT_RESTORED) {
CLOG_INFO(&LOG, 2, "We did restore some properties of %s from its reference", local->name);
}
if (report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
if (local_report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
CLOG_INFO(&LOG, 2, "We did generate library override rules for %s", local->name);
}
else {
CLOG_INFO(&LOG, 2, "No new library override rules for %s", local->name);
}
if (r_report_flags != nullptr) {
*r_report_flags |= local_report_flags;
}
}
return created;
}
struct LibOverrideOpCreateData {
Main *bmain;
bool changed;
eRNAOverrideMatchResult report_flags;
};
static void lib_override_library_operations_create_cb(TaskPool *__restrict pool, void *taskdata)
@ -3369,14 +3367,16 @@ static void lib_override_library_operations_create_cb(TaskPool *__restrict pool,
BLI_task_pool_user_data(pool));
ID *id = static_cast<ID *>(taskdata);
if (BKE_lib_override_library_operations_create(create_data->bmain, id)) {
/* Technically no need for atomic, all jobs write the same value and we only care if one did
* it. But play safe and avoid implicit assumptions. */
atomic_fetch_and_or_uint8(reinterpret_cast<uint8_t *>(&create_data->changed), true);
}
eRNAOverrideMatchResult report_flags = RNA_OVERRIDE_MATCH_RESULT_INIT;
BKE_lib_override_library_operations_create(
create_data->bmain, id, reinterpret_cast<int *>(&report_flags));
atomic_fetch_and_or_uint32(reinterpret_cast<uint32_t *>(&create_data->report_flags),
report_flags);
}
bool BKE_lib_override_library_main_operations_create(Main *bmain, const bool force_auto)
void BKE_lib_override_library_main_operations_create(Main *bmain,
const bool force_auto,
int *r_report_flags)
{
ID *id;
@ -3403,7 +3403,7 @@ bool BKE_lib_override_library_main_operations_create(Main *bmain, const bool for
LibOverrideOpCreateData create_pool_data{};
create_pool_data.bmain = bmain;
create_pool_data.changed = false;
create_pool_data.report_flags = RNA_OVERRIDE_MATCH_RESULT_INIT;
TaskPool *task_pool = BLI_task_pool_create(&create_pool_data, TASK_PRIORITY_HIGH);
FOREACH_MAIN_ID_BEGIN (bmain, id) {
@ -3443,6 +3443,10 @@ bool BKE_lib_override_library_main_operations_create(Main *bmain, const bool for
BLI_task_pool_free(task_pool);
if (r_report_flags != nullptr) {
*r_report_flags |= create_pool_data.report_flags;
}
if (force_auto) {
BKE_lib_override_library_main_unused_cleanup(bmain);
}
@ -3450,8 +3454,6 @@ bool BKE_lib_override_library_main_operations_create(Main *bmain, const bool for
#ifdef DEBUG_OVERRIDE_TIMEIT
TIMEIT_END_AVERAGED(BKE_lib_override_library_main_operations_create);
#endif
return create_pool_data.changed;
}
static bool lib_override_library_id_reset_do(Main *bmain,
@ -3906,7 +3908,7 @@ ID *BKE_lib_override_library_operations_store_start(Main *bmain,
UNUSED_VARS_NDEBUG(override_storage);
/* Forcefully ensure we know about all needed override operations. */
BKE_lib_override_library_operations_create(bmain, local);
BKE_lib_override_library_operations_create(bmain, local, nullptr);
ID *storage_id;
#ifdef DEBUG_OVERRIDE_TIMEIT

View File

@ -27,6 +27,8 @@
#include "BKE_main.h"
#include "BKE_undo_system.h"
#include "RNA_access.h"
#include "MEM_guardedalloc.h"
#define undo_stack _wm_undo_stack_disallow /* pass in as a variable always. */
@ -494,7 +496,9 @@ eUndoPushReturn BKE_undosys_step_push_with_type(UndoStack *ustack,
/* Might not be final place for this to be called - probably only want to call it from some
* undo handlers, not all of them? */
if (BKE_lib_override_library_main_operations_create(G_MAIN, false)) {
eRNAOverrideMatchResult report_flags = RNA_OVERRIDE_MATCH_RESULT_INIT;
BKE_lib_override_library_main_operations_create(G_MAIN, false, (int *)&report_flags);
if (report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
retval |= UNDO_PUSH_RET_OVERRIDE_CHANGED;
}

View File

@ -803,6 +803,8 @@ typedef enum eRNAOverrideMatch {
} eRNAOverrideMatch;
typedef enum eRNAOverrideMatchResult {
RNA_OVERRIDE_MATCH_RESULT_INIT = 0,
/**
* Some new property overrides were created to take into account
* differences between local and reference.

View File

@ -821,7 +821,7 @@ static void rna_ID_override_library_operations_update(ID *id,
return;
}
BKE_lib_override_library_operations_create(bmain, id);
BKE_lib_override_library_operations_create(bmain, id, NULL);
WM_main_add_notifier(NC_WM | ND_LIB_OVERRIDE_CHANGED, NULL);
}

View File

@ -1786,7 +1786,7 @@ static bool wm_file_write(bContext *C,
ED_assets_pre_save(bmain);
/* Enforce full override check/generation on file save. */
BKE_lib_override_library_main_operations_create(bmain, true);
BKE_lib_override_library_main_operations_create(bmain, true, NULL);
/* NOTE: Ideally we would call `WM_redraw_windows` here to remove any open menus.
* But we can crash if saving from a script, see T92704 & T97627.