Blender V5.3
undo_system.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdio>
12#include <cstring>
13
14#include "CLG_log.h"
15
16#include "BLI_listbase.hh"
17#include "BLI_string.hh"
18#include "BLI_sys_types.hh"
19#include "BLI_utildefines.hh"
20
21#include "BLT_translation.hh"
22
23#include "DNA_listBase.h"
25
26#include "BKE_context.hh"
27#include "BKE_global.hh"
28#include "BKE_lib_id.hh"
29#include "BKE_lib_override.hh"
30#include "BKE_library.hh"
31#include "BKE_main.hh"
32#include "BKE_undo_system.hh"
33
34#include "RNA_access.hh"
35
36#include "MEM_guardedalloc.h"
37
38/* Header to pull symbols from the file which otherwise might get stripped away. */
39#include "BKE_blender_undo.hh"
40
41namespace blender {
42
43#define undo_stack _wm_undo_stack_disallow /* pass in as a variable always. */
44
46#define WITH_GLOBAL_UNDO_KEEP_ONE
47
49#define WITH_GLOBAL_UNDO_ENSURE_UPDATED
50
55#define WITH_GLOBAL_UNDO_CORRECT_ORDER
56
58static CLG_LogRef LOG = {"undo"};
59
60/* -------------------------------------------------------------------- */
63
70
71static ListBaseT<UndoType> g_undo_types = {nullptr, nullptr};
72
73/* An unused function with public linkage just to ensure symbols from the blender_undo.cc are not
74 * stripped. */
81
83{
84 for (const UndoType &ut : g_undo_types) {
85 /* No poll means we don't check context. */
86 if (ut.poll && ut.poll(C)) {
87 return &ut;
88 }
89 }
90 return nullptr;
91}
92
94
95/* -------------------------------------------------------------------- */
102
103#define WITH_NESTED_UNDO_CHECK
104
105#ifdef WITH_NESTED_UNDO_CHECK
106static bool g_undo_callback_running = false;
107# define UNDO_NESTED_ASSERT(state) BLI_assert(g_undo_callback_running == state)
108# define UNDO_NESTED_CHECK_BEGIN \
109 { \
110 UNDO_NESTED_ASSERT(false); \
111 g_undo_callback_running = true; \
112 } \
113 ((void)0)
114# define UNDO_NESTED_CHECK_END \
115 { \
116 UNDO_NESTED_ASSERT(true); \
117 g_undo_callback_running = false; \
118 } \
119 ((void)0)
120#else
121# define UNDO_NESTED_ASSERT(state) ((void)0)
122# define UNDO_NESTED_CHECK_BEGIN ((void)0)
123# define UNDO_NESTED_CHECK_END ((void)0)
124#endif
125
127
128/* -------------------------------------------------------------------- */
135
136static void undosys_id_ref_store(void * /*user_data*/, UndoRefID *id_ref)
137{
138 BLI_assert(id_ref->name[0] == '\0');
139 if (id_ref->ptr) {
140 STRNCPY(id_ref->name, id_ref->ptr->name);
141 if (id_ref->ptr->lib) {
143 }
144 else {
145 id_ref->library_filepath_abs[0] = '\0';
146 }
147 /* Not needed, just prevents stale data access. */
148 id_ref->ptr = nullptr;
149 }
150}
151
152static void undosys_id_ref_resolve(void *user_data, UndoRefID *id_ref)
153{
154 /* NOTE: we could optimize this,
155 * for now it's not too bad since it only runs when we access undo! */
156 Main *bmain = static_cast<Main *>(user_data);
158 bmain,
159 GS(id_ref->name),
160 id_ref->name + 2,
161 (id_ref->library_filepath_abs[0] ? id_ref->library_filepath_abs : nullptr));
162}
163
164static bool undosys_step_encode(bContext *C, Main *bmain, UndoStack *ustack, UndoStep *us)
165{
166 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->identifier);
168 bool ok = us->type->step_encode(C, bmain, us);
170 if (ok) {
171 if (us->type->step_foreach_ID_ref != nullptr) {
172 /* Don't use from context yet because sometimes context is fake and
173 * not all members are filled in. */
175 }
176
177#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
178 if (us->type == BKE_UNDOSYS_TYPE_MEMFILE) {
179 ustack->step_active_memfile = us;
180 }
181#endif
182 }
183 if (ok == false) {
184 CLOG_DEBUG(&LOG, "encode callback didn't create undo step");
185 }
186 return ok;
187}
188
190 Main *bmain,
191 UndoStack *ustack,
192 UndoStep *us,
193 const eUndoStepDir dir,
194 bool is_final)
195{
196 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->identifier);
197
198 if (us->type->step_foreach_ID_ref) {
199#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
200 if (us->type != BKE_UNDOSYS_TYPE_MEMFILE) {
201 for (UndoStep *us_iter = us->prev; us_iter; us_iter = us_iter->prev) {
202 if (us_iter->type == BKE_UNDOSYS_TYPE_MEMFILE) {
203 if (us_iter == ustack->step_active_memfile) {
204 /* Common case, we're already using the last memfile state. */
205 }
206 else {
207 /* Load the previous memfile state so any ID's referenced in this
208 * undo step will be correctly resolved, see: #56163. */
209 undosys_step_decode(C, bmain, ustack, us_iter, dir, false);
210 /* May have been freed on memfile read. */
211 bmain = G_MAIN;
212 }
213 break;
214 }
215 }
216 }
217#endif
218 /* Don't use from context yet because sometimes context is fake and
219 * not all members are filled in. */
221 }
222
224 us->type->step_decode(C, bmain, us, dir, is_final);
226
227#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
228 if (us->type == BKE_UNDOSYS_TYPE_MEMFILE) {
229 ustack->step_active_memfile = us;
230 }
231#endif
232}
233
235{
236 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->identifier);
238 us->type->step_free(us);
240
241 BLI_remlink(&ustack->steps, us);
242
243#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
244 if (ustack->step_active_memfile == us) {
245 ustack->step_active_memfile = nullptr;
246 /* The memfile holding the current state of main is gone, so the next push must write a new
247 * one, see #WITH_GLOBAL_UNDO_ENSURE_UPDATED. */
248 if (G_MAIN) {
249 G_MAIN->is_memfile_undo_written = false;
250 }
251 }
252#endif
253
254 MEM_delete(us);
255}
256
258
259/* -------------------------------------------------------------------- */
262
263#ifndef NDEBUG
264static void undosys_stack_validate(UndoStack *ustack, bool expect_non_empty)
265{
266 if (ustack->step_active != nullptr) {
267 BLI_assert(!ustack->steps.is_empty());
268 BLI_assert(BLI_findindex(&ustack->steps, ustack->step_active) != -1);
269 }
270 if (expect_non_empty) {
271 BLI_assert(!ustack->steps.is_empty());
272 }
273}
274#else
275static void undosys_stack_validate(UndoStack * /*ustack*/, bool /*expect_non_empty*/) {}
276#endif
277
279{
280 UndoStack *ustack = MEM_new_zeroed<UndoStack>(__func__);
281 return ustack;
282}
283
285{
287 MEM_delete(ustack);
288}
289
291{
292 UNDO_NESTED_ASSERT(false);
293 CLOG_DEBUG(&LOG, "steps=%d", ustack->steps.count());
294 for (UndoStep *us = ustack->steps.last(), *us_prev; us; us = us_prev) {
295 us_prev = us->prev;
297 }
298 if (UndoStep *us = ustack->step_init) {
300 ustack->step_init = nullptr;
301 }
302 ustack->steps.clear_no_delete();
303 ustack->step_active = nullptr;
304}
305
307{
308 /* Remove active and all following undo-steps.
309 * Rewind over any "skip" steps leading up to the active as these must never be made active. */
310 if (ustack->step_active) {
311 UndoStep *us = ustack->step_active->prev;
312 while (us && us->skip) {
313 us = us->prev;
314 }
315 ustack->step_active = us;
316
317 bool is_not_empty = ustack->step_active != nullptr;
318
319 while (ustack->steps.last() != ustack->step_active) {
320 UndoStep *us_iter = ustack->steps.last();
321 undosys_step_free_and_unlink(ustack, us_iter);
322 undosys_stack_validate(ustack, is_not_empty);
323 }
324 }
325}
326
327/* Caller is responsible for handling active. */
329{
330 if (us) {
331 bool is_not_empty = true;
332 UndoStep *us_iter;
333 do {
334 us_iter = ustack->steps.last();
335 BLI_assert(us_iter != ustack->step_active);
336 undosys_step_free_and_unlink(ustack, us_iter);
337 undosys_stack_validate(ustack, is_not_empty);
338 } while (us != us_iter);
339 }
340}
341
342static void undosys_stack_clear_all_first(UndoStack *ustack, UndoStep *us, UndoStep *us_exclude)
343{
344 if (us && us == us_exclude) {
345 us = us->prev;
346 }
347
348 if (us) {
349 bool is_not_empty = true;
350 UndoStep *us_iter;
351 do {
352 us_iter = ustack->steps.first();
353 if (us_iter == us_exclude) {
354 us_iter = us_iter->next;
355 }
356 BLI_assert(us_iter != ustack->step_active);
357 undosys_step_free_and_unlink(ustack, us_iter);
358 undosys_stack_validate(ustack, is_not_empty);
359 } while (us != us_iter);
360 }
361}
362
363static bool undosys_stack_push_main(UndoStack *ustack, const char *name, Main *bmain)
364{
365 UNDO_NESTED_ASSERT(false);
366 BLI_assert(ustack->step_init == nullptr);
367 CLOG_DEBUG(&LOG, "Push main '%s'", name);
368 bContext *C_temp = CTX_create();
369 CTX_data_main_set(C_temp, bmain);
371 ustack, C_temp, name, UndoEncodeHints::None, BKE_UNDOSYS_TYPE_MEMFILE);
372 CTX_free(C_temp);
373 return (ret & UNDO_PUSH_RET_SUCCESS);
374}
375
377{
378 UNDO_NESTED_ASSERT(false);
379 undosys_stack_push_main(ustack, IFACE_("Original"), bmain);
380}
381
383{
385 if (!ELEM(ut, nullptr, BKE_UNDOSYS_TYPE_MEMFILE)) {
386 BKE_undosys_step_push_with_type(ustack, C, IFACE_("Original Mode"), UndoEncodeHints::None, ut);
387 }
388}
389
390bool BKE_undosys_stack_has_undo(const UndoStack *ustack, const char *name)
391{
392 if (name) {
393 const UndoStep *us = static_cast<UndoStep *>(
394 BLI_rfindstring(&ustack->steps, name, offsetof(UndoStep, name)));
395 return us && us->prev;
396 }
397
398 return !ustack->steps.is_empty();
399}
400
402{
403 if (!ustack->step_active) {
404 return false;
405 }
406 return ustack->step_active->next != nullptr;
407}
408
410{
411 UndoStep *us = ustack->step_active;
412 while (us && (us->type != ut)) {
413 us = us->prev;
414 }
415 return us;
416}
417
419{
420 UNDO_NESTED_ASSERT(false);
421 if (ustack->step_init && (ustack->step_init->type == ut)) {
422 return ustack->step_init;
423 }
424 return BKE_undosys_stack_active_with_type(ustack, ut);
425}
426
427void BKE_undosys_stack_limit_steps_and_memory(UndoStack *ustack, int steps, size_t memory_limit)
428{
429 UNDO_NESTED_ASSERT(false);
430 if ((steps == -1) && (memory_limit == 0)) {
431 return;
432 }
433
434 CLOG_DEBUG(&LOG, "Limit steps=%d, memory_limit=%zu", steps, memory_limit);
435 UndoStep *us;
436 UndoStep *us_exclude = nullptr;
437 /* keep at least two (original + other) */
438 size_t data_size_all = 0;
439 size_t us_count = 0;
440 for (us = ustack->steps.last(); us && us->prev; us = us->prev) {
441 if (memory_limit) {
442 data_size_all += us->data_size;
443 if (data_size_all > memory_limit) {
445 "At step %zu: data_size_all=%zu >= memory_limit=%zu",
446 us_count,
447 data_size_all,
448 memory_limit);
449 break;
450 }
451 }
452 if (steps != -1) {
453 if (us_count == steps) {
454 break;
455 }
456 if (us->skip == false) {
457 us_count += 1;
458 }
459 }
460 }
461
462 CLOG_DEBUG(&LOG, "Total steps %zu: data_size_all=%zu", us_count, data_size_all);
463
464 if (us) {
465#ifdef WITH_GLOBAL_UNDO_KEEP_ONE
466 /* Hack, we need to keep at least one BKE_UNDOSYS_TYPE_MEMFILE. */
467 if (us->type != BKE_UNDOSYS_TYPE_MEMFILE) {
468 us_exclude = us->prev;
469 while (us_exclude && us_exclude->type != BKE_UNDOSYS_TYPE_MEMFILE) {
470 us_exclude = us_exclude->prev;
471 }
472 /* Once this is outside the given number of 'steps', undoing onto this state
473 * may skip past many undo steps which is confusing, instead,
474 * disallow stepping onto this state entirely. */
475 if (us_exclude) {
476 us_exclude->skip = true;
477 }
478 }
479#endif
480 /* Free from first to last, free functions may update de-duplication info
481 * (see #MemFileUndoStep). */
482 undosys_stack_clear_all_first(ustack, us->prev, us_exclude);
483 }
484}
485
487
488/* -------------------------------------------------------------------- */
491
493 bContext *C,
494 const char *name,
495 const UndoType *ut)
496{
497 UNDO_NESTED_ASSERT(false);
498 if (ut->step_encode_init) {
499 undosys_stack_validate(ustack, false);
500
501 if (UndoStep *us = ustack->step_init) {
503 ustack->step_init = nullptr;
504 }
505 if (ustack->step_active) {
507 }
508
509 UndoStep *us = static_cast<UndoStep *>(MEM_new_zeroed(ut->step_size, __func__));
510 if (name != nullptr) {
511 STRNCPY(us->name, name);
512 }
513 us->type = ut;
514 ustack->step_init = us;
515 CLOG_INFO(&LOG, "Initialize type='%s'", ut->identifier);
516 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->identifier);
517 ut->step_encode_init(C, us);
518 undosys_stack_validate(ustack, false);
519 return us;
520 }
521
522 return nullptr;
523}
524
526{
527 UNDO_NESTED_ASSERT(false);
529 if (ut == nullptr) {
530 return nullptr;
531 }
532 return BKE_undosys_step_push_init_with_type(ustack, C, name, ut);
533}
534
536 bContext *C,
537 const char *name,
538 const UndoEncodeHints hints,
539 const UndoType *ut)
540{
541 BLI_assert((ut->flags & UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE) == 0 || C != nullptr);
542
543 UNDO_NESTED_ASSERT(false);
544 undosys_stack_validate(ustack, false);
545 bool is_not_empty = ustack->step_active != nullptr;
547
548 /* Might not be final place for this to be called - probably only want to call it from some
549 * undo handlers, not all of them? */
552 G_MAIN, false, reinterpret_cast<int *>(&report_flags));
553 if (report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
555 }
556
557 /* Remove all undo-steps after (also when 'ustack->step_active == nullptr'). */
558 while (ustack->steps.last() != ustack->step_active) {
559 UndoStep *us_iter = ustack->steps.last();
560 undosys_step_free_and_unlink(ustack, us_iter);
561 undosys_stack_validate(ustack, is_not_empty);
562 }
563
564 if (ustack->step_active) {
565 BLI_assert(BLI_findindex(&ustack->steps, ustack->step_active) != -1);
566 }
567
568 const bool force_memfile_step = bool(hints & UndoEncodeHints::PreMemFileChanges);
569
570 /* The hint must only be used with undo types that opted into `memfile` co-existence.
571 * Otherwise setting it indicates a bug in the caller. */
572 BLI_assert(!force_memfile_step || (ut->flags & UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED));
573
574#ifdef WITH_GLOBAL_UNDO_ENSURE_UPDATED
575 if (ut->step_foreach_ID_ref != nullptr || force_memfile_step) {
576 if (G_MAIN->is_memfile_undo_written == false || force_memfile_step) {
577 const char *name_internal = "MemFile Internal (pre)";
578 /* Don't let 'step_init' cause issues when adding memfile undo step. */
579 void *step_init = ustack->step_init;
580 ustack->step_init = nullptr;
581 const bool ok = undosys_stack_push_main(ustack, name_internal, G_MAIN);
582 /* Restore 'step_init'. */
583 ustack->step_init = static_cast<UndoStep *>(step_init);
584 if (ok) {
585 UndoStep *us = ustack->steps.last();
586 BLI_assert(STREQ(us->name, name_internal));
587 us->skip = true;
588# ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
589 ustack->step_active_memfile = us;
590# endif
591 }
592 }
593 }
594#endif
595
596 bool use_memfile_step = false;
597 {
598 UndoStep *us = ustack->step_init ?
599 ustack->step_init :
600 static_cast<UndoStep *>(MEM_new_zeroed(ut->step_size, __func__));
601 ustack->step_init = nullptr;
602 if (us->name[0] == '\0') {
603 STRNCPY(us->name, name);
604 }
605 us->type = ut;
606 /* True by default, code needs to explicitly set it to false if necessary. */
607 us->use_old_bmain_data = true;
608 /* Initialized, not added yet. */
609
610 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->identifier);
611
612 /* The step is considered changed unless the hint states all changes are in the
613 * memfile step, in that case the local data of the step is unchanged. */
614
615 /* TODO(@ideasman42): Use #UndoEncodeHints::HasMemFileChangesOnly from `hints` to detect
616 * if we actually need to store a new undo step.
617 * To implement this we will likely need to pass `hints` to #undosys_step_encode.
618 * This can save significant overhead by avoiding storage of a new undo step,
619 * using the undo step more as a place-holder
620 * (needed to ensure mode-switching in the case of edit-mode).
621 * This needs to be done carefully for each mode,
622 * so for now keep this as a TODO. See: !161566. */
623
624 if (!undosys_step_encode(C, G_MAIN, ustack, us)) {
625 MEM_delete(us);
626 undosys_stack_validate(ustack, true);
627 return retval;
628 }
629 ustack->step_active = us;
630 BLI_addtail(&ustack->steps, us);
631 use_memfile_step = us->use_memfile_step;
632 }
633
634 if (use_memfile_step) {
635 /* Make this the user visible undo state, so redo always applies
636 * on top of the mem-file undo instead of skipping it. see: #67256. */
637 UndoStep *us_prev = ustack->step_active;
638 const char *name_internal = us_prev->name;
639 const bool ok = undosys_stack_push_main(ustack, name_internal, G_MAIN);
640 if (ok) {
641 UndoStep *us = ustack->steps.last();
642 BLI_assert(STREQ(us->name, name_internal));
643 us_prev->skip = true;
644#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
645 ustack->step_active_memfile = us;
646#endif
647 ustack->step_active = us;
648 }
649 }
650
651 if (ustack->group_level > 0) {
652 /* Temporarily set skip for the active step.
653 * This is an invalid state which must be corrected once the last group ends. */
654 ustack->step_active->skip = true;
655 }
656
657 undosys_stack_validate(ustack, true);
658 return (retval | UNDO_PUSH_RET_SUCCESS);
659}
660
662 bContext *C,
663 const char *name,
664 const UndoEncodeHints hints)
665{
666 UNDO_NESTED_ASSERT(false);
667 const UndoType *ut = ustack->step_init ? ustack->step_init->type :
669 if (ut == nullptr) {
671 }
672 return BKE_undosys_step_push_with_type(ustack, C, name, hints, ut);
673}
674
676{
677 if (us) {
678 const UndoType *ut = us->type;
679 while ((us = us->next)) {
680 if (us->type == ut) {
681 return us;
682 }
683 }
684 }
685 return us;
686}
687
689{
690 if (us) {
691 const UndoType *ut = us->type;
692 while ((us = us->prev)) {
693 if (us->type == ut) {
694 return us;
695 }
696 }
697 }
698 return us;
699}
700
702 const char *name,
703 const UndoType *ut)
704{
705 for (UndoStep &us : ustack->steps.items_reversed()) {
706 if (us.type == ut) {
707 if (STREQ(name, us.name)) {
708 return &us;
709 }
710 }
711 }
712 return nullptr;
713}
714
716{
717 return static_cast<UndoStep *>(BLI_rfindstring(&ustack->steps, name, offsetof(UndoStep, name)));
718}
719
721{
722 for (UndoStep &us : ustack->steps.items_reversed()) {
723 if (us.type == ut) {
724 return &us;
725 }
726 }
727 return nullptr;
728}
729
731 const UndoStep *us_target,
732 const UndoStep *us_reference)
733{
734 if (us_reference == nullptr) {
735 us_reference = ustack->step_active;
736 }
737
738 BLI_assert(us_reference != nullptr);
739
740 /* Note that we use heuristics to make this lookup as fast as possible in most common cases,
741 * assuming that:
742 * - Most cases are just undo or redo of one step from active one.
743 * - Otherwise, it is typically faster to check future steps since active one is usually close
744 * to the end of the list, rather than its start. */
745 /* NOTE: in case target step is the active one, we assume we are in an undo case... */
746 if (ELEM(us_target, us_reference, us_reference->prev)) {
747 return STEP_UNDO;
748 }
749 if (us_target == us_reference->next) {
750 return STEP_REDO;
751 }
752
753 /* Search forward, and then backward. */
754 for (UndoStep *us_iter = us_reference->next; us_iter != nullptr; us_iter = us_iter->next) {
755 if (us_iter == us_target) {
756 return STEP_REDO;
757 }
758 }
759 for (UndoStep *us_iter = us_reference->prev; us_iter != nullptr; us_iter = us_iter->prev) {
760 if (us_iter == us_target) {
761 return STEP_UNDO;
762 }
763 }
764
766 "Target undo step not found, this should not happen and may indicate an undo "
767 "stack corruption");
768 return STEP_INVALID;
769}
770
775static UndoStep *undosys_step_iter_first(UndoStep *us_reference, const eUndoStepDir undo_dir)
776{
777 if (us_reference->type->flags & UNDOTYPE_FLAG_DECODE_ACTIVE_STEP) {
778 /* Reading this step means an undo action reads undo twice.
779 * This should be avoided where possible, however some undo systems require it.
780 *
781 * Redo skips the current state as this represents the currently loaded state. */
782 return (undo_dir == -1) ? us_reference : us_reference->next;
783 }
784
785 /* Typical case, skip reading the current undo step. */
786 return (undo_dir == -1) ? us_reference->prev : us_reference->next;
787}
788
790 bContext *C,
791 UndoStep *us_target,
792 UndoStep *us_reference,
793 const bool use_skip)
794{
795 UNDO_NESTED_ASSERT(false);
796 if (us_target == nullptr) {
797 CLOG_ERROR(&LOG, "called with a nullptr target step");
798 return false;
799 }
800 undosys_stack_validate(ustack, true);
801
802 if (us_reference == nullptr) {
803 us_reference = ustack->step_active;
804 }
805 if (us_reference == nullptr) {
806 CLOG_ERROR(&LOG, "could not find a valid initial active target step as reference");
807 return false;
808 }
809
810 /* This considers we are in undo case if both `us_target` and `us_reference` are the same. */
811 const eUndoStepDir undo_dir = BKE_undosys_step_calc_direction(ustack, us_target, us_reference);
812 BLI_assert(undo_dir != STEP_INVALID);
813
814 /* This will be the active step once the undo process is complete.
815 *
816 * In case we do skip 'skipped' steps, the final active step may be several steps backward from
817 * the one passed as parameter. */
818 UndoStep *us_target_active = us_target;
819 if (use_skip) {
820 while (us_target_active != nullptr && us_target_active->skip) {
821 us_target_active = (undo_dir == -1) ? us_target_active->prev : us_target_active->next;
822 }
823 if (us_target_active == nullptr) {
825 "undo/redo did not find a step after stepping over skip-steps "
826 "(undo limit exceeded)");
827 return false;
828 }
829 }
830
832 "addr=%p, name='%s', type='%s', undo_dir=%d",
833 us_target,
834 us_target->name,
835 us_target->type->identifier,
836 undo_dir);
837
838 /* Undo/Redo steps until we reach given target step (or beyond if it has to be skipped),
839 * from given reference step. */
840 bool is_processing_extra_skipped_steps = false;
841 for (UndoStep *us_iter = undosys_step_iter_first(us_reference, undo_dir); us_iter != nullptr;
842 us_iter = (undo_dir == -1) ? us_iter->prev : us_iter->next)
843 {
844 BLI_assert(us_iter != nullptr);
845
846 const bool is_final = (us_iter == us_target_active);
847
848 if (!is_final && is_processing_extra_skipped_steps) {
849 BLI_assert(us_iter->skip == true);
851 "undo/redo continue with skip addr=%p, name='%s', type='%s'",
852 us_iter,
853 us_iter->name,
854 us_iter->type->identifier);
855 }
856
857 undosys_step_decode(C, G_MAIN, ustack, us_iter, undo_dir, is_final);
858 ustack->step_active = us_iter;
859
860 if (us_iter == us_target) {
861 is_processing_extra_skipped_steps = true;
862 }
863
864 if (is_final) {
865 /* Undo/Redo process is finished and successful. */
866 return true;
867 }
868 }
869
871 false,
872 "This should never be reached, either undo stack is corrupted, or code above is buggy");
873 return false;
874}
875
877{
878 /* Note that here we do not skip 'skipped' steps by default. */
879 return BKE_undosys_step_load_data_ex(ustack, C, us_target, nullptr, false);
880}
881
883{
884 UndoStep *us_target = static_cast<UndoStep *>(BLI_findlink(&ustack->steps, index));
885 BLI_assert(us_target->skip == false);
886 if (us_target == ustack->step_active) {
887 return;
888 }
889 BKE_undosys_step_load_data(ustack, C, us_target);
890}
891
893 bContext *C,
894 UndoStep *us_target,
895 bool use_skip)
896{
897 /* In case there is no active step, we consider we just load given step, so reference must be
898 * itself (due to weird 'load current active step in undo case' thing, see comments in
899 * #BKE_undosys_step_load_data_ex). */
900 UndoStep *us_reference = ustack->step_active != nullptr ? ustack->step_active : us_target;
901
902 BLI_assert(BKE_undosys_step_calc_direction(ustack, us_target, us_reference) == -1);
903
904 return BKE_undosys_step_load_data_ex(ustack, C, us_target, us_reference, use_skip);
905}
906
908{
909 return BKE_undosys_step_undo_with_data_ex(ustack, C, us_target, true);
910}
911
913{
914 if (ustack->step_active != nullptr) {
915 return BKE_undosys_step_undo_with_data(ustack, C, ustack->step_active->prev);
916 }
917 return false;
918}
919
921 bContext *C,
922 UndoStep *us_target,
923 bool use_skip)
924{
925 /* In case there is no active step, we consider we just load given step, so reference must be
926 * the previous one. */
927 UndoStep *us_reference = ustack->step_active != nullptr ? ustack->step_active : us_target->prev;
928
929 BLI_assert(BKE_undosys_step_calc_direction(ustack, us_target, us_reference) == 1);
930
931 return BKE_undosys_step_load_data_ex(ustack, C, us_target, us_reference, use_skip);
932}
933
935{
936 return BKE_undosys_step_redo_with_data_ex(ustack, C, us_target, true);
937}
938
940{
941 if (ustack->step_active != nullptr) {
942 return BKE_undosys_step_redo_with_data(ustack, C, ustack->step_active->next);
943 }
944 return false;
945}
946
948{
949 UndoType *ut = MEM_new<UndoType>(__func__);
950
951 undosys_fn(ut);
952
954
955 return ut;
956}
957
959{
960 while (UndoType *ut = static_cast<UndoType *>(BLI_pophead(&g_undo_types))) {
961 MEM_delete(ut);
962 }
963}
964
966{
967 for (const UndoType &ut : g_undo_types) {
968 if (!fn(&ut)) {
969 return false;
970 }
971 }
972 return true;
973}
974
976
977/* -------------------------------------------------------------------- */
995
997{
998 BLI_assert(ustack->group_level >= 0);
999 ustack->group_level += 1;
1000}
1001
1003{
1004 ustack->group_level -= 1;
1005 BLI_assert(ustack->group_level >= 0);
1006
1007 if (ustack->group_level == 0) {
1008 if (ustack->step_active != nullptr) [[likely]] {
1009 ustack->step_active->skip = false;
1010 }
1011 }
1012}
1013
1015
1016/* -------------------------------------------------------------------- */
1021
1022static void UNUSED_FUNCTION(BKE_undosys_foreach_ID_ref(UndoStack *ustack,
1023 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
1024 void *user_data))
1025{
1026 for (UndoStep &us : ustack->steps) {
1027 const UndoType *ut = us.type;
1028 if (ut->step_foreach_ID_ref != nullptr) {
1029 ut->step_foreach_ID_ref(&us, foreach_ID_ref_fn, user_data);
1030 }
1031 }
1032}
1033
1035
1036/* -------------------------------------------------------------------- */
1039
1041{
1042 if (!ustack) {
1043 printf("No undo steps recorded yet.\n");
1044 return;
1045 }
1046 printf("Undo %d Steps (*: active, #=applied, M=memfile-active, S=skip)\n",
1047 ustack->steps.count());
1048 int index = 0;
1049 for (UndoStep &us : ustack->steps) {
1050 printf("[%c%c%c%c] %3d {%p} type='%s', name='%s'\n",
1051 (&us == ustack->step_active) ? '*' : ' ',
1052 us.is_applied ? '#' : ' ',
1053 (&us == ustack->step_active_memfile) ? 'M' : ' ',
1054 us.skip ? 'S' : ' ',
1055 index,
1056 static_cast<void *>(&us),
1057 us.type->identifier,
1058 us.name);
1059 index++;
1060 }
1061}
1062
1064
1065} // namespace blender
#define G_MAIN
#define IFACE_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
These structs are the foundation for all linked lists in the library system.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNUSED_FUNCTION(x)
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define offsetof(t, d)
BMesh Iterators.
eRNAOverrideMatchResult
@ RNA_OVERRIDE_MATCH_RESULT_CREATED
@ RNA_OVERRIDE_MATCH_RESULT_INIT
void BKE_undosys_stack_init_from_main(UndoStack *ustack, Main *bmain)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_undosys_type_free_all()
char * STRNCPY(char(&dst)[N], const char *src)
static UndoStep * undosys_step_iter_first(UndoStep *us_reference, const eUndoStepDir undo_dir)
UndoStep * BKE_undosys_step_same_type_next(UndoStep *us)
bool BKE_undosys_stack_has_undo(const UndoStack *ustack, const char *name)
static void undosys_stack_clear_all_first(UndoStack *ustack, UndoStep *us, UndoStep *us_exclude)
@ UNDOTYPE_FLAG_DECODE_ACTIVE_STEP
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
const UndoType * BKE_UNDOSYS_TYPE_PAINTCURVE
const UndoType * BKE_UNDOSYS_TYPE_PARTICLE
bool BKE_undosys_step_redo_with_data(UndoStack *ustack, bContext *C, UndoStep *us_target)
ID * BKE_libblock_find_name_and_library_filepath(Main *bmain, short type, const char *name, const char *lib_filepath_abs)
Definition lib_id.cc:1865
void BKE_undosys_stack_clear_active(UndoStack *ustack)
void BKE_undosys_stack_group_end(UndoStack *ustack)
static void undosys_id_ref_resolve(void *user_data, UndoRefID *id_ref)
void * BLI_rfindstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:633
static bool undosys_stack_push_main(UndoStack *ustack, const char *name, Main *bmain)
static void undosys_step_decode(bContext *C, Main *bmain, UndoStack *ustack, UndoStep *us, const eUndoStepDir dir, bool is_final)
UndoStack * BKE_undosys_stack_create()
bool BKE_undosys_step_undo_with_data_ex(UndoStack *ustack, bContext *C, UndoStep *us_target, bool use_skip)
bContext * CTX_create()
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
bool BKE_undosys_step_load_data(UndoStack *ustack, bContext *C, UndoStep *us_target)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void BKE_undosys_stack_group_begin(UndoStack *ustack)
static bool undosys_step_encode(bContext *C, Main *bmain, UndoStack *ustack, UndoStep *us)
void BKE_undosys_stack_clear(UndoStack *ustack)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
eUndoStepDir BKE_undosys_step_calc_direction(const UndoStack *ustack, const UndoStep *us_target, const UndoStep *us_reference)
static void undosys_id_ref_store(void *, UndoRefID *id_ref)
void BKE_undosys_print(UndoStack *ustack)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BKE_undosys_step_load_from_index(UndoStack *ustack, bContext *C, int index)
static void undosys_step_free_and_unlink(UndoStack *ustack, UndoStep *us)
const UndoType * BKE_UNDOSYS_TYPE_TEXT
bool BKE_undosys_type_foreach(FunctionRef< bool(const UndoType *ut)> fn)
UndoStep * BKE_undosys_stack_init_or_active_with_type(UndoStack *ustack, const UndoType *ut)
static void undosys_stack_validate(UndoStack *ustack, bool expect_non_empty)
void CTX_free(bContext *C)
const UndoType * BKE_UNDOSYS_TYPE_SCULPT
static void undosys_stack_clear_all_last(UndoStack *ustack, UndoStep *us)
static ListBaseT< UndoType > g_undo_types
const UndoType * BKE_UNDOSYS_TYPE_MEMFILE
bool BKE_undosys_step_undo(UndoStack *ustack, bContext *C)
bool BKE_undosys_step_undo_with_data(UndoStack *ustack, bContext *C, UndoStep *us_target)
bool BKE_undosys_step_redo(UndoStack *ustack, bContext *C)
void BKE_lib_override_library_main_operations_create(Main *bmain, bool force_auto, int *r_report_flags)
static bool g_undo_callback_running
UndoStep * BKE_undosys_step_push_init(UndoStack *ustack, bContext *C, const char *name)
UndoStep * BKE_undosys_stack_active_with_type(UndoStack *ustack, const UndoType *ut)
@ UNDO_PUSH_RET_OVERRIDE_CHANGED
void bke_undo_system_linker_workaround()
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_undosys_stack_init_from_context(UndoStack *ustack, bContext *C)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_undosys_stack_limit_steps_and_memory(UndoStack *ustack, int steps, size_t memory_limit)
bool BKE_undosys_stack_has_redo(const UndoStack *ustack)
static CLG_LogRef LOG
Definition logging.cpp:12
UndoStep * BKE_undosys_step_find_by_name_with_type(UndoStack *ustack, const char *name, const UndoType *ut)
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
UndoStep * BKE_undosys_step_same_type_prev(UndoStep *us)
void CTX_data_main_set(bContext *C, Main *bmain)
bool BKE_undosys_step_load_data_ex(UndoStack *ustack, bContext *C, UndoStep *us_target, UndoStep *us_reference, bool use_skip)
UndoStep * BKE_undosys_step_find_by_name(UndoStack *ustack, const char *name)
void BKE_undosys_stack_destroy(UndoStack *ustack)
UndoStep * BKE_undosys_step_find_by_type(UndoStack *ustack, const UndoType *ut)
eUndoPushReturn BKE_undosys_step_push_with_type(UndoStack *ustack, bContext *C, const char *name, UndoEncodeHints hints, const UndoType *ut)
bool BKE_undosys_step_redo_with_data_ex(UndoStack *ustack, bContext *C, UndoStep *us_target, bool use_skip)
const UndoType * BKE_UNDOSYS_TYPE_IMAGE
const UndoType * BKE_undosys_type_from_context(bContext *C)
void BKE_memfile_undo_free(MemFileUndoData *mfu)
UndoType * BKE_undosys_type_append(void(*undosys_fn)(UndoType *))
eUndoPushReturn BKE_undosys_step_push(UndoStack *ustack, bContext *C, const char *name, UndoEncodeHints hints)
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, bContext *C, const char *name, const UndoType *ut)
char name[258]
Definition DNA_ID.h:433
struct Library * lib
Definition DNA_ID.h:421
bke::library::LibraryRuntime * runtime
Definition DNA_ID.h:591
char library_filepath_abs[FILE_MAX]
char name[MAX_ID_NAME]
UndoStep * step_active_memfile
ListBaseT< UndoStep > steps
const UndoType * type
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
void(*) step_foreach_ID_ref(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier
void(*) step_encode_init(bContext *C, UndoStep *us)
#define UNDO_NESTED_CHECK_END
#define UNDO_NESTED_CHECK_BEGIN
#define UNDO_NESTED_ASSERT(state)