Remove G.relbase_valid

In almost all cases there is no difference between `G.relbase_valid`
and checking `G.main->filepath` isn't an empty string.

In many places a non-empty string is already being used instead of
`G.relbase_valid`.

The only situation where this was needed was when saving from
`wm_file_write` where they temporarily became out of sync.
This has been replaced by adding a new member to `BlendFileWriteParams`
to account for saving an unsaved file for the first time.

Reviewed By: brecht

Ref D13564
This commit is contained in:
Campbell Barton 2021-12-16 11:38:10 +11:00
parent 4b12f521e3
commit 5de109cc2d
17 changed files with 87 additions and 63 deletions

View File

@ -50,9 +50,6 @@ typedef struct Global {
/** Last used location for library link/append. */
char lib[1024];
/** When set: `G_MAIN->filepath` contains valid relative base path. */
bool relbase_valid;
/**
* Strings of recently opened files to show in the file menu.
* A list of #RecentFile read from #BLENDER_HISTORY_FILE.

View File

@ -116,6 +116,7 @@ enum {
typedef struct Main {
struct Main *next, *prev;
/** The file-path of this blend file, an empty string indicates an unsaved file. */
char filepath[1024]; /* 1024 = FILE_MAX */
short versionfile, subversionfile; /* see BLENDER_FILE_VERSION, BLENDER_FILE_SUBVERSION */
short minversionfile, minsubversionfile;

View File

@ -2062,9 +2062,10 @@ static void stampdata(
time_t t;
if (scene->r.stamp & R_STAMP_FILENAME) {
const char *blendfile_path = BKE_main_blendfile_path_from_global();
SNPRINTF(stamp_data->file,
do_prefix ? "File %s" : "%s",
G.relbase_valid ? BKE_main_blendfile_path_from_global() : "<untitled>");
(blendfile_path[0] != '\0') ? blendfile_path : "<untitled>");
}
else {
stamp_data->file[0] = '\0';

View File

@ -933,7 +933,7 @@ const char *BKE_modifier_path_relbase(Main *bmain, Object *ob)
* - Else if the file has been saved return the blend file path.
* - Else if the file isn't saved and the ID isn't from a library, return the temp dir.
*/
if (G.relbase_valid || ID_IS_LINKED(ob)) {
if ((bmain->filepath[0] != '\0') || ID_IS_LINKED(ob)) {
return ID_BLEND_PATH(bmain, &ob->id);
}
@ -948,7 +948,8 @@ const char *BKE_modifier_path_relbase_from_global(Object *ob)
void BKE_modifier_path_init(char *path, int path_maxlen, const char *name)
{
BLI_join_dirfile(path, path_maxlen, G.relbase_valid ? "//" : BKE_tempdir_session(), name);
const char *blendfile_path = BKE_main_blendfile_path_from_global();
BLI_join_dirfile(path, path_maxlen, blendfile_path[0] ? "//" : BKE_tempdir_session(), name);
}
/**

View File

@ -1322,10 +1322,11 @@ static int ptcache_frame_from_filename(const char *filename, const char *ext)
static int ptcache_path(PTCacheID *pid, char *filename)
{
const char *blendfile_path = BKE_main_blendfile_path_from_global();
Library *lib = (pid->owner_id) ? pid->owner_id->lib : NULL;
const char *blendfilename = (lib && (pid->cache->flag & PTCACHE_IGNORE_LIBPATH) == 0) ?
lib->filepath_abs :
BKE_main_blendfile_path_from_global();
blendfile_path;
size_t i;
if (pid->cache->flag & PTCACHE_EXTERNAL) {
@ -1337,7 +1338,7 @@ static int ptcache_path(PTCacheID *pid, char *filename)
return BLI_path_slash_ensure(filename); /* new strlen() */
}
if (G.relbase_valid || lib) {
if ((blendfile_path[0] != '\0') || lib) {
char file[MAX_PTCACHE_PATH]; /* we don't want the dir, only the file */
BLI_split_file_part(blendfilename, file, sizeof(file));
@ -1422,8 +1423,11 @@ static int ptcache_filename(PTCacheID *pid, char *filename, int cfra, short do_p
filename[0] = '\0';
newname = filename;
if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
return 0; /* save blend file before using disk pointcache */
if ((pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] == '\0') {
return 0; /* save blend file before using disk pointcache */
}
}
/* start with temp dir */
@ -1469,8 +1473,11 @@ static PTCacheFile *ptcache_file_open(PTCacheID *pid, int mode, int cfra)
return NULL;
}
#endif
if (!G.relbase_valid && (pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
return NULL; /* save blend file before using disk pointcache */
if ((pid->cache->flag & PTCACHE_EXTERNAL) == 0) {
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] == '\0') {
return NULL; /* save blend file before using disk pointcache */
}
}
ptcache_filename(pid, filename, cfra, 1, 1);
@ -3444,8 +3451,9 @@ void BKE_ptcache_toggle_disk_cache(PTCacheID *pid)
{
PointCache *cache = pid->cache;
int last_exact = cache->last_exact;
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (!G.relbase_valid) {
if (blendfile_path[0] == '\0') {
cache->flag &= ~PTCACHE_DISK_CACHE;
if (G.debug & G_DEBUG) {
printf("File must be saved before using disk cache!\n");

View File

@ -60,6 +60,11 @@ struct BlendFileWriteParams {
uint use_save_versions : 1;
/** On write, restore paths after editing them (see #BLO_WRITE_PATH_REMAP_RELATIVE). */
uint use_save_as_copy : 1;
/**
* Saving from the UI writes into the #Main.filepath, so a check for the `filepath`
* not having been set is needed.
*/
uint use_save_first_time : 1;
uint use_userdef : 1;
const struct BlendThumbnail *thumb;
};

View File

@ -1326,6 +1326,8 @@ bool BLO_write_file(Main *mainvar,
const bool use_save_as_copy = params->use_save_as_copy;
const bool use_userdef = params->use_userdef;
const BlendThumbnail *thumb = params->thumb;
const bool relbase_valid = (mainvar->filepath[0] != '\0') &&
(params->use_save_first_time == false);
/* path backup/restore */
void *path_list_backup = NULL;
@ -1353,9 +1355,8 @@ bool BLO_write_file(Main *mainvar,
if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) {
if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {
/* Make all relative as none of the existing paths can be relative in an unsaved document.
*/
if (G.relbase_valid == false) {
/* Make all relative as none of the existing paths can be relative in an unsaved document. */
if (relbase_valid == false) {
remap_mode = BLO_WRITE_PATH_REMAP_RELATIVE_ALL;
}
}
@ -1371,13 +1372,13 @@ bool BLO_write_file(Main *mainvar,
/* Only for relative, not relative-all, as this means making existing paths relative. */
if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) {
if (G.relbase_valid && (BLI_path_cmp(dir_dst, dir_src) == 0)) {
if (relbase_valid && (BLI_path_cmp(dir_dst, dir_src) == 0)) {
/* Saved to same path. Nothing to do. */
remap_mode = BLO_WRITE_PATH_REMAP_NONE;
}
}
else if (remap_mode == BLO_WRITE_PATH_REMAP_ABSOLUTE) {
if (G.relbase_valid == false) {
if (relbase_valid == false) {
/* Unsaved, all paths are absolute.Even if the user manages to set a relative path,
* there is no base-path that can be used to make it absolute. */
remap_mode = BLO_WRITE_PATH_REMAP_NONE;

View File

@ -180,8 +180,9 @@ static int screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *event)
/* extension is added by 'screenshot_check' after */
char filepath[FILE_MAX] = "//screen";
if (G.relbase_valid) {
BLI_strncpy(filepath, BKE_main_blendfile_path_from_global(), sizeof(filepath));
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] != '\0') {
BLI_strncpy(filepath, blendfile_path, sizeof(filepath));
BLI_path_extension_replace(filepath, sizeof(filepath), ""); /* strip '.blend' */
}
RNA_string_set(op->ptr, "filepath", filepath);

View File

@ -2463,9 +2463,10 @@ static void file_expand_directory(bContext *C)
if (params) {
if (BLI_path_is_rel(params->dir)) {
/* Use of 'default' folder here is just to avoid an error message on '//' prefix. */
const char *blendfile_path = BKE_main_blendfile_path(bmain);
BLI_path_abs(params->dir,
G.relbase_valid ? BKE_main_blendfile_path(bmain) :
BKE_appdir_folder_default_or_root());
(blendfile_path[0] != '\0') ? blendfile_path :
BKE_appdir_folder_default_or_root());
}
else if (params->dir[0] == '~') {
char tmpstr[sizeof(params->dir) - 1];

View File

@ -408,13 +408,14 @@ void FILE_OT_unpack_item(wmOperatorType *ot)
static int make_paths_relative_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
const char *blendfile_path = BKE_main_blendfile_path(bmain);
if (!G.relbase_valid) {
if (blendfile_path[0] == '\0') {
BKE_report(op->reports, RPT_WARNING, "Cannot set relative paths with an unsaved blend file");
return OPERATOR_CANCELLED;
}
BKE_bpath_relative_convert(bmain, BKE_main_blendfile_path(bmain), op->reports);
BKE_bpath_relative_convert(bmain, blendfile_path, op->reports);
/* redraw everything so any changed paths register */
WM_main_add_notifier(NC_WINDOW, NULL);
@ -445,13 +446,14 @@ void FILE_OT_make_paths_relative(wmOperatorType *ot)
static int make_paths_absolute_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
const char *blendfile_path = BKE_main_blendfile_path(bmain);
if (!G.relbase_valid) {
if (blendfile_path[0] == '\0') {
BKE_report(op->reports, RPT_WARNING, "Cannot set absolute paths with an unsaved blend file");
return OPERATOR_CANCELLED;
}
BKE_bpath_absolute_convert(bmain, BKE_main_blendfile_path(bmain), op->reports);
BKE_bpath_absolute_convert(bmain, blendfile_path, op->reports);
/* redraw everything so any changed paths register */
WM_main_add_notifier(NC_WINDOW, NULL);

View File

@ -352,6 +352,7 @@ void unpack_menu(bContext *C,
uiLayout *layout;
char line[FILE_MAX + 100];
wmOperatorType *ot = WM_operatortype_find(opname, 1);
const char *blendfile_path = BKE_main_blendfile_path(bmain);
pup = UI_popup_menu_begin(C, IFACE_("Unpack File"), ICON_NONE);
layout = UI_popup_menu_layout(pup);
@ -361,13 +362,13 @@ void unpack_menu(bContext *C,
RNA_enum_set(&props_ptr, "method", PF_REMOVE);
RNA_string_set(&props_ptr, "id", id_name);
if (G.relbase_valid) {
if (blendfile_path[0] != '\0') {
char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
BLI_split_file_part(abs_name, fi, sizeof(fi));
BLI_snprintf(local_name, sizeof(local_name), "//%s/%s", folder, fi);
if (!STREQ(abs_name, local_name)) {
switch (BKE_packedfile_compare_to_file(BKE_main_blendfile_path(bmain), local_name, pf)) {
switch (BKE_packedfile_compare_to_file(blendfile_path, local_name, pf)) {
case PF_CMP_NOFILE:
BLI_snprintf(line, sizeof(line), TIP_("Create %s"), local_name);
uiItemFullO_ptr(layout, ot, line, ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, 0, &props_ptr);
@ -400,7 +401,7 @@ void unpack_menu(bContext *C,
}
}
switch (BKE_packedfile_compare_to_file(BKE_main_blendfile_path(bmain), abs_name, pf)) {
switch (BKE_packedfile_compare_to_file(blendfile_path, abs_name, pf)) {
case PF_CMP_NOFILE:
BLI_snprintf(line, sizeof(line), TIP_("Create %s"), abs_name);
// uiItemEnumO_ptr(layout, ot, line, 0, "method", PF_WRITE_ORIGINAL);

View File

@ -56,9 +56,10 @@ static void rna_Main_use_autopack_set(PointerRNA *UNUSED(ptr), bool value)
}
}
static bool rna_Main_is_saved_get(PointerRNA *UNUSED(ptr))
static bool rna_Main_is_saved_get(PointerRNA *ptr)
{
return G.relbase_valid;
const Main *bmain = (Main *)ptr->data;
return (bmain->filepath[0] != '\0');
}
static bool rna_Main_is_dirty_get(PointerRNA *ptr)

View File

@ -955,14 +955,6 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
/* #BKE_blendfile_read_result_setup sets new Main into context. */
Main *bmain = CTX_data_main(C);
/* When recovering a session from an unsaved file, this can have a blank path. */
if (BKE_main_blendfile_path(bmain)[0] != '\0') {
G.relbase_valid = 1;
}
else {
G.relbase_valid = 0;
}
/* match the read WM with current WM */
wm_window_match_do(C, &wmbase, &bmain->wm, &bmain->wm);
WM_check(C); /* opens window(s), checks keymaps */
@ -1149,8 +1141,6 @@ void wm_homefile_read_ex(bContext *C,
wm_file_read_pre(C, use_data, use_userdef);
if (use_data) {
G.relbase_valid = 0;
/* put aside screens to match with persistent windows later */
wm_window_match_init(C, &wmbase);
}
@ -1825,7 +1815,8 @@ static bool wm_file_write(bContext *C,
/* First time saving. */
/* XXX(ton): temp solution to solve bug, real fix coming. */
if ((BKE_main_blendfile_path(bmain)[0] == '\0') && (use_save_as_copy == false)) {
const bool relbase_valid = (bmain->filepath[0] != '\0');
if ((relbase_valid == false) && (use_save_as_copy == false)) {
STRNCPY(bmain->filepath, filepath);
}
@ -1839,6 +1830,7 @@ static bool wm_file_write(bContext *C,
.remap_mode = remap_mode,
.use_save_versions = true,
.use_save_as_copy = use_save_as_copy,
.use_save_first_time = !relbase_valid,
.thumb = thumb,
},
reports)) {
@ -1846,7 +1838,6 @@ static bool wm_file_write(bContext *C,
(CTX_wm_manager(C)->op_undo_depth == 0);
if (use_save_as_copy == false) {
G.relbase_valid = 1;
STRNCPY(bmain->filepath, filepath); /* is guaranteed current file */
}
@ -1898,8 +1889,13 @@ static void wm_autosave_location(char *filepath)
const char *savedir;
#endif
if (G_MAIN && G.relbase_valid) {
const char *basename = BLI_path_basename(BKE_main_blendfile_path_from_global());
/* Normally there is no need to check for this to be NULL,
* however this runs on exit when it may be cleared. */
Main *bmain = G_MAIN;
const char *blendfile_path = bmain ? BKE_main_blendfile_path(bmain) : NULL;
if (blendfile_path && (blendfile_path[0] != '\0')) {
const char *basename = BLI_path_basename(blendfile_path);
int len = strlen(basename) - 6;
BLI_snprintf(path, sizeof(path), "%.*s_%d_autosave.blend", len, basename, pid);
}
@ -2109,7 +2105,7 @@ static int wm_homefile_write_exec(bContext *C, wmOperator *op)
fileflags,
&(const struct BlendFileWriteParams){
/* Make all paths absolute when saving the startup file.
* On load the `G.relbase_valid` will be false so the paths
* On load the `G.main->filepath` will be empty so the paths
* won't have a base for resolving the relative paths. */
.remap_mode = BLO_WRITE_PATH_REMAP_ABSOLUTE,
/* Don't apply any path changes to the current blend file. */
@ -2847,7 +2843,8 @@ static int wm_revert_mainfile_exec(bContext *C, wmOperator *op)
static bool wm_revert_mainfile_poll(bContext *UNUSED(C))
{
return G.relbase_valid;
const char *blendfile_path = BKE_main_blendfile_path_from_global();
return (blendfile_path[0] != '\0');
}
void WM_OT_revert_mainfile(wmOperatorType *ot)
@ -3004,9 +3001,9 @@ void WM_OT_recover_auto_save(wmOperatorType *ot)
* Both #WM_OT_save_as_mainfile & #WM_OT_save_mainfile.
* \{ */
static void wm_filepath_default(char *filepath)
static void wm_filepath_default(const Main *bmain, char *filepath)
{
if (G.relbase_valid == false) {
if (bmain->filepath[0] == '\0') {
BLI_path_filename_ensure(filepath, FILE_MAX, "untitled.blend");
}
}
@ -3017,7 +3014,8 @@ static void save_set_compress(wmOperator *op)
prop = RNA_struct_find_property(op->ptr, "compress");
if (!RNA_property_is_set(op->ptr, prop)) {
if (G.relbase_valid) { /* keep flag for existing file */
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] != '\0') { /* Keep flag for existing file. */
RNA_property_boolean_set(op->ptr, prop, (G.fileflags & G_FILE_COMPRESS) != 0);
}
else { /* use userdef for new file */
@ -3034,16 +3032,17 @@ static void save_set_filepath(bContext *C, wmOperator *op)
prop = RNA_struct_find_property(op->ptr, "filepath");
if (!RNA_property_is_set(op->ptr, prop)) {
const char *blendfile_path = BKE_main_blendfile_path(bmain);
/* if not saved before, get the name of the most recently used .blend file */
if (BKE_main_blendfile_path(bmain)[0] == '\0' && G.recent_files.first) {
if ((blendfile_path[0] == '\0') && G.recent_files.first) {
struct RecentFile *recent = G.recent_files.first;
STRNCPY(filepath, recent->filepath);
}
else {
STRNCPY(filepath, bmain->filepath);
STRNCPY(filepath, blendfile_path);
}
wm_filepath_default(filepath);
wm_filepath_default(bmain, filepath);
RNA_property_string_set(op->ptr, prop, filepath);
}
}
@ -3081,7 +3080,7 @@ static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
}
else {
BLI_strncpy(path, BKE_main_blendfile_path(bmain), FILE_MAX);
wm_filepath_default(path);
wm_filepath_default(bmain, path);
}
const int fileflags_orig = G.fileflags;
@ -3198,14 +3197,15 @@ static int wm_save_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *U
/* if we're saving for the first time and prefer relative paths -
* any existing paths will be absolute,
* enable the option to remap paths to avoid confusion T37240. */
if ((G.relbase_valid == false) && (U.flag & USER_RELPATHS)) {
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if ((blendfile_path[0] == '\0') && (U.flag & USER_RELPATHS)) {
PropertyRNA *prop = RNA_struct_find_property(op->ptr, "relative_remap");
if (!RNA_property_is_set(op->ptr, prop)) {
RNA_property_boolean_set(op->ptr, prop, true);
}
}
if (G.relbase_valid) {
if (blendfile_path[0] != '\0') {
char path[FILE_MAX];
RNA_string_get(op->ptr, "filepath", path);
@ -3307,6 +3307,7 @@ static uiBlock *block_create_autorun_warning(struct bContext *C,
struct ARegion *region,
void *UNUSED(arg1))
{
const char *blendfile_path = BKE_main_blendfile_path_from_global();
wmWindowManager *wm = CTX_wm_manager(C);
uiBlock *block = UI_block_begin(C, region, "autorun_warning_popup", UI_EMBOSS);
@ -3354,7 +3355,7 @@ static uiBlock *block_create_autorun_warning(struct bContext *C,
/* Allow reload if we have a saved file.
* Otherwise just enable scripts and reset the depsgraphs. */
if (G.relbase_valid && wm->file_saved) {
if ((blendfile_path[0] != '\0') && wm->file_saved) {
but = uiDefIconTextBut(block,
UI_BTYPE_BUT,
0,

View File

@ -113,12 +113,13 @@ static bool wm_link_append_poll(bContext *C)
static int wm_link_append_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (G.lib[0] != '\0') {
RNA_string_set(op->ptr, "filepath", G.lib);
}
else if (G.relbase_valid) {
else if (blendfile_path[0] != '\0') {
char path[FILE_MAX];
BLI_strncpy(path, BKE_main_blendfile_path_from_global(), sizeof(path));
STRNCPY(path, blendfile_path);
BLI_path_parent_dir(path);
RNA_string_set(op->ptr, "filepath", path);
}

View File

@ -428,13 +428,14 @@ void wm_window_title(wmWindowManager *wm, wmWindow *win)
}
else if (win->ghostwin) {
/* this is set to 1 if you don't have startup.blend open */
if (G.relbase_valid && BKE_main_blendfile_path_from_global()[0]) {
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] != '\0') {
char str[sizeof(((Main *)NULL)->filepath) + 24];
BLI_snprintf(str,
sizeof(str),
"Blender%s [%s%s]",
wm->file_saved ? "" : "*",
BKE_main_blendfile_path_from_global(),
blendfile_path,
G_MAIN->recovered ? " (Recovered)" : "");
GHOST_SetTitle(win->ghostwin, str);
}

View File

@ -58,6 +58,7 @@
#include "BKE_gpencil_modifier.h"
#include "BKE_idtype.h"
#include "BKE_image.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_modifier.h"
#include "BKE_node.h"
@ -553,7 +554,8 @@ int main(int argc,
}
else {
/* When no file is loaded, show the splash screen. */
if (!G.relbase_valid) {
const char *blendfile_path = BKE_main_blendfile_path_from_global();
if (blendfile_path[0] == '\0') {
WM_init_splash(C);
}
WM_main(C);

View File

@ -1995,7 +1995,6 @@ static int arg_handle_load_file(int UNUSED(argc), const char **argv, void *data)
/* Just pretend a file was loaded, so the user can press Save and it'll
* save at the filename from the CLI. */
STRNCPY(G_MAIN->filepath, filename);
G.relbase_valid = true;
printf("... opened default scene instead; saving will write to: %s\n", filename);
}
else {