Blender  V2.93
blender_undo.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
24 #ifndef _WIN32
25 # include <unistd.h> /* for read close */
26 #else
27 # include <io.h> /* for open close read */
28 #endif
29 
30 #include <errno.h>
31 #include <fcntl.h> /* for open */
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "MEM_guardedalloc.h"
38 
39 #include "DNA_scene_types.h"
40 
41 #include "BLI_path_util.h"
42 #include "BLI_string.h"
43 #include "BLI_utildefines.h"
44 
45 #include "BKE_appdir.h"
46 #include "BKE_blender_undo.h" /* own include */
47 #include "BKE_blendfile.h"
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51 #include "BKE_undo_system.h"
52 
53 #include "BLO_readfile.h"
54 #include "BLO_undofile.h"
55 #include "BLO_writefile.h"
56 
57 #include "DEG_depsgraph.h"
58 
59 /* -------------------------------------------------------------------- */
63 #define UNDO_DISK 0
64 
66  const eUndoStepDir undo_direction,
67  const bool use_old_bmain_data,
68  bContext *C)
69 {
70  Main *bmain = CTX_data_main(C);
71  char mainstr[sizeof(bmain->name)];
72  int success = 0, fileflags;
73 
74  BLI_strncpy(mainstr, BKE_main_blendfile_path(bmain), sizeof(mainstr)); /* temporal store */
75 
76  fileflags = G.fileflags;
77  G.fileflags |= G_FILE_NO_UI;
78 
79  if (UNDO_DISK) {
80  const struct BlendFileReadParams params = {0};
81  struct BlendFileData *bfd = BKE_blendfile_read(mfu->filename, &params, NULL);
82  if (bfd != NULL) {
84  success = true;
85  }
86  }
87  else {
88  struct BlendFileReadParams params = {0};
89  params.undo_direction = undo_direction;
90  if (!use_old_bmain_data) {
91  params.skip_flags |= BLO_READ_SKIP_UNDO_OLD_MAIN;
92  }
94  bmain, &mfu->memfile, &params, NULL);
95  if (bfd != NULL) {
97  success = true;
98  }
99  }
100 
101  /* Restore, bmain has been re-allocated. */
102  bmain = CTX_data_main(C);
103  BLI_strncpy(bmain->name, mainstr, sizeof(bmain->name));
104  G.fileflags = fileflags;
105 
106  if (success) {
107  /* important not to update time here, else non keyed transforms are lost */
108  DEG_on_visible_update(bmain, false);
109  }
110 
111  return success;
112 }
113 
115 {
116  MemFileUndoData *mfu = MEM_callocN(sizeof(MemFileUndoData), __func__);
117 
118  /* Include recovery information since undo-data is written out as #BLENDER_QUIT_FILE. */
119  const int fileflags = G.fileflags | G_FILE_RECOVER_WRITE;
120 
121  /* disk save version */
122  if (UNDO_DISK) {
123  static int counter = 0;
124  char filename[FILE_MAX];
125  char numstr[32];
126 
127  /* Calculate current filename. */
128  counter++;
129  counter = counter % U.undosteps;
130 
131  BLI_snprintf(numstr, sizeof(numstr), "%d.blend", counter);
133 
134  /* success = */ /* UNUSED */ BLO_write_file(
135  bmain, filename, fileflags, &(const struct BlendFileWriteParams){0}, NULL);
136 
137  BLI_strncpy(mfu->filename, filename, sizeof(mfu->filename));
138  }
139  else {
140  MemFile *prevfile = (mfu_prev) ? &(mfu_prev->memfile) : NULL;
141  if (prevfile) {
142  BLO_memfile_clear_future(prevfile);
143  }
144  /* success = */ /* UNUSED */ BLO_write_file_mem(bmain, prevfile, &mfu->memfile, fileflags);
145  mfu->undo_size = mfu->memfile.size;
146  }
147 
148  bmain->is_memfile_undo_written = true;
149 
150  return mfu;
151 }
152 
154 {
155  BLO_memfile_free(&mfu->memfile);
156  MEM_freeN(mfu);
157 }
158 
void BKE_blendfile_read_setup(struct bContext *C, struct BlendFileData *bfd, const struct BlendFileReadParams *params, struct ReportList *reports)
Definition: blendfile.c:459
struct BlendFileData * BKE_blendfile_read_from_memfile(struct Main *bmain, struct MemFile *memfile, const struct BlendFileReadParams *params, struct ReportList *reports)
Definition: blendfile.c:511
struct BlendFileData * BKE_blendfile_read(const char *filepath, const struct BlendFileReadParams *params, struct ReportList *reports)
Definition: blendfile.c:470
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
@ G_FILE_RECOVER_WRITE
Definition: BKE_global.h:190
@ G_FILE_NO_UI
Definition: BKE_global.h:170
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
eUndoStepDir
#define FILE_MAX
void BLI_join_dirfile(char *__restrict dst, const size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1737
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
external readfile function prototypes.
@ BLO_READ_SKIP_UNDO_OLD_MAIN
Definition: BLO_readfile.h:98
void BLO_memfile_clear_future(MemFile *memfile)
Definition: undofile.c:109
void BLO_memfile_free(MemFile *memfile)
Definition: undofile.c:58
external writefile function prototypes.
bool BLO_write_file(struct Main *mainvar, const char *filepath, const int write_flags, const struct BlendFileWriteParams *params, struct ReportList *reports)
Definition: writefile.c:1128
bool BLO_write_file_mem(struct Main *mainvar, struct MemFile *compare, struct MemFile *current, int write_flags)
Definition: writefile.c:1276
void DEG_on_visible_update(struct Main *bmain, const bool do_time)
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define UNDO_DISK
Definition: blender_undo.c:63
MemFileUndoData * BKE_memfile_undo_encode(Main *bmain, MemFileUndoData *mfu_prev)
Definition: blender_undo.c:114
void BKE_memfile_undo_free(MemFileUndoData *mfu)
Definition: blender_undo.c:153
bool BKE_memfile_undo_decode(MemFileUndoData *mfu, const eUndoStepDir undo_direction, const bool use_old_bmain_data, bContext *C)
Definition: blender_undo.c:65
unsigned int U
Definition: btGjkEpa3.h:78
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * BKE_tempdir_session
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
char filename[1024]
Definition: BLO_readfile.h:75
Definition: BKE_main.h:116
char is_memfile_undo_written
Definition: BKE_main.h:125
char name[1024]
Definition: BKE_main.h:118
char filename[1024]
Definition: BLO_undofile.h:63
size_t size
Definition: BLO_undofile.h:48
#define G(x, y, z)