[Bf-blender-cvs] [15c36170092] master: Cleanup: simplify file saving logic

Campbell Barton noreply at git.blender.org
Thu Dec 16 06:29:27 CET 2021


Commit: 15c36170092d26b7d6106924270b6e590627fcb6
Author: Campbell Barton
Date:   Thu Dec 16 16:17:26 2021 +1100
Branches: master
https://developer.blender.org/rB15c36170092d26b7d6106924270b6e590627fcb6

Cleanup: simplify file saving logic

Revert part of the fix from 073669dd8588a3b80dfffee98b4f239b4baee8c8
that initialized the file-path on first save as it's no longer needed.

Also remove relbase argument to BLI_path_normalize as the destination
file paths shouldn't use relative locations.

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

M	source/blender/blenloader/BLO_writefile.h
M	source/blender/blenloader/intern/writefile.c
M	source/blender/windowmanager/intern/wm_files.c

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

diff --git a/source/blender/blenloader/BLO_writefile.h b/source/blender/blenloader/BLO_writefile.h
index 57b1199a870..9bc3714ff38 100644
--- a/source/blender/blenloader/BLO_writefile.h
+++ b/source/blender/blenloader/BLO_writefile.h
@@ -60,11 +60,6 @@ struct BlendFileWriteParams {
   uint use_save_versions : 1;
   /** On write, restore paths after editing them (see #BLO_WRITE_PATH_REMAP_RELATIVE). */
   uint use_save_as_copy : 1;
-  /**
-   * Saving from the UI writes into the #Main.filepath, so a check for the `filepath`
-   * not having been set is needed.
-   */
-  uint use_save_first_time : 1;
   uint use_userdef : 1;
   const struct BlendThumbnail *thumb;
 };
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 3709ff4db5f..e0ff70d4b49 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -1318,6 +1318,8 @@ bool BLO_write_file(Main *mainvar,
                     const struct BlendFileWriteParams *params,
                     ReportList *reports)
 {
+  BLI_assert(!BLI_path_is_rel(filepath));
+
   char tempname[FILE_MAX + 1];
   WriteWrap ww;
 
@@ -1326,8 +1328,7 @@ bool BLO_write_file(Main *mainvar,
   const bool use_save_as_copy = params->use_save_as_copy;
   const bool use_userdef = params->use_userdef;
   const BlendThumbnail *thumb = params->thumb;
-  const bool relbase_valid = (mainvar->filepath[0] != '\0') &&
-                             (params->use_save_first_time == false);
+  const bool relbase_valid = (mainvar->filepath[0] != '\0');
 
   /* path backup/restore */
   void *path_list_backup = NULL;
@@ -1351,6 +1352,13 @@ bool BLO_write_file(Main *mainvar,
     return 0;
   }
 
+  if (remap_mode == BLO_WRITE_PATH_REMAP_ABSOLUTE) {
+    /* Paths will already be absolute, no remapping to do. */
+    if (relbase_valid == false) {
+      remap_mode = BLO_WRITE_PATH_REMAP_NONE;
+    }
+  }
+
   /* Remapping of relative paths to new file location. */
   if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) {
 
@@ -1361,14 +1369,20 @@ bool BLO_write_file(Main *mainvar,
       }
     }
 
+    /* The source path only makes sense to set if the file was saved (`relbase_valid`). */
     char dir_src[FILE_MAX];
     char dir_dst[FILE_MAX];
-    BLI_split_dir_part(mainvar->filepath, dir_src, sizeof(dir_src));
-    BLI_split_dir_part(filepath, dir_dst, sizeof(dir_dst));
 
-    /* Just in case there is some subtle difference. */
-    BLI_path_normalize(mainvar->filepath, dir_dst);
-    BLI_path_normalize(mainvar->filepath, dir_src);
+    /* Normalize the paths in case there is some subtle difference (so they can be compared). */
+    if (relbase_valid) {
+      BLI_split_dir_part(mainvar->filepath, dir_src, sizeof(dir_src));
+      BLI_path_normalize(NULL, dir_src);
+    }
+    else {
+      dir_src[0] = '\0';
+    }
+    BLI_split_dir_part(filepath, dir_dst, sizeof(dir_dst));
+    BLI_path_normalize(NULL, dir_dst);
 
     /* Only for relative, not relative-all, as this means making existing paths relative. */
     if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {
@@ -1394,6 +1408,7 @@ bool BLO_write_file(Main *mainvar,
       switch (remap_mode) {
         case BLO_WRITE_PATH_REMAP_RELATIVE:
           /* Saved, make relative paths relative to new location (if possible). */
+          BLI_assert(relbase_valid);
           BKE_bpath_relative_rebase(mainvar, dir_src, dir_dst, NULL);
           break;
         case BLO_WRITE_PATH_REMAP_RELATIVE_ALL:
@@ -1402,6 +1417,7 @@ bool BLO_write_file(Main *mainvar,
           break;
         case BLO_WRITE_PATH_REMAP_ABSOLUTE:
           /* Make all absolute (when requested or unsaved). */
+          BLI_assert(relbase_valid);
           BKE_bpath_absolute_convert(mainvar, dir_src, NULL);
           break;
         case BLO_WRITE_PATH_REMAP_NONE:
diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index 0867ed5303c..7ac9395e16c 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -1813,24 +1813,16 @@ static bool wm_file_write(bContext *C,
 
   ED_editors_flush_edits(bmain);
 
-  /* First time saving. */
-  /* XXX(ton): temp solution to solve bug, real fix coming. */
-  const bool relbase_valid = (bmain->filepath[0] != '\0');
-  if ((relbase_valid == false) && (use_save_as_copy == false)) {
-    STRNCPY(bmain->filepath, filepath);
-  }
-
   /* XXX(ton): temp solution to solve bug, real fix coming. */
   bmain->recovered = 0;
 
-  if (BLO_write_file(CTX_data_main(C),
+  if (BLO_write_file(bmain,
                      filepath,
                      fileflags,
                      &(const struct BlendFileWriteParams){
                          .remap_mode = remap_mode,
                          .use_save_versions = true,
                          .use_save_as_copy = use_save_as_copy,
-                         .use_save_first_time = !relbase_valid,
                          .thumb = thumb,
                      },
                      reports)) {



More information about the Bf-blender-cvs mailing list