static void format_collection( JsonIn &jsin, JsonOut &jsout, int depth, std::function<void(JsonIn &, JsonOut &, int, bool )>write_func ) { if( depth > 1 ) { // We're backtracking by storing jsin and jsout state before formatting // and restoring it afterwards if necessary. int in_start_pos = jsin.tell(); bool ate_seperator = jsin.get_ate_separator(); int out_start_pos = jsout.tell(); bool need_separator = jsout.get_need_separator(); write_func( jsin, jsout, depth, false ); if( jsout.tell() - out_start_pos <= 120 ) { // Line is short enough, so we're done. return; } else { // Reset jsin and jsout to their initial state, // and we'll serialize while forcing wrapping. jsin.seek( in_start_pos ); jsin.set_ate_separator( ate_seperator ); jsout.seek( out_start_pos ); if( need_separator ) { jsout.set_need_separator(); } } } write_func( jsin, jsout, depth, true ); }
void zone_manager::deserialize( JsonIn &jsin ) { zones.clear(); jsin.start_array(); while( !jsin.end_array() ) { JsonObject jo_zone = jsin.get_object(); const std::string name = jo_zone.get_string( "name" ); const std::string type = jo_zone.get_string( "type" ); const bool invert = jo_zone.get_bool( "invert" ); const bool enabled = jo_zone.get_bool( "enabled" ); // Z coords need to have a default value - old saves won't have those const int start_x = jo_zone.get_int( "start_x" ); const int start_y = jo_zone.get_int( "start_y" ); const int start_z = jo_zone.get_int( "start_z", 0 ); const int end_x = jo_zone.get_int( "end_x" ); const int end_y = jo_zone.get_int( "end_y" ); const int end_z = jo_zone.get_int( "end_z", 0 ); if( has_type( type ) ) { add( name, type, invert, enabled, tripoint( start_x, start_y, start_z ), tripoint( end_x, end_y, end_z ) ); } else { debugmsg( "Invalid zone type: %s", type.c_str() ); } } }
mabuff_id get_next( JsonIn &jin ) const { if( jin.test_string() ) { return mabuff_id( jin.get_string() ); } JsonObject jsobj = jin.get_object(); return ma_buffs.load( jsobj ).id; }
void NameGenerator::load( JsonIn &jsin ) { jsin.start_array(); while( !jsin.end_array() ) { JsonObject json_name = jsin.get_object(); load_name( json_name ); } }
static void write_array( JsonIn &jsin, JsonOut &jsout, int depth, bool force_wrap ) { jsout.start_array( force_wrap ); jsin.start_array(); while( !jsin.end_array() ) { format( jsin, jsout, depth ); } jsout.end_array(); }
static void write_object( JsonIn &jsin, JsonOut &jsout, int depth, bool force_wrap ) { jsout.start_object( force_wrap ); jsin.start_object(); while( !jsin.end_object() ) { std::string name = jsin.get_member_name(); jsout.member( name ); format( jsin, jsout, depth ); } jsout.end_object(); }
void load_season_array( JsonIn &js, C &container, F load_func ) { if( js.test_array() ) { js.start_array(); for( auto &season_entry : container ) { season_entry = load_func( js ); js.end_array(); // consume separator } } else { container.fill( load_func( js ) ); } }
profession::itypedec get_next( JsonIn &jin ) const { // either a plain item type id string, or an array with item type id // and as second entry the item description. if( jin.test_string() ) { return profession::itypedec( jin.get_string(), "" ); } JsonArray jarr = jin.get_array(); const auto id = jarr.get_string( 0 ); const auto s = jarr.get_string( 1 ); const auto snippet = _( s.c_str() ); return profession::itypedec( id, snippet ); }
long string_to_symbol( JsonIn &js ) { const std::string s = js.get_string(); if( s == "LINE_XOXO" ) { return LINE_XOXO; } else if( s == "LINE_OXOX" ) { return LINE_OXOX; } else if( s.length() != 1 ) { js.error( "Symbol string must be exactly 1 character long." ); } return s[0]; }
static void write_object( JsonIn &jsin, JsonOut &jsout, int depth, bool force_wrap ) { jsout.start_object( force_wrap ); jsin.start_object(); while( !jsin.end_object() ) { std::string name = jsin.get_member_name(); jsout.member( name ); bool override_wrap = false; if( name == "rows" || name == "blueprint" ) { // Introspect into the row, if it has more than one element, force it to wrap. int in_start_pos = jsin.tell(); bool ate_seperator = jsin.get_ate_separator(); { JsonArray arr = jsin.get_array(); if( arr.size() > 1 ) { override_wrap = true; } } jsin.seek( in_start_pos ); jsin.set_ate_separator( ate_seperator ); } format( jsin, jsout, depth, override_wrap ); } jsout.end_object(); }
void auto_pickup::deserialize(JsonIn &jsin) { vRules[(bChar) ? CHARACTER : GLOBAL].clear(); jsin.start_array(); while (!jsin.end_array()) { JsonObject jo = jsin.get_object(); const std::string sRule = jo.get_string("rule"); const bool bActive = jo.get_bool("active"); const bool bExclude = jo.get_bool("exclude"); vRules[(bChar) ? CHARACTER : GLOBAL].push_back(cRules(sRule, bActive, bExclude)); } }
void zone_data::deserialize( JsonIn &jsin ) { JsonObject data = jsin.get_object(); data.read( "name", name ); data.read( "type", type ); data.read( "invert", invert ); data.read( "enabled", enabled ); //Legacy support if( data.has_member( "is_vehicle" ) ) { data.read( "is_vehicle", is_vehicle ); } else { is_vehicle = false; } //Legacy support if( data.has_member( "start_x" ) ) { tripoint s; tripoint e; data.read( "start_x", s.x ); data.read( "start_y", s.y ); data.read( "start_z", s.z ); data.read( "end_x", e.x ); data.read( "end_y", e.y ); data.read( "end_z", e.z ); start = s; end = e; } else { data.read( "start", start ); data.read( "end", end ); } auto new_options = zone_options::create( type ); new_options->deserialize( data ); options = new_options; }
void item_location::deserialize( JsonIn &js ) { auto obj = js.get_object(); auto type = obj.get_string( "type" ); int idx = -1; tripoint pos = tripoint_min; obj.read( "idx", idx ); obj.read( "pos", pos ); if( type == "character" ) { ptr.reset( new impl::item_on_person( g->u, idx ) ); } else if( type == "map" ) { ptr.reset( new impl::item_on_map( pos, idx ) ); } else if( type == "vehicle" ) { auto *veh = g->m.veh_at( pos ); int part = obj.get_int( "part" ); if( veh && part >= 0 && part < int( veh->parts.size() ) ) { ptr.reset( new impl::item_on_vehicle( vehicle_cursor( *veh, part ), idx ) ); } } }
////////////////// mission.h //// void mission::deserialize(JsonIn &jsin) { JsonObject jo = jsin.get_object(); if (jo.has_member("type_id")) { type = &(g->mission_types[jo.get_int("type_id")]); } jo.read("description", description); jo.read("failed", failed); jo.read("value", value); jo.read("reward", reward); jo.read("uid", uid ); JsonArray ja = jo.get_array("target"); if (ja.size() == 2) { target.x = ja.get_int(0); target.y = ja.get_int(1); } follow_up = mission_id(jo.get_int("follow_up", follow_up)); item_id = itype_id(jo.get_string("item_id", item_id)); jo.read("deadline", deadline ); jo.read("step", step ); jo.read("count", count ); jo.read("npc_id", npc_id ); jo.read("good_fac_id", good_fac_id ); jo.read("bad_fac_id", bad_fac_id ); }
/* * vehicle_part */ void vehicle_part::deserialize(JsonIn &jsin) { JsonObject data = jsin.get_object(); int intpid; std::string pid; if ( data.read("id_enum", intpid) && intpid < 74 ) { pid = legacy_vpart_id[intpid]; } else { data.read("id",pid); } // if we don't know what type of part it is, it'll cause problems later. if (vehicle_part_types.find(pid) == vehicle_part_types.end()) { if (pid == "wheel_underbody") { pid = "wheel_wide"; } else { throw (std::string)"bad vehicle part, id: %s" + pid; } } setid(pid); data.read("mount_dx", mount_dx); data.read("mount_dy", mount_dy); data.read("hp", hp ); data.read("amount", amount ); data.read("blood", blood ); data.read("bigness", bigness ); data.read( "flags", flags ); data.read( "passenger_id", passenger_id ); data.read("items", items); }
// The loaded name is one of usage with optional gender. // The combinations used in names files are as follows. // // Backer | (Female|Male|Unisex) // Given | (Female|Male) // unisex names are duplicated in each group // Family | Unisex // City // World static void load( JsonIn &jsin ) { jsin.start_array(); while( !jsin.end_array() ) { JsonObject jo = jsin.get_object(); // get flags of name. const nameFlags type = usage_flag( jo.get_string( "usage" ) ) | gender_flag( jo.get_string( "gender", "" ) ); // find group type and add name to group names[type].push_back( jo.get_string( "name" ) ); } }
////////////////// faction.h //// void faction::deserialize(JsonIn &jsin) { JsonObject jo = jsin.get_object(); jo.read("id", id); jo.read("name", name); goal = faction_goal(jo.get_int("goal", goal)); values = jo.get_int("values", values); job1 = faction_job(jo.get_int("job1", job1)); job2 = faction_job(jo.get_int("job2", job2)); jo.read("likes_u", likes_u); jo.read("respects_u", respects_u); jo.read("known_by_u", known_by_u); jo.read("strength", strength); jo.read("sneak", sneak); jo.read("crime", crime); jo.read("cult", cult); jo.read("good", good); jo.read("omx", omx); jo.read("omy", omy); jo.read("mapx", mapx); jo.read("mapy", mapy); jo.read("size", size); jo.read("power", power); if (jo.has_array("opinion_of")) { opinion_of = jo.get_int_array("opinion_of"); } }
void Messages::game_message::deserialize( JsonIn &jsin ) { JsonObject obj = jsin.get_object(); turn = obj.get_int( "turn" ); message = obj.get_string( "message" ); count = obj.get_int( "count" ); type = static_cast<game_message_type>( obj.get_int( "type" ) ); }
void translation::deserialize( JsonIn &jsin ) { if( jsin.test_string() ) { ctxt = cata::nullopt; raw = jsin.get_string(); needs_translation = true; } else { JsonObject jsobj = jsin.get_object(); if( jsobj.has_string( "ctxt" ) ) { ctxt = jsobj.get_string( "ctxt" ); } else { ctxt = cata::nullopt; } raw = jsobj.get_string( "str" ); needs_translation = true; } }
Trait_group_tag trait_group::load_trait_group( JsonIn &stream, const std::string &default_subtype ) { if( stream.test_string() ) { return Trait_group_tag( stream.get_string() ); } else if( stream.test_object() ) { const Trait_group_tag group = get_unique_trait_group_id(); JsonObject jo = stream.get_object(); const std::string subtype = jo.get_string( "subtype", default_subtype ); mutation_branch::load_trait_group( jo, group, subtype ); return group; } else if( stream.test_array() ) { const Trait_group_tag group = get_unique_trait_group_id(); JsonArray jarr = stream.get_array(); if( default_subtype != "collection" && default_subtype != "distribution" ) { jarr.throw_error( "invalid subtype for trait group" ); } mutation_branch::load_trait_group( jarr, group, default_subtype == "collection" ); return group; } else { stream.error( "invalid trait group, must be string (group id) or object/array (the group data)" ); return Trait_group_tag{}; } }
Group_tag item_group::load_item_group( JsonIn& stream, const std::string& default_subtype ) { if( stream.test_string() ) { return stream.get_string(); } else if( stream.test_object() ) { const Group_tag group = get_unique_group_id(); JsonObject jo = stream.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() ) { const Group_tag group = get_unique_group_id(); JsonArray jarr = stream.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" ) { debugmsg( "invalid subtype for item group: %s", default_subtype.c_str() ); } item_controller->load_item_group( jarr, group, default_subtype == "collection" ); return group; } else { stream.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{}; } }
void npc_combat_rules::deserialize(JsonIn &jsin) { JsonObject data = jsin.get_object(); int tmpeng; data.read("engagement", tmpeng); engagement = (combat_engagement)tmpeng; data.read( "use_guns", use_guns); data.read( "use_grenades", use_grenades); data.read( "use_silent", use_silent); }
void npc_opinion::deserialize(JsonIn &jsin) { JsonObject data = jsin.get_object(); data.read("trust",trust); data.read("fear",fear); data.read("value",value); data.read("anger",anger); data.read("owed",owed); data.read("favors",favors); }
/* * Load vehicle from a json blob that might just exceed player in size. */ void vehicle::deserialize(JsonIn &jsin) { JsonObject data = jsin.get_object(); int fdir, mdir; data.read("type", type); data.read("posx", posx); data.read("posy", posy); data.read("levx", levx); data.read("levy", levy); data.read("om_id", om_id); data.read("faceDir", fdir); data.read("moveDir", mdir); data.read("turn_dir", turn_dir); data.read("velocity", velocity); data.read("cruise_velocity", cruise_velocity); data.read("cruise_on", cruise_on); data.read("engine_on", engine_on); data.read("has_pedals", has_pedals); data.read("tracking_on", tracking_on); data.read("lights_on", lights_on); data.read("overhead_lights_on", overhead_lights_on); data.read("fridge_on", fridge_on); data.read("recharger_on", recharger_on); data.read("skidding", skidding); data.read("turret_mode", turret_mode); data.read("of_turn_carry", of_turn_carry); face.init (fdir); move.init (mdir); data.read("name",name); data.read("parts", parts); /* for(int i=0;i < parts.size();i++ ) { parts[i].setid(parts[i].id); } */ /* After loading, check if the vehicle is from the old rules and is missing * frames. */ if ( savegame_loading_version < 11 ) { add_missing_frames(); } find_horns (); find_parts (); find_power (); find_fuel_tanks (); find_exhaust (); insides_dirty = true; precalc_mounts (0, face.dir()); data.read("tags",tags); }
void safemode::deserialize( JsonIn &jsin ) { auto &temp_rules = ( is_character ) ? character_rules : global_rules; temp_rules.clear(); jsin.start_array(); while( !jsin.end_array() ) { JsonObject jo = jsin.get_object(); const std::string rule = jo.get_string( "rule" ); const bool active = jo.get_bool( "active" ); const bool whitelist = jo.get_bool( "whitelist" ); const Creature::Attitude attitude = ( Creature::Attitude ) jo.get_int( "attitude" ); const int proximity = jo.get_int( "proximity" ); temp_rules.push_back( rules_class( rule, active, whitelist, attitude, proximity ) ); } }
void zone_manager::deserialize( JsonIn &jsin ) { jsin.read( zones ); for( auto it = zones.begin(); it != zones.end(); ++it ) { const zone_type_id zone_type = it->get_type(); if( !has_type( zone_type ) ) { zones.erase( it ); debugmsg( "Invalid zone type: %s", zone_type.c_str() ); } } }
void inventory::json_load_items(JsonIn &jsin) { try { JsonArray ja = jsin.get_array(); while ( ja.has_more() ) { JsonObject jo = ja.next_object(); add_item(item( jo ), false, false); } } catch (std::string& jsonerr) { debugmsg("bad inventory json:\n%s", jsonerr.c_str() ); } }
void color_manager::deserialize( JsonIn &jsin ) { jsin.start_array(); while( !jsin.end_array() ) { JsonObject joColors = jsin.get_object(); const std::string name = joColors.get_string( "name" ); const std::string name_custom = joColors.get_string( "custom" ); const std::string name_invert_custom = joColors.get_string( "invertcustom" ); color_id id = name_to_id( name ); auto &entry = color_array[id]; if( !name_custom.empty() ) { entry.name_custom = name_custom; } if( !name_invert_custom.empty() ) { entry.name_invert_custom = name_invert_custom; } } }
void npc_favor::deserialize(JsonIn &jsin) { JsonObject jo = jsin.get_object(); type = npc_favor_type(jo.get_int("type")); jo.read("value", value); jo.read("itype_id", item_id); skill = NULL; if (jo.has_int("skill_id")) { skill = Skill::skill(jo.get_int("skill_id")); } else if (jo.has_string("skill_id")) { skill = Skill::skill(jo.get_string("skill_id")); } }
void SkillLevel::deserialize(JsonIn & jsin) { JsonObject data = jsin.get_object(); int lastpractice=0; data.read( "level", _level ); data.read( "exercise", _exercise ); data.read( "istraining", _isTraining ); data.read( "lastpracticed", lastpractice ); if(lastpractice == 0) { _lastPracticed = HOURS(OPTIONS["INITIAL_TIME"]); } else { _lastPracticed = lastpractice; } }