From 55a9ff83bb2fbf918cdafdba3fb94df0f95f5d3b Mon Sep 17 00:00:00 2001 From: BevapDin Date: Sun, 2 Feb 2020 10:52:08 +0100 Subject: [PATCH] Use JsonValue to forward a value of arbitrary type to some function in mapgen. This is a bit more generic as we don't need to forward a JSON object along with the name of the member. --- src/mapgen.cpp | 52 ++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 619b6b77d8fa4..2e6dfb81cc9d6 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -1826,13 +1826,12 @@ an overload below. The mapgen piece is loaded from the member of the json object named key. */ template -void load_place_mapings( const JsonObject &pjo, const std::string &key, - mapgen_palette::placing_map::mapped_type &vect ) +void load_place_mapings( const JsonValue &value, mapgen_palette::placing_map::mapped_type &vect ) { - if( pjo.has_object( key ) ) { - load_place_mapings( pjo.get_object( key ), vect ); + if( value.test_object() ) { + load_place_mapings( value.get_object(), vect ); } else { - for( JsonObject jo : pjo.get_array( key ) ) { + for( JsonObject jo : value.get_array() ) { load_place_mapings( jo, vect ); jo.allow_omitted_members(); } @@ -1841,23 +1840,22 @@ void load_place_mapings( const JsonObject &pjo, const std::string &key, /* This function allows loading the mapgen pieces from a single string, *or* a json object. -The mapgen piece is loaded from the member of the json object named key. */ template -void load_place_mapings_string( const JsonObject &pjo, const std::string &key, +void load_place_mapings_string( const JsonValue &value, mapgen_palette::placing_map::mapped_type &vect ) { - if( pjo.has_string( key ) ) { + if( value.test_string() ) { try { - vect.push_back( make_shared_fast( pjo.get_string( key ) ) ); + vect.push_back( make_shared_fast( value.get_string() ) ); } catch( const std::runtime_error &err ) { // Using the json object here adds nice formatting and context information - pjo.throw_error( err.what(), key ); + value.throw_error( err.what() ); } - } else if( pjo.has_object( key ) ) { - load_place_mapings( pjo.get_object( key ), vect ); + } else if( value.test_object() ) { + load_place_mapings( value.get_object(), vect ); } else { - for( const JsonValue entry : pjo.get_array( key ) ) { + for( const JsonValue entry : value.get_array() ) { if( entry.test_string() ) { try { vect.push_back( make_shared_fast( entry.get_string() ) ); @@ -1877,14 +1875,14 @@ instance of jmapgen_alternativly which will chose the mapgen piece to apply to t Use this with terrain or traps or other things that can not be applied twice to the same place. */ template -void load_place_mapings_alternatively( const JsonObject &pjo, const std::string &key, +void load_place_mapings_alternatively( const JsonValue &value, mapgen_palette::placing_map::mapped_type &vect ) { - if( !pjo.has_array( key ) ) { - load_place_mapings_string( pjo, key, vect ); + if( !value.test_array() ) { + load_place_mapings_string( value, vect ); } else { auto alter = make_shared_fast< jmapgen_alternativly >(); - for( const JsonValue entry : pjo.get_array( key ) ) { + for( const JsonValue entry : value.get_array() ) { if( entry.test_string() ) { try { alter->alternatives.emplace_back( entry.get_string() ); @@ -1933,24 +1931,24 @@ void load_place_mapings_alternatively( const JsonObject &pjo, const std::string } template<> -void load_place_mapings( const JsonObject &pjo, const std::string &key, +void load_place_mapings( const JsonValue &value, mapgen_palette::placing_map::mapped_type &vect ) { - load_place_mapings_alternatively( pjo, key, vect ); + load_place_mapings_alternatively( value, vect ); } template<> -void load_place_mapings( const JsonObject &pjo, const std::string &key, +void load_place_mapings( const JsonValue &value, mapgen_palette::placing_map::mapped_type &vect ) { - load_place_mapings_alternatively( pjo, key, vect ); + load_place_mapings_alternatively( value, vect ); } template<> -void load_place_mapings( const JsonObject &pjo, const std::string &key, +void load_place_mapings( const JsonValue &value, mapgen_palette::placing_map::mapped_type &vect ) { - load_place_mapings_alternatively( pjo, key, vect ); + load_place_mapings_alternatively( value, vect ); } template @@ -1969,7 +1967,7 @@ void mapgen_palette::load_place_mapings( const JsonObject &jo, const std::string continue; } auto &vect = format_placings[ key[0] ]; - ::load_place_mapings( sub, member_name, vect ); + ::load_place_mapings( sub.get_member( member_name ), vect ); } } if( !jo.has_object( member_name ) ) { @@ -1987,7 +1985,7 @@ void mapgen_palette::load_place_mapings( const JsonObject &jo, const std::string pjo.throw_error( "format map key must be 1 character", key ); } auto &vect = format_placings[ key[0] ]; - ::load_place_mapings( pjo, key, vect ); + ::load_place_mapings( pjo.get_member( key ), vect ); } } @@ -2072,7 +2070,7 @@ mapgen_palette mapgen_palette::load_internal( const JsonObject &jo, const std::s format_terrain[key[0]] = ter_id( pjo.get_string( key ) ); } else { auto &vect = format_placings[ key[0] ]; - ::load_place_mapings( pjo, key, vect ); + ::load_place_mapings( pjo.get_member( key ), vect ); if( !vect.empty() ) { // Dummy entry to signal that this terrain is actually defined, because // the code below checks that each square on the map has a valid terrain @@ -2093,7 +2091,7 @@ mapgen_palette mapgen_palette::load_internal( const JsonObject &jo, const std::s format_furniture[key[0]] = furn_id( pjo.get_string( key ) ); } else { auto &vect = format_placings[ key[0] ]; - ::load_place_mapings( pjo, key, vect ); + ::load_place_mapings( pjo.get_member( key ), vect ); } } }