Skip to content

Commit

Permalink
Change item_group::load_item_group to take a JsonValue instead of a s…
Browse files Browse the repository at this point in the history
…tream.
  • Loading branch information
BevapDin committed Dec 18, 2019
1 parent 4ce7e13 commit 59bad15
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 26 deletions.
3 changes: 1 addition & 2 deletions src/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,8 +1398,7 @@ void load_construction( const JsonObject &jo )
con.post_flags = jo.get_tags( "post_flags" );

if( jo.has_member( "byproducts" ) ) {
JsonIn &stream = *jo.get_raw( "byproducts" );
con.byproduct_item_group = item_group::load_item_group( stream, "collection" );
con.byproduct_item_group = item_group::load_item_group( jo.get_member( "byproducts" ), "collection" );
}

static const std::map<std::string, std::function<bool( const tripoint & )>> pre_special_map = {{
Expand Down
16 changes: 8 additions & 8 deletions src/item_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,22 +588,22 @@ static Group_tag get_unique_group_id()
}
}

Group_tag item_group::load_item_group( JsonIn &stream, const std::string &default_subtype )
Group_tag item_group::load_item_group( const JsonValue &value, const std::string &default_subtype )
{
if( stream.test_string() ) {
return stream.get_string();
} else if( stream.test_object() ) {
if( value.test_string() ) {
return value.get_string();
} else if( value.test_object() ) {
const Group_tag group = get_unique_group_id();

JsonObject jo = stream.get_object();
JsonObject jo = value.get_object();
const std::string subtype = jo.get_string( "subtype", default_subtype );
item_controller->load_item_group( jo, group, subtype );

return group;
} else if( stream.test_array() ) {
} else if( value.test_array() ) {
const Group_tag group = get_unique_group_id();

JsonArray jarr = stream.get_array();
JsonArray jarr = value.get_array();
// load_item_group needs a bool, invalid subtypes are unexpected and most likely errors
// from the caller of this function.
if( default_subtype != "collection" && default_subtype != "distribution" ) {
Expand All @@ -613,7 +613,7 @@ Group_tag item_group::load_item_group( JsonIn &stream, const std::string &defaul

return group;
} else {
stream.error( "invalid item group, must be string (group id) or object/array (the group data)" );
value.throw_error( "invalid item group, must be string (group id) or object/array (the group data)" );
// stream.error always throws, this is here to prevent a warning
return Group_tag{};
}
Expand Down
10 changes: 5 additions & 5 deletions src/item_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct itype;
using Item_tag = std::string;
using Group_tag = std::string;
class JsonObject;
class JsonIn;
class JsonValue;
class time_point;

namespace item_group
Expand Down Expand Up @@ -79,14 +79,14 @@ void load_item_group( const JsonObject &jsobj, const Group_tag &group_id,
/**
* Get an item group id and (optionally) load an inlined item group.
*
* If the next value in the JSON stream is string, it's assumed to be an item group id and it's
* If the value is string, it's assumed to be an item group id and it's
* returned directly.
*
* If the next value is a JSON object, it is loaded as item group. The group will be given a
* If the value is a JSON object, it is loaded as item group. The group will be given a
* unique id (if the JSON object contains an id, it is ignored) and that id will be returned.
* If the JSON object does not contain a subtype, the given default is used.
*
* If the next value is a JSON array, it is loaded as item group: the default_subtype will be
* If the value is a JSON array, it is loaded as item group: the default_subtype will be
* used as subtype of the new item group and the array is loaded like the "entries" array of
* a item group definition (see format of item groups).
*
Expand All @@ -95,7 +95,7 @@ void load_item_group( const JsonObject &jsobj, const Group_tag &group_id,
* subtype. It must be either "distribution" or "collection". See @ref Item_group.
* @throw JsonError as usual for JSON errors, including invalid input values.
*/
Group_tag load_item_group( JsonIn &stream, const std::string &default_subtype );
Group_tag load_item_group( const JsonValue &value, const std::string &default_subtype );
} // namespace item_group

/**
Expand Down
6 changes: 2 additions & 4 deletions src/mapdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ bool map_bash_info::load( const JsonObject &jsobj, const std::string &member, bo
}

if( j.has_member( "items" ) ) {
JsonIn &stream = *j.get_raw( "items" );
drop_group = item_group::load_item_group( stream, "collection" );
drop_group = item_group::load_item_group( j.get_member( "items" ), "collection" );
} else {
drop_group = "EMPTY_GROUP";
}
Expand Down Expand Up @@ -270,8 +269,7 @@ bool map_deconstruct_info::load( const JsonObject &jsobj, const std::string &mem
can_do = true;
deconstruct_above = j.get_bool( "deconstruct_above", false );

JsonIn &stream = *j.get_raw( "items" );
drop_group = item_group::load_item_group( stream, "collection" );
drop_group = item_group::load_item_group( j.get_member( "items" ), "collection" );
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/monstergenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,7 @@ void mtype::load( const JsonObject &jo, const std::string &src )
}

if( jo.has_member( "death_drops" ) ) {
JsonIn &stream = *jo.get_raw( "death_drops" );
death_drops = item_group::load_item_group( stream, "distribution" );
death_drops = item_group::load_item_group( jo.get_member( "death_drops" ), "distribution" );
}

assign( jo, "harvest", harvest );
Expand Down
6 changes: 3 additions & 3 deletions src/profession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,19 @@ void profession::load( const JsonObject &jo, const std::string & )
optional( items_obj, was_loaded, "both", legacy_starting_items, item_reader {} );
}
if( items_obj.has_object( "both" ) ) {
_starting_items = item_group::load_item_group( *items_obj.get_raw( "both" ), "collection" );
_starting_items = item_group::load_item_group( items_obj.get_member( "both" ), "collection" );
}
if( items_obj.has_array( "male" ) ) {
optional( items_obj, was_loaded, "male", legacy_starting_items_male, item_reader {} );
}
if( items_obj.has_object( "male" ) ) {
_starting_items_male = item_group::load_item_group( *items_obj.get_raw( "male" ), "collection" );
_starting_items_male = item_group::load_item_group( items_obj.get_member( "male" ), "collection" );
}
if( items_obj.has_array( "female" ) ) {
optional( items_obj, was_loaded, "female", legacy_starting_items_female, item_reader {} );
}
if( items_obj.has_object( "female" ) ) {
_starting_items_female = item_group::load_item_group( *items_obj.get_raw( "female" ),
_starting_items_female = item_group::load_item_group( items_obj.get_member( "female" ),
"collection" );
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/veh_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ void vpart_info::load( const JsonObject &jo, const std::string &src )
}

if( jo.has_member( "breaks_into" ) ) {
JsonIn &stream = *jo.get_raw( "breaks_into" );
def.breaks_into_group = item_group::load_item_group( stream, "collection" );
def.breaks_into_group = item_group::load_item_group( jo.get_member( "breaks_into" ), "collection" );
}

auto qual = jo.get_array( "qualities" );
Expand Down

0 comments on commit 59bad15

Please sign in to comment.