From 7c141d74c2e8f3fbf57fea93521c717496e01dcd Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Tue, 25 Jun 2019 20:23:11 +0200 Subject: [PATCH] Serialize new item members I forgot to add serialization for `tools_to_continue` and `cached_tool_selections` which were added to `item` in a previous commit. This commit adds io support for `comp_selection` and properly serializes the new item members. --- src/craft_command.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++ src/craft_command.h | 3 ++ src/savegame_json.cpp | 3 ++ 3 files changed, 72 insertions(+) diff --git a/src/craft_command.cpp b/src/craft_command.cpp index ddec823f809c9..102079d418945 100644 --- a/src/craft_command.cpp +++ b/src/craft_command.cpp @@ -9,7 +9,9 @@ #include "debug.h" #include "game_constants.h" #include "inventory.h" +#include "io.h" #include "item.h" +#include "json.h" #include "output.h" #include "player.h" #include "recipe.h" @@ -35,6 +37,70 @@ std::string comp_selection::nname() const return item::nname( comp.type, comp.count ); } +namespace io +{ + +static const std::map usage_map = {{ + { "map", usage::use_from_map }, + { "player", usage::use_from_player }, + { "both", usage::use_from_both }, + { "none", usage::use_from_none }, + { "cancel", usage::cancel } + } +}; + +template<> +usage string_to_enum( const std::string &data ) +{ + return string_to_enum_look_up( usage_map, data ); +} + +template<> +const std::string enum_to_string( usage data ) +{ + const auto iter = std::find_if( usage_map.begin(), usage_map.end(), + [data]( const std::pair &kv ) { + return kv.second == data; + } ); + + if( iter == usage_map.end() ) { + throw InvalidEnumString{}; + } + + return iter->first; +} + +} // namespace io + +template +void comp_selection::serialize( JsonOut &jsout ) const +{ + jsout.start_object(); + + jsout.member( "use_from", io::enum_to_string( use_from ) ); + jsout.member( "type", comp.type ); + jsout.member( "count", comp.count ); + + jsout.end_object(); +} + +template +void comp_selection::deserialize( JsonIn &jsin ) +{ + JsonObject data = jsin.get_object(); + + std::string use_from_str; + data.read( "use_from", use_from_str ); + use_from = io::string_to_enum( use_from_str ); + data.read( "type", comp.type ); + data.read( "count", comp.count ); +} + +template void comp_selection::serialize( JsonOut &jsout ) const; +template void comp_selection::serialize( JsonOut &jsout ) const; +template void comp_selection::deserialize( JsonIn &jsin ); +template void comp_selection::deserialize( JsonIn &jsin ); + void craft_command::execute( const tripoint &new_loc ) { if( empty() ) { diff --git a/src/craft_command.h b/src/craft_command.h index a54bfcaf551a7..ba8f8fd08e149 100644 --- a/src/craft_command.h +++ b/src/craft_command.h @@ -35,6 +35,9 @@ struct comp_selection { /** provides a translated name for 'comp', suffixed with it's location e.g '(nearby)'. */ std::string nname() const; + + void serialize( JsonOut &jsout ) const; + void deserialize( JsonIn &jsin ); }; /** diff --git a/src/savegame_json.cpp b/src/savegame_json.cpp index 19c262d4d75bd..8a6fffbaffa21 100644 --- a/src/savegame_json.cpp +++ b/src/savegame_json.cpp @@ -29,6 +29,7 @@ #include "basecamp.h" #include "bionics.h" #include "calendar.h" +#include "craft_command.h" #include "debug.h" #include "effect.h" #include "game.h" @@ -2019,6 +2020,8 @@ void item::io( Archive &archive ) archive.io( "light_dir", light.direction, nolight.direction ); archive.io( "comps_used", comps_used, io::empty_default_tag() ); archive.io( "next_failure_point", next_failure_point, -1 ); + archive.io( "tools_to_continue", tools_to_continue, false ); + archive.io( "cached_tool_selections", cached_tool_selections, io::empty_default_tag() ); item_controller->migrate_item( orig, *this );