Fixes some cases where initial sculpt step would not be undone (see the
view3d_sculpt_with_memfile_step test in
lib/tests/ui_simulate/test_undo.py).
Part of T83806.
From a design point of view, this commit formalizes the difference
between 'stateful' undo types (which steps can be loaded, replacing all
existing data whithin their context), and 'differential' undo types
(which steps only store differences from the previous step, and can only
be applied or un-applied from a neighbor state). See also initial
documentation https://wiki.blender.org/wiki/Source/Architecture/Undo .
This commit also transfers the responsibility of dealing with
differential steps to main BKE_undo_system code. Since the undo stack
can now mix different kind of steps, decode callbacks of each type
should not have to deal with undostack at all anymore.
Finally, this commit also fixes the extremely weak and bad hack of
calling undosys_step_decode recursively to load a previous memfile
step to restore Main data-base IDs to expected state for future
non-memfile undo steps (the main #ifdef WITH_GLOBAL_UNDO_CORRECT_ORDER
block at the start of this function).
As a final note, Image undo type should likely also be a differential
one. However, it is currently close to impossible to fully track down
its code, and needs first a proper refactor in itself. See T85797 for
more details about this.
To test this change and its fixes, remove the early return in view3d_sculpt_with_memfile_step (lib/tests/ui_simulate/test_undo.py), and use:
path/to/lib/tests/ui_simulate/run.py --blender "path/to/bin/blender" --tests 'test_undo.view3d_sculpt_with_memfile_step'
This test fails with current master, and is fixed by this patch.
After a full day of frustrating attempts to simplify image undo code in the same way as we do for Sculpt one in this patch, I stopped trying (see also T85797). Especially since current Image undo code has no known issue, besides being a nightmare.