Add BKE_blendfile_userdef_read_from_memory

Needed to read user-preferences from in-memory startup.blend

Also skip data-blocks when reading preferences.
This commit is contained in:
Campbell Barton 2017-03-17 07:01:11 +11:00
parent b2d3956e7b
commit d4d8da28fc
4 changed files with 49 additions and 8 deletions

@ -51,8 +51,13 @@ bool BKE_blendfile_read_from_memfile(
struct bContext *C, struct MemFile *memfile,
struct ReportList *reports, int skip_flag);
struct UserDef *BKE_blendfile_userdef_read(const char *filepath, struct ReportList *reports);
int BKE_blendfile_userdef_write(const char *filepath, struct ReportList *reports);
struct UserDef *BKE_blendfile_userdef_read(
const char *filepath, struct ReportList *reports);
struct UserDef *BKE_blendfile_userdef_read_from_memory(
const void *filebuf, int filelength,
struct ReportList *reports);
int BKE_blendfile_userdef_write(const char *filepath, struct ReportList *reports);
/* partial blend file writing */

@ -430,7 +430,7 @@ UserDef *BKE_blendfile_userdef_read(const char *filepath, ReportList *reports)
BlendFileData *bfd;
UserDef *userdef = NULL;
bfd = BLO_read_from_file(filepath, reports, BLO_READ_SKIP_NONE);
bfd = BLO_read_from_file(filepath, reports, BLO_READ_SKIP_ALL & ~BLO_READ_SKIP_USERDEF);
if (bfd) {
if (bfd->user) {
userdef = bfd->user;
@ -442,6 +442,30 @@ UserDef *BKE_blendfile_userdef_read(const char *filepath, ReportList *reports)
return userdef;
}
UserDef *BKE_blendfile_userdef_read_from_memory(
const void *filebuf, int filelength,
ReportList *reports)
{
BlendFileData *bfd;
UserDef *userdef = NULL;
bfd = BLO_read_from_memory(filebuf, filelength, reports, BLO_READ_SKIP_ALL & ~BLO_READ_SKIP_USERDEF);
if (bfd) {
if (bfd->user) {
userdef = bfd->user;
}
BKE_main_free(bfd->main);
MEM_freeN(bfd);
}
else {
BKE_reports_prepend(reports, "Loading failed: ");
}
return userdef;
}
/* only write the userdef in a .blend */
int BKE_blendfile_userdef_write(const char *filepath, ReportList *reports)
{

@ -74,9 +74,12 @@ typedef struct BlendFileData {
/* skip reading some data-block types (may want to skip screen data too). */
typedef enum eBLOReadSkip {
BLO_READ_SKIP_NONE = 0,
BLO_READ_SKIP_USERDEF = (1 << 0),
BLO_READ_SKIP_NONE = 0,
BLO_READ_SKIP_USERDEF = (1 << 0),
BLO_READ_SKIP_DATA = (1 << 1),
} eBLOReadSkip;
#define BLO_READ_SKIP_ALL \
(BLO_READ_SKIP_USERDEF | BLO_READ_SKIP_DATA)
BlendFileData *BLO_read_from_file(
const char *filepath,

@ -8611,15 +8611,24 @@ BlendFileData *blo_read_file_internal(FileData *fd, const char *filepath)
case ID_ID:
/* Always adds to the most recently loaded ID_LI block, see direct_link_library.
* This is part of the file format definition. */
bhead = read_libblock(fd, mainlist.last, bhead, LIB_TAG_READ | LIB_TAG_EXTERN, NULL);
if (fd->skip_flags & BLO_READ_SKIP_DATA) {
bhead = blo_nextbhead(fd, bhead);
}
else {
bhead = read_libblock(fd, mainlist.last, bhead, LIB_TAG_READ | LIB_TAG_EXTERN, NULL);
}
break;
/* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
case ID_SCRN:
bhead->code = ID_SCR;
/* deliberate pass on to default */
default:
bhead = read_libblock(fd, bfd->main, bhead, LIB_TAG_LOCAL, NULL);
if (fd->skip_flags & BLO_READ_SKIP_DATA) {
bhead = blo_nextbhead(fd, bhead);
}
else {
bhead = read_libblock(fd, bfd->main, bhead, LIB_TAG_LOCAL, NULL);
}
}
}