Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / blendfile.c
index 6df008e7ac9f8235386b72ed993302e2f1bc6341..f6dfb7f922c36ff448912b0b730b9f7ae825f3be 100644 (file)
@@ -243,9 +243,8 @@ static void setup_app_data(
        if (bfd->user) {
 
                /* only here free userdef themes... */
-               BKE_blender_userdef_free_data(&U);
-
-               U = *bfd->user;
+               BKE_blender_userdef_data_set_and_free(bfd->user);
+               bfd->user = NULL;
 
                /* Security issue: any blend file could include a USER block.
                 *
@@ -256,8 +255,6 @@ static void setup_app_data(
                 * enable scripts auto-execution by loading a '.blend' file.
                 */
                U.flag |= USER_SCRIPT_AUTOEXEC_DISABLE;
-
-               MEM_freeN(bfd->user);
        }
 
        /* case G_FILE_NO_UI or no screens in file */
@@ -539,6 +536,29 @@ bool BKE_blendfile_userdef_write(const char *filepath, ReportList *reports)
        return ok;
 }
 
+/**
+ * Only write the userdef in a .blend, merging with the existing blend file.
+ * \return success
+ *
+ * \note In the future we should re-evaluate user preferences,
+ * possibly splitting out system/hardware specific prefs.
+ */
+bool BKE_blendfile_userdef_write_app_template(const char *filepath, ReportList *reports)
+{
+       /* if it fails, overwrite is OK. */
+       UserDef *userdef_default = BKE_blendfile_userdef_read(filepath, NULL);
+       if (userdef_default == NULL) {
+               return BKE_blendfile_userdef_write(filepath, reports);
+       }
+
+       BKE_blender_userdef_app_template_data_swap(&U, userdef_default);
+       bool ok = BKE_blendfile_userdef_write(filepath, reports);
+       BKE_blender_userdef_app_template_data_swap(&U, userdef_default);
+       BKE_blender_userdef_data_free(userdef_default, false);
+       MEM_freeN(userdef_default);
+       return ok;
+}
+
 WorkspaceConfigFileData *BKE_blendfile_workspace_config_read(const char *filepath, ReportList *reports)
 {
        BlendFileData *bfd;