Fix T87489: Text Data-Blocks get deleted on Recursive Purge

Text data block were not considered special in the recursive purge
function.  So they would get deleted if they had no actual users.

To fix this we instead make text data block use "fake user" so that
addon authors can specify script files that should be removed if nothing
is using it anymore.

Per default, new text object have "fake user" set. So functionality
wise, the user has to explicitly specify that they want the text object
to be purge-able.

Reviewed By: Bastien

Differential Revision: http://developer.blender.org/D10983
This commit is contained in:
Sebastian Parborg 2021-05-04 14:46:32 +02:00
parent 4599cea15d
commit d0b3f9c81b
Notes: blender-bot 2023-02-14 07:36:17 +01:00
Referenced by issue #87489, Text Data-Blocks get deleted on Recursive Purge
7 changed files with 46 additions and 26 deletions

View File

@ -39,7 +39,7 @@ extern "C" {
/* Blender file format version. */
#define BLENDER_FILE_VERSION BLENDER_VERSION
#define BLENDER_FILE_SUBVERSION 0
#define BLENDER_FILE_SUBVERSION 1
/* Minimum Blender version that supports reading file written with the current
* version. Older Blender versions will test this and show a warning if the file

View File

@ -171,6 +171,9 @@ static void text_free_data(ID *id)
static void text_blend_write(BlendWriter *writer, ID *id, const void *id_address)
{
if (id->us < 1 && !BLO_write_is_undo(writer)) {
return;
}
Text *text = (Text *)id;
/* Note: we are clearing local temp data here, *not* the flag in the actual 'real' ID. */
@ -231,8 +234,6 @@ static void text_blend_read_data(BlendDataReader *reader, ID *id)
}
text->flags = (text->flags) & ~TXT_ISEXT;
id_us_ensure_real(&text->id);
}
IDTypeInfo IDType_ID_TXT = {
@ -293,8 +294,10 @@ Text *BKE_text_add(Main *bmain, const char *name)
Text *ta;
ta = BKE_id_new(bmain, ID_TXT, name);
/* Texts always have 'real' user (see also read code). */
id_us_ensure_real(&ta->id);
/* Texts have no users by default... Set the fake user flag to ensure that this text block
* doesn't get deleted by default when cleaning up data blocks. */
id_us_min(&ta->id);
id_fake_user_set(&ta->id);
return ta;
}
@ -468,7 +471,7 @@ bool BKE_text_reload(Text *text)
* \param is_internal: If \a true, this text data-block only exists in memory,
* not as a file on disk.
*
* \note text data-blocks have no user by default, only the 'real user' flag.
* \note text data-blocks have no real user but have 'fake user' enabled by default
*/
Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const bool is_internal)
{
@ -489,9 +492,8 @@ Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const
}
ta = BKE_libblock_alloc(bmain, ID_TXT, BLI_path_basename(filepath_abs), 0);
/* Texts have no user by default... Only the 'real' user flag. */
id_us_ensure_real(&ta->id);
id_us_min(&ta->id);
id_fake_user_set(&ta->id);
BLI_listbase_clear(&ta->lines);
ta->curl = ta->sell = NULL;

View File

@ -2912,7 +2912,7 @@ static void lib_link_workspace_layout_restore(struct IDNameLib_Map *id_map,
else if (sl->spacetype == SPACE_TEXT) {
SpaceText *st = (SpaceText *)sl;
st->text = restore_pointer_by_name(id_map, (ID *)st->text, USER_REAL);
st->text = restore_pointer_by_name(id_map, (ID *)st->text, USER_IGNORE);
if (st->text == NULL) {
st->text = newmain->texts.first;
}

View File

@ -48,6 +48,7 @@
#include "DNA_screen_types.h"
#include "DNA_shader_fx_types.h"
#include "DNA_space_types.h"
#include "DNA_text_types.h"
#include "DNA_tracking_types.h"
#include "DNA_workspace_types.h"
@ -686,6 +687,15 @@ void do_versions_after_linking_290(Main *bmain, ReportList *UNUSED(reports))
}
}
if (!MAIN_VERSION_ATLEAST(bmain, 293, 20)) {
/* Set zero user text objects to have a fake user. */
LISTBASE_FOREACH (Text *, text, &bmain->texts) {
if (text->id.us == 0) {
id_fake_user_set(&text->id);
}
}
}
/**
* Versioning code until next subversion bump goes here.
*

View File

@ -25,14 +25,24 @@
#include "DNA_genfile.h"
#include "DNA_modifier_types.h"
#include "DNA_text_types.h"
#include "BKE_lib_id.h"
#include "BKE_main.h"
#include "BLO_readfile.h"
#include "readfile.h"
void do_versions_after_linking_300(Main *UNUSED(bmain), ReportList *UNUSED(reports))
void do_versions_after_linking_300(Main *bmain, ReportList *UNUSED(reports))
{
if (MAIN_VERSION_ATLEAST(bmain, 300, 0) && !MAIN_VERSION_ATLEAST(bmain, 300, 1)) {
/* Set zero user text objects to have a fake user. */
LISTBASE_FOREACH (Text *, text, &bmain->texts) {
if (text->id.us == 0) {
id_fake_user_set(&text->id);
}
}
}
/**
* Versioning code until next subversion bump goes here.
*
@ -51,19 +61,7 @@ void do_versions_after_linking_300(Main *UNUSED(bmain), ReportList *UNUSED(repor
/* NOLINTNEXTLINE: readability-function-size */
void blo_do_versions_300(FileData *fd, Library *UNUSED(lib), Main *bmain)
{
/**
* Versioning code until next subversion bump goes here.
*
* \note Be sure to check when bumping the version:
* - "versioning_userdef.c", #blo_do_versions_userdef
* - "versioning_userdef.c", #do_versions_theme
*
* \note Keep this message at the bottom of the function.
*/
{
/* Keep this block, even when empty. */
if (!MAIN_VERSION_ATLEAST(bmain, 300, 1)) {
/* Set default value for the new bisect_threshold parameter in the mirror modifier. */
if (!DNA_struct_elem_find(fd->filesdna, "MirrorModifierData", "float", "bisect_threshold")) {
LISTBASE_FOREACH (Object *, ob, &bmain->objects) {
@ -77,4 +75,16 @@ void blo_do_versions_300(FileData *fd, Library *UNUSED(lib), Main *bmain)
}
}
}
/**
* Versioning code until next subversion bump goes here.
*
* \note Be sure to check when bumping the version:
* - "versioning_userdef.c", #blo_do_versions_userdef
* - "versioning_userdef.c", #do_versions_theme
*
* \note Keep this message at the bottom of the function.
*/
{
/* Keep this block, even when empty. */
}
}

View File

@ -1110,7 +1110,7 @@ static void template_ID(const bContext *C,
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
if (id->lib == NULL && !(ELEM(GS(id->name), ID_GR, ID_SCE, ID_SCR, ID_TXT, ID_OB, ID_WS)) &&
if (id->lib == NULL && !(ELEM(GS(id->name), ID_GR, ID_SCE, ID_SCR, ID_OB, ID_WS)) &&
(hide_buttons == false)) {
uiDefIconButR(block,
UI_BTYPE_ICON_TOGGLE,

View File

@ -266,8 +266,6 @@ static int text_new_exec(bContext *C, wmOperator *UNUSED(op))
PropertyRNA *prop;
text = BKE_text_add(bmain, "Text");
/* Texts have no user by default... Only the 'real' user flag. */
id_us_min(&text->id);
/* hook into UI */
UI_context_active_but_prop_get_templateID(C, &ptr, &prop);