Blender V5.0
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.h"
17#include "BLI_string.h"
18#include "BLI_sys_types.h"
19#include "BLI_utildefines.h"
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
41#define undo_stack _wm_undo_stack_disallow /* pass in as a variable always. */
42
44#define WITH_GLOBAL_UNDO_KEEP_ONE
45
47#define WITH_GLOBAL_UNDO_ENSURE_UPDATED
48
53#define WITH_GLOBAL_UNDO_CORRECT_ORDER
54
56static CLG_LogRef LOG = {"undo"};
57
58/* -------------------------------------------------------------------- */
61
68
69static ListBase g_undo_types = {nullptr, nullptr};
70
71/* An unused function with public linkage just to ensure symbols from the blender_undo.cc are not
72 * stripped. */
79
81{
82 LISTBASE_FOREACH (const UndoType *, ut, &g_undo_types) {
83 /* No poll means we don't check context. */
84 if (ut->poll && ut->poll(C)) {
85 return ut;
86 }
87 }
88 return nullptr;
89}
90
92
93/* -------------------------------------------------------------------- */
100
101#define WITH_NESTED_UNDO_CHECK
102
103#ifdef WITH_NESTED_UNDO_CHECK
104static bool g_undo_callback_running = false;
105# define UNDO_NESTED_ASSERT(state) BLI_assert(g_undo_callback_running == state)
106# define UNDO_NESTED_CHECK_BEGIN \
107 { \
108 UNDO_NESTED_ASSERT(false); \
109 g_undo_callback_running = true; \
110 } \
111 ((void)0)
112# define UNDO_NESTED_CHECK_END \
113 { \
114 UNDO_NESTED_ASSERT(true); \
115 g_undo_callback_running = false; \
116 } \
117 ((void)0)
118#else
119# define UNDO_NESTED_ASSERT(state) ((void)0)
120# define UNDO_NESTED_CHECK_BEGIN ((void)0)
121# define UNDO_NESTED_CHECK_END ((void)0)
122#endif
123
125
126/* -------------------------------------------------------------------- */
133
134static void undosys_id_ref_store(void * /*user_data*/, UndoRefID *id_ref)
135{
136 BLI_assert(id_ref->name[0] == '\0');
137 if (id_ref->ptr) {
138 STRNCPY(id_ref->name, id_ref->ptr->name);
139 if (id_ref->ptr->lib) {
140 STRNCPY(id_ref->library_filepath_abs, id_ref->ptr->lib->runtime->filepath_abs);
141 }
142 else {
143 id_ref->library_filepath_abs[0] = '\0';
144 }
145 /* Not needed, just prevents stale data access. */
146 id_ref->ptr = nullptr;
147 }
148}
149
150static void undosys_id_ref_resolve(void *user_data, UndoRefID *id_ref)
151{
152 /* NOTE: we could optimize this,
153 * for now it's not too bad since it only runs when we access undo! */
154 Main *bmain = static_cast<Main *>(user_data);
156 bmain,
157 GS(id_ref->name),
158 id_ref->name + 2,
159 (id_ref->library_filepath_abs[0] ? id_ref->library_filepath_abs : nullptr));
160}
161
162static bool undosys_step_encode(bContext *C, Main *bmain, UndoStack *ustack, UndoStep *us)
163{
164 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->name);
166 bool ok = us->type->step_encode(C, bmain, us);
168 if (ok) {
169 if (us->type->step_foreach_ID_ref != nullptr) {
170 /* Don't use from context yet because sometimes context is fake and
171 * not all members are filled in. */
173 }
174
175#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
176 if (us->type == BKE_UNDOSYS_TYPE_MEMFILE) {
177 ustack->step_active_memfile = us;
178 }
179#endif
180 }
181 if (ok == false) {
182 CLOG_DEBUG(&LOG, "encode callback didn't create undo step");
183 }
184 return ok;
185}
186
188 Main *bmain,
189 UndoStack *ustack,
190 UndoStep *us,
191 const eUndoStepDir dir,
192 bool is_final)
193{
194 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->name);
195
196 if (us->type->step_foreach_ID_ref) {
197#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
198 if (us->type != BKE_UNDOSYS_TYPE_MEMFILE) {
199 for (UndoStep *us_iter = us->prev; us_iter; us_iter = us_iter->prev) {
200 if (us_iter->type == BKE_UNDOSYS_TYPE_MEMFILE) {
201 if (us_iter == ustack->step_active_memfile) {
202 /* Common case, we're already using the last memfile state. */
203 }
204 else {
205 /* Load the previous memfile state so any ID's referenced in this
206 * undo step will be correctly resolved, see: #56163. */
207 undosys_step_decode(C, bmain, ustack, us_iter, dir, false);
208 /* May have been freed on memfile read. */
209 bmain = G_MAIN;
210 }
211 break;
212 }
213 }
214 }
215#endif
216 /* Don't use from context yet because sometimes context is fake and
217 * not all members are filled in. */
219 }
220
222 us->type->step_decode(C, bmain, us, dir, is_final);
224
225#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
226 if (us->type == BKE_UNDOSYS_TYPE_MEMFILE) {
227 ustack->step_active_memfile = us;
228 }
229#endif
230}
231
233{
234 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->name);
236 us->type->step_free(us);
238
239 BLI_remlink(&ustack->steps, us);
240 MEM_freeN(us);
241
242#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
243 if (ustack->step_active_memfile == us) {
244 ustack->step_active_memfile = nullptr;
245 }
246#endif
247}
248
250
251/* -------------------------------------------------------------------- */
254
255#ifndef NDEBUG
256static void undosys_stack_validate(UndoStack *ustack, bool expect_non_empty)
257{
258 if (ustack->step_active != nullptr) {
260 BLI_assert(BLI_findindex(&ustack->steps, ustack->step_active) != -1);
261 }
262 if (expect_non_empty) {
264 }
265}
266#else
267static void undosys_stack_validate(UndoStack * /*ustack*/, bool /*expect_non_empty*/) {}
268#endif
269
271{
272 UndoStack *ustack = MEM_callocN<UndoStack>(__func__);
273 return ustack;
274}
275
277{
279 MEM_freeN(ustack);
280}
281
283{
284 UNDO_NESTED_ASSERT(false);
285 CLOG_DEBUG(&LOG, "steps=%d", BLI_listbase_count(&ustack->steps));
286 for (UndoStep *us = static_cast<UndoStep *>(ustack->steps.last), *us_prev; us; us = us_prev) {
287 us_prev = us->prev;
289 }
290 if (UndoStep *us = ustack->step_init) {
292 ustack->step_init = nullptr;
293 }
294 BLI_listbase_clear(&ustack->steps);
295 ustack->step_active = nullptr;
296}
297
299{
300 /* Remove active and all following undo-steps. */
301 UndoStep *us = ustack->step_active;
302
303 if (us) {
304 ustack->step_active = us->prev;
305 bool is_not_empty = ustack->step_active != nullptr;
306
307 while (ustack->steps.last != ustack->step_active) {
308 UndoStep *us_iter = static_cast<UndoStep *>(ustack->steps.last);
309 undosys_step_free_and_unlink(ustack, us_iter);
310 undosys_stack_validate(ustack, is_not_empty);
311 }
312 }
313}
314
315/* Caller is responsible for handling active. */
317{
318 if (us) {
319 bool is_not_empty = true;
320 UndoStep *us_iter;
321 do {
322 us_iter = static_cast<UndoStep *>(ustack->steps.last);
323 BLI_assert(us_iter != ustack->step_active);
324 undosys_step_free_and_unlink(ustack, us_iter);
325 undosys_stack_validate(ustack, is_not_empty);
326 } while (us != us_iter);
327 }
328}
329
330static void undosys_stack_clear_all_first(UndoStack *ustack, UndoStep *us, UndoStep *us_exclude)
331{
332 if (us && us == us_exclude) {
333 us = us->prev;
334 }
335
336 if (us) {
337 bool is_not_empty = true;
338 UndoStep *us_iter;
339 do {
340 us_iter = static_cast<UndoStep *>(ustack->steps.first);
341 if (us_iter == us_exclude) {
342 us_iter = us_iter->next;
343 }
344 BLI_assert(us_iter != ustack->step_active);
345 undosys_step_free_and_unlink(ustack, us_iter);
346 undosys_stack_validate(ustack, is_not_empty);
347 } while (us != us_iter);
348 }
349}
350
351static bool undosys_stack_push_main(UndoStack *ustack, const char *name, Main *bmain)
352{
353 UNDO_NESTED_ASSERT(false);
354 BLI_assert(ustack->step_init == nullptr);
355 CLOG_DEBUG(&LOG, "Push main '%s'", name);
356 bContext *C_temp = CTX_create();
357 CTX_data_main_set(C_temp, bmain);
359 ustack, C_temp, name, BKE_UNDOSYS_TYPE_MEMFILE);
360 CTX_free(C_temp);
361 return (ret & UNDO_PUSH_RET_SUCCESS);
362}
363
365{
366 UNDO_NESTED_ASSERT(false);
367 undosys_stack_push_main(ustack, IFACE_("Original"), bmain);
368}
369
371{
373 if (!ELEM(ut, nullptr, BKE_UNDOSYS_TYPE_MEMFILE)) {
374 BKE_undosys_step_push_with_type(ustack, C, IFACE_("Original Mode"), ut);
375 }
376}
377
378bool BKE_undosys_stack_has_undo(const UndoStack *ustack, const char *name)
379{
380 if (name) {
381 const UndoStep *us = static_cast<UndoStep *>(
383 return us && us->prev;
384 }
385
386 return !BLI_listbase_is_empty(&ustack->steps);
387}
388
390{
391 UndoStep *us = ustack->step_active;
392 while (us && (us->type != ut)) {
393 us = us->prev;
394 }
395 return us;
396}
397
399{
400 UNDO_NESTED_ASSERT(false);
401 CLOG_INFO(&LOG, "Initialize type='%s'", ut->name);
402 if (ustack->step_init && (ustack->step_init->type == ut)) {
403 return ustack->step_init;
404 }
405 return BKE_undosys_stack_active_with_type(ustack, ut);
406}
407
409{
410 UNDO_NESTED_ASSERT(false);
411 if ((steps == -1) && (memory_limit == 0)) {
412 return;
413 }
414
415 CLOG_DEBUG(&LOG, "Limit steps=%d, memory_limit=%zu", steps, memory_limit);
416 UndoStep *us;
417 UndoStep *us_exclude = nullptr;
418 /* keep at least two (original + other) */
419 size_t data_size_all = 0;
420 size_t us_count = 0;
421 for (us = static_cast<UndoStep *>(ustack->steps.last); us && us->prev; us = us->prev) {
422 if (memory_limit) {
423 data_size_all += us->data_size;
424 if (data_size_all > memory_limit) {
426 "At step %zu: data_size_all=%zu >= memory_limit=%zu",
427 us_count,
428 data_size_all,
430 break;
431 }
432 }
433 if (steps != -1) {
434 if (us_count == steps) {
435 break;
436 }
437 if (us->skip == false) {
438 us_count += 1;
439 }
440 }
441 }
442
443 CLOG_DEBUG(&LOG, "Total steps %zu: data_size_all=%zu", us_count, data_size_all);
444
445 if (us) {
446#ifdef WITH_GLOBAL_UNDO_KEEP_ONE
447 /* Hack, we need to keep at least one BKE_UNDOSYS_TYPE_MEMFILE. */
448 if (us->type != BKE_UNDOSYS_TYPE_MEMFILE) {
449 us_exclude = us->prev;
450 while (us_exclude && us_exclude->type != BKE_UNDOSYS_TYPE_MEMFILE) {
451 us_exclude = us_exclude->prev;
452 }
453 /* Once this is outside the given number of 'steps', undoing onto this state
454 * may skip past many undo steps which is confusing, instead,
455 * disallow stepping onto this state entirely. */
456 if (us_exclude) {
457 us_exclude->skip = true;
458 }
459 }
460#endif
461 /* Free from first to last, free functions may update de-duplication info
462 * (see #MemFileUndoStep). */
463 undosys_stack_clear_all_first(ustack, us->prev, us_exclude);
464 }
465}
466
468
469/* -------------------------------------------------------------------- */
472
474 bContext *C,
475 const char *name,
476 const UndoType *ut)
477{
478 UNDO_NESTED_ASSERT(false);
479 if (ut->step_encode_init) {
480 undosys_stack_validate(ustack, false);
481
482 if (UndoStep *us = ustack->step_init) {
484 ustack->step_init = nullptr;
485 }
486 if (ustack->step_active) {
488 }
489
490 UndoStep *us = static_cast<UndoStep *>(MEM_callocN(ut->step_size, __func__));
491 if (name != nullptr) {
492 STRNCPY(us->name, name);
493 }
494 us->type = ut;
495 ustack->step_init = us;
496 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->name);
497 ut->step_encode_init(C, us);
498 undosys_stack_validate(ustack, false);
499 return us;
500 }
501
502 return nullptr;
503}
504
506{
507 UNDO_NESTED_ASSERT(false);
509 if (ut == nullptr) {
510 return nullptr;
511 }
512 return BKE_undosys_step_push_init_with_type(ustack, C, name, ut);
513}
514
516 bContext *C,
517 const char *name,
518 const UndoType *ut)
519{
520 BLI_assert((ut->flags & UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE) == 0 || C != nullptr);
521
522 UNDO_NESTED_ASSERT(false);
523 undosys_stack_validate(ustack, false);
524 bool is_not_empty = ustack->step_active != nullptr;
526
527 /* Might not be final place for this to be called - probably only want to call it from some
528 * undo handlers, not all of them? */
530 BKE_lib_override_library_main_operations_create(G_MAIN, false, (int *)&report_flags);
531 if (report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
533 }
534
535 /* Remove all undo-steps after (also when 'ustack->step_active == nullptr'). */
536 while (ustack->steps.last != ustack->step_active) {
537 UndoStep *us_iter = static_cast<UndoStep *>(ustack->steps.last);
538 undosys_step_free_and_unlink(ustack, us_iter);
539 undosys_stack_validate(ustack, is_not_empty);
540 }
541
542 if (ustack->step_active) {
543 BLI_assert(BLI_findindex(&ustack->steps, ustack->step_active) != -1);
544 }
545
546#ifdef WITH_GLOBAL_UNDO_ENSURE_UPDATED
547 if (ut->step_foreach_ID_ref != nullptr) {
548 if (G_MAIN->is_memfile_undo_written == false) {
549 const char *name_internal = "MemFile Internal (pre)";
550 /* Don't let 'step_init' cause issues when adding memfile undo step. */
551 void *step_init = ustack->step_init;
552 ustack->step_init = nullptr;
553 const bool ok = undosys_stack_push_main(ustack, name_internal, G_MAIN);
554 /* Restore 'step_init'. */
555 ustack->step_init = static_cast<UndoStep *>(step_init);
556 if (ok) {
557 UndoStep *us = static_cast<UndoStep *>(ustack->steps.last);
558 BLI_assert(STREQ(us->name, name_internal));
559 us->skip = true;
560# ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
561 ustack->step_active_memfile = us;
562# endif
563 }
564 }
565 }
566#endif
567
568 bool use_memfile_step = false;
569 {
570 UndoStep *us = ustack->step_init ?
571 ustack->step_init :
572 static_cast<UndoStep *>(MEM_callocN(ut->step_size, __func__));
573 ustack->step_init = nullptr;
574 if (us->name[0] == '\0') {
575 STRNCPY(us->name, name);
576 }
577 us->type = ut;
578 /* True by default, code needs to explicitly set it to false if necessary. */
579 us->use_old_bmain_data = true;
580 /* Initialized, not added yet. */
581
582 CLOG_DEBUG(&LOG, "addr=%p, name='%s', type='%s'", us, us->name, us->type->name);
583
584 if (!undosys_step_encode(C, G_MAIN, ustack, us)) {
585 MEM_freeN(us);
586 undosys_stack_validate(ustack, true);
587 return retval;
588 }
589 ustack->step_active = us;
590 BLI_addtail(&ustack->steps, us);
591 use_memfile_step = us->use_memfile_step;
592 }
593
594 if (use_memfile_step) {
595 /* Make this the user visible undo state, so redo always applies
596 * on top of the mem-file undo instead of skipping it. see: #67256. */
597 UndoStep *us_prev = ustack->step_active;
598 const char *name_internal = us_prev->name;
599 const bool ok = undosys_stack_push_main(ustack, name_internal, G_MAIN);
600 if (ok) {
601 UndoStep *us = static_cast<UndoStep *>(ustack->steps.last);
602 BLI_assert(STREQ(us->name, name_internal));
603 us_prev->skip = true;
604#ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
605 ustack->step_active_memfile = us;
606#endif
607 ustack->step_active = us;
608 }
609 }
610
611 if (ustack->group_level > 0) {
612 /* Temporarily set skip for the active step.
613 * This is an invalid state which must be corrected once the last group ends. */
614 ustack->step_active->skip = true;
615 }
616
617 undosys_stack_validate(ustack, true);
618 return (retval | UNDO_PUSH_RET_SUCCESS);
619}
620
622{
623 UNDO_NESTED_ASSERT(false);
624 const UndoType *ut = ustack->step_init ? ustack->step_init->type :
626 if (ut == nullptr) {
628 }
629 return BKE_undosys_step_push_with_type(ustack, C, name, ut);
630}
631
633{
634 if (us) {
635 const UndoType *ut = us->type;
636 while ((us = us->next)) {
637 if (us->type == ut) {
638 return us;
639 }
640 }
641 }
642 return us;
643}
644
646{
647 if (us) {
648 const UndoType *ut = us->type;
649 while ((us = us->prev)) {
650 if (us->type == ut) {
651 return us;
652 }
653 }
654 }
655 return us;
656}
657
659 const char *name,
660 const UndoType *ut)
661{
662 LISTBASE_FOREACH_BACKWARD (UndoStep *, us, &ustack->steps) {
663 if (us->type == ut) {
664 if (STREQ(name, us->name)) {
665 return us;
666 }
667 }
668 }
669 return nullptr;
670}
671
673{
674 return static_cast<UndoStep *>(BLI_rfindstring(&ustack->steps, name, offsetof(UndoStep, name)));
675}
676
678{
679 LISTBASE_FOREACH_BACKWARD (UndoStep *, us, &ustack->steps) {
680 if (us->type == ut) {
681 return us;
682 }
683 }
684 return nullptr;
685}
686
688 const UndoStep *us_target,
689 const UndoStep *us_reference)
690{
691 if (us_reference == nullptr) {
692 us_reference = ustack->step_active;
693 }
694
695 BLI_assert(us_reference != nullptr);
696
697 /* Note that we use heuristics to make this lookup as fast as possible in most common cases,
698 * assuming that:
699 * - Most cases are just undo or redo of one step from active one.
700 * - Otherwise, it is typically faster to check future steps since active one is usually close
701 * to the end of the list, rather than its start. */
702 /* NOTE: in case target step is the active one, we assume we are in an undo case... */
703 if (ELEM(us_target, us_reference, us_reference->prev)) {
704 return STEP_UNDO;
705 }
706 if (us_target == us_reference->next) {
707 return STEP_REDO;
708 }
709
710 /* Search forward, and then backward. */
711 for (UndoStep *us_iter = us_reference->next; us_iter != nullptr; us_iter = us_iter->next) {
712 if (us_iter == us_target) {
713 return STEP_REDO;
714 }
715 }
716 for (UndoStep *us_iter = us_reference->prev; us_iter != nullptr; us_iter = us_iter->prev) {
717 if (us_iter == us_target) {
718 return STEP_UNDO;
719 }
720 }
721
723 "Target undo step not found, this should not happen and may indicate an undo "
724 "stack corruption");
725 return STEP_INVALID;
726}
727
732static UndoStep *undosys_step_iter_first(UndoStep *us_reference, const eUndoStepDir undo_dir)
733{
734 if (us_reference->type->flags & UNDOTYPE_FLAG_DECODE_ACTIVE_STEP) {
735 /* Reading this step means an undo action reads undo twice.
736 * This should be avoided where possible, however some undo systems require it.
737 *
738 * Redo skips the current state as this represents the currently loaded state. */
739 return (undo_dir == -1) ? us_reference : us_reference->next;
740 }
741
742 /* Typical case, skip reading the current undo step. */
743 return (undo_dir == -1) ? us_reference->prev : us_reference->next;
744}
745
747 bContext *C,
748 UndoStep *us_target,
749 UndoStep *us_reference,
750 const bool use_skip)
751{
752 UNDO_NESTED_ASSERT(false);
753 if (us_target == nullptr) {
754 CLOG_ERROR(&LOG, "called with a nullptr target step");
755 return false;
756 }
757 undosys_stack_validate(ustack, true);
758
759 if (us_reference == nullptr) {
760 us_reference = ustack->step_active;
761 }
762 if (us_reference == nullptr) {
763 CLOG_ERROR(&LOG, "could not find a valid initial active target step as reference");
764 return false;
765 }
766
767 /* This considers we are in undo case if both `us_target` and `us_reference` are the same. */
768 const eUndoStepDir undo_dir = BKE_undosys_step_calc_direction(ustack, us_target, us_reference);
769 BLI_assert(undo_dir != STEP_INVALID);
770
771 /* This will be the active step once the undo process is complete.
772 *
773 * In case we do skip 'skipped' steps, the final active step may be several steps backward from
774 * the one passed as parameter. */
775 UndoStep *us_target_active = us_target;
776 if (use_skip) {
777 while (us_target_active != nullptr && us_target_active->skip) {
778 us_target_active = (undo_dir == -1) ? us_target_active->prev : us_target_active->next;
779 }
780 if (us_target_active == nullptr) {
782 "undo/redo did not find a step after stepping over skip-steps "
783 "(undo limit exceeded)");
784 return false;
785 }
786 }
787
789 "addr=%p, name='%s', type='%s', undo_dir=%d",
790 us_target,
791 us_target->name,
792 us_target->type->name,
793 undo_dir);
794
795 /* Undo/Redo steps until we reach given target step (or beyond if it has to be skipped),
796 * from given reference step. */
797 bool is_processing_extra_skipped_steps = false;
798 for (UndoStep *us_iter = undosys_step_iter_first(us_reference, undo_dir); us_iter != nullptr;
799 us_iter = (undo_dir == -1) ? us_iter->prev : us_iter->next)
800 {
801 BLI_assert(us_iter != nullptr);
802
803 const bool is_final = (us_iter == us_target_active);
804
805 if (!is_final && is_processing_extra_skipped_steps) {
806 BLI_assert(us_iter->skip == true);
808 "undo/redo continue with skip addr=%p, name='%s', type='%s'",
809 us_iter,
810 us_iter->name,
811 us_iter->type->name);
812 }
813
814 undosys_step_decode(C, G_MAIN, ustack, us_iter, undo_dir, is_final);
815 ustack->step_active = us_iter;
816
817 if (us_iter == us_target) {
818 is_processing_extra_skipped_steps = true;
819 }
820
821 if (is_final) {
822 /* Undo/Redo process is finished and successful. */
823 return true;
824 }
825 }
826
828 false,
829 "This should never be reached, either undo stack is corrupted, or code above is buggy");
830 return false;
831}
832
834{
835 /* Note that here we do not skip 'skipped' steps by default. */
836 return BKE_undosys_step_load_data_ex(ustack, C, us_target, nullptr, false);
837}
838
839void BKE_undosys_step_load_from_index(UndoStack *ustack, bContext *C, const int index)
840{
841 UndoStep *us_target = static_cast<UndoStep *>(BLI_findlink(&ustack->steps, index));
842 BLI_assert(us_target->skip == false);
843 if (us_target == ustack->step_active) {
844 return;
845 }
846 BKE_undosys_step_load_data(ustack, C, us_target);
847}
848
850 bContext *C,
851 UndoStep *us_target,
852 bool use_skip)
853{
854 /* In case there is no active step, we consider we just load given step, so reference must be
855 * itself (due to weird 'load current active step in undo case' thing, see comments in
856 * #BKE_undosys_step_load_data_ex). */
857 UndoStep *us_reference = ustack->step_active != nullptr ? ustack->step_active : us_target;
858
859 BLI_assert(BKE_undosys_step_calc_direction(ustack, us_target, us_reference) == -1);
860
861 return BKE_undosys_step_load_data_ex(ustack, C, us_target, us_reference, use_skip);
862}
863
865{
866 return BKE_undosys_step_undo_with_data_ex(ustack, C, us_target, true);
867}
868
870{
871 if (ustack->step_active != nullptr) {
872 return BKE_undosys_step_undo_with_data(ustack, C, ustack->step_active->prev);
873 }
874 return false;
875}
876
878 bContext *C,
879 UndoStep *us_target,
880 bool use_skip)
881{
882 /* In case there is no active step, we consider we just load given step, so reference must be
883 * the previous one. */
884 UndoStep *us_reference = ustack->step_active != nullptr ? ustack->step_active : us_target->prev;
885
886 BLI_assert(BKE_undosys_step_calc_direction(ustack, us_target, us_reference) == 1);
887
888 return BKE_undosys_step_load_data_ex(ustack, C, us_target, us_reference, use_skip);
889}
890
892{
893 return BKE_undosys_step_redo_with_data_ex(ustack, C, us_target, true);
894}
895
897{
898 if (ustack->step_active != nullptr) {
899 return BKE_undosys_step_redo_with_data(ustack, C, ustack->step_active->next);
900 }
901 return false;
902}
903
905{
906 UndoType *ut = MEM_callocN<UndoType>(__func__);
907
908 undosys_fn(ut);
909
911
912 return ut;
913}
914
916{
917 while (UndoType *ut = static_cast<UndoType *>(BLI_pophead(&g_undo_types))) {
918 MEM_freeN(ut);
919 }
920}
921
923
924/* -------------------------------------------------------------------- */
942
944{
945 BLI_assert(ustack->group_level >= 0);
946 ustack->group_level += 1;
947}
948
950{
951 ustack->group_level -= 1;
952 BLI_assert(ustack->group_level >= 0);
953
954 if (ustack->group_level == 0) {
955 if (LIKELY(ustack->step_active != nullptr)) {
956 ustack->step_active->skip = false;
957 }
958 }
959}
960
962
963/* -------------------------------------------------------------------- */
968
969static void UNUSED_FUNCTION(BKE_undosys_foreach_ID_ref(UndoStack *ustack,
970 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
971 void *user_data))
972{
973 LISTBASE_FOREACH (UndoStep *, us, &ustack->steps) {
974 const UndoType *ut = us->type;
975 if (ut->step_foreach_ID_ref != nullptr) {
976 ut->step_foreach_ID_ref(us, foreach_ID_ref_fn, user_data);
977 }
978 }
979}
980
982
983/* -------------------------------------------------------------------- */
986
988{
989 printf("Undo %d Steps (*: active, #=applied, M=memfile-active, S=skip)\n",
990 BLI_listbase_count(&ustack->steps));
991 int index = 0;
992 LISTBASE_FOREACH (UndoStep *, us, &ustack->steps) {
993 printf("[%c%c%c%c] %3d {%p} type='%s', name='%s'\n",
994 (us == ustack->step_active) ? '*' : ' ',
995 us->is_applied ? '#' : ' ',
996 (us == ustack->step_active_memfile) ? 'M' : ' ',
997 us->skip ? 'S' : ' ',
998 index,
999 (void *)us,
1000 us->type->name,
1001 us->name);
1002 index++;
1003 }
1004}
1005
void BKE_memfile_undo_free(MemFileUndoData *mfu)
void CTX_data_main_set(bContext *C, Main *bmain)
void CTX_free(bContext *C)
bContext * CTX_create()
#define G_MAIN
ID * BKE_libblock_find_name_and_library_filepath(Main *bmain, short type, const char *name, const char *lib_filepath_abs)
Definition lib_id.cc:1767
void BKE_lib_override_library_main_operations_create(Main *bmain, bool force_auto, int *r_report_flags)
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_DECODE_ACTIVE_STEP
const UndoType * BKE_UNDOSYS_TYPE_SCULPT
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
const UndoType * BKE_UNDOSYS_TYPE_MEMFILE
const UndoType * BKE_UNDOSYS_TYPE_PARTICLE
eUndoStepDir
@ STEP_INVALID
@ STEP_UNDO
@ STEP_REDO
const UndoType * BKE_UNDOSYS_TYPE_TEXT
eUndoPushReturn
@ UNDO_PUSH_RET_SUCCESS
@ UNDO_PUSH_RET_OVERRIDE_CHANGED
@ UNDO_PUSH_RET_FAILURE
const UndoType * BKE_UNDOSYS_TYPE_PAINTCURVE
const UndoType * BKE_UNDOSYS_TYPE_IMAGE
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:586
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:534
#define LISTBASE_FOREACH(type, var, list)
BLI_INLINE void BLI_listbase_clear(ListBase *lb)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
#define LISTBASE_FOREACH_BACKWARD(type, var, list)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
void * BLI_rfindstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:626
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:524
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:252
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
#define UNUSED_FUNCTION(x)
#define ELEM(...)
#define STREQ(a, b)
#define LIKELY(x)
#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 Guarded memory(de)allocation.
eRNAOverrideMatchResult
@ RNA_OVERRIDE_MATCH_RESULT_CREATED
@ RNA_OVERRIDE_MATCH_RESULT_INIT
#define C
Definition RandGen.cpp:29
#define offsetof(t, d)
#define GS(x)
#define printf(...)
#define LOG(level)
Definition log.h:97
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
const char * name
return ret
struct Library * lib
Definition DNA_ID.h:420
char name[258]
Definition DNA_ID.h:432
LibraryRuntimeHandle * runtime
Definition DNA_ID.h:579
void * last
void * first
char name[MAX_ID_NAME]
struct ID * ptr
char library_filepath_abs[FILE_MAX]
UndoStep * step_init
UndoStep * step_active_memfile
UndoStep * step_active
ListBase steps
size_t data_size
UndoStep * prev
bool use_old_bmain_data
UndoStep * next
const UndoType * type
bool use_memfile_step
char name[64]
void(* step_encode_init)(bContext *C, UndoStep *us)
void(* step_foreach_ID_ref)(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
const char * name
void(* step_free)(UndoStep *us)
void(* step_decode)(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
bool(* step_encode)(bContext *C, Main *bmain, UndoStep *us)
static void undosys_id_ref_resolve(void *user_data, UndoRefID *id_ref)
static UndoStep * undosys_step_iter_first(UndoStep *us_reference, const eUndoStepDir undo_dir)
bool BKE_undosys_step_undo_with_data_ex(UndoStack *ustack, bContext *C, UndoStep *us_target, bool use_skip)
bool BKE_undosys_stack_has_undo(const UndoStack *ustack, const char *name)
bool BKE_undosys_step_redo_with_data(UndoStack *ustack, bContext *C, UndoStep *us_target)
void BKE_undosys_stack_init_from_context(UndoStack *ustack, bContext *C)
UndoStep * BKE_undosys_step_find_by_name_with_type(UndoStack *ustack, const char *name, const UndoType *ut)
bool BKE_undosys_step_redo(UndoStack *ustack, bContext *C)
UndoStep * BKE_undosys_step_find_by_type(UndoStack *ustack, const UndoType *ut)
UndoStep * BKE_undosys_step_same_type_next(UndoStep *us)
static void undosys_stack_validate(UndoStack *ustack, bool expect_non_empty)
bool BKE_undosys_step_undo(UndoStack *ustack, bContext *C)
void BKE_undosys_stack_clear_active(UndoStack *ustack)
#define UNDO_NESTED_CHECK_END
static const UndoType * BKE_undosys_type_from_context(bContext *C)
static ListBase g_undo_types
bool BKE_undosys_step_undo_with_data(UndoStack *ustack, bContext *C, UndoStep *us_target)
static bool g_undo_callback_running
#define UNDO_NESTED_CHECK_BEGIN
eUndoPushReturn BKE_undosys_step_push(UndoStack *ustack, bContext *C, const char *name)
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)
static void undosys_stack_clear_all_last(UndoStack *ustack, UndoStep *us)
UndoStep * BKE_undosys_step_same_type_prev(UndoStep *us)
static void undosys_stack_clear_all_first(UndoStack *ustack, UndoStep *us, UndoStep *us_exclude)
void BKE_undosys_type_free_all()
void BKE_undosys_stack_clear(UndoStack *ustack)
static bool undosys_stack_push_main(UndoStack *ustack, const char *name, Main *bmain)
void BKE_undosys_stack_limit_steps_and_memory(UndoStack *ustack, int steps, size_t memory_limit)
bool BKE_undosys_step_load_data_ex(UndoStack *ustack, bContext *C, UndoStep *us_target, UndoStep *us_reference, const bool use_skip)
UndoStep * BKE_undosys_step_find_by_name(UndoStack *ustack, const char *name)
static void undosys_step_free_and_unlink(UndoStack *ustack, UndoStep *us)
void BKE_undosys_step_load_from_index(UndoStack *ustack, bContext *C, const int index)
eUndoPushReturn BKE_undosys_step_push_with_type(UndoStack *ustack, bContext *C, const char *name, const UndoType *ut)
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, bContext *C, const char *name, const UndoType *ut)
void BKE_undosys_stack_destroy(UndoStack *ustack)
void BKE_undosys_stack_init_from_main(UndoStack *ustack, Main *bmain)
#define UNDO_NESTED_ASSERT(state)
void bke_undo_system_linker_workaround()
void BKE_undosys_stack_group_end(UndoStack *ustack)
static bool undosys_step_encode(bContext *C, Main *bmain, UndoStack *ustack, UndoStep *us)
UndoStep * BKE_undosys_stack_active_with_type(UndoStack *ustack, const UndoType *ut)
bool BKE_undosys_step_load_data(UndoStack *ustack, bContext *C, UndoStep *us_target)
UndoStack * BKE_undosys_stack_create()
bool BKE_undosys_step_redo_with_data_ex(UndoStack *ustack, bContext *C, UndoStep *us_target, bool use_skip)
UndoType * BKE_undosys_type_append(void(*undosys_fn)(UndoType *))
void BKE_undosys_print(UndoStack *ustack)
static void undosys_step_decode(bContext *C, Main *bmain, UndoStack *ustack, UndoStep *us, const eUndoStepDir dir, bool is_final)
UndoStep * BKE_undosys_stack_init_or_active_with_type(UndoStack *ustack, const UndoType *ut)
UndoStep * BKE_undosys_step_push_init(UndoStack *ustack, bContext *C, const char *name)
void BKE_undosys_stack_group_begin(UndoStack *ustack)
size_t memory_limit