Skip to content

Commit

Permalink
Serialize new item members
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ifreund committed Jun 26, 2019
1 parent 386f483 commit 7c141d7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/craft_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,6 +37,70 @@ std::string comp_selection<CompType>::nname() const
return item::nname( comp.type, comp.count );
}

namespace io
{

static const std::map<std::string, usage> 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<usage>( const std::string &data )
{
return string_to_enum_look_up( usage_map, data );
}

template<>
const std::string enum_to_string<usage>( usage data )
{
const auto iter = std::find_if( usage_map.begin(), usage_map.end(),
[data]( const std::pair<std::string, usage> &kv ) {
return kv.second == data;
} );

if( iter == usage_map.end() ) {
throw InvalidEnumString{};
}

return iter->first;
}

} // namespace io

template<typename CompType>
void comp_selection<CompType>::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<typename CompType>
void comp_selection<CompType>::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<usage>( use_from_str );
data.read( "type", comp.type );
data.read( "count", comp.count );
}

template void comp_selection<tool_comp>::serialize( JsonOut &jsout ) const;
template void comp_selection<item_comp>::serialize( JsonOut &jsout ) const;
template void comp_selection<tool_comp>::deserialize( JsonIn &jsin );
template void comp_selection<item_comp>::deserialize( JsonIn &jsin );

void craft_command::execute( const tripoint &new_loc )
{
if( empty() ) {
Expand Down
3 changes: 3 additions & 0 deletions src/craft_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
};

/**
Expand Down
3 changes: 3 additions & 0 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 );

Expand Down

0 comments on commit 7c141d7

Please sign in to comment.