From be5cbbabcea555fc5ba3ac84922342ad2fb71dd4 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Thu, 10 Sep 2020 17:22:35 +0200 Subject: Refactor: move Mask .blend I/O to IDTypeInfo callbacks --- source/blender/blenkernel/intern/mask.c | 161 ++++++++++++++++++++++++++- source/blender/blenloader/intern/readfile.c | 113 +------------------ source/blender/blenloader/intern/writefile.c | 52 +-------- 3 files changed, 160 insertions(+), 166 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenkernel/intern/mask.c b/source/blender/blenkernel/intern/mask.c index 272c9fda9ae..c8c4fea7ab1 100644 --- a/source/blender/blenkernel/intern/mask.c +++ b/source/blender/blenkernel/intern/mask.c @@ -28,6 +28,7 @@ #include "MEM_guardedalloc.h" +#include "BLI_endian_switch.h" #include "BLI_ghash.h" #include "BLI_listbase.h" #include "BLI_math.h" @@ -43,6 +44,7 @@ #include "BKE_curve.h" #include "BKE_idtype.h" +#include "BKE_anim_data.h" #include "BKE_image.h" #include "BKE_lib_id.h" #include "BKE_lib_query.h" @@ -53,6 +55,8 @@ #include "DEG_depsgraph_build.h" +#include "BLO_read_write.h" + static CLG_LogRef LOG = {"bke.mask"}; static void mask_copy_data(Main *UNUSED(bmain), @@ -94,6 +98,155 @@ static void mask_foreach_id(ID *id, LibraryForeachIDData *data) } } +static void mask_blend_write(BlendWriter *writer, ID *id, const void *id_address) +{ + Mask *mask = (Mask *)id; + if (mask->id.us > 0 || BLO_write_is_undo(writer)) { + MaskLayer *masklay; + + BLO_write_id_struct(writer, Mask, id_address, &mask->id); + BKE_id_blend_write(writer, &mask->id); + + if (mask->adt) { + BKE_animdata_blend_write(writer, mask->adt); + } + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + MaskLayerShape *masklay_shape; + + BLO_write_struct(writer, MaskLayer, masklay); + + for (spline = masklay->splines.first; spline; spline = spline->next) { + int i; + + void *points_deform = spline->points_deform; + spline->points_deform = NULL; + + BLO_write_struct(writer, MaskSpline, spline); + BLO_write_struct_array(writer, MaskSplinePoint, spline->tot_point, spline->points); + + spline->points_deform = points_deform; + + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + if (point->tot_uw) { + BLO_write_struct_array(writer, MaskSplinePointUW, point->tot_uw, point->uw); + } + } + } + + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + BLO_write_struct(writer, MaskLayerShape, masklay_shape); + BLO_write_float_array( + writer, masklay_shape->tot_vert * MASK_OBJECT_SHAPE_ELEM_SIZE, masklay_shape->data); + } + } + } +} + +static void mask_blend_read_data(BlendDataReader *reader, ID *id) +{ + Mask *mask = (Mask *)id; + BLO_read_data_address(reader, &mask->adt); + + BLO_read_list(reader, &mask->masklayers); + + LISTBASE_FOREACH (MaskLayer *, masklay, &mask->masklayers) { + /* can't use newdataadr since it's a pointer within an array */ + MaskSplinePoint *act_point_search = NULL; + + BLO_read_list(reader, &masklay->splines); + + LISTBASE_FOREACH (MaskSpline *, spline, &masklay->splines) { + MaskSplinePoint *points_old = spline->points; + + BLO_read_data_address(reader, &spline->points); + + for (int i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + if (point->tot_uw) { + BLO_read_data_address(reader, &point->uw); + } + } + + /* detect active point */ + if ((act_point_search == NULL) && (masklay->act_point >= points_old) && + (masklay->act_point < points_old + spline->tot_point)) { + act_point_search = &spline->points[masklay->act_point - points_old]; + } + } + + BLO_read_list(reader, &masklay->splines_shapes); + + LISTBASE_FOREACH (MaskLayerShape *, masklay_shape, &masklay->splines_shapes) { + BLO_read_data_address(reader, &masklay_shape->data); + + if (masklay_shape->tot_vert) { + if (BLO_read_requires_endian_switch(reader)) { + BLI_endian_switch_float_array(masklay_shape->data, + masklay_shape->tot_vert * sizeof(float) * + MASK_OBJECT_SHAPE_ELEM_SIZE); + } + } + } + + BLO_read_data_address(reader, &masklay->act_spline); + masklay->act_point = act_point_search; + } +} + +static void lib_link_mask_parent(BlendLibReader *reader, Mask *mask, MaskParent *parent) +{ + BLO_read_id_address(reader, mask->id.lib, &parent->id); +} + +static void mask_blend_read_lib(BlendLibReader *reader, ID *id) +{ + Mask *mask = (Mask *)id; + LISTBASE_FOREACH (MaskLayer *, masklay, &mask->masklayers) { + MaskSpline *spline; + + spline = masklay->splines.first; + while (spline) { + for (int i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + lib_link_mask_parent(reader, mask, &point->parent); + } + + lib_link_mask_parent(reader, mask, &spline->parent); + + spline = spline->next; + } + } +} + +static void expand_mask_parent(BlendExpander *expander, MaskParent *parent) +{ + if (parent->id) { + BLO_expand(expander, parent->id); + } +} + +static void mask_blend_read_expand(BlendExpander *expander, ID *id) +{ + Mask *mask = (Mask *)id; + LISTBASE_FOREACH (MaskLayer *, mask_layer, &mask->masklayers) { + LISTBASE_FOREACH (MaskSpline *, spline, &mask_layer->splines) { + for (int i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + expand_mask_parent(expander, &point->parent); + } + + expand_mask_parent(expander, &spline->parent); + } + } +} + IDTypeInfo IDType_ID_MSK = { .id_code = ID_MSK, .id_filter = FILTER_ID_MSK, @@ -111,10 +264,10 @@ IDTypeInfo IDType_ID_MSK = { .foreach_id = mask_foreach_id, .foreach_cache = NULL, - .blend_write = NULL, - .blend_read_data = NULL, - .blend_read_lib = NULL, - .blend_read_expand = NULL, + .blend_write = mask_blend_write, + .blend_read_data = mask_blend_read_data, + .blend_read_lib = mask_blend_read_lib, + .blend_read_expand = mask_blend_read_expand, }; static struct { diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index d205a4f7ebf..ccc5248c472 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -6504,88 +6504,6 @@ static void lib_link_sound(BlendLibReader *reader, bSound *sound) /** \} */ -/* -------------------------------------------------------------------- */ -/** \name Read ID: Masks - * \{ */ - -static void direct_link_mask(BlendDataReader *reader, Mask *mask) -{ - BLO_read_data_address(reader, &mask->adt); - - BLO_read_list(reader, &mask->masklayers); - - LISTBASE_FOREACH (MaskLayer *, masklay, &mask->masklayers) { - /* can't use newdataadr since it's a pointer within an array */ - MaskSplinePoint *act_point_search = NULL; - - BLO_read_list(reader, &masklay->splines); - - LISTBASE_FOREACH (MaskSpline *, spline, &masklay->splines) { - MaskSplinePoint *points_old = spline->points; - - BLO_read_data_address(reader, &spline->points); - - for (int i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - if (point->tot_uw) { - BLO_read_data_address(reader, &point->uw); - } - } - - /* detect active point */ - if ((act_point_search == NULL) && (masklay->act_point >= points_old) && - (masklay->act_point < points_old + spline->tot_point)) { - act_point_search = &spline->points[masklay->act_point - points_old]; - } - } - - BLO_read_list(reader, &masklay->splines_shapes); - - LISTBASE_FOREACH (MaskLayerShape *, masklay_shape, &masklay->splines_shapes) { - BLO_read_data_address(reader, &masklay_shape->data); - - if (masklay_shape->tot_vert) { - if (BLO_read_requires_endian_switch(reader)) { - BLI_endian_switch_float_array(masklay_shape->data, - masklay_shape->tot_vert * sizeof(float) * - MASK_OBJECT_SHAPE_ELEM_SIZE); - } - } - } - - BLO_read_data_address(reader, &masklay->act_spline); - masklay->act_point = act_point_search; - } -} - -static void lib_link_mask_parent(BlendLibReader *reader, Mask *mask, MaskParent *parent) -{ - BLO_read_id_address(reader, mask->id.lib, &parent->id); -} - -static void lib_link_mask(BlendLibReader *reader, Mask *mask) -{ - LISTBASE_FOREACH (MaskLayer *, masklay, &mask->masklayers) { - MaskSpline *spline; - - spline = masklay->splines.first; - while (spline) { - for (int i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - lib_link_mask_parent(reader, mask, &point->parent); - } - - lib_link_mask_parent(reader, mask, &spline->parent); - - spline = spline->next; - } - } -} - -/** \} */ - /* -------------------------------------------------------------------- */ /** \name Read ID: Hair * \{ */ @@ -6892,9 +6810,6 @@ static bool direct_link_id(FileData *fd, Main *main, const int tag, ID *id, ID * case ID_GD: direct_link_gpencil(&reader, (bGPdata *)id); break; - case ID_MSK: - direct_link_mask(&reader, (Mask *)id); - break; case ID_CF: direct_link_cachefile(&reader, (CacheFile *)id); break; @@ -6931,6 +6846,7 @@ static bool direct_link_id(FileData *fd, Main *main, const int tag, ID *id, ID * case ID_CU: case ID_CA: case ID_WO: + case ID_MSK: /* Do nothing. Handled by IDTypeInfo callback. */ break; } @@ -7536,9 +7452,6 @@ static void lib_link_all(FileData *fd, Main *bmain) * Please keep order of entries in that switch matching that order, it's easier to quickly see * whether something is wrong then. */ switch (GS(id->name)) { - case ID_MSK: - lib_link_mask(&reader, (Mask *)id); - break; case ID_WM: lib_link_windowmanager(&reader, (wmWindowManager *)id); break; @@ -7624,6 +7537,7 @@ static void lib_link_all(FileData *fd, Main *bmain) case ID_CU: case ID_CA: case ID_WO: + case ID_MSK: /* Do nothing. Handled by IDTypeInfo callback. */ break; } @@ -8596,26 +8510,6 @@ static void expand_lightprobe(BlendExpander *UNUSED(expander), LightProbe *UNUSE { } -static void expand_mask_parent(BlendExpander *expander, MaskParent *parent) -{ - if (parent->id) { - BLO_expand(expander, parent->id); - } -} - -static void expand_mask(BlendExpander *expander, Mask *mask) -{ - LISTBASE_FOREACH (MaskLayer *, mask_layer, &mask->masklayers) { - LISTBASE_FOREACH (MaskSpline *, spline, &mask_layer->splines) { - for (int i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - expand_mask_parent(expander, &point->parent); - } - - expand_mask_parent(expander, &spline->parent); - } - } -} static void expand_gpencil(BlendExpander *expander, bGPdata *gpd) { @@ -8739,9 +8633,6 @@ void BLO_expand_main(void *fdhandle, Main *mainvar) case ID_PA: expand_particlesettings(&expander, (ParticleSettings *)id); break; - case ID_MSK: - expand_mask(&expander, (Mask *)id); - break; case ID_GD: expand_gpencil(&expander, (bGPdata *)id); break; diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c index df32cd7ec2f..dff161172c7 100644 --- a/source/blender/blenloader/intern/writefile.c +++ b/source/blender/blenloader/intern/writefile.c @@ -2230,54 +2230,6 @@ static void write_probe(BlendWriter *writer, LightProbe *prb, const void *id_add } } -static void write_mask(BlendWriter *writer, Mask *mask, const void *id_address) -{ - if (mask->id.us > 0 || BLO_write_is_undo(writer)) { - MaskLayer *masklay; - - BLO_write_id_struct(writer, Mask, id_address, &mask->id); - BKE_id_blend_write(writer, &mask->id); - - if (mask->adt) { - BKE_animdata_blend_write(writer, mask->adt); - } - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - MaskLayerShape *masklay_shape; - - BLO_write_struct(writer, MaskLayer, masklay); - - for (spline = masklay->splines.first; spline; spline = spline->next) { - int i; - - void *points_deform = spline->points_deform; - spline->points_deform = NULL; - - BLO_write_struct(writer, MaskSpline, spline); - BLO_write_struct_array(writer, MaskSplinePoint, spline->tot_point, spline->points); - - spline->points_deform = points_deform; - - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - if (point->tot_uw) { - BLO_write_struct_array(writer, MaskSplinePointUW, point->tot_uw, point->uw); - } - } - } - - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; - masklay_shape = masklay_shape->next) { - BLO_write_struct(writer, MaskLayerShape, masklay_shape); - BLO_write_float_array( - writer, masklay_shape->tot_vert * MASK_OBJECT_SHAPE_ELEM_SIZE, masklay_shape->data); - } - } - } -} - static void write_cachefile(BlendWriter *writer, CacheFile *cache_file, const void *id_address) { if (cache_file->id.us > 0 || BLO_write_is_undo(writer)) { @@ -2707,9 +2659,6 @@ static bool write_file_handle(Main *mainvar, case ID_SCR: write_screen(&writer, (bScreen *)id_buffer, id); break; - case ID_MSK: - write_mask(&writer, (Mask *)id_buffer, id); - break; case ID_SCE: write_scene(&writer, (Scene *)id_buffer, id); break; @@ -2776,6 +2725,7 @@ static bool write_file_handle(Main *mainvar, case ID_CU: case ID_CA: case ID_WO: + case ID_MSK: /* Do nothing, handled in IDTypeInfo callback. */ break; case ID_LI: -- cgit v1.2.3