Blender V5.3
blender_undo.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
11
12#ifndef _WIN32
13# include <unistd.h> /* for read close */
14#else
15# include <io.h> /* for open close read */
16#endif
17
18#include <cerrno>
19#include <cstdio>
20#include <cstdlib>
21#include <cstring>
22#include <fcntl.h> /* for open */
23
24#include "DNA_userdef_types.h"
25
26#include "BLI_path_utils.hh"
27#include "BLI_string.hh"
28
29#include "BKE_appdir.hh"
30#include "BKE_blender_undo.hh" /* own include */
31#include "BKE_blendfile.hh"
32#include "BKE_context.hh"
33#include "BKE_global.hh"
34#include "BKE_main.hh"
35#include "BKE_undo_system.hh"
36
37#include "BLO_readfile.hh"
38#include "BLO_undofile.hh"
39#include "BLO_writefile.hh"
40
41#include "DEG_depsgraph.hh"
42
43namespace blender {
44
45/* -------------------------------------------------------------------- */
48
49#define UNDO_DISK 0
50
52 const eUndoStepDir undo_direction,
53 const bool use_old_bmain_data,
54 bContext *C)
55{
56 Main *bmain = CTX_data_main(C);
57 char mainstr[sizeof(bmain->filepath)];
58 int success = 0, fileflags;
59
60 STRNCPY(mainstr, BKE_main_blendfile_path(bmain)); /* temporal store */
61
62 fileflags = G.fileflags;
63 G.fileflags |= G_FILE_NO_UI;
64
65 if (UNDO_DISK) {
66 const BlendFileReadParams params{};
67 BlendFileReadReport bf_reports{};
68 BlendFileData *bfd = BKE_blendfile_read(mfu->filepath, &params, &bf_reports);
69 if (bfd != nullptr) {
70 BKE_blendfile_read_setup_undo(C, bfd, &params, &bf_reports);
71 success = true;
72 }
73 }
74 else {
75 BlendFileReadParams params = {0};
76 params.undo_direction = undo_direction;
77 if (!use_old_bmain_data) {
79 }
80 BlendFileReadReport blend_file_read_report{};
81 BlendFileData *bfd = BKE_blendfile_read_from_memfile(bmain, &mfu->memfile, &params, nullptr);
82 if (bfd != nullptr) {
83 BKE_blendfile_read_setup_undo(C, bfd, &params, &blend_file_read_report);
84 success = true;
85 }
86 }
87
88 /* Restore, bmain has been re-allocated. */
89 bmain = CTX_data_main(C);
90 STRNCPY(bmain->filepath, mainstr);
91 G.fileflags = fileflags;
92
93 if (success) {
94 /* important not to update time here, else non keyed transforms are lost */
95 DEG_tag_on_visible_update(bmain, false);
96 }
97
98 return success;
99}
100
102{
104
105 /* This flag used to be set because the undo step was written as #BLENDER_QUIT_FILE. It's not
106 * clear whether there are still good reasons to keep it. Undo can also be thought of as a kind
107 * of recovery, so better keep it for now. */
108 const int fileflags = G.fileflags | G_FILE_RECOVER_WRITE;
109
110 /* disk save version */
111 if (UNDO_DISK) {
112 static int counter = 0;
113 char filepath[FILE_MAX];
114 char numstr[32];
115
116 /* Calculate current filepath. */
117 counter++;
118 counter = counter % U.undosteps;
119
120 SNPRINTF(numstr, "%d.blend", counter);
121 BLI_path_join(filepath, sizeof(filepath), BKE_tempdir_session(), numstr);
122
123 const BlendFileWriteParams blend_file_write_params{};
124 /* success = */ /* UNUSED */ BLO_write_file(
125 bmain, filepath, fileflags, &blend_file_write_params, nullptr);
126
127 STRNCPY(mfu->filepath, filepath);
128 }
129 else {
130 MemFile *prevfile = (mfu_prev) ? &(mfu_prev->memfile) : nullptr;
131 if (prevfile) {
132 BLO_memfile_clear_future(prevfile);
133 }
134 /* success = */ /* UNUSED */ BLO_write_file_mem(bmain, prevfile, &mfu->memfile, fileflags);
135 mfu->undo_size = mfu->memfile.size;
136 }
137
138 bmain->is_memfile_undo_written = true;
139
140 return mfu;
141}
142
144{
146 MEM_delete(mfu);
147}
148
150
151} // namespace blender
#define FILE_MAX
#define BLI_path_join(...)
#define SNPRINTF(dst, format,...)
external readfile function prototypes.
external writefile.cc function prototypes.
#define C
Definition RandGen.cpp:29
#define UNDO_DISK
BMesh Iterators.
UserDef U
Definition blender.cc:58
char * STRNCPY(char(&dst)[N], const char *src)
bool BLO_write_file(Main *mainvar, const char *filepath, int write_flags, const BlendFileWriteParams *params, ReportList *reports)
bool BKE_memfile_undo_decode(MemFileUndoData *mfu, eUndoStepDir undo_direction, bool use_old_bmain_data, bContext *C)
const char * BKE_tempdir_session() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1283
void BLO_memfile_free(MemFile *memfile)
Definition undofile.cc:43
void DEG_tag_on_visible_update(Main *bmain, bool do_time)
Global G
Definition blender.cc:57
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
MemFileUndoData * BKE_memfile_undo_encode(Main *bmain, MemFileUndoData *mfu_prev)
@ G_FILE_RECOVER_WRITE
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags)
BlendFileData * BKE_blendfile_read_from_memfile(Main *bmain, MemFile *memfile, const BlendFileReadParams *params, ReportList *reports)
void BKE_blendfile_read_setup_undo(bContext *C, BlendFileData *bfd, const BlendFileReadParams *params, BlendFileReadReport *reports)
@ BLO_READ_SKIP_UNDO_OLD_MAIN
Main * CTX_data_main(const bContext *C)
BlendFileData * BKE_blendfile_read(const char *filepath, const BlendFileReadParams *params, BlendFileReadReport *reports)
void BKE_memfile_undo_free(MemFileUndoData *mfu)
void BLO_memfile_clear_future(MemFile *memfile)
Definition undofile.cc:94
char filepath[1024]
Definition BKE_main.hh:267
bool is_memfile_undo_written
Definition BKE_main.hh:297