From 8ab52daa3aafcec7c8780653090c2be49eeb6748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 16 Mar 2021 15:57:00 +0100 Subject: BLO: Functions for temporarily loading a single datablock This introduces two functions to the blenloader module, here shown as calls for brevity: * `temp_lib_ctx = BLO_library_temp_load_id(real_main, blend_file_path, idcode, idname, reports);` * Now the data in `temp_lib_ctx->temp_id` can be used (but ought not to be not assigned to non-temp datablocks). * `BLO_library_temp_free(temp_lib_ctx);` The first loads a datablock from a blend file, and returns it as part of a `struct TempLibraryContext`. This struct contains the temp-loaded ID, as well as enough information to correctly free everything again. Differential Revision: https://developer.blender.org/D10736 --- source/blender/blenloader/BLO_readfile.h | 21 ++++++++ source/blender/blenloader/CMakeLists.txt | 1 + .../blender/blenloader/intern/readfile_tempload.c | 58 ++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 source/blender/blenloader/intern/readfile_tempload.c (limited to 'source/blender/blenloader') diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h index 09f4c405613..d8c613ba900 100644 --- a/source/blender/blenloader/BLO_readfile.h +++ b/source/blender/blenloader/BLO_readfile.h @@ -18,6 +18,7 @@ */ #pragma once +#include "BLI_listbase.h" #include "BLI_sys_types.h" /** \file @@ -221,6 +222,26 @@ void BLO_library_link_end(struct Main *mainl, int BLO_library_link_copypaste(struct Main *mainl, BlendHandle *bh, const uint64_t id_types_mask); +/** + * Struct for temporarily loading datablocks from a blend file. + */ +typedef struct TempLibraryContext { + struct Main *temp_main; + struct BlendHandle *blendhandle; + struct LibraryLink_Params liblink_params; + struct Library *lib; + + /* The ID datablock that was loaded. Is NULL if loading failed. */ + struct ID *temp_id; +} TempLibraryContext; + +TempLibraryContext *BLO_library_temp_load_id(struct Main *real_main, + const char *blend_file_path, + const short idcode, + const char *idname, + struct ReportList *reports); +void BLO_library_temp_free(TempLibraryContext *temp_lib_ctx); + /** \} */ void *BLO_library_read_struct(struct FileData *fd, struct BHead *bh, const char *blockname); diff --git a/source/blender/blenloader/CMakeLists.txt b/source/blender/blenloader/CMakeLists.txt index b65a3c4d58e..ee9b9a49768 100644 --- a/source/blender/blenloader/CMakeLists.txt +++ b/source/blender/blenloader/CMakeLists.txt @@ -50,6 +50,7 @@ set(SRC intern/blend_validate.c intern/readblenentry.c intern/readfile.c + intern/readfile_tempload.c intern/undofile.c intern/versioning_250.c intern/versioning_260.c diff --git a/source/blender/blenloader/intern/readfile_tempload.c b/source/blender/blenloader/intern/readfile_tempload.c new file mode 100644 index 00000000000..691d8f542ab --- /dev/null +++ b/source/blender/blenloader/intern/readfile_tempload.c @@ -0,0 +1,58 @@ +/* + * 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. + */ + +/** \file + * \ingroup blenloader + */ +#include "BLO_readfile.h" + +#include "MEM_guardedalloc.h" + +#include "BKE_report.h" + +#include "DNA_ID.h" + +TempLibraryContext *BLO_library_temp_load_id(struct Main *real_main, + const char *blend_file_path, + const short idcode, + const char *idname, + struct ReportList *reports) +{ + TempLibraryContext *temp_lib_ctx = MEM_callocN(sizeof(*temp_lib_ctx), __func__); + + temp_lib_ctx->blendhandle = BLO_blendhandle_from_file(blend_file_path, reports); + + BLO_library_link_params_init(&temp_lib_ctx->liblink_params, real_main, 0, LIB_TAG_TEMP_MAIN); + + temp_lib_ctx->temp_main = BLO_library_link_begin( + &temp_lib_ctx->blendhandle, blend_file_path, &temp_lib_ctx->liblink_params); + + temp_lib_ctx->temp_id = BLO_library_link_named_part(temp_lib_ctx->temp_main, + &temp_lib_ctx->blendhandle, + idcode, + idname, + &temp_lib_ctx->liblink_params); + + return temp_lib_ctx; +} + +void BLO_library_temp_free(TempLibraryContext *temp_lib_ctx) +{ + BLO_library_link_end( + temp_lib_ctx->temp_main, &temp_lib_ctx->blendhandle, &temp_lib_ctx->liblink_params); + BLO_blendhandle_close(temp_lib_ctx->blendhandle); + MEM_freeN(temp_lib_ctx); +} -- cgit v1.2.3