Load user-preferences before startup file

Internal change needed for template support.
Loading the user preferences first so it's possible
for preferences to control startup behavior.

In general it's useful to load preferences before data-files,
so we know security settings for eg.
This commit is contained in:
Campbell Barton
2017-03-16 03:54:58 +11:00
parent c44cdd5905
commit c832354e33
9 changed files with 71 additions and 36 deletions

View File

@@ -34,20 +34,21 @@ 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 */
};
int BKE_blendfile_read(
struct bContext *C, const char *filepath,
struct ReportList *reports, int skip_flag);
bool BKE_blendfile_read_from_memory(
struct bContext *C, const void *filebuf,
int filelength, struct ReportList *reports, bool update_defaults);
struct bContext *C, const void *filebuf, int filelength,
struct ReportList *reports, int skip_flag, bool update_defaults);
bool BKE_blendfile_read_from_memfile(
struct bContext *C, struct MemFile *memfile,
struct ReportList *reports);
struct ReportList *reports, int skip_flag);
int BKE_blendfile_read_userdef(const char *filepath, struct ReportList *reports);
int BKE_blendfile_write_userdef(const char *filepath, struct ReportList *reports);

View File

@@ -108,9 +108,9 @@ static int read_undosave(bContext *C, UndoElem *uel)
G.fileflags |= G_FILE_NO_UI;
if (UNDO_DISK)
success = (BKE_blendfile_read(C, uel->str, NULL) != BKE_BLENDFILE_READ_FAIL);
success = (BKE_blendfile_read(C, uel->str, NULL, 0) != BKE_BLENDFILE_READ_FAIL);
else
success = BKE_blendfile_read_from_memfile(C, &uel->memfile, NULL);
success = BKE_blendfile_read_from_memfile(C, &uel->memfile, NULL, 0);
/* restore */
BLI_strncpy(G.main->name, mainstr, sizeof(G.main->name)); /* restore */
@@ -389,7 +389,7 @@ bool BKE_undo_save_file(const char *filename)
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);
BlendFileData *bfd = BLO_read_from_memfile(G.main, G.main->name, &curundo->memfile, NULL, 0);
if (bfd) {
mainp = bfd->main;

View File

@@ -349,7 +349,9 @@ static int handle_subversion_warning(Main *main, ReportList *reports)
return 1;
}
int BKE_blendfile_read(bContext *C, const char *filepath, ReportList *reports)
int BKE_blendfile_read(
bContext *C, const char *filepath,
ReportList *reports, int skip_flags)
{
BlendFileData *bfd;
int retval = BKE_BLENDFILE_READ_OK;
@@ -357,9 +359,11 @@ int BKE_blendfile_read(bContext *C, const char *filepath, ReportList *reports)
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);
bfd = BLO_read_from_file(filepath, reports, skip_flags);
if (bfd) {
if (bfd->user) retval = BKE_BLENDFILE_READ_OK_USERPREFS;
if (bfd->user) {
retval = BKE_BLENDFILE_READ_OK_USERPREFS;
}
if (0 == handle_subversion_warning(bfd->main, reports)) {
BKE_main_free(bfd->main);
@@ -379,11 +383,11 @@ int BKE_blendfile_read(bContext *C, const char *filepath, ReportList *reports)
bool BKE_blendfile_read_from_memory(
bContext *C, const void *filebuf, int filelength,
ReportList *reports, bool update_defaults)
ReportList *reports, int skip_flags, bool update_defaults)
{
BlendFileData *bfd;
bfd = BLO_read_from_memory(filebuf, filelength, reports);
bfd = BLO_read_from_memory(filebuf, filelength, reports, skip_flags);
if (bfd) {
if (update_defaults)
BLO_update_defaults_startup_blend(bfd->main);
@@ -399,11 +403,11 @@ bool BKE_blendfile_read_from_memory(
/* memfile is the undo buffer */
bool BKE_blendfile_read_from_memfile(
bContext *C, struct MemFile *memfile,
ReportList *reports)
ReportList *reports, int skip_flags)
{
BlendFileData *bfd;
bfd = BLO_read_from_memfile(CTX_data_main(C), G.main->name, memfile, reports);
bfd = BLO_read_from_memfile(CTX_data_main(C), G.main->name, memfile, reports, skip_flags);
if (bfd) {
/* remove the unused screens and wm */
while (bfd->main->wm.first)
@@ -426,7 +430,7 @@ 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);
bfd = BLO_read_from_file(filepath, reports, 0);
if (bfd) {
if (bfd->user) {
retval = BKE_BLENDFILE_READ_OK_USERPREFS;

View File

@@ -71,11 +71,21 @@ typedef struct BlendFileData {
BlenFileType type;
} BlendFileData;
BlendFileData *BLO_read_from_file(const char *filepath, struct ReportList *reports);
BlendFileData *BLO_read_from_memory(const void *mem, int memsize, struct ReportList *reports);
/* skip reading some data-block types (may want to skip screen data too). */
typedef enum eBLOReadSkip {
BLO_READ_SKIP_USERDEF = (1 << 0),
} eBLOReadSkip;
BlendFileData *BLO_read_from_file(
const char *filepath,
struct ReportList *reports, eBLOReadSkip skip_flag);
BlendFileData *BLO_read_from_memory(
const void *mem, int memsize,
struct ReportList *reports, eBLOReadSkip skip_flag);
BlendFileData *BLO_read_from_memfile(
struct Main *oldmain, const char *filename, struct MemFile *memfile,
struct ReportList *reports);
struct ReportList *reports, eBLOReadSkip skip_flag);
void BLO_blendfiledata_free(BlendFileData *bfd);

View File

@@ -317,7 +317,9 @@ void BLO_blendhandle_close(BlendHandle *bh)
* \param reports If the return value is NULL, errors indicating the cause of the failure.
* \return The data of the file.
*/
BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
BlendFileData *BLO_read_from_file(
const char *filepath,
ReportList *reports, eBLOReadSkip skip_flags)
{
BlendFileData *bfd = NULL;
FileData *fd;
@@ -325,6 +327,7 @@ BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
fd = blo_openblenderfile(filepath, reports);
if (fd) {
fd->reports = reports;
fd->skip_flags = skip_flags;
bfd = blo_read_file_internal(fd, filepath);
blo_freefiledata(fd);
}
@@ -341,7 +344,9 @@ BlendFileData *BLO_read_from_file(const char *filepath, ReportList *reports)
* \param reports If the return value is NULL, errors indicating the cause of the failure.
* \return The data of the file.
*/
BlendFileData *BLO_read_from_memory(const void *mem, int memsize, ReportList *reports)
BlendFileData *BLO_read_from_memory(
const void *mem, int memsize,
ReportList *reports, eBLOReadSkip skip_flags)
{
BlendFileData *bfd = NULL;
FileData *fd;
@@ -349,6 +354,7 @@ BlendFileData *BLO_read_from_memory(const void *mem, int memsize, ReportList *re
fd = blo_openblendermemory(mem, memsize, reports);
if (fd) {
fd->reports = reports;
fd->skip_flags = skip_flags;
bfd = blo_read_file_internal(fd, "");
blo_freefiledata(fd);
}
@@ -362,7 +368,9 @@ BlendFileData *BLO_read_from_memory(const void *mem, int memsize, ReportList *re
* \param oldmain old main, from which we will keep libraries and other datablocks that should not have changed.
* \param filename current file, only for retrieving library data.
*/
BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
BlendFileData *BLO_read_from_memfile(
Main *oldmain, const char *filename, MemFile *memfile,
ReportList *reports, eBLOReadSkip skip_flags)
{
BlendFileData *bfd = NULL;
FileData *fd;
@@ -371,6 +379,7 @@ BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFil
fd = blo_openblendermemfile(memfile, reports);
if (fd) {
fd->reports = reports;
fd->skip_flags = skip_flags;
BLI_strncpy(fd->relabase, filename, sizeof(fd->relabase));
/* clear ob->proxy_from pointers in old main */

View File

@@ -8597,7 +8597,12 @@ BlendFileData *blo_read_file_internal(FileData *fd, const char *filepath)
bhead = read_global(bfd, fd, bhead);
break;
case USER:
bhead = read_userdef(bfd, fd, bhead);
if (fd->skip_flags & BLO_READ_SKIP_USERDEF) {
bhead = blo_nextbhead(fd, bhead);
}
else {
bhead = read_userdef(bfd, fd, bhead);
}
break;
case ENDB:
bhead = NULL;

View File

@@ -81,6 +81,8 @@ typedef struct FileData {
int id_name_offs; /* used to retrieve ID names from (bhead+1) */
int globalf, fileflags; /* for do_versions patching */
eBLOReadSkip skip_flags; /* skip some data-blocks */
struct OldNewMap *datamap;
struct OldNewMap *globmap;
struct OldNewMap *libmap;

View File

@@ -196,7 +196,7 @@ static Main *load_main_from_memory(const void *blend, int blend_size)
BlendFileData *bfd;
G.fileflags |= G_FILE_NO_UI;
bfd = BLO_read_from_memory(blend, blend_size, NULL);
bfd = BLO_read_from_memory(blend, blend_size, NULL, 0);
if (bfd) {
bmain = bfd->main;

View File

@@ -554,7 +554,7 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
/* confusing this global... */
G.relbase_valid = 1;
retval = BKE_blendfile_read(C, filepath, reports);
retval = BKE_blendfile_read(C, filepath, reports, 0);
/* when loading startup.blend's, we can be left with a blank path */
if (G.main->name[0]) {
G.save_over = 1;
@@ -652,6 +652,7 @@ int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const c
* And in this case versioning code is to be run.
*/
bool read_userdef_from_memory = true;
eBLOReadSkip skip_flags = 0;
/* options exclude eachother */
BLI_assert((from_memory && custom_file) == 0);
@@ -691,9 +692,19 @@ int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const c
/* put aside screens to match with persistent windows later */
wm_window_match_init(C, &wmbase);
/* load preferences before startup.blend */
if (!from_memory && BLI_exists(prefstr)) {
int done = BKE_blendfile_read_userdef(prefstr, NULL);
if (done != BKE_BLENDFILE_READ_FAIL) {
read_userdef_from_memory = false;
skip_flags |= BLO_READ_SKIP_USERDEF;
printf("Read new prefs: %s\n", prefstr);
}
}
if (!from_memory) {
if (BLI_access(startstr, R_OK) == 0) {
success = (BKE_blendfile_read(C, startstr, NULL) != BKE_BLENDFILE_READ_FAIL);
success = (BKE_blendfile_read(C, startstr, NULL, skip_flags) != BKE_BLENDFILE_READ_FAIL);
}
if (BLI_listbase_is_empty(&U.themes)) {
if (G.debug & G_DEBUG)
@@ -708,7 +719,9 @@ int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const c
}
if (success == 0) {
success = BKE_blendfile_read_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, skip_flags, true);
if (BLI_listbase_is_empty(&wmbase)) {
wm_clear_default_size(C);
}
@@ -721,15 +734,6 @@ int wm_homefile_read(bContext *C, ReportList *reports, bool from_memory, const c
#endif
}
/* check new prefs only after startup.blend was finished */
if (!from_memory && BLI_exists(prefstr)) {
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);
}
}
/* prevent buggy files that had G_FILE_RELATIVE_REMAP written out by mistake. Screws up autosaves otherwise
* can remove this eventually, only in a 2.53 and older, now its not written */
G.fileflags &= ~G_FILE_RELATIVE_REMAP;