[Bf-blender-cvs] [5812bc7d890] master: Cleanup: split file read and setup into separate steps

Campbell Barton noreply at git.blender.org
Thu Mar 11 14:51:41 CET 2021


Commit: 5812bc7d8908015b8a06d240da7964be0a19a2c3
Author: Campbell Barton
Date:   Fri Mar 12 00:34:21 2021 +1100
Branches: master
https://developer.blender.org/rB5812bc7d8908015b8a06d240da7964be0a19a2c3

Cleanup: split file read and setup into separate steps

Currently file loading performs almost all reloading logic
even in the case loading the file fails, causing the file to be in
a state that isn't well defined: undo is cleared, timers are canceled &
scripts are re-registered.

===================================================================

M	source/blender/blenkernel/BKE_blendfile.h
M	source/blender/blenkernel/intern/blender_undo.c
M	source/blender/blenkernel/intern/blendfile.c
M	source/blender/windowmanager/intern/wm_files.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_blendfile.h b/source/blender/blenkernel/BKE_blendfile.h
index f73c4a70809..429e294a337 100644
--- a/source/blender/blenkernel/BKE_blendfile.h
+++ b/source/blender/blenkernel/BKE_blendfile.h
@@ -23,6 +23,7 @@
 extern "C" {
 #endif
 
+struct BlendFileData;
 struct BlendFileReadParams;
 struct ID;
 struct Main;
@@ -31,36 +32,32 @@ struct ReportList;
 struct UserDef;
 struct bContext;
 
-bool BKE_blendfile_read_ex(struct bContext *C,
-                           const char *filepath,
-                           const struct BlendFileReadParams *params,
-                           struct ReportList *reports,
-                           /* Extra args. */
-                           const bool startup_update_defaults,
-                           const char *startup_app_template);
-bool BKE_blendfile_read(struct bContext *C,
-                        const char *filepath,
-                        const struct BlendFileReadParams *params,
-                        struct ReportList *reports);
+void BKE_blendfile_read_setup_ex(struct bContext *C,
+                                 struct BlendFileData *bfd,
+                                 const struct BlendFileReadParams *params,
+                                 struct ReportList *reports,
+                                 /* Extra args. */
+                                 const bool startup_update_defaults,
+                                 const char *startup_app_template);
 
-bool BKE_blendfile_read_from_memory_ex(struct bContext *C,
-                                       const void *filebuf,
-                                       int filelength,
-                                       const struct BlendFileReadParams *params,
-                                       struct ReportList *reports,
-                                       /* Extra args. */
-                                       const bool startup_update_defaults,
-                                       const char *startup_app_template);
-bool BKE_blendfile_read_from_memory(struct bContext *C,
-                                    const void *filebuf,
-                                    int filelength,
-                                    const struct BlendFileReadParams *params,
-                                    struct ReportList *reports);
+void BKE_blendfile_read_setup(struct bContext *C,
+                              struct BlendFileData *bfd,
+                              const struct BlendFileReadParams *params,
+                              struct ReportList *reports);
 
-bool BKE_blendfile_read_from_memfile(struct bContext *C,
-                                     struct MemFile *memfile,
-                                     const struct BlendFileReadParams *params,
-                                     struct ReportList *reports);
+struct BlendFileData *BKE_blendfile_read(const char *filepath,
+                                         const struct BlendFileReadParams *params,
+                                         struct ReportList *reports);
+
+struct BlendFileData *BKE_blendfile_read_from_memory(const void *filebuf,
+                                                     int filelength,
+                                                     const struct BlendFileReadParams *params,
+                                                     struct ReportList *reports);
+
+struct BlendFileData *BKE_blendfile_read_from_memfile(struct Main *bmain,
+                                                      struct MemFile *memfile,
+                                                      const struct BlendFileReadParams *params,
+                                                      struct ReportList *reports);
 void BKE_blendfile_read_make_empty(struct bContext *C);
 
 struct UserDef *BKE_blendfile_userdef_read(const char *filepath, struct ReportList *reports);
diff --git a/source/blender/blenkernel/intern/blender_undo.c b/source/blender/blenkernel/intern/blender_undo.c
index d826aaf24e3..9f8a30722c2 100644
--- a/source/blender/blenkernel/intern/blender_undo.c
+++ b/source/blender/blenkernel/intern/blender_undo.c
@@ -77,7 +77,12 @@ bool BKE_memfile_undo_decode(MemFileUndoData *mfu,
   G.fileflags |= G_FILE_NO_UI;
 
   if (UNDO_DISK) {
-    success = BKE_blendfile_read(C, mfu->filename, &(const struct BlendFileReadParams){0}, NULL);
+    const struct BlendFileReadParams params = {0};
+    struct BlendFileData *bfd = BKE_blendfile_read(mfu->filename, &params, NULL);
+    if (bfd != NULL) {
+      BKE_blendfile_read_setup(C, bfd, &params, NULL);
+      success = true;
+    }
   }
   else {
     struct BlendFileReadParams params = {0};
@@ -85,7 +90,12 @@ bool BKE_memfile_undo_decode(MemFileUndoData *mfu,
     if (!use_old_bmain_data) {
       params.skip_flags |= BLO_READ_SKIP_UNDO_OLD_MAIN;
     }
-    success = BKE_blendfile_read_from_memfile(C, &mfu->memfile, &params, NULL);
+    struct BlendFileData *bfd = BKE_blendfile_read_from_memfile(
+        bmain, &mfu->memfile, &params, NULL);
+    if (bfd != NULL) {
+      BKE_blendfile_read_setup(C, bfd, &params, NULL);
+      success = true;
+    }
   }
 
   /* Restore, bmain has been re-allocated. */
diff --git a/source/blender/blenkernel/intern/blendfile.c b/source/blender/blenkernel/intern/blendfile.c
index c3edd84fd43..d3dfe60c444 100644
--- a/source/blender/blenkernel/intern/blendfile.c
+++ b/source/blender/blenkernel/intern/blendfile.c
@@ -429,15 +429,46 @@ static void handle_subversion_warning(Main *main, ReportList *reports)
   }
 }
 
-bool BKE_blendfile_read_ex(bContext *C,
-                           const char *filepath,
-                           const struct BlendFileReadParams *params,
-                           ReportList *reports,
-                           /* Extra args. */
-                           const bool startup_update_defaults,
-                           const char *startup_app_template)
+/**
+ * Shared setup function that makes the data from `bfd` into the current blend file,
+ * replacing the contents of #G.main.
+ * This uses the bfd #BKE_blendfile_read and similarly named functions.
+ *
+ * This is done in a separate step so the caller may perform actions after it is known the file
+ * loaded correctly but before the file replaces the existing blend file contents.
+ */
+void BKE_blendfile_read_setup_ex(bContext *C,
+                                 BlendFileData *bfd,
+                                 const struct BlendFileReadParams *params,
+                                 ReportList *reports,
+                                 /* Extra args. */
+                                 const bool startup_update_defaults,
+                                 const char *startup_app_template)
 {
+  if (startup_update_defaults) {
+    if ((params->skip_flags & BLO_READ_SKIP_DATA) == 0) {
+      BLO_update_defaults_startup_blend(bfd->main, startup_app_template);
+    }
+  }
+  setup_app_blend_file_data(C, bfd, params, reports);
+  BLO_blendfiledata_free(bfd);
+}
 
+void BKE_blendfile_read_setup(bContext *C,
+                              BlendFileData *bfd,
+                              const struct BlendFileReadParams *params,
+                              ReportList *reports)
+{
+  BKE_blendfile_read_setup_ex(C, bfd, params, reports, false, NULL);
+}
+
+/**
+ * \return Blend file data, this must be passed to #BKE_blendfile_read_setup when non-NULL.
+ */
+struct BlendFileData *BKE_blendfile_read(const char *filepath,
+                                         const struct BlendFileReadParams *params,
+                                         ReportList *reports)
+{
   /* Don't print startup file loading. */
   if (params->is_startup == false) {
     printf("Read blend: %s\n", filepath);
@@ -446,69 +477,40 @@ bool BKE_blendfile_read_ex(bContext *C,
   BlendFileData *bfd = BLO_read_from_file(filepath, params->skip_flags, reports);
   if (bfd) {
     handle_subversion_warning(bfd->main, reports);
-    if (startup_update_defaults) {
-      if ((params->skip_flags & BLO_READ_SKIP_DATA) == 0) {
-        BLO_update_defaults_startup_blend(bfd->main, startup_app_template);
-      }
-    }
-    setup_app_blend_file_data(C, bfd, params, reports);
-    BLO_blendfiledata_free(bfd);
   }
   else {
     BKE_reports_prependf(reports, "Loading '%s' failed: ", filepath);
   }
-  return (bfd != NULL);
+  return bfd;
 }
 
-bool BKE_blendfile_read(bContext *C,
-                        const char *filepath,
-                        const struct BlendFileReadParams *params,
-                        ReportList *reports)
-{
-  return BKE_blendfile_read_ex(C, filepath, params, reports, false, NULL);
-}
-
-bool BKE_blendfile_read_from_memory_ex(bContext *C,
-                                       const void *filebuf,
-                                       int filelength,
-                                       const struct BlendFileReadParams *params,
-                                       ReportList *reports,
-                                       /* Extra args. */
-                                       const bool startup_update_defaults,
-                                       const char *startup_app_template)
+/**
+ * \return Blend file data, this must be passed to #BKE_blendfile_read_setup when non-NULL.
+ */
+struct BlendFileData *BKE_blendfile_read_from_memory(const void *filebuf,
+                                                     int filelength,
+                                                     const struct BlendFileReadParams *params,
+                                                     ReportList *reports)
 {
   BlendFileData *bfd = BLO_read_from_memory(filebuf, filelength, params->skip_flags, reports);
   if (bfd) {
-    if (startup_update_defaults) {
-      if ((params->skip_flags & BLO_READ_SKIP_DATA) == 0) {
-        BLO_update_defaults_startup_blend(bfd->main, startup_app_template);
-      }
-    }
-    setup_app_blend_file_data(C, bfd, params, reports);
-    BLO_blendfiledata_free(bfd);
+    /* Pass. */
   }
   else {
     BKE_reports_prepend(reports, "Loading failed: ");
   }
-  return (bfd != NULL);
+  return bfd;
 }
 
-bool BKE_blendfile_read_from_memory(bContext *C,
-                                    const void *filebuf,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list