Writefile: move file flags to BlendFileWriteParams

This removes G_FILE_HISTORY, G_FILE_SAVE_COPY & G_FILE_USERPREFS.

Using file-flags made logic harder to follow since it's not so clear
which flags are expected to be in G.fileflags & which are meant to be
set and passed as arguments, these are shared between read & write
functions too.

Add BlendFileWriteParams so options which don't need to be stored
aren't mixed up with flags that are stored for reuse.
This commit is contained in:
Campbell Barton 2020-06-19 15:41:07 +10:00
parent 5a77f643f4
commit fade37ff07
7 changed files with 94 additions and 73 deletions

View File

@ -169,7 +169,7 @@ enum {
G_FILE_AUTOPACK = (1 << 0),
G_FILE_COMPRESS = (1 << 1),
G_FILE_USERPREFS = (1 << 9),
// G_FILE_DEPRECATED_9 = (1 << 9),
G_FILE_NO_UI = (1 << 10),
/* Bits 11 to 22 (inclusive) are deprecated & need to be cleared */
@ -177,12 +177,8 @@ enum {
/** On read, use #FileGlobal.filename instead of the real location on-disk,
* needed for recovering temp files so relative paths resolve */
G_FILE_RECOVER = (1 << 23),
/** On write, make backup `.blend1`, `.blend2` ... files, when the users preference is enabled */
G_FILE_HISTORY = (1 << 25),
/** BMesh option to save as older mesh format */
/* #define G_FILE_MESH_COMPAT (1 << 26) */
/** On write, restore paths after editing them (see #BLO_WRITE_PATH_REMAP_RELATIVE). */
G_FILE_SAVE_COPY = (1 << 27),
/* #define G_FILE_GLSL_NO_ENV_LIGHTING (1 << 28) */ /* deprecated */
};
@ -190,7 +186,7 @@ enum {
* Run-time only #G.fileflags which are never read or written to/from Blend files.
* This means we can change the values without worrying about do-versions.
*/
#define G_FILE_FLAG_ALL_RUNTIME (G_FILE_NO_UI | G_FILE_HISTORY | G_FILE_SAVE_COPY)
#define G_FILE_FLAG_ALL_RUNTIME (G_FILE_NO_UI)
/** ENDIAN_ORDER: indicates what endianness the platform where the file was written had. */
#if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)

View File

@ -109,8 +109,6 @@ MemFileUndoData *BKE_memfile_undo_encode(Main *bmain, MemFileUndoData *mfu_prev)
static int counter = 0;
char filename[FILE_MAX];
char numstr[32];
/* Don't do file history on undo. */
const int fileflags = G.fileflags & ~G_FILE_HISTORY;
/* Calculate current filename. */
counter++;
@ -119,7 +117,8 @@ MemFileUndoData *BKE_memfile_undo_encode(Main *bmain, MemFileUndoData *mfu_prev)
BLI_snprintf(numstr, sizeof(numstr), "%d.blend", counter);
BLI_join_dirfile(filename, sizeof(filename), BKE_tempdir_session(), numstr);
/* success = */ /* UNUSED */ BLO_write_file(bmain, filename, fileflags, NULL);
/* success = */ /* UNUSED */ BLO_write_file(
bmain, filename, G.fileflags, &(const struct BlendFileWriteParams){0}, NULL);
BLI_strncpy(mfu->filename, filename, sizeof(mfu->filename));
}

View File

@ -646,7 +646,13 @@ bool BKE_blendfile_userdef_write(const char *filepath, ReportList *reports)
Main *mainb = MEM_callocN(sizeof(Main), "empty main");
bool ok = false;
if (BLO_write_file(mainb, filepath, G_FILE_USERPREFS, reports)) {
if (BLO_write_file(mainb,
filepath,
0,
&(const struct BlendFileWriteParams){
.use_userdef = true,
},
reports)) {
ok = true;
}
@ -768,7 +774,7 @@ WorkspaceConfigFileData *BKE_blendfile_workspace_config_read(const char *filepat
bool BKE_blendfile_workspace_config_write(Main *bmain, const char *filepath, ReportList *reports)
{
int fileflags = G.fileflags & ~(G_FILE_NO_UI | G_FILE_HISTORY);
const int fileflags = G.fileflags & ~G_FILE_NO_UI;
bool retval = false;
BKE_blendfile_write_partial_begin(bmain);
@ -883,7 +889,13 @@ bool BKE_blendfile_write_partial(Main *bmain_src,
}
/* save the buffer */
retval = BLO_write_file_ex(bmain_dst, filepath, write_flags, reports, remap_mode, NULL);
retval = BLO_write_file(bmain_dst,
filepath,
write_flags,
&(const struct BlendFileWriteParams){
.remap_mode = remap_mode,
},
reports);
if (path_list_backup) {
BKE_bpath_list_restore(bmain_dst, path_list_flag, path_list_backup);

View File

@ -35,25 +35,30 @@ struct ReportList;
*/
typedef enum eBLO_WritePathRemap {
/** No path manipulation. */
BLO_WRITE_PATH_REMAP_NONE = 1,
BLO_WRITE_PATH_REMAP_NONE = 0,
/** Remap existing relative paths (default). */
BLO_WRITE_PATH_REMAP_RELATIVE = 2,
BLO_WRITE_PATH_REMAP_RELATIVE = 1,
/** Remap paths making all paths relative to the new location. */
BLO_WRITE_PATH_REMAP_RELATIVE_ALL = 3,
BLO_WRITE_PATH_REMAP_RELATIVE_ALL = 2,
/** Make all paths absolute. */
BLO_WRITE_PATH_REMAP_ABSOLUTE = 4,
BLO_WRITE_PATH_REMAP_ABSOLUTE = 3,
} eBLO_WritePathRemap;
extern bool BLO_write_file_ex(struct Main *mainvar,
const char *filepath,
const int write_flags,
struct ReportList *reports,
/* Extra arguments. */
eBLO_WritePathRemap remap_mode,
const struct BlendThumbnail *thumb);
/** Similar to #BlendFileReadParams. */
struct BlendFileWriteParams {
eBLO_WritePathRemap remap_mode;
/** Save `.blend1`, `.blend2`... etc. */
uint use_save_versions : 1;
/** On write, restore paths after editing them (see #BLO_WRITE_PATH_REMAP_RELATIVE). */
uint use_save_as_copy : 1;
uint use_userdef : 1;
const struct BlendThumbnail *thumb;
};
extern bool BLO_write_file(struct Main *mainvar,
const char *filepath,
const int write_flags,
const struct BlendFileWriteParams *params,
struct ReportList *reports);
extern bool BLO_write_file_mem(struct Main *mainvar,

View File

@ -4076,6 +4076,7 @@ static bool write_file_handle(Main *mainvar,
MemFile *compare,
MemFile *current,
int write_flags,
bool use_userdef,
const BlendThumbnail *thumb)
{
BHead bhead;
@ -4329,7 +4330,7 @@ static bool write_file_handle(Main *mainvar,
/* So changes above don't cause a 'DNA1' to be detected as changed on undo. */
mywrite_flush(wd);
if (write_flags & G_FILE_USERPREFS) {
if (use_userdef) {
write_userdef(&writer, &U);
}
@ -4400,18 +4401,22 @@ static bool do_history(const char *name, ReportList *reports)
/**
* \return Success.
*/
bool BLO_write_file_ex(Main *mainvar,
const char *filepath,
const int write_flags,
ReportList *reports,
/* Extra arguments. */
eBLO_WritePathRemap remap_mode,
const BlendThumbnail *thumb)
bool BLO_write_file(Main *mainvar,
const char *filepath,
const int write_flags,
const struct BlendFileWriteParams *params,
ReportList *reports)
{
char tempname[FILE_MAX + 1];
eWriteWrapType ww_type;
WriteWrap ww;
eBLO_WritePathRemap remap_mode = params->remap_mode;
const bool use_save_versions = params->use_save_versions;
const bool use_save_as_copy = params->use_save_as_copy;
const bool use_userdef = params->use_userdef;
const BlendThumbnail *thumb = params->thumb;
/* path backup/restore */
void *path_list_backup = NULL;
const int path_list_flag = (BKE_BPATH_TRAVERSE_SKIP_LIBRARY | BKE_BPATH_TRAVERSE_SKIP_MULTIFILE);
@ -4476,7 +4481,7 @@ bool BLO_write_file_ex(Main *mainvar,
if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) {
/* Check if we need to backup and restore paths. */
if (UNLIKELY(G_FILE_SAVE_COPY & write_flags)) {
if (UNLIKELY(use_save_as_copy)) {
path_list_backup = BKE_bpath_list_backup(mainvar, path_list_flag);
}
@ -4501,7 +4506,7 @@ bool BLO_write_file_ex(Main *mainvar,
}
/* actual file writing */
const bool err = write_file_handle(mainvar, &ww, NULL, NULL, write_flags, thumb);
const bool err = write_file_handle(mainvar, &ww, NULL, NULL, write_flags, use_userdef, thumb);
ww.close(&ww);
@ -4519,7 +4524,7 @@ bool BLO_write_file_ex(Main *mainvar,
/* file save to temporary file was successful */
/* now do reverse file history (move .blend1 -> .blend2, .blend -> .blend1) */
if (write_flags & G_FILE_HISTORY) {
if (use_save_versions) {
const bool err_hist = do_history(filepath, reports);
if (err_hist) {
BKE_report(reports, RPT_ERROR, "Version backup failed (file saved with @)");
@ -4540,23 +4545,15 @@ bool BLO_write_file_ex(Main *mainvar,
return 1;
}
bool BLO_write_file(Main *mainvar,
const char *filepath,
const int write_flags,
ReportList *reports)
{
return BLO_write_file_ex(
mainvar, filepath, write_flags, reports, BLO_WRITE_PATH_REMAP_NONE, NULL);
}
/**
* \return Success.
*/
bool BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags)
{
write_flags &= ~G_FILE_USERPREFS;
bool use_userdef = false;
const bool err = write_file_handle(mainvar, NULL, compare, current, write_flags, NULL);
const bool err = write_file_handle(
mainvar, NULL, compare, current, write_flags, use_userdef, NULL);
return (err == 0);
}

View File

@ -679,7 +679,8 @@ static void wm_file_read_post(bContext *C,
bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
{
/* assume automated tasks with background, don't write recent file list */
const bool do_history = (G.background == false) && (CTX_wm_manager(C)->op_undo_depth == 0);
const bool do_history_file_update = (G.background == false) &&
(CTX_wm_manager(C)->op_undo_depth == 0);
bool success = false;
const bool use_data = true;
@ -745,7 +746,7 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
WM_check(C); /* opens window(s), checks keymaps */
if (success) {
if (do_history) {
if (do_history_file_update) {
wm_history_file_update();
}
}
@ -777,7 +778,7 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
if (success == false) {
/* remove from recent files list */
if (do_history) {
if (do_history_file_update) {
RecentFile *recent = wm_file_history_find(filepath);
if (recent) {
wm_history_file_free(recent);
@ -1404,12 +1405,9 @@ bool write_crash_blend(void)
{
char path[FILE_MAX];
/* Don't do file history on crash file. */
const int fileflags = G.fileflags & ~G_FILE_HISTORY;
BLI_strncpy(path, BKE_main_blendfile_path_from_global(), sizeof(path));
BLI_path_extension_replace(path, sizeof(path), "_crash.blend");
if (BLO_write_file(G_MAIN, path, fileflags, NULL)) {
if (BLO_write_file(G_MAIN, path, G.fileflags, &(const struct BlendFileWriteParams){0}, NULL)) {
printf("written: %s\n", path);
return 1;
}
@ -1426,6 +1424,7 @@ static bool wm_file_write(bContext *C,
const char *filepath,
int fileflags,
eBLO_WritePathRemap remap_mode,
bool use_save_as_copy,
ReportList *reports)
{
Main *bmain = CTX_data_main(C);
@ -1492,21 +1491,29 @@ static bool wm_file_write(bContext *C,
ED_editors_flush_edits(bmain);
fileflags |= G_FILE_HISTORY; /* write file history */
/* first time saving */
/* XXX temp solution to solve bug, real fix coming (ton) */
if ((BKE_main_blendfile_path(bmain)[0] == '\0') && !(fileflags & G_FILE_SAVE_COPY)) {
if ((BKE_main_blendfile_path(bmain)[0] == '\0') && (use_save_as_copy == false)) {
BLI_strncpy(bmain->name, filepath, sizeof(bmain->name));
}
/* XXX temp solution to solve bug, real fix coming (ton) */
bmain->recovered = 0;
if (BLO_write_file_ex(CTX_data_main(C), filepath, fileflags, reports, remap_mode, thumb)) {
const bool do_history = (G.background == false) && (CTX_wm_manager(C)->op_undo_depth == 0);
if (BLO_write_file(CTX_data_main(C),
filepath,
fileflags,
&(const struct BlendFileWriteParams){
.remap_mode = remap_mode,
.use_save_versions = true,
.use_save_as_copy = use_save_as_copy,
.thumb = thumb,
},
reports)) {
const bool do_history_file_update = (G.background == false) &&
(CTX_wm_manager(C)->op_undo_depth == 0);
if (!(fileflags & G_FILE_SAVE_COPY)) {
if (use_save_as_copy == false) {
G.relbase_valid = 1;
BLI_strncpy(bmain->name, filepath, sizeof(bmain->name)); /* is guaranteed current file */
@ -1516,7 +1523,7 @@ static bool wm_file_write(bContext *C,
SET_FLAG_FROM_TEST(G.fileflags, fileflags & G_FILE_COMPRESS, G_FILE_COMPRESS);
/* prevent background mode scripts from clobbering history */
if (do_history) {
if (do_history_file_update) {
wm_history_file_update();
}
@ -1631,12 +1638,12 @@ void wm_autosave_timer(Main *bmain, wmWindowManager *wm, wmTimer *UNUSED(wt))
}
else {
/* Save as regular blend file. */
int fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_HISTORY);
const int fileflags = G.fileflags & ~G_FILE_COMPRESS;
ED_editors_flush_edits(bmain);
/* Error reporting into console */
BLO_write_file(bmain, filepath, fileflags, NULL);
/* Error reporting into console. */
BLO_write_file(bmain, filepath, fileflags, &(const struct BlendFileWriteParams){0}, NULL);
}
/* do timer after file write, just in case file write takes a long time */
wm->autosavetimer = WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime * 60.0);
@ -1754,10 +1761,15 @@ static int wm_homefile_write_exec(bContext *C, wmOperator *op)
ED_editors_flush_edits(bmain);
/* Force save as regular blend file. */
fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_HISTORY);
fileflags = G.fileflags & ~G_FILE_COMPRESS;
if (BLO_write_file_ex(
bmain, filepath, fileflags, op->reports, BLO_WRITE_PATH_REMAP_RELATIVE, NULL) == 0) {
if (BLO_write_file(bmain,
filepath,
fileflags,
&(const struct BlendFileWriteParams){
.remap_mode = BLO_WRITE_PATH_REMAP_RELATIVE,
},
op->reports) == 0) {
printf("fail\n");
return OPERATOR_CANCELLED;
}
@ -2672,6 +2684,8 @@ static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
Main *bmain = CTX_data_main(C);
char path[FILE_MAX];
const bool is_save_as = (op->type->invoke == wm_save_as_mainfile_invoke);
const bool use_save_as_copy = (RNA_struct_property_is_set(op->ptr, "copy") &&
RNA_boolean_get(op->ptr, "copy"));
/* We could expose all options to the users however in most cases remapping
* existing relative paths is a good default.
@ -2690,16 +2704,12 @@ static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
}
const int fileflags_orig = G.fileflags;
int fileflags = G.fileflags & ~G_FILE_USERPREFS;
int fileflags = G.fileflags;
/* set compression flag */
SET_FLAG_FROM_TEST(fileflags, RNA_boolean_get(op->ptr, "compress"), G_FILE_COMPRESS);
SET_FLAG_FROM_TEST(
fileflags,
(RNA_struct_property_is_set(op->ptr, "copy") && RNA_boolean_get(op->ptr, "copy")),
G_FILE_SAVE_COPY);
const bool ok = wm_file_write(C, path, fileflags, remap_mode, op->reports);
const bool ok = wm_file_write(C, path, fileflags, remap_mode, use_save_as_copy, op->reports);
if ((op->flag & OP_IS_INVOKE) == 0) {
/* OP_IS_INVOKE is set when the operator is called from the GUI.

View File

@ -494,13 +494,15 @@ void WM_exit_ex(bContext *C, const bool do_python)
Main *bmain = CTX_data_main(C);
char filename[FILE_MAX];
bool has_edited;
int fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_HISTORY);
const int fileflags = G.fileflags & ~G_FILE_COMPRESS;
BLI_join_dirfile(filename, sizeof(filename), BKE_tempdir_base(), BLENDER_QUIT_FILE);
has_edited = ED_editors_flush_edits(bmain);
if ((has_edited && BLO_write_file(bmain, filename, fileflags, NULL)) ||
if ((has_edited &&
BLO_write_file(
bmain, filename, fileflags, &(const struct BlendFileWriteParams){0}, NULL)) ||
(undo_memfile && BLO_memfile_write_file(undo_memfile, filename))) {
printf("Saved session recovery to '%s'\n", filename);
}