Refactor BKE_blender into separate headers
authorCampbell Barton <ideasman42@gmail.com>
Sun, 24 Apr 2016 12:42:41 +0000 (22:42 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 25 Apr 2016 09:27:45 +0000 (19:27 +1000)
- BKE_blender_version.h (only version defines & versionstr).
- BKE_blender_copybuffer.h (currently only used for view3d copy/paste).
- BKE_blender_undo.h (global undo functions).
- BKE_blendfile.h (high level blend file read/write API).

49 files changed:
build_files/buildbot/slave_compile.py
build_files/buildbot/slave_pack.py
build_files/cmake/macros.cmake
doc/python_api/sphinx_doc_gen.sh
source/blender/blenkernel/BKE_blender.h
source/blender/blenkernel/BKE_blender_copybuffer.h [new file with mode: 0644]
source/blender/blenkernel/BKE_blender_undo.h [new file with mode: 0644]
source/blender/blenkernel/BKE_blender_version.h [new file with mode: 0644]
source/blender/blenkernel/BKE_blendfile.h [new file with mode: 0644]
source/blender/blenkernel/CMakeLists.txt
source/blender/blenkernel/intern/appdir.c
source/blender/blenkernel/intern/blender.c
source/blender/blenkernel/intern/blender_copybuffer.c [new file with mode: 0644]
source/blender/blenkernel/intern/blender_undo.c [new file with mode: 0644]
source/blender/blenkernel/intern/blendfile.c [new file with mode: 0644]
source/blender/blenkernel/intern/pointcache.c
source/blender/blenloader/intern/readfile.c
source/blender/blenloader/intern/writefile.c
source/blender/collada/DocumentExporter.cpp
source/blender/collada/MeshImporter.cpp
source/blender/editors/gpencil/gpencil_undo.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/resources.c
source/blender/editors/object/object_bake.c
source/blender/editors/physics/dynamicpaint_ops.c
source/blender/editors/render/render_internal.c
source/blender/editors/sculpt_paint/paint_undo.c
source/blender/editors/space_image/image_buttons.c
source/blender/editors/space_image/image_ops.c
source/blender/editors/space_info/info_stats.c
source/blender/editors/space_view3d/view3d_ops.c
source/blender/editors/util/editmode_undo.c
source/blender/editors/util/undo.c
source/blender/makesdna/DNA_fileglobal_types.h
source/blender/makesrna/intern/rna_userdef.c
source/blender/nodes/shader/node_shader_util.h
source/blender/nodes/texture/node_texture_util.h
source/blender/python/intern/bpy.c
source/blender/python/intern/bpy_app.c
source/blender/python/intern/bpy_library_write.c
source/blender/windowmanager/intern/wm_files.c
source/blender/windowmanager/intern/wm_init_exit.c
source/blender/windowmanager/intern/wm_operators.c
source/blender/windowmanager/intern/wm_window.c
source/creator/creator.c
source/creator/creator_args.c
source/creator/creator_signals.c
source/gameengine/GamePlayer/ghost/GPG_ghost.cpp
source/gameengine/Ketsji/KX_PythonInit.cpp

index 0f34551cfc89548a65dcebff12f60e3a6451112c..4d7793d60c891fed976b6d8f10913ff4059c19a1 100644 (file)
@@ -188,7 +188,7 @@ if 'cmake' in builder:
             sys.exit(retcode)
 
         if builder.startswith('linux') and target == 'cuda':
-            blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender.h")
+            blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender_version.h")
             blender_version = int(parse_header_file(blender_h, 'BLENDER_VERSION'))
             blender_version = "%d.%d" % (blender_version // 100, blender_version % 100)
             kernels = os.path.join(target_build_dir, 'intern', 'cycles', 'kernel')
index 4deb0b1a081abcc1569d727e5c699fdc73b512da..b27017568cb9ee713fc3867cec29e69fe6b1828a 100644 (file)
@@ -130,7 +130,7 @@ if builder.find('cmake') != -1:
         blenderplayer = os.path.join(install_dir, 'blenderplayer')
 
         buildinfo_h = os.path.join(build_dir, "source", "creator", "buildinfo.h")
-        blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender.h")
+        blender_h = os.path.join(blender_dir, "source", "blender", "blenkernel", "BKE_blender_version.h")
 
         # Get version information
         blender_version = int(parse_header_file(blender_h, 'BLENDER_VERSION'))
index f26b22a9c5f13a1383e64129fdbcd267e9997c27..7f19e22c39e17dfe7cfde7c94984998fab6680d2 100644 (file)
@@ -1096,10 +1096,10 @@ function(get_blender_version)
        # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
 
        # So cmake depends on BKE_blender.h, beware of inf-loops!
-       CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h
-                      ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
+       CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender_version.h
+                      ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender_version.h.done)
 
-       file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
+       file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender_version.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
 
        string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
        string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
index 697cd5a9b3f7a3630dc1e706fd08d9dc8872ff74..7095808f2516f3584df3b2c0b57364977ae90cef 100755 (executable)
@@ -33,10 +33,10 @@ SSH_UPLOAD="/data/www/vhosts/www.blender.org/api" # blender_python_api_VERSION,
 # "_".join(str(v) for v in bpy.app.version)
 # custom blender vars
 blender_srcdir=$(dirname -- $0)/../..
-blender_version=$(grep "BLENDER_VERSION\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender.h" | awk '{print $3}')
-blender_version_char=$(grep "BLENDER_VERSION_CHAR\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender.h" | awk '{print $3}')
-blender_version_cycle=$(grep "BLENDER_VERSION_CYCLE\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender.h" | awk '{print $3}')
-blender_subversion=$(grep "BLENDER_SUBVERSION\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender.h" | awk '{print $3}')
+blender_version=$(grep "BLENDER_VERSION\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender_version.h" | awk '{print $3}')
+blender_version_char=$(grep "BLENDER_VERSION_CHAR\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender_version.h" | awk '{print $3}')
+blender_version_cycle=$(grep "BLENDER_VERSION_CYCLE\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender_version.h" | awk '{print $3}')
+blender_subversion=$(grep "BLENDER_SUBVERSION\s" "$blender_srcdir/source/blender/blenkernel/BKE_blender_version.h" | awk '{print $3}')
 
 if [ "$blender_version_cycle" = "release" ] ; then
        BLENDER_VERSION=$(expr $blender_version / 100)_$(expr $blender_version % 100)$blender_version_char"_release"
index fdb34743f3626398a49c1d868490f5befbdf71f4..8ce85c8e6155641a1ec9a3f2f290acdcf928111a 100644 (file)
 extern "C" {
 #endif
 
-/* these lines are grep'd, watch out for our not-so-awesome regex
- * and keep comment above the defines.
- * Use STRINGIFY() rather than defining with quotes */
-#define BLENDER_VERSION         277
-#define BLENDER_SUBVERSION      0
-/* Several breakages with 270, e.g. constraint deg vs rad */
-#define BLENDER_MINVERSION      270
-#define BLENDER_MINSUBVERSION   6
+void BKE_blender_free(void);
 
-/* used by packaging tools */
-/* can be left blank, otherwise a,b,c... etc with no quotes */
-#define BLENDER_VERSION_CHAR   
-/* alpha/beta/rc/release, docs use this */
-#define BLENDER_VERSION_CYCLE   alpha
+void BKE_blender_globals_init(void);
+void BKE_blender_globals_clear(void);
 
-extern char versionstr[]; /* from blender.c */
-
-struct MemFile;
-struct bContext;
-struct ReportList;
-struct Scene;
-struct Main;
-struct ID;
-
-int BKE_read_file(struct bContext *C, const char *filepath, struct ReportList *reports);
-
-#define BKE_READ_FILE_FAIL              0 /* no load */
-#define BKE_READ_FILE_OK                1 /* OK */
-#define BKE_READ_FILE_OK_USERPREFS      2 /* OK, and with new user settings */
-
-bool BKE_read_file_from_memory(
-        struct bContext *C, const void *filebuf,
-        int filelength, struct ReportList *reports, bool update_defaults);
-bool BKE_read_file_from_memfile(
-        struct bContext *C, struct MemFile *memfile,
-        struct ReportList *reports);
-
-int BKE_read_file_userdef(const char *filepath, struct ReportList *reports);
-int BKE_write_file_userdef(const char *filepath, struct ReportList *reports);
-
-void free_blender(void);
-void initglobals(void);
-
-/* load new userdef from file, exit blender */
-void BKE_userdef_free(void);
-/* handle changes in userdef */
-void BKE_userdef_state(void);
+void BKE_blender_userdef_free(void);
+void BKE_blender_userdef_refresh(void);
        
 /* set this callback when a UI is running */
 void BKE_blender_callback_test_break_set(void (*func)(void));
-int blender_test_break(void);
-
-#define BKE_UNDO_STR_MAX 64
-
-/* global undo */
-extern void          BKE_undo_write(struct bContext *C, const char *name);
-extern void          BKE_undo_step(struct bContext *C, int step);
-extern void          BKE_undo_name(struct bContext *C, const char *name);
-extern bool          BKE_undo_is_valid(const char *name);
-extern void          BKE_undo_reset(void);
-extern void          BKE_undo_number(struct bContext *C, int nr);
-extern const char   *BKE_undo_get_name(int nr, bool *r_active);
-extern bool          BKE_undo_save_file(const char *filename);
-extern struct Main  *BKE_undo_get_main(struct Scene **r_scene);
-
-/* partial blend file writing */
-void BKE_blendfile_write_partial_tag_ID(struct ID *id, bool set);
-void BKE_blendfile_write_partial_begin(struct Main *bmain_src);
-bool BKE_blendfile_write_partial(
-        struct Main *bmain_src, const char *filepath, const int write_flags, struct ReportList *reports);
-void BKE_blendfile_write_partial_end(struct Main *bmain_src);
-
-
-/* copybuffer (wrapper for BKE_blendfile_write_partial) */
-void BKE_copybuffer_begin(struct Main *bmain_src);
-void BKE_copybuffer_tag_ID(struct ID *id);
-bool BKE_copybuffer_save(struct Main *bmain_src, const char *filename, struct ReportList *reports);
-bool BKE_copybuffer_paste(struct bContext *C, const char *libname, const short flag, struct ReportList *reports);
+int  BKE_blender_test_break(void);
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif
-
+#endif  /* __BKE_BLENDER_H__ */
diff --git a/source/blender/blenkernel/BKE_blender_copybuffer.h b/source/blender/blenkernel/BKE_blender_copybuffer.h
new file mode 100644 (file)
index 0000000..8aaf295
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef __BKE_BLENDER_COPYBUFFER_H__
+#define __BKE_BLENDER_COPYBUFFER_H__
+
+/** \file BKE_blender_copybuffer.h
+ *  \ingroup bke
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bContext;
+struct ReportList;
+struct Main;
+struct ID;
+
+/* copybuffer (wrapper for BKE_blendfile_write_partial) */
+void BKE_copybuffer_begin(struct Main *bmain_src);
+void BKE_copybuffer_tag_ID(struct ID *id);
+bool BKE_copybuffer_save(struct Main *bmain_src, const char *filename, struct ReportList *reports);
+bool BKE_copybuffer_paste(struct bContext *C, const char *libname, const short flag, struct ReportList *reports);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  /* __BKE_BLENDER_COPYBUFFER_H__ */
diff --git a/source/blender/blenkernel/BKE_blender_undo.h b/source/blender/blenkernel/BKE_blender_undo.h
new file mode 100644 (file)
index 0000000..cd18bd8
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef __BKE_BLENDER_UNDO_H__
+#define __BKE_BLENDER_UNDO_H__
+
+/** \file BKE_blender_undo.h
+ *  \ingroup bke
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bContext;
+struct Scene;
+struct Main;
+
+#define BKE_UNDO_STR_MAX 64
+
+/* global undo */
+extern void          BKE_undo_write(struct bContext *C, const char *name);
+extern void          BKE_undo_step(struct bContext *C, int step);
+extern void          BKE_undo_name(struct bContext *C, const char *name);
+extern bool          BKE_undo_is_valid(const char *name);
+extern void          BKE_undo_reset(void);
+extern void          BKE_undo_number(struct bContext *C, int nr);
+extern const char   *BKE_undo_get_name(int nr, bool *r_active);
+extern bool          BKE_undo_save_file(const char *filename);
+extern struct Main  *BKE_undo_get_main(struct Scene **r_scene);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  /* __BKE_BLENDER_UNDO_H__ */
diff --git a/source/blender/blenkernel/BKE_blender_version.h b/source/blender/blenkernel/BKE_blender_version.h
new file mode 100644 (file)
index 0000000..5e7fdb9
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef __BKE_BLENDER_VERSION_H__
+#define __BKE_BLENDER_VERSION_H__
+
+/** \file BKE_blender_version.h
+ *  \ingroup bke
+ */
+
+/* these lines are grep'd, watch out for our not-so-awesome regex
+ * and keep comment above the defines.
+ * Use STRINGIFY() rather than defining with quotes */
+#define BLENDER_VERSION         277
+#define BLENDER_SUBVERSION      0
+/* Several breakages with 270, e.g. constraint deg vs rad */
+#define BLENDER_MINVERSION      270
+#define BLENDER_MINSUBVERSION   6
+
+/* used by packaging tools */
+/* can be left blank, otherwise a,b,c... etc with no quotes */
+#define BLENDER_VERSION_CHAR
+/* alpha/beta/rc/release, docs use this */
+#define BLENDER_VERSION_CYCLE   alpha
+
+extern char versionstr[]; /* from blender.c */
+
+#endif  /* __BKE_BLENDER_VERSION_H__ */
diff --git a/source/blender/blenkernel/BKE_blendfile.h b/source/blender/blenkernel/BKE_blendfile.h
new file mode 100644 (file)
index 0000000..6767fce
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef __BKE_BLENDFILE_H__
+#define __BKE_BLENDFILE_H__
+
+/** \file BKE_blendfile.h
+ *  \ingroup bke
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bContext;
+struct ID;
+struct Main;
+struct MemFile;
+struct ReportList;
+
+int BKE_blendfile_read(struct bContext *C, const char *filepath, struct ReportList *reports);
+
+enum {
+       BKE_BLENDFILE_READ_FAIL             = 0, /* no load */
+       BKE_BLENDFILE_READ_OK               = 1, /* OK */
+       BKE_BLENDFILE_READ_OK_USERPREFS     = 2, /* OK, and with new user settings */
+};
+
+bool BKE_blendfile_read_from_memory(
+        struct bContext *C, const void *filebuf,
+        int filelength, struct ReportList *reports, bool update_defaults);
+bool BKE_blendfile_read_from_memfile(
+        struct bContext *C, struct MemFile *memfile,
+        struct ReportList *reports);
+
+int BKE_blendfile_read_userdef(const char *filepath, struct ReportList *reports);
+int BKE_blendfile_write_userdef(const char *filepath, struct ReportList *reports);
+
+
+/* partial blend file writing */
+void BKE_blendfile_write_partial_tag_ID(struct ID *id, bool set);
+void BKE_blendfile_write_partial_begin(struct Main *bmain_src);
+bool BKE_blendfile_write_partial(
+        struct Main *bmain_src, const char *filepath, const int write_flags, struct ReportList *reports);
+void BKE_blendfile_write_partial_end(struct Main *bmain_src);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  /* __BKE_BLENDFILE_H__ */
index 2bffb16e03497863eec686f119803d7df0732dff..afab0ccc5a55d84834bd5448c193fc7adfd27988 100644 (file)
@@ -75,6 +75,9 @@ set(SRC
        intern/armature_update.c
        intern/autoexec.c
        intern/blender.c
+       intern/blender_copybuffer.c
+       intern/blender_undo.c
+       intern/blendfile.c
        intern/bmfont.c
        intern/boids.c
        intern/bpath.c
@@ -194,6 +197,10 @@ set(SRC
        BKE_armature.h
        BKE_autoexec.h
        BKE_blender.h
+       BKE_blender_copybuffer.h
+       BKE_blender_undo.h
+       BKE_blender_version.h
+       BKE_blendfile.h
        BKE_bmfont.h
        BKE_bmfont_types.h
        BKE_boids.h
index de21d9105e23baafef72864c4dca46c80bc108f5..b1dcc40279f490242e468cdf3f8129643d6c07e9 100644 (file)
@@ -32,7 +32,7 @@
 #include "BLI_fileops.h"
 #include "BLI_path_util.h"
 
-#include "BKE_blender.h"  /* BLENDER_VERSION */
+#include "BKE_blender_version.h"
 #include "BKE_appdir.h"  /* own include */
 
 #include "GHOST_Path-api.h"
index baf93ffd824ab8717c5ec13c4a9610ad57f34d0e..15492fbd20d7f3a8bfa49a57287df545b1e48db2 100644 (file)
 
 /** \file blender/blenkernel/intern/blender.c
  *  \ingroup bke
+ *
+ * Application level startup/shutdown functionality.
  */
 
-#ifndef _GNU_SOURCE
-/* Needed for O_NOFOLLOW on some platforms. */
-#  define _GNU_SOURCE 1
-#endif
-
-#ifndef _WIN32 
-#  include <unistd.h> // for read close
-#else
-#  include <io.h> // for open close read
-#endif
-
 #include <stdlib.h>
 #include <stdio.h>
-#include <stddef.h>
 #include <string.h>
-#include <fcntl.h>  /* for open */
-#include <errno.h>
 
 #include "MEM_guardedalloc.h"
 
-#include "DNA_userdef_types.h"
-#include "DNA_scene_types.h"
-#include "DNA_screen_types.h"
-#include "DNA_view3d_types.h"
-#include "DNA_windowmanager_types.h"
-
-#include "BLI_blenlib.h"
+#include "BLI_string.h"
+#include "BLI_listbase.h"
 #include "BLI_utildefines.h"
 #include "BLI_callbacks.h"
 
 #include "IMB_imbuf.h"
 #include "IMB_moviecache.h"
 
-#include "BKE_appdir.h"
-#include "BKE_blender.h"
-#include "BKE_bpath.h"
+#include "BKE_blender.h"  /* own include */
+#include "BKE_blender_version.h"  /* own include */
+#include "BKE_blendfile.h"
 #include "BKE_brush.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_global.h"
 #include "BKE_idprop.h"
 #include "BKE_image.h"
-#include "BKE_ipo.h"
 #include "BKE_library.h"
-#include "BKE_main.h"
 #include "BKE_node.h"
 #include "BKE_report.h"
 #include "BKE_scene.h"
 #include "BKE_screen.h"
 #include "BKE_sequencer.h"
-#include "BKE_sound.h"
 
 #include "RE_pipeline.h"
 #include "RE_render_ext.h"
 
 #include "BLF_api.h"
 
-#include "BLO_undofile.h"
-#include "BLO_readfile.h" 
-#include "BLO_writefile.h" 
-
-#include "RNA_access.h"
-
-#include "WM_api.h" // XXXXX BAD, very BAD dependency (bad level call) - remove asap, elubie
-
-#include "IMB_colormanagement.h"
-
-#ifdef WITH_PYTHON
-#  include "BPY_extern.h"
-#endif
 
 Global G;
 UserDef U;
-/* ListBase = {NULL, NULL}; */
 
 char versionstr[48] = "";
 
 /* ********** free ********** */
 
 /* only to be called on exit blender */
-void free_blender(void)
+void BKE_blender_free(void)
 {
        /* samples are in a global list..., also sets G.main->sound->sample NULL */
        BKE_main_free(G.main);
@@ -132,7 +98,7 @@ void free_blender(void)
        free_nodesystem();
 }
 
-void initglobals(void)
+void BKE_blender_globals_init(void)
 {
        memset(&G, 0, sizeof(Global));
        
@@ -154,300 +120,14 @@ void initglobals(void)
 #endif
 }
 
-/***/
-
-static void clear_global(void) 
+void BKE_blender_globals_clear(void)
 {
-//     extern short winqueue_break;    /* screen.c */
-
        BKE_main_free(G.main);          /* free all lib data */
-       
-//     free_vertexpaint();
 
        G.main = NULL;
 }
 
-static bool clean_paths_visit_cb(void *UNUSED(userdata), char *path_dst, const char *path_src)
-{
-       strcpy(path_dst, path_src);
-       BLI_path_native_slash(path_dst);
-       return !STREQ(path_dst, path_src);
-}
-
-/* make sure path names are correct for OS */
-static void clean_paths(Main *main)
-{
-       Scene *scene;
-
-       BKE_bpath_traverse_main(main, clean_paths_visit_cb, BKE_BPATH_TRAVERSE_SKIP_MULTIFILE, NULL);
-
-       for (scene = main->scene.first; scene; scene = scene->id.next) {
-               BLI_path_native_slash(scene->r.pic);
-       }
-}
-
-static bool wm_scene_is_visible(wmWindowManager *wm, Scene *scene)
-{
-       wmWindow *win;
-       for (win = wm->windows.first; win; win = win->next) {
-               if (win->screen->scene == scene) {
-                       return true;
-               }
-       }
-       return false;
-}
-
-/**
- * Context matching, handle no-ui case
- *
- * \note this is called on Undo so any slow conversion functions here
- * should be avoided or check (mode != LOAD_UNDO).
- *
- * \param bfd: Blend file data, freed by this function on exit.
- * \param filepath: File path or identifier.
- */
-static void setup_app_data(
-        bContext *C, BlendFileData *bfd,
-        const char *filepath, ReportList *reports)
-{
-       Scene *curscene = NULL;
-       const bool recover = (G.fileflags & G_FILE_RECOVER) != 0;
-       enum {
-               LOAD_UI = 1,
-               LOAD_UI_OFF,
-               LOAD_UNDO,
-       } mode;
-
-       if (BLI_listbase_is_empty(&bfd->main->screen)) {
-               mode = LOAD_UNDO;
-       }
-       else if (G.fileflags & G_FILE_NO_UI) {
-               mode = LOAD_UI_OFF;
-       }
-       else {
-               mode = LOAD_UI;
-       }
-
-       if (mode != LOAD_UNDO) {
-               /* may happen with library files */
-               if (ELEM(NULL, bfd->curscreen, bfd->curscene)) {
-                       BKE_report(reports, RPT_WARNING, "Library file, loading empty scene");
-                       mode = LOAD_UI_OFF;
-               }
-       }
-
-       /* Free all render results, without this stale data gets displayed after loading files */
-       if (mode != LOAD_UNDO) {
-               RE_FreeAllRenderResults();
-       }
-
-       /* Only make filepaths compatible when loading for real (not undo) */
-       if (mode != LOAD_UNDO) {
-               clean_paths(bfd->main);
-       }
-
-       /* XXX here the complex windowmanager matching */
-       
-       /* no load screens? */
-       if (mode != LOAD_UI) {
-               /* Logic for 'track_undo_scene' is to keep using the scene which the active screen has,
-                * as long as the scene associated with the undo operation is visible in one of the open windows.
-                *
-                * - 'curscreen->scene' - scene the user is currently looking at.
-                * - 'bfd->curscene' - scene undo-step was created in.
-                *
-                * This means users can have 2+ windows open and undo in both without screens switching.
-                * But if they close one of the screens,
-                * undo will ensure that the scene being operated on will be activated
-                * (otherwise we'd be undoing on an off-screen scene which isn't acceptable).
-                * see: T43424
-                */
-               bScreen *curscreen = NULL;
-               bool track_undo_scene;
-
-               /* comes from readfile.c */
-               SWAP(ListBase, G.main->wm, bfd->main->wm);
-               SWAP(ListBase, G.main->screen, bfd->main->screen);
-               
-               /* we re-use current screen */
-               curscreen = CTX_wm_screen(C);
-               /* but use new Scene pointer */
-               curscene = bfd->curscene;
-
-               track_undo_scene = (mode == LOAD_UNDO && curscreen && curscene && bfd->main->wm.first);
-
-               if (curscene == NULL) {
-                       curscene = bfd->main->scene.first;
-               }
-               /* empty file, we add a scene to make Blender work */
-               if (curscene == NULL) {
-                       curscene = BKE_scene_add(bfd->main, "Empty");
-               }
-
-               if (track_undo_scene) {
-                       /* keep the old (free'd) scene, let 'blo_lib_link_screen_restore'
-                        * replace it with 'curscene' if its needed */
-               }
-               else {
-                       /* and we enforce curscene to be in current screen */
-                       if (curscreen) {
-                               /* can run in bgmode */
-                               curscreen->scene = curscene;
-                       }
-               }
-
-               /* clear_global will free G.main, here we can still restore pointers */
-               blo_lib_link_screen_restore(bfd->main, curscreen, curscene);
-               /* curscreen might not be set when loading without ui (see T44217) so only re-assign if available */
-               if (curscreen) {
-                       curscene = curscreen->scene;
-               }
-
-               if (track_undo_scene) {
-                       wmWindowManager *wm = bfd->main->wm.first;
-                       if (wm_scene_is_visible(wm, bfd->curscene) == false) {
-                               curscene = bfd->curscene;
-                               curscreen->scene = curscene;
-                               BKE_screen_view3d_scene_sync(curscreen);
-                       }
-               }
-       }
-       
-       /* free G.main Main database */
-//     CTX_wm_manager_set(C, NULL);
-       clear_global();
-       
-       /* clear old property update cache, in case some old references are left dangling */
-       RNA_property_update_cache_free();
-       
-       G.main = bfd->main;
-
-       CTX_data_main_set(C, G.main);
-
-       if (bfd->user) {
-               
-               /* only here free userdef themes... */
-               BKE_userdef_free();
-               
-               U = *bfd->user;
-
-               /* Security issue: any blend file could include a USER block.
-                *
-                * Currently we load prefs from BLENDER_STARTUP_FILE and later on load BLENDER_USERPREF_FILE,
-                * to load the preferences defined in the users home dir.
-                *
-                * This means we will never accidentally (or maliciously)
-                * 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 */
-       if (mode != LOAD_UI) {
-               /* leave entire context further unaltered? */
-               CTX_data_scene_set(C, curscene);
-       }
-       else {
-               G.fileflags = bfd->fileflags;
-               CTX_wm_manager_set(C, G.main->wm.first);
-               CTX_wm_screen_set(C, bfd->curscreen);
-               CTX_data_scene_set(C, bfd->curscene);
-               CTX_wm_area_set(C, NULL);
-               CTX_wm_region_set(C, NULL);
-               CTX_wm_menu_set(C, NULL);
-               curscene = bfd->curscene;
-       }
-       
-       /* this can happen when active scene was lib-linked, and doesn't exist anymore */
-       if (CTX_data_scene(C) == NULL) {
-               /* in case we don't even have a local scene, add one */
-               if (!G.main->scene.first)
-                       BKE_scene_add(G.main, "Empty");
-
-               CTX_data_scene_set(C, G.main->scene.first);
-               CTX_wm_screen(C)->scene = CTX_data_scene(C);
-               curscene = CTX_data_scene(C);
-       }
-
-       BLI_assert(curscene == CTX_data_scene(C));
-
-
-       /* special cases, override loaded flags: */
-       if (G.f != bfd->globalf) {
-               const int flags_keep = (G_SWAP_EXCHANGE | G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF);
-               bfd->globalf = (bfd->globalf & ~flags_keep) | (G.f & flags_keep);
-       }
-
-
-       G.f = bfd->globalf;
-
-#ifdef WITH_PYTHON
-       /* let python know about new main */
-       BPY_context_update(C);
-#endif
-
-       /* FIXME: this version patching should really be part of the file-reading code,
-        * but we still get too many unrelated data-corruption crashes otherwise... */
-       if (G.main->versionfile < 250)
-               do_versions_ipos_to_animato(G.main);
-       
-       G.main->recovered = 0;
-       
-       /* startup.blend or recovered startup */
-       if (bfd->filename[0] == 0) {
-               G.main->name[0] = 0;
-       }
-       else if (recover && G.relbase_valid) {
-               /* in case of autosave or quit.blend, use original filename instead
-                * use relbase_valid to make sure the file is saved, else we get <memory2> in the filename */
-               filepath = bfd->filename;
-               G.main->recovered = 1;
-       
-               /* these are the same at times, should never copy to the same location */
-               if (G.main->name != filepath)
-                       BLI_strncpy(G.main->name, filepath, FILE_MAX);
-       }
-       
-       /* baseflags, groups, make depsgraph, etc */
-       /* first handle case if other windows have different scenes visible */
-       if (mode == LOAD_UI) {
-               wmWindowManager *wm = G.main->wm.first;
-               
-               if (wm) {
-                       wmWindow *win;
-                       
-                       for (win = wm->windows.first; win; win = win->next) {
-                               if (win->screen && win->screen->scene) /* zealous check... */
-                                       if (win->screen->scene != curscene)
-                                               BKE_scene_set_background(G.main, win->screen->scene);
-                       }
-               }
-       }
-       BKE_scene_set_background(G.main, curscene);
-
-       if (mode != LOAD_UNDO) {
-               RE_FreeAllPersistentData();
-               IMB_colormanagement_check_file_config(G.main);
-       }
-
-       MEM_freeN(bfd);
-
-}
-
-static int handle_subversion_warning(Main *main, ReportList *reports)
-{
-       if (main->minversionfile > BLENDER_VERSION ||
-           (main->minversionfile == BLENDER_VERSION &&
-            main->minsubversionfile > BLENDER_SUBVERSION))
-       {
-               BKE_reportf(reports, RPT_ERROR, "File written by newer Blender binary (%d.%d), expect loss of data!",
-                           main->minversionfile, main->minsubversionfile);
-       }
-
-       return 1;
-}
+/***/
 
 static void keymap_item_free(wmKeyMapItem *kmi)
 {
@@ -459,7 +139,11 @@ static void keymap_item_free(wmKeyMapItem *kmi)
                MEM_freeN(kmi->ptr);
 }
 
-void BKE_userdef_free(void)
+/**
+ * When loading a new userdef from file,
+ * or when exiting Blender.
+ */
+void BKE_blender_userdef_free(void)
 {
        wmKeyMap *km;
        wmKeyMapItem *kmi;
@@ -509,8 +193,10 @@ void BKE_userdef_free(void)
        BLI_freelistN(&U.user_keymaps);
 }
 
-/* handle changes in settings that need recalc */
-void BKE_userdef_state(void)
+/**
+ * Handle changes in settings that need refreshing.
+ */
+void BKE_blender_userdef_refresh(void)
 {
        /* prevent accidents */
        if (U.pixelsize == 0) U.pixelsize = 1;
@@ -520,116 +206,6 @@ void BKE_userdef_state(void)
 
 }
 
-int BKE_read_file(bContext *C, const char *filepath, ReportList *reports)
-{
-       BlendFileData *bfd;
-       int retval = BKE_READ_FILE_OK;
-
-       if (strstr(filepath, BLENDER_STARTUP_FILE) == NULL) /* don't print user-pref loading */
-               printf("read blend: %s\n", filepath);
-
-       bfd = BLO_read_from_file(filepath, reports);
-       if (bfd) {
-               if (bfd->user) retval = BKE_READ_FILE_OK_USERPREFS;
-               
-               if (0 == handle_subversion_warning(bfd->main, reports)) {
-                       BKE_main_free(bfd->main);
-                       MEM_freeN(bfd);
-                       bfd = NULL;
-                       retval = BKE_READ_FILE_FAIL;
-               }
-               else {
-                       setup_app_data(C, bfd, filepath, reports);
-               }
-       }
-       else
-               BKE_reports_prependf(reports, "Loading '%s' failed: ", filepath);
-               
-       return (bfd ? retval : BKE_READ_FILE_FAIL);
-}
-
-bool BKE_read_file_from_memory(
-        bContext *C, const void *filebuf, int filelength,
-        ReportList *reports, bool update_defaults)
-{
-       BlendFileData *bfd;
-
-       bfd = BLO_read_from_memory(filebuf, filelength, reports);
-       if (bfd) {
-               if (update_defaults)
-                       BLO_update_defaults_startup_blend(bfd->main);
-               setup_app_data(C, bfd, "<memory2>", reports);
-       }
-       else {
-               BKE_reports_prepend(reports, "Loading failed: ");
-       }
-
-       return (bfd != NULL);
-}
-
-/* memfile is the undo buffer */
-bool BKE_read_file_from_memfile(
-        bContext *C, MemFile *memfile,
-        ReportList *reports)
-{
-       BlendFileData *bfd;
-
-       bfd = BLO_read_from_memfile(CTX_data_main(C), G.main->name, memfile, reports);
-       if (bfd) {
-               /* remove the unused screens and wm */
-               while (bfd->main->wm.first)
-                       BKE_libblock_free_ex(bfd->main, bfd->main->wm.first, true);
-               while (bfd->main->screen.first)
-                       BKE_libblock_free_ex(bfd->main, bfd->main->screen.first, true);
-               
-               setup_app_data(C, bfd, "<memory1>", reports);
-       }
-       else {
-               BKE_reports_prepend(reports, "Loading failed: ");
-       }
-
-       return (bfd != NULL);
-}
-
-/* only read the userdef from a .blend */
-int BKE_read_file_userdef(const char *filepath, ReportList *reports)
-{
-       BlendFileData *bfd;
-       int retval = BKE_READ_FILE_FAIL;
-
-       bfd = BLO_read_from_file(filepath, reports);
-       if (bfd) {
-               if (bfd->user) {
-                       retval = BKE_READ_FILE_OK_USERPREFS;
-
-                       /* only here free userdef themes... */
-                       BKE_userdef_free();
-
-                       U = *bfd->user;
-                       MEM_freeN(bfd->user);
-               }
-               BKE_main_free(bfd->main);
-               MEM_freeN(bfd);
-       }
-       
-       return retval;
-}
-
-/* only write the userdef in a .blend */
-int BKE_write_file_userdef(const char *filepath, ReportList *reports)
-{
-       Main *mainb = MEM_callocN(sizeof(Main), "empty main");
-       int retval = 0;
-       
-       if (BLO_write_file(mainb, filepath, G_FILE_USERPREFS, reports, NULL)) {
-               retval = 1;
-       }
-       
-       MEM_freeN(mainb);
-       
-       return retval;
-}
-
 /* *****************  testing for break ************* */
 
 static void (*blender_test_break_cb)(void) = NULL;
@@ -640,7 +216,7 @@ void BKE_blender_callback_test_break_set(void (*func)(void))
 }
 
 
-int blender_test_break(void)
+int BKE_blender_test_break(void)
 {
        if (!G.background) {
                if (blender_test_break_cb)
@@ -650,514 +226,3 @@ int blender_test_break(void)
        return (G.is_break == true);
 }
 
-
-/* -------------------------------------------------------------------- */
-
-/** \name Global Undo
- * \{ */
-
-#define UNDO_DISK   0
-
-typedef struct UndoElem {
-       struct UndoElem *next, *prev;
-       char str[FILE_MAX];
-       char name[BKE_UNDO_STR_MAX];
-       MemFile memfile;
-       uintptr_t undosize;
-} UndoElem;
-
-static ListBase undobase = {NULL, NULL};
-static UndoElem *curundo = NULL;
-
-
-static int read_undosave(bContext *C, UndoElem *uel)
-{
-       char mainstr[sizeof(G.main->name)];
-       int success = 0, fileflags;
-       
-       /* This is needed so undoing/redoing doesn't crash with threaded previews going */
-       WM_jobs_kill_all_except(CTX_wm_manager(C), CTX_wm_screen(C));
-
-       BLI_strncpy(mainstr, G.main->name, sizeof(mainstr));    /* temporal store */
-
-       fileflags = G.fileflags;
-       G.fileflags |= G_FILE_NO_UI;
-
-       if (UNDO_DISK) 
-               success = (BKE_read_file(C, uel->str, NULL) != BKE_READ_FILE_FAIL);
-       else
-               success = BKE_read_file_from_memfile(C, &uel->memfile, NULL);
-
-       /* restore */
-       BLI_strncpy(G.main->name, mainstr, sizeof(G.main->name)); /* restore */
-       G.fileflags = fileflags;
-
-       if (success) {
-               /* important not to update time here, else non keyed tranforms are lost */
-               DAG_on_visible_update(G.main, false);
-       }
-
-       return success;
-}
-
-/* name can be a dynamic string */
-void BKE_undo_write(bContext *C, const char *name)
-{
-       uintptr_t maxmem, totmem, memused;
-       int nr /*, success */ /* UNUSED */;
-       UndoElem *uel;
-       
-       if ((U.uiflag & USER_GLOBALUNDO) == 0) {
-               return;
-       }
-
-       if (U.undosteps == 0) {
-               return;
-       }
-       
-       /* remove all undos after (also when curundo == NULL) */
-       while (undobase.last != curundo) {
-               uel = undobase.last;
-               BLI_remlink(&undobase, uel);
-               BLO_memfile_free(&uel->memfile);
-               MEM_freeN(uel);
-       }
-       
-       /* make new */
-       curundo = uel = MEM_callocN(sizeof(UndoElem), "undo file");
-       BLI_strncpy(uel->name, name, sizeof(uel->name));
-       BLI_addtail(&undobase, uel);
-       
-       /* and limit amount to the maximum */
-       nr = 0;
-       uel = undobase.last;
-       while (uel) {
-               nr++;
-               if (nr == U.undosteps) break;
-               uel = uel->prev;
-       }
-       if (uel) {
-               while (undobase.first != uel) {
-                       UndoElem *first = undobase.first;
-                       BLI_remlink(&undobase, first);
-                       /* the merge is because of compression */
-                       BLO_memfile_merge(&first->memfile, &first->next->memfile);
-                       MEM_freeN(first);
-               }
-       }
-
-
-       /* disk save version */
-       if (UNDO_DISK) {
-               static int counter = 0;
-               char filepath[FILE_MAX];
-               char numstr[32];
-               int fileflags = G.fileflags & ~(G_FILE_HISTORY); /* don't do file history on undo */
-
-               /* calculate current filepath */
-               counter++;
-               counter = counter % U.undosteps;
-       
-               BLI_snprintf(numstr, sizeof(numstr), "%d.blend", counter);
-               BLI_make_file_string("/", filepath, BKE_tempdir_session(), numstr);
-       
-               /* success = */ /* UNUSED */ BLO_write_file(CTX_data_main(C), filepath, fileflags, NULL, NULL);
-               
-               BLI_strncpy(curundo->str, filepath, sizeof(curundo->str));
-       }
-       else {
-               MemFile *prevfile = NULL;
-               
-               if (curundo->prev) prevfile = &(curundo->prev->memfile);
-               
-               memused = MEM_get_memory_in_use();
-               /* success = */ /* UNUSED */ BLO_write_file_mem(CTX_data_main(C), prevfile, &curundo->memfile, G.fileflags);
-               curundo->undosize = MEM_get_memory_in_use() - memused;
-       }
-
-       if (U.undomemory != 0) {
-               /* limit to maximum memory (afterwards, we can't know in advance) */
-               totmem = 0;
-               maxmem = ((uintptr_t)U.undomemory) * 1024 * 1024;
-
-               /* keep at least two (original + other) */
-               uel = undobase.last;
-               while (uel && uel->prev) {
-                       totmem += uel->undosize;
-                       if (totmem > maxmem) break;
-                       uel = uel->prev;
-               }
-
-               if (uel) {
-                       if (uel->prev && uel->prev->prev)
-                               uel = uel->prev;
-
-                       while (undobase.first != uel) {
-                               UndoElem *first = undobase.first;
-                               BLI_remlink(&undobase, first);
-                               /* the merge is because of compression */
-                               BLO_memfile_merge(&first->memfile, &first->next->memfile);
-                               MEM_freeN(first);
-                       }
-               }
-       }
-}
-
-/* 1 = an undo, -1 is a redo. we have to make sure 'curundo' remains at current situation */
-void BKE_undo_step(bContext *C, int step)
-{
-       
-       if (step == 0) {
-               read_undosave(C, curundo);
-       }
-       else if (step == 1) {
-               /* curundo should never be NULL, after restart or load file it should call undo_save */
-               if (curundo == NULL || curundo->prev == NULL) {
-                       // XXX error("No undo available");
-               }
-               else {
-                       if (G.debug & G_DEBUG) printf("undo %s\n", curundo->name);
-                       curundo = curundo->prev;
-                       read_undosave(C, curundo);
-               }
-       }
-       else {
-               /* curundo has to remain current situation! */
-               
-               if (curundo == NULL || curundo->next == NULL) {
-                       // XXX error("No redo available");
-               }
-               else {
-                       read_undosave(C, curundo->next);
-                       curundo = curundo->next;
-                       if (G.debug & G_DEBUG) printf("redo %s\n", curundo->name);
-               }
-       }
-}
-
-void BKE_undo_reset(void)
-{
-       UndoElem *uel;
-       
-       uel = undobase.first;
-       while (uel) {
-               BLO_memfile_free(&uel->memfile);
-               uel = uel->next;
-       }
-       
-       BLI_freelistN(&undobase);
-       curundo = NULL;
-}
-
-/* based on index nr it does a restore */
-void BKE_undo_number(bContext *C, int nr)
-{
-       curundo = BLI_findlink(&undobase, nr);
-       BKE_undo_step(C, 0);
-}
-
-/* go back to the last occurance of name in stack */
-void BKE_undo_name(bContext *C, const char *name)
-{
-       UndoElem *uel = BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
-
-       if (uel && uel->prev) {
-               curundo = uel->prev;
-               BKE_undo_step(C, 0);
-       }
-}
-
-/* name optional */
-bool BKE_undo_is_valid(const char *name)
-{
-       if (name) {
-               UndoElem *uel = BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
-               return uel && uel->prev;
-       }
-       
-       return undobase.last != undobase.first;
-}
-
-/* get name of undo item, return null if no item with this index */
-/* if active pointer, set it to 1 if true */
-const char *BKE_undo_get_name(int nr, bool *r_active)
-{
-       UndoElem *uel = BLI_findlink(&undobase, nr);
-       
-       if (r_active) *r_active = false;
-       
-       if (uel) {
-               if (r_active && (uel == curundo)) {
-                       *r_active = true;
-               }
-               return uel->name;
-       }
-       return NULL;
-}
-
-/**
- * Saves .blend using undo buffer.
- *
- * \return success.
- */
-bool BKE_undo_save_file(const char *filename)
-{
-       UndoElem *uel;
-       MemFileChunk *chunk;
-       int file, oflags;
-
-       if ((U.uiflag & USER_GLOBALUNDO) == 0) {
-               return false;
-       }
-
-       uel = curundo;
-       if (uel == NULL) {
-               fprintf(stderr, "No undo buffer to save recovery file\n");
-               return false;
-       }
-
-       /* note: This is currently used for autosave and 'quit.blend', where _not_ following symlinks is OK,
-        * however if this is ever executed explicitly by the user, we may want to allow writing to symlinks.
-        */
-
-       oflags = O_BINARY | O_WRONLY | O_CREAT | O_TRUNC;
-#ifdef O_NOFOLLOW
-       /* use O_NOFOLLOW to avoid writing to a symlink - use 'O_EXCL' (CVE-2008-1103) */
-       oflags |= O_NOFOLLOW;
-#else
-       /* TODO(sergey): How to deal with symlinks on windows? */
-#  ifndef _MSC_VER
-#    warning "Symbolic links will be followed on undo save, possibly causing CVE-2008-1103"
-#  endif
-#endif
-       file = BLI_open(filename,  oflags, 0666);
-
-       if (file == -1) {
-               fprintf(stderr, "Unable to save '%s': %s\n",
-                       filename, errno ? strerror(errno) : "Unknown error opening file");
-               return false;
-       }
-
-       for (chunk = uel->memfile.chunks.first; chunk; chunk = chunk->next) {
-               if (write(file, chunk->buf, chunk->size) != chunk->size) {
-                       break;
-               }
-       }
-       
-       close(file);
-       
-       if (chunk) {
-               fprintf(stderr, "Unable to save '%s': %s\n",
-                       filename, errno ? strerror(errno) : "Unknown error writing file");
-               return false;
-       }
-       return true;
-}
-
-/* sets curscene */
-Main *BKE_undo_get_main(Scene **r_scene)
-{
-       Main *mainp = NULL;
-       BlendFileData *bfd = BLO_read_from_memfile(G.main, G.main->name, &curundo->memfile, NULL);
-       
-       if (bfd) {
-               mainp = bfd->main;
-               if (r_scene) {
-                       *r_scene = bfd->curscene;
-               }
-               
-               MEM_freeN(bfd);
-       }
-       
-       return mainp;
-}
-
-/** \} */
-
-
-/* -------------------------------------------------------------------- */
-
-/** \name Partial `.blend` file save.
- * \{ */
-
-void BKE_blendfile_write_partial_begin(Main *bmain_src)
-{
-       BKE_main_id_tag_all(bmain_src, LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT, false);
-}
-
-void BKE_blendfile_write_partial_tag_ID(ID *id, bool set)
-{
-       if (set) {
-               id->tag |= LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT;
-       }
-       else {
-               id->tag &= ~(LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT);
-       }
-}
-
-static void blendfile_write_partial_cb(void *UNUSED(handle), Main *UNUSED(bmain), void *vid)
-{
-       if (vid) {
-               ID *id = vid;
-               /* only tag for need-expand if not done, prevents eternal loops */
-               if ((id->tag & LIB_TAG_DOIT) == 0)
-                       id->tag |= LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT;
-       }
-}
-
-/**
- * \return Success.
- */
-bool BKE_blendfile_write_partial(
-        Main *bmain_src, const char *filepath, const int write_flags, ReportList *reports)
-{
-       Main *bmain_dst = MEM_callocN(sizeof(Main), "copybuffer");
-       ListBase *lbarray_dst[MAX_LIBARRAY], *lbarray_src[MAX_LIBARRAY];
-       int a, retval;
-
-       void     *path_list_backup = NULL;
-       const int path_list_flag = (BKE_BPATH_TRAVERSE_SKIP_LIBRARY | BKE_BPATH_TRAVERSE_SKIP_MULTIFILE);
-
-       if (write_flags & G_FILE_RELATIVE_REMAP) {
-               path_list_backup = BKE_bpath_list_backup(bmain_src, path_list_flag);
-       }
-
-       BLO_main_expander(blendfile_write_partial_cb);
-       BLO_expand_main(NULL, bmain_src);
-
-       /* move over all tagged blocks */
-       set_listbasepointers(bmain_src, lbarray_src);
-       a = set_listbasepointers(bmain_dst, lbarray_dst);
-       while (a--) {
-               ID *id, *nextid;
-               ListBase *lb_dst = lbarray_dst[a], *lb_src = lbarray_src[a];
-               
-               for (id = lb_src->first; id; id = nextid) {
-                       nextid = id->next;
-                       if (id->tag & LIB_TAG_DOIT) {
-                               BLI_remlink(lb_src, id);
-                               BLI_addtail(lb_dst, id);
-                       }
-               }
-       }
-       
-       
-       /* save the buffer */
-       retval = BLO_write_file(bmain_dst, filepath, write_flags, reports, NULL);
-       
-       /* move back the main, now sorted again */
-       set_listbasepointers(bmain_src, lbarray_dst);
-       a = set_listbasepointers(bmain_dst, lbarray_src);
-       while (a--) {
-               ID *id;
-               ListBase *lb_dst = lbarray_dst[a], *lb_src = lbarray_src[a];
-               
-               while ((id = BLI_pophead(lb_src))) {
-                       BLI_addtail(lb_dst, id);
-                       id_sort_by_name(lb_dst, id);
-               }
-       }
-       
-       MEM_freeN(bmain_dst);
-       
-       if (path_list_backup) {
-               BKE_bpath_list_restore(bmain_src, path_list_flag, path_list_backup);
-               BKE_bpath_list_free(path_list_backup);
-       }
-
-       return retval;
-}
-
-void BKE_blendfile_write_partial_end(Main *bmain_src)
-{
-       BKE_main_id_tag_all(bmain_src, LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT, false);
-}
-
-/** \} */
-
-
-/* -------------------------------------------------------------------- */
-
-/** \name Copy/Paste `.blend`, partial saves.
- * \{ */
-
-void BKE_copybuffer_begin(Main *bmain_src)
-{
-       BKE_blendfile_write_partial_begin(bmain_src);
-}
-
-void BKE_copybuffer_tag_ID(ID *id)
-{
-       BKE_blendfile_write_partial_tag_ID(id, true);
-}
-
-/**
- * \return Success.
- */
-bool BKE_copybuffer_save(Main *bmain_src, const char *filename, ReportList *reports)
-{
-       const int write_flags = G_FILE_RELATIVE_REMAP;
-
-       bool retval = BKE_blendfile_write_partial(bmain_src, filename, write_flags, reports);
-
-       BKE_blendfile_write_partial_end(bmain_src);
-
-       return retval;
-}
-
-/**
- * \return Success.
- */
-bool BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, ReportList *reports)
-{
-       Main *bmain = CTX_data_main(C);
-       Scene *scene = CTX_data_scene(C);
-       View3D *v3d = CTX_wm_view3d(C);
-       Main *mainl = NULL;
-       Library *lib;
-       BlendHandle *bh;
-               
-       bh = BLO_blendhandle_from_file(libname, reports);
-       
-       if (bh == NULL) {
-               /* error reports will have been made by BLO_blendhandle_from_file() */
-               return false;
-       }
-
-       BKE_scene_base_deselect_all(scene);
-       
-       /* tag everything, all untagged data can be made local
-        * its also generally useful to know what is new
-        *
-        * take extra care BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false) is called after! */
-       BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);
-       
-       /* here appending/linking starts */
-       mainl = BLO_library_link_begin(bmain, &bh, libname);
-       
-       BLO_library_link_copypaste(mainl, bh);
-
-       BLO_library_link_end(mainl, &bh, flag, scene, v3d);
-       
-       /* mark all library linked objects to be updated */
-       BKE_main_lib_objects_recalc_all(bmain);
-       IMB_colormanagement_check_file_config(bmain);
-       
-       /* append, rather than linking */
-       lib = BLI_findstring(&bmain->library, libname, offsetof(Library, filepath));
-       BKE_library_make_local(bmain, lib, true, false);
-       
-       /* important we unset, otherwise these object wont
-        * link into other scenes from this blend file */
-       BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);
-       
-       /* recreate dependency graph to include new objects */
-       DAG_relations_tag_update(bmain);
-       
-       BLO_blendhandle_close(bh);
-       /* remove library... */
-       
-       return true;
-}
-
-/** \} */
diff --git a/source/blender/blenkernel/intern/blender_copybuffer.c b/source/blender/blenkernel/intern/blender_copybuffer.c
new file mode 100644 (file)
index 0000000..ad01fac
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/blenkernel/intern/blender_copybuffer.c
+ *  \ingroup bke
+ *
+ * Used for copy/paste operator, (using a temporary file).
+ */
+
+#include <stdlib.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_userdef_types.h"
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_view3d_types.h"
+#include "DNA_windowmanager_types.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_utildefines.h"
+#include "BLI_callbacks.h"
+
+#include "IMB_imbuf.h"
+#include "IMB_moviecache.h"
+
+#include "BKE_blender_copybuffer.h"  /* own include */
+#include "BKE_blendfile.h"
+#include "BKE_context.h"
+#include "BKE_depsgraph.h"
+#include "BKE_global.h"
+#include "BKE_library.h"
+#include "BKE_main.h"
+#include "BKE_scene.h"
+
+#include "BLO_readfile.h"
+#include "BLO_writefile.h"
+
+#include "IMB_colormanagement.h"
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Copy/Paste `.blend`, partial saves.
+ * \{ */
+
+void BKE_copybuffer_begin(Main *bmain_src)
+{
+       BKE_blendfile_write_partial_begin(bmain_src);
+}
+
+void BKE_copybuffer_tag_ID(ID *id)
+{
+       BKE_blendfile_write_partial_tag_ID(id, true);
+}
+
+/**
+ * \return Success.
+ */
+bool BKE_copybuffer_save(Main *bmain_src, const char *filename, ReportList *reports)
+{
+       const int write_flags = G_FILE_RELATIVE_REMAP;
+
+       bool retval = BKE_blendfile_write_partial(bmain_src, filename, write_flags, reports);
+
+       BKE_blendfile_write_partial_end(bmain_src);
+
+       return retval;
+}
+
+/**
+ * \return Success.
+ */
+bool BKE_copybuffer_paste(bContext *C, const char *libname, const short flag, ReportList *reports)
+{
+       Main *bmain = CTX_data_main(C);
+       Scene *scene = CTX_data_scene(C);
+       View3D *v3d = CTX_wm_view3d(C);
+       Main *mainl = NULL;
+       Library *lib;
+       BlendHandle *bh;
+
+       bh = BLO_blendhandle_from_file(libname, reports);
+
+       if (bh == NULL) {
+               /* error reports will have been made by BLO_blendhandle_from_file() */
+               return false;
+       }
+
+       BKE_scene_base_deselect_all(scene);
+
+       /* tag everything, all untagged data can be made local
+        * its also generally useful to know what is new
+        *
+        * take extra care BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false) is called after! */
+       BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true);
+
+       /* here appending/linking starts */
+       mainl = BLO_library_link_begin(bmain, &bh, libname);
+
+       BLO_library_link_copypaste(mainl, bh);
+
+       BLO_library_link_end(mainl, &bh, flag, scene, v3d);
+
+       /* mark all library linked objects to be updated */
+       BKE_main_lib_objects_recalc_all(bmain);
+       IMB_colormanagement_check_file_config(bmain);
+
+       /* append, rather than linking */
+       lib = BLI_findstring(&bmain->library, libname, offsetof(Library, filepath));
+       BKE_library_make_local(bmain, lib, true, false);
+
+       /* important we unset, otherwise these object wont
+        * link into other scenes from this blend file */
+       BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false);
+
+       /* recreate dependency graph to include new objects */
+       DAG_relations_tag_update(bmain);
+
+       BLO_blendhandle_close(bh);
+       /* remove library... */
+
+       return true;
+}
+
+/** \} */
diff --git a/source/blender/blenkernel/intern/blender_undo.c b/source/blender/blenkernel/intern/blender_undo.c
new file mode 100644 (file)
index 0000000..ca0a1b9
--- /dev/null
@@ -0,0 +1,393 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/blenkernel/intern/blender_undo.c
+ *  \ingroup bke
+ *
+ * Blend file undo (known as 'Global Undo').
+ * DNA level diffing for undo.
+ */
+
+#ifndef _WIN32
+#  include <unistd.h> // for read close
+#else
+#  include <io.h> // for open close read
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stddef.h>
+#include <string.h>
+#include <fcntl.h>  /* for open */
+#include <errno.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_scene_types.h"
+
+#include "BLI_fileops.h"
+#include "BLI_listbase.h"
+#include "BLI_path_util.h"
+#include "BLI_string.h"
+#include "BLI_utildefines.h"
+
+#include "IMB_imbuf.h"
+#include "IMB_moviecache.h"
+
+#include "BKE_blender_undo.h"  /* own include */
+#include "BKE_blendfile.h"
+#include "BKE_appdir.h"
+#include "BKE_brush.h"
+#include "BKE_context.h"
+#include "BKE_depsgraph.h"
+#include "BKE_global.h"
+#include "BKE_image.h"
+#include "BKE_main.h"
+#include "RE_pipeline.h"
+
+#include "BLO_undofile.h"
+#include "BLO_readfile.h"
+#include "BLO_writefile.h"
+
+#include "WM_api.h" // XXXXX BAD, very BAD dependency (bad level call) - remove asap, elubie
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Global Undo
+ * \{ */
+
+#define UNDO_DISK   0
+
+typedef struct UndoElem {
+       struct UndoElem *next, *prev;
+       char str[FILE_MAX];
+       char name[BKE_UNDO_STR_MAX];
+       MemFile memfile;
+       uintptr_t undosize;
+} UndoElem;
+
+static ListBase undobase = {NULL, NULL};
+static UndoElem *curundo = NULL;
+
+
+static int read_undosave(bContext *C, UndoElem *uel)
+{
+       char mainstr[sizeof(G.main->name)];
+       int success = 0, fileflags;
+
+       /* This is needed so undoing/redoing doesn't crash with threaded previews going */
+       WM_jobs_kill_all_except(CTX_wm_manager(C), CTX_wm_screen(C));
+
+       BLI_strncpy(mainstr, G.main->name, sizeof(mainstr));    /* temporal store */
+
+       fileflags = G.fileflags;
+       G.fileflags |= G_FILE_NO_UI;
+
+       if (UNDO_DISK)
+               success = (BKE_blendfile_read(C, uel->str, NULL) != BKE_BLENDFILE_READ_FAIL);
+       else
+               success = BKE_blendfile_read_from_memfile(C, &uel->memfile, NULL);
+
+       /* restore */
+       BLI_strncpy(G.main->name, mainstr, sizeof(G.main->name)); /* restore */
+       G.fileflags = fileflags;
+
+       if (success) {
+               /* important not to update time here, else non keyed tranforms are lost */
+               DAG_on_visible_update(G.main, false);
+       }
+
+       return success;
+}
+
+/* name can be a dynamic string */
+void BKE_undo_write(bContext *C, const char *name)
+{
+       uintptr_t maxmem, totmem, memused;
+       int nr /*, success */ /* UNUSED */;
+       UndoElem *uel;
+
+       if ((U.uiflag & USER_GLOBALUNDO) == 0) {
+               return;
+       }
+
+       if (U.undosteps == 0) {
+               return;
+       }
+
+       /* remove all undos after (also when curundo == NULL) */
+       while (undobase.last != curundo) {
+               uel = undobase.last;
+               BLI_remlink(&undobase, uel);
+               BLO_memfile_free(&uel->memfile);
+               MEM_freeN(uel);
+       }
+
+       /* make new */
+       curundo = uel = MEM_callocN(sizeof(UndoElem), "undo file");
+       BLI_strncpy(uel->name, name, sizeof(uel->name));
+       BLI_addtail(&undobase, uel);
+
+       /* and limit amount to the maximum */
+       nr = 0;
+       uel = undobase.last;
+       while (uel) {
+               nr++;
+               if (nr == U.undosteps) break;
+               uel = uel->prev;
+       }
+       if (uel) {
+               while (undobase.first != uel) {
+                       UndoElem *first = undobase.first;
+                       BLI_remlink(&undobase, first);
+                       /* the merge is because of compression */
+                       BLO_memfile_merge(&first->memfile, &first->next->memfile);
+                       MEM_freeN(first);
+               }
+       }
+
+
+       /* disk save version */
+       if (UNDO_DISK) {
+               static int counter = 0;
+               char filepath[FILE_MAX];
+               char numstr[32];
+               int fileflags = G.fileflags & ~(G_FILE_HISTORY); /* don't do file history on undo */
+
+               /* calculate current filepath */
+               counter++;
+               counter = counter % U.undosteps;
+
+               BLI_snprintf(numstr, sizeof(numstr), "%d.blend", counter);
+               BLI_make_file_string("/", filepath, BKE_tempdir_session(), numstr);
+
+               /* success = */ /* UNUSED */ BLO_write_file(CTX_data_main(C), filepath, fileflags, NULL, NULL);
+
+               BLI_strncpy(curundo->str, filepath, sizeof(curundo->str));
+       }
+       else {
+               MemFile *prevfile = NULL;
+
+               if (curundo->prev) prevfile = &(curundo->prev->memfile);
+
+               memused = MEM_get_memory_in_use();
+               /* success = */ /* UNUSED */ BLO_write_file_mem(CTX_data_main(C), prevfile, &curundo->memfile, G.fileflags);
+               curundo->undosize = MEM_get_memory_in_use() - memused;
+       }
+
+       if (U.undomemory != 0) {
+               /* limit to maximum memory (afterwards, we can't know in advance) */
+               totmem = 0;
+               maxmem = ((uintptr_t)U.undomemory) * 1024 * 1024;
+
+               /* keep at least two (original + other) */
+               uel = undobase.last;
+               while (uel && uel->prev) {
+                       totmem += uel->undosize;
+                       if (totmem > maxmem) break;
+                       uel = uel->prev;
+               }
+
+               if (uel) {
+                       if (uel->prev && uel->prev->prev)
+                               uel = uel->prev;
+
+                       while (undobase.first != uel) {
+                               UndoElem *first = undobase.first;
+                               BLI_remlink(&undobase, first);
+                               /* the merge is because of compression */
+                               BLO_memfile_merge(&first->memfile, &first->next->memfile);
+                               MEM_freeN(first);
+                       }
+               }
+       }
+}
+
+/* 1 = an undo, -1 is a redo. we have to make sure 'curundo' remains at current situation */
+void BKE_undo_step(bContext *C, int step)
+{
+
+       if (step == 0) {
+               read_undosave(C, curundo);
+       }
+       else if (step == 1) {
+               /* curundo should never be NULL, after restart or load file it should call undo_save */
+               if (curundo == NULL || curundo->prev == NULL) {
+                       // XXX error("No undo available");
+               }
+               else {
+                       if (G.debug & G_DEBUG) printf("undo %s\n", curundo->name);
+                       curundo = curundo->prev;
+                       read_undosave(C, curundo);
+               }
+       }
+       else {
+               /* curundo has to remain current situation! */
+
+               if (curundo == NULL || curundo->next == NULL) {
+                       // XXX error("No redo available");
+               }
+               else {
+                       read_undosave(C, curundo->next);
+                       curundo = curundo->next;
+                       if (G.debug & G_DEBUG) printf("redo %s\n", curundo->name);
+               }
+       }
+}
+
+void BKE_undo_reset(void)
+{
+       UndoElem *uel;
+
+       uel = undobase.first;
+       while (uel) {
+               BLO_memfile_free(&uel->memfile);
+               uel = uel->next;
+       }
+
+       BLI_freelistN(&undobase);
+       curundo = NULL;
+}
+
+/* based on index nr it does a restore */
+void BKE_undo_number(bContext *C, int nr)
+{
+       curundo = BLI_findlink(&undobase, nr);
+       BKE_undo_step(C, 0);
+}
+
+/* go back to the last occurance of name in stack */
+void BKE_undo_name(bContext *C, const char *name)
+{
+       UndoElem *uel = BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
+
+       if (uel && uel->prev) {
+               curundo = uel->prev;
+               BKE_undo_step(C, 0);
+       }
+}
+
+/* name optional */
+bool BKE_undo_is_valid(const char *name)
+{
+       if (name) {
+               UndoElem *uel = BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
+               return uel && uel->prev;
+       }
+
+       return undobase.last != undobase.first;
+}
+
+/* get name of undo item, return null if no item with this index */
+/* if active pointer, set it to 1 if true */
+const char *BKE_undo_get_name(int nr, bool *r_active)
+{
+       UndoElem *uel = BLI_findlink(&undobase, nr);
+
+       if (r_active) *r_active = false;
+
+       if (uel) {
+               if (r_active && (uel == curundo)) {
+                       *r_active = true;
+               }
+               return uel->name;
+       }
+       return NULL;
+}
+
+/**
+ * Saves .blend using undo buffer.
+ *
+ * \return success.
+ */
+bool BKE_undo_save_file(const char *filename)
+{
+       UndoElem *uel;
+       MemFileChunk *chunk;
+       int file, oflags;
+
+       if ((U.uiflag & USER_GLOBALUNDO) == 0) {
+               return false;
+       }
+
+       uel = curundo;
+       if (uel == NULL) {
+               fprintf(stderr, "No undo buffer to save recovery file\n");
+               return false;
+       }
+
+       /* note: This is currently used for autosave and 'quit.blend', where _not_ following symlinks is OK,
+        * however if this is ever executed explicitly by the user, we may want to allow writing to symlinks.
+        */
+
+       oflags = O_BINARY | O_WRONLY | O_CREAT | O_TRUNC;
+#ifdef O_NOFOLLOW
+       /* use O_NOFOLLOW to avoid writing to a symlink - use 'O_EXCL' (CVE-2008-1103) */
+       oflags |= O_NOFOLLOW;
+#else
+       /* TODO(sergey): How to deal with symlinks on windows? */
+#  ifndef _MSC_VER
+#    warning "Symbolic links will be followed on undo save, possibly causing CVE-2008-1103"
+#  endif
+#endif
+       file = BLI_open(filename,  oflags, 0666);
+
+       if (file == -1) {
+               fprintf(stderr, "Unable to save '%s': %s\n",
+                       filename, errno ? strerror(errno) : "Unknown error opening file");
+               return false;
+       }
+
+       for (chunk = uel->memfile.chunks.first; chunk; chunk = chunk->next) {
+               if (write(file, chunk->buf, chunk->size) != chunk->size) {
+                       break;
+               }
+       }
+
+       close(file);
+
+       if (chunk) {
+               fprintf(stderr, "Unable to save '%s': %s\n",
+                       filename, errno ? strerror(errno) : "Unknown error writing file");
+               return false;
+       }
+       return true;
+}
+
+/* sets curscene */
+Main *BKE_undo_get_main(Scene **r_scene)
+{
+       Main *mainp = NULL;
+       BlendFileData *bfd = BLO_read_from_memfile(G.main, G.main->name, &curundo->memfile, NULL);
+
+       if (bfd) {
+               mainp = bfd->main;
+               if (r_scene) {
+                       *r_scene = bfd->curscene;
+               }
+
+               MEM_freeN(bfd);
+       }
+
+       return mainp;
+}
+
+/** \} */
diff --git a/source/blender/blenkernel/intern/blendfile.c b/source/blender/blenkernel/intern/blendfile.c
new file mode 100644 (file)
index 0000000..4605a12
--- /dev/null
@@ -0,0 +1,566 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/blenkernel/intern/blendfile.c
+ *  \ingroup bke
+ *
+ * High level `.blend` file read/write,
+ * and functions for writing *partial* files (only selected data-blocks).
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+
+#include "BLI_listbase.h"
+#include "BLI_string.h"
+#include "BLI_path_util.h"
+#include "BLI_utildefines.h"
+
+#include "IMB_colormanagement.h"
+
+#include "BKE_appdir.h"
+#include "BKE_blender.h"
+#include "BKE_blender_version.h"
+#include "BKE_blendfile.h"
+#include "BKE_bpath.h"
+#include "BKE_context.h"
+#include "BKE_global.h"
+#include "BKE_ipo.h"
+#include "BKE_library.h"
+#include "BKE_main.h"
+#include "BKE_report.h"
+#include "BKE_scene.h"
+#include "BKE_screen.h"
+
+#include "BLO_readfile.h"
+#include "BLO_writefile.h"
+
+#include "RNA_access.h"
+
+#include "RE_pipeline.h"
+
+#ifdef WITH_PYTHON
+#  include "BPY_extern.h"
+#endif
+
+/* -------------------------------------------------------------------- */
+
+/** \name High Level `.blend` file read/write.
+ * \{ */
+
+static bool clean_paths_visit_cb(void *UNUSED(userdata), char *path_dst, const char *path_src)
+{
+       strcpy(path_dst, path_src);
+       BLI_path_native_slash(path_dst);
+       return !STREQ(path_dst, path_src);
+}
+
+/* make sure path names are correct for OS */
+static void clean_paths(Main *main)
+{
+       Scene *scene;
+
+       BKE_bpath_traverse_main(main, clean_paths_visit_cb, BKE_BPATH_TRAVERSE_SKIP_MULTIFILE, NULL);
+
+       for (scene = main->scene.first; scene; scene = scene->id.next) {
+               BLI_path_native_slash(scene->r.pic);
+       }
+}
+
+static bool wm_scene_is_visible(wmWindowManager *wm, Scene *scene)
+{
+       wmWindow *win;
+       for (win = wm->windows.first; win; win = win->next) {
+               if (win->screen->scene == scene) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+/**
+ * Context matching, handle no-ui case
+ *
+ * \note this is called on Undo so any slow conversion functions here
+ * should be avoided or check (mode != LOAD_UNDO).
+ *
+ * \param bfd: Blend file data, freed by this function on exit.
+ * \param filepath: File path or identifier.
+ */
+static void setup_app_data(
+        bContext *C, BlendFileData *bfd,
+        const char *filepath, ReportList *reports)
+{
+       Scene *curscene = NULL;
+       const bool recover = (G.fileflags & G_FILE_RECOVER) != 0;
+       enum {
+               LOAD_UI = 1,
+               LOAD_UI_OFF,
+               LOAD_UNDO,
+       } mode;
+
+       if (BLI_listbase_is_empty(&bfd->main->screen)) {
+               mode = LOAD_UNDO;
+       }
+       else if (G.fileflags & G_FILE_NO_UI) {
+               mode = LOAD_UI_OFF;
+       }
+       else {
+               mode = LOAD_UI;
+       }
+
+       if (mode != LOAD_UNDO) {
+               /* may happen with library files */
+               if (ELEM(NULL, bfd->curscreen, bfd->curscene)) {
+                       BKE_report(reports, RPT_WARNING, "Library file, loading empty scene");
+                       mode = LOAD_UI_OFF;
+               }
+       }
+
+       /* Free all render results, without this stale data gets displayed after loading files */
+       if (mode != LOAD_UNDO) {
+               RE_FreeAllRenderResults();
+       }
+
+       /* Only make filepaths compatible when loading for real (not undo) */
+       if (mode != LOAD_UNDO) {
+               clean_paths(bfd->main);
+       }
+
+       /* XXX here the complex windowmanager matching */
+
+       /* no load screens? */
+       if (mode != LOAD_UI) {
+               /* Logic for 'track_undo_scene' is to keep using the scene which the active screen has,
+                * as long as the scene associated with the undo operation is visible in one of the open windows.
+                *
+                * - 'curscreen->scene' - scene the user is currently looking at.
+                * - 'bfd->curscene' - scene undo-step was created in.
+                *
+                * This means users can have 2+ windows open and undo in both without screens switching.
+                * But if they close one of the screens,
+                * undo will ensure that the scene being operated on will be activated
+                * (otherwise we'd be undoing on an off-screen scene which isn't acceptable).
+                * see: T43424
+                */
+               bScreen *curscreen = NULL;
+               bool track_undo_scene;
+
+               /* comes from readfile.c */
+               SWAP(ListBase, G.main->wm, bfd->main->wm);
+               SWAP(ListBase, G.main->screen, bfd->main->screen);
+
+               /* we re-use current screen */
+               curscreen = CTX_wm_screen(C);
+               /* but use new Scene pointer */
+               curscene = bfd->curscene;
+
+               track_undo_scene = (mode == LOAD_UNDO && curscreen && curscene && bfd->main->wm.first);
+
+               if (curscene == NULL) {
+                       curscene = bfd->main->scene.first;
+               }
+               /* empty file, we add a scene to make Blender work */
+               if (curscene == NULL) {
+                       curscene = BKE_scene_add(bfd->main, "Empty");
+               }
+
+               if (track_undo_scene) {
+                       /* keep the old (free'd) scene, let 'blo_lib_link_screen_restore'
+                        * replace it with 'curscene' if its needed */
+               }
+               else {
+                       /* and we enforce curscene to be in current screen */
+                       if (curscreen) {
+                               /* can run in bgmode */
+                               curscreen->scene = curscene;
+                       }
+               }
+
+               /* BKE_blender_globals_clear will free G.main, here we can still restore pointers */
+               blo_lib_link_screen_restore(bfd->main, curscreen, curscene);
+               /* curscreen might not be set when loading without ui (see T44217) so only re-assign if available */
+               if (curscreen) {
+                       curscene = curscreen->scene;
+               }
+
+               if (track_undo_scene) {
+                       wmWindowManager *wm = bfd->main->wm.first;
+                       if (wm_scene_is_visible(wm, bfd->curscene) == false) {
+                               curscene = bfd->curscene;
+                               curscreen->scene = curscene;
+                               BKE_screen_view3d_scene_sync(curscreen);
+                       }
+               }
+       }
+
+       /* free G.main Main database */
+//     CTX_wm_manager_set(C, NULL);
+       BKE_blender_globals_clear();
+
+       /* clear old property update cache, in case some old references are left dangling */
+       RNA_property_update_cache_free();
+
+       G.main = bfd->main;
+
+       CTX_data_main_set(C, G.main);
+
+       if (bfd->user) {
+
+               /* only here free userdef themes... */
+               BKE_blender_userdef_free();
+
+               U = *bfd->user;
+
+               /* Security issue: any blend file could include a USER block.
+                *
+                * Currently we load prefs from BLENDER_STARTUP_FILE and later on load BLENDER_USERPREF_FILE,
+                * to load the preferences defined in the users home dir.
+                *
+                * This means we will never accidentally (or maliciously)
+                * 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 */
+       if (mode != LOAD_UI) {
+               /* leave entire context further unaltered? */
+               CTX_data_scene_set(C, curscene);
+       }
+       else {
+               G.fileflags = bfd->fileflags;
+               CTX_wm_manager_set(C, G.main->wm.first);
+               CTX_wm_screen_set(C, bfd->curscreen);
+               CTX_data_scene_set(C, bfd->curscene);
+               CTX_wm_area_set(C, NULL);
+               CTX_wm_region_set(C, NULL);
+               CTX_wm_menu_set(C, NULL);
+               curscene = bfd->curscene;
+       }
+
+       /* this can happen when active scene was lib-linked, and doesn't exist anymore */
+       if (CTX_data_scene(C) == NULL) {
+               /* in case we don't even have a local scene, add one */
+               if (!G.main->scene.first)
+                       BKE_scene_add(G.main, "Empty");
+
+               CTX_data_scene_set(C, G.main->scene.first);
+               CTX_wm_screen(C)->scene = CTX_data_scene(C);
+               curscene = CTX_data_scene(C);
+       }
+
+       BLI_assert(curscene == CTX_data_scene(C));
+
+
+       /* special cases, override loaded flags: */
+       if (G.f != bfd->globalf) {
+               const int flags_keep = (G_SWAP_EXCHANGE | G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF);
+               bfd->globalf = (bfd->globalf & ~flags_keep) | (G.f & flags_keep);
+       }
+
+
+       G.f = bfd->globalf;
+
+#ifdef WITH_PYTHON
+       /* let python know about new main */
+       BPY_context_update(C);
+#endif
+
+       /* FIXME: this version patching should really be part of the file-reading code,
+        * but we still get too many unrelated data-corruption crashes otherwise... */
+       if (G.main->versionfile < 250)
+               do_versions_ipos_to_animato(G.main);
+
+       G.main->recovered = 0;
+
+       /* startup.blend or recovered startup */
+       if (bfd->filename[0] == 0) {
+               G.main->name[0] = 0;
+       }
+       else if (recover && G.relbase_valid) {
+               /* in case of autosave or quit.blend, use original filename instead
+                * use relbase_valid to make sure the file is saved, else we get <memory2> in the filename */
+               filepath = bfd->filename;
+               G.main->recovered = 1;
+
+               /* these are the same at times, should never copy to the same location */
+               if (G.main->name != filepath)
+                       BLI_strncpy(G.main->name, filepath, FILE_MAX);
+       }
+
+       /* baseflags, groups, make depsgraph, etc */
+       /* first handle case if other windows have different scenes visible */
+       if (mode == LOAD_UI) {
+               wmWindowManager *wm = G.main->wm.first;
+
+               if (wm) {
+                       wmWindow *win;
+
+                       for (win = wm->windows.first; win; win = win->next) {
+                               if (win->screen && win->screen->scene) /* zealous check... */
+                                       if (win->screen->scene != curscene)
+                                               BKE_scene_set_background(G.main, win->screen->scene);
+                       }
+               }
+       }
+       BKE_scene_set_background(G.main, curscene);
+
+       if (mode != LOAD_UNDO) {
+               RE_FreeAllPersistentData();
+               IMB_colormanagement_check_file_config(G.main);
+       }
+
+       MEM_freeN(bfd);
+
+}
+
+static int handle_subversion_warning(Main *main, ReportList *reports)
+{
+       if (main->minversionfile > BLENDER_VERSION ||
+           (main->minversionfile == BLENDER_VERSION &&
+            main->minsubversionfile > BLENDER_SUBVERSION))
+       {
+               BKE_reportf(reports, RPT_ERROR, "File written by newer Blender binary (%d.%d), expect loss of data!",
+                           main->minversionfile, main->minsubversionfile);
+       }
+
+       return 1;
+}
+
+int BKE_blendfile_read(bContext *C, const char *filepath, ReportList *reports)
+{
+       BlendFileData *bfd;
+       int retval = BKE_BLENDFILE_READ_OK;
+
+       if (strstr(filepath, BLENDER_STARTUP_FILE) == NULL) /* don't print user-pref loading */
+               printf("read blend: %s\n", filepath);
+
+       bfd = BLO_read_from_file(filepath, reports);
+       if (bfd) {
+               if (bfd->user) retval = BKE_BLENDFILE_READ_OK_USERPREFS;
+
+               if (0 == handle_subversion_warning(bfd->main, reports)) {
+                       BKE_main_free(bfd->main);
+                       MEM_freeN(bfd);
+                       bfd = NULL;
+                       retval = BKE_BLENDFILE_READ_FAIL;
+               }
+               else {
+                       setup_app_data(C, bfd, filepath, reports);
+               }
+       }
+       else
+               BKE_reports_prependf(reports, "Loading '%s' failed: ", filepath);
+
+       return (bfd ? retval : BKE_BLENDFILE_READ_FAIL);
+}
+
+bool BKE_blendfile_read_from_memory(
+        bContext *C, const void *filebuf, int filelength,
+        ReportList *reports, bool update_defaults)
+{
+       BlendFileData *bfd;
+
+       bfd = BLO_read_from_memory(filebuf, filelength, reports);
+       if (bfd) {
+               if (update_defaults)
+                       BLO_update_defaults_startup_blend(bfd->main);
+               setup_app_data(C, bfd, "<memory2>", reports);
+       }
+       else {
+               BKE_reports_prepend(reports, "Loading failed: ");
+       }
+
+       return (bfd != NULL);
+}
+
+/* memfile is the undo buffer */
+bool BKE_blendfile_read_from_memfile(
+        bContext *C, struct MemFile *memfile,
+        ReportList *reports)
+{
+       BlendFileData *bfd;
+
+       bfd = BLO_read_from_memfile(CTX_data_main(C), G.main->name, memfile, reports);
+       if (bfd) {
+               /* remove the unused screens and wm */
+               while (bfd->main->wm.first)
+                       BKE_libblock_free_ex(bfd->main, bfd->main->wm.first, true);
+               while (bfd->main->screen.first)
+                       BKE_libblock_free_ex(bfd->main, bfd->main->screen.first, true);
+
+               setup_app_data(C, bfd, "<memory1>", reports);
+       }
+       else {
+               BKE_reports_prepend(reports, "Loading failed: ");
+       }
+
+       return (bfd != NULL);
+}
+
+/* only read the userdef from a .blend */
+int BKE_blendfile_read_userdef(const char *filepath, ReportList *reports)
+{
+       BlendFileData *bfd;
+       int retval = BKE_BLENDFILE_READ_FAIL;
+
+       bfd = BLO_read_from_file(filepath, reports);
+       if (bfd) {
+               if (bfd->user) {
+                       retval = BKE_BLENDFILE_READ_OK_USERPREFS;
+
+                       /* only here free userdef themes... */
+                       BKE_blender_userdef_free();
+
+                       U = *bfd->user;
+                       MEM_freeN(bfd->user);
+               }
+               BKE_main_free(bfd->main);
+               MEM_freeN(bfd);
+       }
+
+       return retval;
+}
+
+/* only write the userdef in a .blend */
+int BKE_blendfile_write_userdef(const char *filepath, ReportList *reports)
+{
+       Main *mainb = MEM_callocN(sizeof(Main), "empty main");
+       int retval = 0;
+
+       if (BLO_write_file(mainb, filepath, G_FILE_USERPREFS, reports, NULL)) {
+               retval = 1;
+       }
+
+       MEM_freeN(mainb);
+
+       return retval;
+}
+
+/** \} */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Partial `.blend` file save.
+ * \{ */
+
+void BKE_blendfile_write_partial_begin(Main *bmain_src)
+{
+       BKE_main_id_tag_all(bmain_src, LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT, false);
+}
+
+void BKE_blendfile_write_partial_tag_ID(ID *id, bool set)
+{
+       if (set) {
+               id->tag |= LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT;
+       }
+       else {
+               id->tag &= ~(LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT);
+       }
+}
+
+static void blendfile_write_partial_cb(void *UNUSED(handle), Main *UNUSED(bmain), void *vid)
+{
+       if (vid) {
+               ID *id = vid;
+               /* only tag for need-expand if not done, prevents eternal loops */
+               if ((id->tag & LIB_TAG_DOIT) == 0)
+                       id->tag |= LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT;
+       }
+}
+
+/**
+ * \return Success.
+ */
+bool BKE_blendfile_write_partial(
+        Main *bmain_src, const char *filepath, const int write_flags, ReportList *reports)
+{
+       Main *bmain_dst = MEM_callocN(sizeof(Main), "copybuffer");
+       ListBase *lbarray_dst[MAX_LIBARRAY], *lbarray_src[MAX_LIBARRAY];
+       int a, retval;
+
+       void     *path_list_backup = NULL;
+       const int path_list_flag = (BKE_BPATH_TRAVERSE_SKIP_LIBRARY | BKE_BPATH_TRAVERSE_SKIP_MULTIFILE);
+
+       if (write_flags & G_FILE_RELATIVE_REMAP) {
+               path_list_backup = BKE_bpath_list_backup(bmain_src, path_list_flag);
+       }
+
+       BLO_main_expander(blendfile_write_partial_cb);
+       BLO_expand_main(NULL, bmain_src);
+
+       /* move over all tagged blocks */
+       set_listbasepointers(bmain_src, lbarray_src);
+       a = set_listbasepointers(bmain_dst, lbarray_dst);
+       while (a--) {
+               ID *id, *nextid;
+               ListBase *lb_dst = lbarray_dst[a], *lb_src = lbarray_src[a];
+
+               for (id = lb_src->first; id; id = nextid) {
+                       nextid = id->next;
+                       if (id->tag & LIB_TAG_DOIT) {
+                               BLI_remlink(lb_src, id);
+                               BLI_addtail(lb_dst, id);
+                       }
+               }
+       }
+
+
+       /* save the buffer */
+       retval = BLO_write_file(bmain_dst, filepath, write_flags, reports, NULL);
+
+       /* move back the main, now sorted again */
+       set_listbasepointers(bmain_src, lbarray_dst);
+       a = set_listbasepointers(bmain_dst, lbarray_src);
+       while (a--) {
+               ID *id;
+               ListBase *lb_dst = lbarray_dst[a], *lb_src = lbarray_src[a];
+
+               while ((id = BLI_pophead(lb_src))) {
+                       BLI_addtail(lb_dst, id);
+                       id_sort_by_name(lb_dst, id);
+               }
+       }
+
+       MEM_freeN(bmain_dst);
+
+       if (path_list_backup) {
+               BKE_bpath_list_restore(bmain_src, path_list_flag, path_list_backup);
+               BKE_bpath_list_free(path_list_backup);
+       }
+
+       return retval;
+}
+
+void BKE_blendfile_write_partial_end(Main *bmain_src)
+{
+       BKE_main_id_tag_all(bmain_src, LIB_TAG_NEED_EXPAND | LIB_TAG_DOIT, false);
+}
+
+/** \} */
index 448aaaa78306d2e22433c9f2ae691271d8b81854..f0ba63e35b442ded1a200fd8eb829f5374f9ace6 100644 (file)
@@ -59,7 +59,6 @@
 
 #include "BKE_appdir.h"
 #include "BKE_anim.h"
-#include "BKE_blender.h"
 #include "BKE_cloth.h"
 #include "BKE_dynamicpaint.h"
 #include "BKE_global.h"
index a5267175dfa6df116a8e199f6fa984f77f59aea5..ca367d8d06bf3363ae168bd87a0e975625fe0cbc 100644 (file)
@@ -8206,7 +8206,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init see do_versions_userdef() above! */
 
-       /* don't forget to set version number in BKE_blender.h! */
+       /* don't forget to set version number in BKE_blender_version.h! */
 }
 
 #if 0 // XXX: disabled for now... we still don't have this in the right place in the loading code for it to work
index 4871dc2f161b164a0c36d3e51ed476d265a9822d..124b1efddac0a11c2f59d16d2139099129247bce 100644 (file)
 #include "BLI_mempool.h"
 
 #include "BKE_action.h"
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_bpath.h"
 #include "BKE_curve.h"
 #include "BKE_constraint.h"
index 15e95a05425e3578f5e89b13f6b021b69b264fbd..bd32e989ae368fe5a319c7bff5d77209f1bb7005 100644 (file)
@@ -90,7 +90,7 @@ extern "C"
 #include "BKE_action.h" // pose functions
 #include "BKE_animsys.h"
 #include "BKE_armature.h"
-#include "BKE_blender.h" // version info
+#include "BKE_blender_version.h"
 #include "BKE_fcurve.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
index 64df42fc823e08ef00de6c030cbf8fcec60994dd..a884268fd5e4890f1fec9da959a486ddbd3671d8 100644 (file)
@@ -39,7 +39,6 @@
 #include "COLLADAFWPolygons.h"
 
 extern "C" {
-       #include "BKE_blender.h"
        #include "BKE_customdata.h"
        #include "BKE_displist.h"
        #include "BKE_global.h"
index 34e640a4b7baf1ea81c66c6dbedd3ac4cfc3cd66..c39a3aa5cfc2acb03bf9209fa81aba00dc252d92 100644 (file)
@@ -41,7 +41,7 @@
 
 #include "BLI_listbase.h"
 
-#include "BKE_blender.h"
+#include "BKE_blender_undo.h"
 #include "BKE_context.h"
 #include "BKE_gpencil.h"
 
index 6c961179c6f1ff39803f8ab92b7a2d327de85089..dd686f18a78fb6a8aa60683f64a7aec4eb8797f8 100644 (file)
@@ -60,7 +60,7 @@
 
 #include "PIL_time.h"
 
-#include "BKE_blender.h"
+#include "BKE_blender_undo.h"
 #include "BKE_brush.h"
 #include "BKE_colortools.h"
 #include "BKE_context.h"
index b1d2e329da10b558cbba42d447840b0f6af6c41d..3c862666aa6c24687e605815b278ec8997e5b0f1 100644 (file)
@@ -96,7 +96,7 @@ const unsigned char *UI_ThemeGetColorPtr(bTheme *btheme, int spacetype, int colo
        static char setting = 0;
        const char *cp = error;
        
-       /* ensure we're not getting a color after running BKE_userdef_free */
+       /* ensure we're not getting a color after running BKE_blender_userdef_free */
        BLI_assert(BLI_findindex(&U.themes, theme_active) != -1);
        BLI_assert(colorid != TH_UNDEFINED);
 
index 888bf83204261e5d086a61075430f1a5bc17dd53..11d96da578689483220cada01994e2e028ad8893 100644 (file)
@@ -700,7 +700,7 @@ static void bake_startjob(void *bkv, short *stop, short *do_update, float *progr
        bkr->progress = progress;
 
        RE_test_break_cb(bkr->re, NULL, thread_break);
-       G.is_break = false;   /* blender_test_break uses this global */
+       G.is_break = false;   /* BKE_blender_test_break uses this global */
 
        RE_Database_Baking(bkr->re, bmain, scene, scene->lay, scene->r.bake_mode, bkr->actob);
 
@@ -827,7 +827,7 @@ static int bake_image_exec(bContext *C, wmOperator *op)
                        bkr.reports = op->reports;
 
                        RE_test_break_cb(bkr.re, NULL, thread_break);
-                       G.is_break = false;   /* blender_test_break uses this global */
+                       G.is_break = false;   /* BKE_blender_test_break uses this global */
 
                        RE_Database_Baking(bkr.re, bmain, scene, scene->lay, scene->r.bake_mode, (scene->r.bake_flag & R_BAKE_TO_ACTIVE) ? OBACT : NULL);
 
@@ -843,7 +843,7 @@ static int bake_image_exec(bContext *C, wmOperator *op)
 
                                /* used to redraw in 2.4x but this is just for exec in 2.5 */
                                if (!G.background)
-                                       blender_test_break();
+                                       BKE_blender_test_break();
                        }
                        BLI_end_threads(&threads);
 
index 7cc17e4bfeaacce6ea96d00b3054ba7f9611bb11..a5d2d2c8be716e6e247f4c3b9e703f843655d1cf 100644 (file)
@@ -39,7 +39,6 @@
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
 
-#include "BKE_blender.h"
 #include "BKE_context.h"
 #include "BKE_deform.h"
 #include "BKE_object_deform.h"
@@ -426,7 +425,7 @@ static void dpaint_bake_startjob(void *customdata, short *stop, short *do_update
        job->start = PIL_check_seconds_timer();
        job->success = 1;
 
-    G.is_break = false; /* reset blender_test_break*/
+    G.is_break = false; /* reset BKE_blender_test_break*/
 
        /* XXX annoying hack: needed to prevent data corruption when changing
         * scene frame in separate threads
index eb8ea01f7505283e03488b350fd377cbf4009398..1203889cf0ef726f588af05901f0ae23e3f6809b 100644 (file)
@@ -49,7 +49,8 @@
 #include "DNA_view3d_types.h"
 #include "DNA_userdef_types.h"
 
-#include "BKE_blender.h"
+#include "BKE_blender_undo.h"
+#include "BKE_blender_version.h"
 #include "BKE_camera.h"
 #include "BKE_context.h"
 #include "BKE_colortools.h"
index 42f0aaab173c3c408f83fbf5e379522473ea27da..27d3f6648a219bc90b919ded3c3f3005480034f5 100644 (file)
@@ -34,7 +34,7 @@
 
 #include "DNA_userdef_types.h"
 
-#include "BKE_blender.h"
+#include "BKE_blender_undo.h"
 #include "BKE_context.h"
 #include "BKE_global.h"
 
index f22152651e240b50a57b7417e24cd21259c6c3ef..a2db6827b0e75c7c2c3af66e8e051cca4c059867 100644 (file)
@@ -177,7 +177,7 @@ void image_preview_event(int event)
 
                G.is_break = false;
                G.scene->nodetree->timecursor = set_timecursor;
-               G.scene->nodetree->test_break = blender_test_break;
+               G.scene->nodetree->test_break = BKE_blender_test_break;
                
                BIF_store_spare();
                
index 80dbfa140f69fad8d47f8b4842841e5aaa8db388..b02a46de97f438c9ec69998509ead256051f374d 100644 (file)
@@ -3140,7 +3140,7 @@ static int image_record_composite_apply(bContext *C, wmOperator *op)
        
        WM_cursor_time(CTX_wm_window(C), scene->r.cfra);
 
-       // XXX scene->nodetree->test_break = blender_test_break;
+       // XXX scene->nodetree->test_break = BKE_blender_test_break;
        // XXX scene->nodetree->test_break = NULL;
        
        BKE_image_all_free_anim_ibufs(scene->r.cfra);
index dafb71b448062ead7c5af32df18ef49a95859596..8dc6c4229b24cb453029bb3762e09300121f800f 100644 (file)
@@ -44,7 +44,7 @@
 #include "BLT_translation.h"
 
 #include "BKE_anim.h"
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_curve.h"
 #include "BKE_displist.h"
 #include "BKE_DerivedMesh.h"
index a5411da131b1adc3adc97dae611e992ba53d89ef..c30e688f1e71117cba3f2433713e90886bca0866 100644 (file)
@@ -44,7 +44,7 @@
 #include "BLI_utildefines.h"
 
 #include "BKE_appdir.h"
-#include "BKE_blender.h"
+#include "BKE_blender_copybuffer.h"
 #include "BKE_context.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
index bc7a8374c73046a5301de29bd8911aefabdeef01..441fd446cd6e601e3660d368e033dc87c16960b1 100644 (file)
@@ -40,7 +40,7 @@
 #include "BLI_blenlib.h"
 #include "BLI_utildefines.h"
 
-#include "BKE_blender.h"
+#include "BKE_blender_undo.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_global.h"
index a1853bf8daacc1eb5a2b79a132bca5554d4b4588..fe9fd1cd3ba5c978bc0c5dc2c834ef4cf930698f 100644 (file)
@@ -42,7 +42,7 @@
 
 #include "BLT_translation.h"
 
-#include "BKE_blender.h"
+#include "BKE_blender_undo.h"
 #include "BKE_context.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
index fc7959c00431443f1e96470266b1d2e27f83e258..b2ab0d2a08d875777e36f85bd28a4d20268cfaed 100644 (file)
@@ -58,7 +58,7 @@ typedef struct FileGlobal {
 /* minversion: in file, the oldest past blender version you can use compliant */
 /* example: if in 2.43 the meshes lose mesh data, minversion is 2.43 then too */
 /* or: in 2.42, subversion 1, same as above, minversion then is 2.42, min subversion 1 */
-/* (defines for version are in the BKE_blender.h file, for historic reasons) */
+/* (defines for version are in the BKE_blender_version.h file, for historic reasons) */
 
 
 #endif
index efd302ce451b7b95458edd460d25429e1060e3ab..0fbc1ce1854071d0dde44144a930be6a74b9be4a 100644 (file)
@@ -139,7 +139,7 @@ static void rna_userdef_dpi_update(Main *UNUSED(bmain), Scene *UNUSED(scene), Po
        /* font's are stored at each DPI level, without this we can easy load 100's of fonts */
        BLF_cache_clear();
 
-       BKE_userdef_state();
+       BKE_blender_userdef_refresh();
        WM_main_add_notifier(NC_WINDOW, NULL);      /* full redraw */
        WM_main_add_notifier(NC_SCREEN | NA_EDITED, NULL);    /* refresh region sizes */
 }
@@ -149,7 +149,7 @@ static void rna_userdef_virtual_pixel_update(Main *bmain, Scene *UNUSED(scene),
        /* font's are stored at each DPI level, without this we can easy load 100's of fonts */
        BLF_cache_clear();
        
-       BKE_userdef_state();
+       BKE_blender_userdef_refresh();
 
        /* force setting drawable again */
        wmWindowManager *wm = bmain->wm.first;
index 2f96bdbe3df7c81d284c8eaf58e932d13b47c55c..4cb39e0f13bde96390c0c03de3db29926c9913d1 100644 (file)
@@ -55,7 +55,6 @@
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 
-#include "BKE_blender.h"
 #include "BKE_colortools.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
index 9000cbb90d82c08272af5d052abfe155936082b6..2263c271ccf6f8c9ac2c9235a0c88080053c9cce 100644 (file)
@@ -53,7 +53,6 @@
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 
-#include "BKE_blender.h"
 #include "BKE_colortools.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
index c09b39139c2a90f0b814261236629599b39906cd..5bbfb4912e6aadc1015bc08130af622066f031e6 100644 (file)
@@ -35,7 +35,7 @@
 
 #include "BKE_appdir.h"
 #include "BKE_global.h" /* XXX, G.main only */
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_bpath.h"
 
 #include "RNA_types.h"
index 595bb7b0f226955c9afd38999fab152e81bc5d92..17617a231b2fcd8a89acdcdf8a19e78612dcc878 100644 (file)
@@ -48,7 +48,7 @@
 #include "BLI_utildefines.h"
 
 #include "BKE_appdir.h"
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_global.h"
 
 #include "DNA_ID.h"
index b9ba54e0104eb799efdc8b8f721ed44b00fc8204..f582cebb2601daa8b6f144a55bbb00842460082e 100644 (file)
@@ -35,7 +35,7 @@
 #include "BLI_path_util.h"
 
 #include "BKE_library.h"
-#include "BKE_blender.h"
+#include "BKE_blendfile.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
index 498a3f5bddacb1bcc4c63e72e06c5b38dfcd96d8..5e74eff645d7ac9a2c569d6f2fdce5983da90528 100644 (file)
@@ -74,6 +74,8 @@
 #include "BKE_utildefines.h"
 #include "BKE_autoexec.h"
 #include "BKE_blender.h"
+#include "BKE_blendfile.h"
+#include "BKE_blender_undo.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_global.h"
@@ -341,7 +343,7 @@ static void wm_init_userdef(bContext *C, const bool from_memory)
        /* update tempdir from user preferences */
        BKE_tempdir_init(U.tempdir);
 
-       BKE_userdef_state();
+       BKE_blender_userdef_refresh();
 }
 
 
@@ -551,7 +553,7 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
                
                /* confusing this global... */
                G.relbase_valid = 1;
-               retval = BKE_read_file(C, filepath, reports);
+               retval = BKE_blendfile_read(C, filepath, reports);
                /* when loading startup.blend's, we can be left with a blank path */
                if (G.main->name[0]) {
                        G.save_over = 1;
@@ -572,12 +574,12 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
                wm_window_match_do(C, &wmbase);
                WM_check(C); /* opens window(s), checks keymaps */
 
-               if (retval == BKE_READ_FILE_OK_USERPREFS) {
+               if (retval == BKE_BLENDFILE_READ_OK_USERPREFS) {
                        /* in case a userdef is read from regular .blend */
                        wm_init_userdef(C, false);
                }
                
-               if (retval != BKE_READ_FILE_FAIL) {
+               if (retval != BKE_BLENDFILE_READ_FAIL) {
                        if (do_history) {
                                wm_history_file_update();
                        }
@@ -690,7 +692,7 @@ int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const c
        
        if (!from_memory) {
                if (BLI_access(startstr, R_OK) == 0) {
-                       success = (BKE_read_file(C, startstr, NULL) != BKE_READ_FILE_FAIL);
+                       success = (BKE_blendfile_read(C, startstr, NULL) != BKE_BLENDFILE_READ_FAIL);
                }
                if (BLI_listbase_is_empty(&U.themes)) {
                        if (G.debug & G_DEBUG)
@@ -705,7 +707,7 @@ int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const c
        }
 
        if (success == 0) {
-               success = BKE_read_file_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL, true);
+               success = BKE_blendfile_read_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL, true);
                if (BLI_listbase_is_empty(&wmbase)) {
                        wm_clear_default_size(C);
                }
@@ -720,8 +722,8 @@ int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const c
        
        /* check new prefs only after startup.blend was finished */
        if (!from_memory && BLI_exists(prefstr)) {
-               int done = BKE_read_file_userdef(prefstr, NULL);
-               if (done != BKE_READ_FILE_FAIL) {
+               int done = BKE_blendfile_read_userdef(prefstr, NULL);
+               if (done != BKE_BLENDFILE_READ_FAIL) {
                        read_userdef_from_memory = false;
                        printf("Read new prefs: %s\n", prefstr);
                }
@@ -1352,7 +1354,7 @@ static int wm_userpref_write_exec(bContext *C, wmOperator *op)
        BLI_make_file_string("/", filepath, BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, NULL), BLENDER_USERPREF_FILE);
        printf("trying to save userpref at %s ", filepath);
 
-       if (BKE_write_file_userdef(filepath, op->reports) == 0) {
+       if (BKE_blendfile_write_userdef(filepath, op->reports) == 0) {
                printf("fail\n");
                return OPERATOR_CANCELLED;
        }
index e08d0afb906be5651d4e089d41d1d854b30e2708..6300d2ed3c74448873419247c099aabff72e08af 100644 (file)
@@ -54,6 +54,7 @@
 #include "BLO_writefile.h"
 
 #include "BKE_blender.h"
+#include "BKE_blender_undo.h"
 #include "BKE_context.h"
 #include "BKE_screen.h"
 #include "BKE_DerivedMesh.h"
@@ -492,10 +493,10 @@ void WM_exit_ext(bContext *C, const bool do_python)
        RE_FreeAllRender();
        RE_engines_exit();
        
-       ED_preview_free_dbase();  /* frees a Main dbase, before free_blender! */
+       ED_preview_free_dbase();  /* frees a Main dbase, before BKE_blender_free! */
 
        if (C && wm)
-               wm_free_reports(C);  /* before free_blender! - since the ListBases get freed there */
+               wm_free_reports(C);  /* before BKE_blender_free! - since the ListBases get freed there */
 
        BKE_sequencer_free_clipboard(); /* sequencer.c */
        BKE_tracking_clipboard_free();
@@ -506,7 +507,7 @@ void WM_exit_ext(bContext *C, const bool do_python)
        COM_deinitialize();
 #endif
        
-       free_blender();  /* blender.c, does entire library and spacetypes */
+       BKE_blender_free();  /* blender.c, does entire library and spacetypes */
 //     free_matcopybuf();
        ANIM_fcurves_copybuf_free();
        ANIM_drivers_copybuf_free();
@@ -534,10 +535,10 @@ void WM_exit_ext(bContext *C, const bool do_python)
        /* option not to close python so we can use 'atexit' */
        if (do_python) {
                /* XXX - old note */
-               /* before free_blender so py's gc happens while library still exists */
+               /* before BKE_blender_free so py's gc happens while library still exists */
                /* needed at least for a rare sigsegv that can happen in pydrivers */
 
-               /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
+               /* Update for blender 2.5, move after BKE_blender_free because blender now holds references to PyObject's
                 * so decref'ing them after python ends causes bad problems every time
                 * the pyDriver bug can be fixed if it happens again we can deal with it then */
                BPY_python_end();
@@ -562,7 +563,7 @@ void WM_exit_ext(bContext *C, const bool do_python)
        ED_file_exit(); /* for fsmenu */
 
        UI_exit();
-       BKE_userdef_free();
+       BKE_blender_userdef_free();
 
        RNA_exit(); /* should be after BPY_python_end so struct python slots are cleared */
        
index 1d78bb8c072ca9f89cd158c2ae76cd14232e13c9..e4ba3969824473680a6f877a7724cb394d110bee 100644 (file)
@@ -67,7 +67,7 @@
 #include "BLO_readfile.h"
 
 #include "BKE_appdir.h"
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_brush.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
index 688be21cdd09e1c5b67b5a30470674d9a79e3a95..42f6585f152989dfa39462e9b4c61b480aa3f58b 100644 (file)
@@ -457,7 +457,7 @@ static void wm_window_ghostwindow_add(wmWindowManager *wm, const char *title, wm
                /* displays with larger native pixels, like Macbook. Used to scale dpi with */
                /* needed here, because it's used before it reads userdef */
                U.pixelsize = wm_window_pixelsize(win);
-               BKE_userdef_state();
+               BKE_blender_userdef_refresh();
                
                wm_window_swap_buffers(win);
                
@@ -834,7 +834,7 @@ void wm_window_make_drawable(wmWindowManager *wm, wmWindow *win)
                
                /* this can change per window */
                U.pixelsize = wm_window_pixelsize(win);
-               BKE_userdef_state();
+               BKE_blender_userdef_refresh();
        }
 }
 
@@ -1197,7 +1197,7 @@ static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_ptr
                                // printf("change, pixel size %f\n", GHOST_GetNativePixelSize(win->ghostwin));
                                
                                U.pixelsize = wm_window_pixelsize(win);
-                               BKE_userdef_state();
+                               BKE_blender_userdef_refresh();
                                WM_event_add_notifier(C, NC_SCREEN | NA_EDITED, NULL);
                                WM_event_add_notifier(C, NC_WINDOW | NA_EDITED, NULL);
 
index 1010c9f06c812b441bb0b096df8cebbc931a85c7..c32ba9474fd0d07914028f855d79bb018d1feec0 100644 (file)
@@ -318,7 +318,7 @@ int main(
 
        BLI_threadapi_init();
 
-       initglobals();  /* blender.c */
+       BKE_blender_globals_init();  /* blender.c */
 
        IMB_init();
        BKE_images_init();
index c2ded1f2b090242393b3059f48deb15f4ecdd5b2..69765fc23414b388a758ee6cc57f378132b6f45d 100644 (file)
@@ -44,7 +44,7 @@
 #include "BLI_fileops.h"
 #include "BLI_mempool.h"
 
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_context.h"
 
 #include "BKE_global.h"
index b25d7c56f6e3529f8a619c53cb96c354268e3bec..80aba762cfc04b33fee26730d2fc05ad801cea27 100644 (file)
@@ -59,7 +59,7 @@
 #include BLI_SYSTEM_PID_H
 
 #include "BKE_appdir.h"  /* BKE_tempdir_base */
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
index eee53b775a974df82efea3b2cc1973e7a61a63b4..a69a8eae55dea13e2d87b70aad07a041a2a88259 100644 (file)
@@ -455,9 +455,9 @@ int main(int argc, char** argv)
 
        init_nodesystem();
        
-       initglobals();
+       BKE_blender_globals_init();
 
-       // We load our own G.main, so free the one that initglobals() gives us
+       // We load our own G.main, so free the one that BKE_blender_globals_init() gives us
        BKE_main_free(G.main);
        G.main = NULL;
 
@@ -1132,7 +1132,7 @@ int main(int argc, char** argv)
                }
        }
 
-       /* refer to WM_exit_ext() and free_blender(),
+       /* refer to WM_exit_ext() and BKE_blender_free(),
         * these are not called in the player but we need to match some of there behavior here,
         * if the order of function calls or blenders state isn't matching that of blender proper,
         * we may get troubles later on */
index 76eb915a30d1f08c49c4d04625d98620d0e52654..9f173a567ee458ae880963e00303ba090b89d69f 100644 (file)
@@ -134,7 +134,7 @@ extern "C" {
 #include "BKE_global.h"
 #include "BKE_library.h"
 #include "BKE_appdir.h"
-#include "BKE_blender.h"
+#include "BKE_blender_version.h"
 #include "BLI_blenlib.h"
 #include "GPU_material.h"
 #include "MEM_guardedalloc.h"