From 218e9e76a8b7a737ac9277e54de009461b43ecf8 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 11 Sep 2020 11:46:26 +0200 Subject: Refactor: move Simulation .blend I/O to IDTypeInfo callbacks --- source/blender/blenkernel/intern/simulation.cc | 98 ++++++++++++++++++++++++-- source/blender/blenloader/intern/readfile.c | 49 +------------ source/blender/blenloader/intern/writefile.c | 56 +-------------- 3 files changed, 97 insertions(+), 106 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/intern/simulation.cc b/source/blender/blenkernel/intern/simulation.cc index 9dc1f073e2a..f08051510db 100644 --- a/source/blender/blenkernel/intern/simulation.cc +++ b/source/blender/blenkernel/intern/simulation.cc @@ -63,6 +63,8 @@ #include "SIM_simulation_update.hh" +#include "BLO_read_write.h" + using StateInitFunction = void (*)(SimulationState *state); using StateResetFunction = void (*)(SimulationState *state); using StateRemoveFunction = void (*)(SimulationState *state); @@ -145,6 +147,94 @@ static void simulation_foreach_id(ID *id, LibraryForeachIDData *data) } } +static void simulation_blend_write(BlendWriter *writer, ID *id, const void *id_address) +{ + Simulation *simulation = (Simulation *)id; + if (simulation->id.us > 0 || BLO_write_is_undo(writer)) { + BLO_write_id_struct(writer, Simulation, id_address, &simulation->id); + BKE_id_blend_write(writer, &simulation->id); + + if (simulation->adt) { + BKE_animdata_blend_write(writer, simulation->adt); + } + + /* nodetree is integral part of simulation, no libdata */ + if (simulation->nodetree) { + BLO_write_struct(writer, bNodeTree, simulation->nodetree); + ntreeBlendWrite(writer, simulation->nodetree); + } + + LISTBASE_FOREACH (SimulationState *, state, &simulation->states) { + BLO_write_string(writer, state->name); + BLO_write_string(writer, state->type); + /* TODO: Decentralize this part. */ + if (STREQ(state->type, SIM_TYPE_NAME_PARTICLE_SIMULATION)) { + ParticleSimulationState *particle_state = (ParticleSimulationState *)state; + + CustomDataLayer *players = NULL, players_buff[CD_TEMP_CHUNK_SIZE]; + CustomData_blend_write_prepare( + &particle_state->attributes, &players, players_buff, ARRAY_SIZE(players_buff)); + + BLO_write_struct(writer, ParticleSimulationState, particle_state); + + CustomData_blend_write(writer, + &particle_state->attributes, + players, + particle_state->tot_particles, + CD_MASK_ALL, + &simulation->id); + + /* Remove temporary data. */ + if (players && players != players_buff) { + MEM_freeN(players); + } + } + else if (STREQ(state->type, SIM_TYPE_NAME_PARTICLE_MESH_EMITTER)) { + ParticleMeshEmitterSimulationState *emitter_state = (ParticleMeshEmitterSimulationState *) + state; + BLO_write_struct(writer, ParticleMeshEmitterSimulationState, emitter_state); + } + } + + BLO_write_struct_list(writer, SimulationDependency, &simulation->dependencies); + } +} + +static void simulation_blend_read_data(BlendDataReader *reader, ID *id) +{ + Simulation *simulation = (Simulation *)id; + BLO_read_data_address(reader, &simulation->adt); + BKE_animdata_blend_read_data(reader, simulation->adt); + + BLO_read_list(reader, &simulation->states); + LISTBASE_FOREACH (SimulationState *, state, &simulation->states) { + BLO_read_data_address(reader, &state->name); + BLO_read_data_address(reader, &state->type); + if (STREQ(state->type, SIM_TYPE_NAME_PARTICLE_SIMULATION)) { + ParticleSimulationState *particle_state = (ParticleSimulationState *)state; + CustomData_blend_read(reader, &particle_state->attributes, particle_state->tot_particles); + } + } + + BLO_read_list(reader, &simulation->dependencies); +} + +static void simulation_blend_read_lib(BlendLibReader *reader, ID *id) +{ + Simulation *simulation = (Simulation *)id; + LISTBASE_FOREACH (SimulationDependency *, dependency, &simulation->dependencies) { + BLO_read_id_address(reader, simulation->id.lib, &dependency->id); + } +} + +static void simulation_blend_read_expand(BlendExpander *expander, ID *id) +{ + Simulation *simulation = (Simulation *)id; + LISTBASE_FOREACH (SimulationDependency *, dependency, &simulation->dependencies) { + BLO_expand(expander, dependency->id); + } +} + IDTypeInfo IDType_ID_SIM = { /* id_code */ ID_SIM, /* id_filter */ FILTER_ID_SIM, @@ -162,10 +252,10 @@ IDTypeInfo IDType_ID_SIM = { /* foreach_id */ simulation_foreach_id, /* foreach_cache */ NULL, - /* blend_write */ NULL, - /* blend_read_data */ NULL, - /* blend_read_lib */ NULL, - /* blend_read_expand */ NULL, + /* blend_write */ simulation_blend_write, + /* blend_read_data */ simulation_blend_read_data, + /* blend_read_lib */ simulation_blend_read_lib, + /* blend_read_expand */ simulation_blend_read_expand, }; void *BKE_simulation_add(Main *bmain, const char *name) diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index f32168092eb..c6217b56565 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -6238,37 +6238,6 @@ static void lib_link_sound(BlendLibReader *reader, bSound *sound) /** \} */ -/* -------------------------------------------------------------------- */ -/** \name Read ID: Simulation - * \{ */ - -static void lib_link_simulation(BlendLibReader *reader, Simulation *simulation) -{ - LISTBASE_FOREACH (SimulationDependency *, dependency, &simulation->dependencies) { - BLO_read_id_address(reader, simulation->id.lib, &dependency->id); - } -} - -static void direct_link_simulation(BlendDataReader *reader, Simulation *simulation) -{ - BLO_read_data_address(reader, &simulation->adt); - BKE_animdata_blend_read_data(reader, simulation->adt); - - BLO_read_list(reader, &simulation->states); - LISTBASE_FOREACH (SimulationState *, state, &simulation->states) { - BLO_read_data_address(reader, &state->name); - BLO_read_data_address(reader, &state->type); - if (STREQ(state->type, SIM_TYPE_NAME_PARTICLE_SIMULATION)) { - ParticleSimulationState *particle_state = (ParticleSimulationState *)state; - CustomData_blend_read(reader, &particle_state->attributes, particle_state->tot_particles); - } - } - - BLO_read_list(reader, &simulation->dependencies); -} - -/** \} */ - /* -------------------------------------------------------------------- */ /** \name Read Library Data Block * \{ */ @@ -6449,9 +6418,6 @@ static bool direct_link_id(FileData *fd, Main *main, const int tag, ID *id, ID * case ID_WS: direct_link_workspace(&reader, (WorkSpace *)id, main); break; - case ID_SIM: - direct_link_simulation(&reader, (Simulation *)id); - break; case ID_ME: case ID_LT: case ID_AC: @@ -6480,6 +6446,7 @@ static bool direct_link_id(FileData *fd, Main *main, const int tag, ID *id, ID * case ID_HA: case ID_PT: case ID_VO: + case ID_SIM: /* Do nothing. Handled by IDTypeInfo callback. */ break; } @@ -7115,9 +7082,6 @@ static void lib_link_all(FileData *fd, Main *bmain) case ID_CF: lib_link_cachefiles(&reader, (CacheFile *)id); break; - case ID_SIM: - lib_link_simulation(&reader, (Simulation *)id); - break; case ID_IP: /* XXX deprecated... still needs to be maintained for version patches still. */ lib_link_ipo(&reader, (Ipo *)id); @@ -7153,6 +7117,7 @@ static void lib_link_all(FileData *fd, Main *bmain) case ID_HA: case ID_PT: case ID_VO: + case ID_SIM: /* Do nothing. Handled by IDTypeInfo callback. */ break; } @@ -8096,13 +8061,6 @@ static void expand_workspace(BlendExpander *expander, WorkSpace *workspace) } } -static void expand_simulation(BlendExpander *expander, Simulation *simulation) -{ - LISTBASE_FOREACH (SimulationDependency *, dependency, &simulation->dependencies) { - BLO_expand(expander, dependency->id); - } -} - /** * Set the callback func used over all ID data found by \a BLO_expand_main func. * @@ -8170,9 +8128,6 @@ void BLO_expand_main(void *fdhandle, Main *mainvar) case ID_WS: expand_workspace(&expander, (WorkSpace *)id); break; - case ID_SIM: - expand_simulation(&expander, (Simulation *)id); - break; default: break; } diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c index 8390838163d..8bbdee78381 100644 --- a/source/blender/blenloader/intern/writefile.c +++ b/source/blender/blenloader/intern/writefile.c @@ -2104,58 +2104,6 @@ static void write_workspace(BlendWriter *writer, WorkSpace *workspace, const voi } } -static void write_simulation(BlendWriter *writer, Simulation *simulation, const void *id_address) -{ - if (simulation->id.us > 0 || BLO_write_is_undo(writer)) { - BLO_write_id_struct(writer, Simulation, id_address, &simulation->id); - BKE_id_blend_write(writer, &simulation->id); - - if (simulation->adt) { - BKE_animdata_blend_write(writer, simulation->adt); - } - - /* nodetree is integral part of simulation, no libdata */ - if (simulation->nodetree) { - BLO_write_struct(writer, bNodeTree, simulation->nodetree); - ntreeBlendWrite(writer, simulation->nodetree); - } - - LISTBASE_FOREACH (SimulationState *, state, &simulation->states) { - BLO_write_string(writer, state->name); - BLO_write_string(writer, state->type); - /* TODO: Decentralize this part. */ - if (STREQ(state->type, SIM_TYPE_NAME_PARTICLE_SIMULATION)) { - ParticleSimulationState *particle_state = (ParticleSimulationState *)state; - - CustomDataLayer *players = NULL, players_buff[CD_TEMP_CHUNK_SIZE]; - CustomData_blend_write_prepare( - &particle_state->attributes, &players, players_buff, ARRAY_SIZE(players_buff)); - - BLO_write_struct(writer, ParticleSimulationState, particle_state); - - CustomData_blend_write(writer, - &particle_state->attributes, - players, - particle_state->tot_particles, - CD_MASK_ALL, - &simulation->id); - - /* Remove temporary data. */ - if (players && players != players_buff) { - MEM_freeN(players); - } - } - else if (STREQ(state->type, SIM_TYPE_NAME_PARTICLE_MESH_EMITTER)) { - ParticleMeshEmitterSimulationState *emitter_state = (ParticleMeshEmitterSimulationState *) - state; - BLO_write_struct(writer, ParticleMeshEmitterSimulationState, emitter_state); - } - } - - BLO_write_struct_list(writer, SimulationDependency, &simulation->dependencies); - } -} - /* Keep it last of write_foodata functions. */ static void write_libraries(WriteData *wd, Main *main) { @@ -2441,9 +2389,6 @@ static bool write_file_handle(Main *mainvar, case ID_CF: write_cachefile(&writer, (CacheFile *)id_buffer, id); break; - case ID_SIM: - write_simulation(&writer, (Simulation *)id_buffer, id); - break; case ID_ME: case ID_LT: case ID_AC: @@ -2472,6 +2417,7 @@ static bool write_file_handle(Main *mainvar, case ID_HA: case ID_PT: case ID_VO: + case ID_SIM: /* Do nothing, handled in IDTypeInfo callback. */ break; case ID_LI: -- cgit v1.2.3