std::string serialize_wrapper( const std::function<void( JsonOut & )> &callback ) { std::ostringstream buffer; JsonOut jsout( buffer ); callback( jsout ); return buffer.str(); }
bool zone_manager::save_zones() { std::string savefile = world_generator->active_world->world_path + "/" + base64_encode( g->u.name ) + ".zones.json"; return write_to_file_exclusive( savefile, [&]( std::ostream & fout ) { JsonOut jsout( fout ); serialize( jsout ); }, _( "zones date" ) ); }
bool zone_manager::save_zones() { std::string savefile = g->get_player_base_save_path() + ".zones.json"; added_vzones.clear(); changed_vzones.clear(); removed_vzones.clear(); return write_to_file_exclusive( savefile, [&]( std::ostream & fout ) { JsonOut jsout( fout ); serialize( jsout ); }, _( "zones date" ) ); }
void input_manager::save() { write_to_file( FILENAMES["user_keybindings"], [&]( std::ostream & data_file ) { JsonOut jsout( data_file, true ); jsout.start_array(); for( t_action_contexts::const_iterator a = action_contexts.begin(); a != action_contexts.end(); ++a ) { const t_actions &actions = a->second; for( const auto &action : actions ) { const t_input_event_list &events = action.second.input_events; jsout.start_object(); jsout.member( "id", action.first ); jsout.member( "category", a->first ); bool is_user_created = action.second.is_user_created; if( is_user_created ) { jsout.member( "is_user_created", is_user_created ); } jsout.member( "bindings" ); jsout.start_array(); for( const auto &event : events ) { jsout.start_object(); switch( event.type ) { case CATA_INPUT_KEYBOARD: jsout.member( "input_method", "keyboard" ); break; case CATA_INPUT_GAMEPAD: jsout.member( "input_method", "gamepad" ); break; case CATA_INPUT_MOUSE: jsout.member( "input_method", "mouse" ); break; default: throw std::runtime_error( "unknown input_event_t" ); } jsout.member( "key" ); jsout.start_array(); for( size_t i = 0; i < event.sequence.size(); i++ ) { jsout.write( get_keyname( event.sequence[i], event.type, true ) ); } jsout.end_array(); jsout.end_object(); } jsout.end_array(); jsout.end_object(); } } jsout.end_array(); }, _( "key bindings configuration" ) ); }
void mapbuffer::save_quad( const std::string &filename, const tripoint &om_addr, std::list<tripoint> &submaps_to_delete, bool delete_after_save ) { std::ofstream fout; fopen_exclusive(fout, filename.c_str()); if(!fout.is_open()) { return; } std::vector<point> offsets; offsets.push_back( point(0, 0) ); offsets.push_back( point(0, 1) ); offsets.push_back( point(1, 0) ); offsets.push_back( point(1, 1) ); JsonOut jsout( fout ); jsout.start_array(); for( std::vector<point>::iterator offset = offsets.begin(); offset != offsets.end(); ++offset ) { tripoint submap_addr = overmapbuffer::omt_to_sm_copy( om_addr ); submap_addr.x += offset->x; submap_addr.y += offset->y; if (submaps.count(submap_addr) == 0) { continue; } submap *sm = submaps[submap_addr]; if( sm == NULL ) { continue; } jsout.start_object(); jsout.member( "version", savegame_version); jsout.member( "coordinates" ); jsout.start_array(); jsout.write( submap_addr.x ); jsout.write( submap_addr.y ); jsout.write( submap_addr.z ); jsout.end_array(); jsout.member( "turn_last_touched", sm->turn_last_touched ); jsout.member( "temperature", sm->temperature ); jsout.member( "terrain" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save terrains jsout.write( terlist[sm->ter[i][j]].id ); } } jsout.end_array(); // Write out the radiation array in a simple RLE scheme. // written in intensity, count pairs jsout.member( "radiation" ); jsout.start_array(); int lastrad = -1; int count = 0; for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save radiation, re-examine this because it doesnt look like it works right int r = sm->get_radiation(i, j); if (r == lastrad) { count++; } else { if (count) { jsout.write( count ); } jsout.write( r ); lastrad = r; count = 1; } } } jsout.write( count ); jsout.end_array(); jsout.member("furniture"); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save furniture if( sm->get_furn( i, j ) != f_null ) { jsout.start_array(); jsout.write( i ); jsout.write( j ); jsout.write( furnlist[ sm->get_furn( i, j ) ].id ); jsout.end_array(); } } } jsout.end_array(); jsout.member( "items" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { if( sm->itm[i][j].empty() ) { continue; } jsout.write( i ); jsout.write( j ); jsout.write( sm->itm[i][j] ); } } jsout.end_array(); jsout.member( "traps" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save traps if (sm->get_trap( i, j ) != tr_null) { jsout.start_array(); jsout.write( i ); jsout.write( j ); jsout.write( traplist[ sm->get_trap( i, j ) ]->id ); jsout.end_array(); } } } jsout.end_array(); jsout.member( "fields" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save fields if (sm->fld[i][j].fieldCount() > 0) { jsout.write( i ); jsout.write( j ); jsout.start_array(); for(std::map<field_id, field_entry *>::iterator it = sm->fld[i][j].getFieldStart(); it != sm->fld[i][j].getFieldEnd(); ++it) { if(it->second != NULL) { // We don't seem to have a string identifier for fields anywhere. jsout.write( it->second->getFieldType() ); jsout.write( it->second->getFieldDensity() ); jsout.write( it->second->getFieldAge() ); } } jsout.end_array(); } } } jsout.end_array(); jsout.member( "graffiti" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save graffiti if (sm->get_graffiti(i, j).contents) { jsout.start_array(); jsout.write( i ); jsout.write( j ); jsout.write( *sm->get_graffiti(i, j).contents ); jsout.end_array(); } } } jsout.end_array(); jsout.member("cosmetics"); jsout.start_array(); for (int j = 0; j < SEEY; j++) { for (int i = 0; i < SEEX; i++) { if (sm->cosmetics[i][j].size() > 0) { jsout.start_array(); jsout.write(i); jsout.write(j); jsout.write(sm->cosmetics[i][j]); jsout.end_array(); } } } jsout.end_array(); // Output the spawn points jsout.member( "spawns" ); jsout.start_array(); for( std::vector<spawn_point>::iterator spawn_it = sm->spawns.begin(); spawn_it != sm->spawns.end(); ++spawn_it ) { jsout.start_array(); jsout.write( spawn_it->type ); jsout.write( spawn_it->count ); jsout.write( spawn_it->posx ); jsout.write( spawn_it->posy ); jsout.write( spawn_it->faction_id ); jsout.write( spawn_it->mission_id ); jsout.write( spawn_it->friendly ); jsout.write( spawn_it->name ); jsout.end_array(); } jsout.end_array(); jsout.member( "vehicles" ); jsout.start_array(); for( std::vector<vehicle *>::iterator vehicle_it = sm->vehicles.begin(); vehicle_it != sm->vehicles.end(); ++vehicle_it ) { // json lib doesn't know how to turn a vehicle * into a vehicle, // so we have to iterate manually. jsout.write( **vehicle_it ); } jsout.end_array(); // Output the computer if (sm->comp.name != "") { jsout.member( "computers", sm->comp.save_data() ); } // Output base camp if any if (sm->camp.is_valid()) { jsout.member( "camp" ); jsout.write( sm->camp.save_data() ); } if( delete_after_save ) { submaps_to_delete.push_back( submap_addr ); } jsout.end_object(); } jsout.end_array(); fclose_exclusive(fout, filename.c_str()); }
void input_manager::save() { std::ofstream data_file; std::string file_name = FILENAMES["user_keybindings"]; std::string file_name_tmp = file_name + ".tmp"; data_file.open(file_name_tmp.c_str(), std::ifstream::binary); if(!data_file.good()) { throw std::runtime_error(file_name_tmp + ": could not write"); } data_file.exceptions(std::ios::badbit | std::ios::failbit); JsonOut jsout(data_file, true); jsout.start_array(); for (t_action_contexts::const_iterator a = action_contexts.begin(); a != action_contexts.end(); ++a) { const t_actions &actions = a->second; for( const auto &action : actions ) { const t_input_event_list &events = action.second.input_events; jsout.start_object(); jsout.member( "id", action.first ); jsout.member("category", a->first); bool is_user_created = action.second.is_user_created; if (is_user_created) { jsout.member("is_user_created", is_user_created); } jsout.member("bindings"); jsout.start_array(); for( const auto &event : events ) { jsout.start_object(); switch( event.type ) { case CATA_INPUT_KEYBOARD: jsout.member("input_method", "keyboard"); break; case CATA_INPUT_GAMEPAD: jsout.member("input_method", "gamepad"); break; case CATA_INPUT_MOUSE: jsout.member("input_method", "mouse"); break; default: throw std::runtime_error("unknown input_event_t"); } jsout.member("key"); jsout.start_array(); for( size_t i = 0; i < event.sequence.size(); i++ ) { jsout.write( get_keyname( event.sequence[i], event.type, true ) ); } jsout.end_array(); jsout.end_object(); } jsout.end_array(); jsout.end_object(); } } jsout.end_array(); data_file.close(); if(!rename_file(file_name_tmp, file_name)) { throw std::string("Could not rename file to ") + file_name; } }
void mapbuffer::save_quad( const std::string &dirname, const std::string &filename, const tripoint &om_addr, std::list<tripoint> &submaps_to_delete, bool delete_after_save ) { std::vector<point> offsets; std::vector<tripoint> submap_addrs; offsets.push_back( point(0, 0) ); offsets.push_back( point(0, 1) ); offsets.push_back( point(1, 0) ); offsets.push_back( point(1, 1) ); bool all_uniform = true; for( auto &offsets_offset : offsets ) { tripoint submap_addr = overmapbuffer::omt_to_sm_copy( om_addr ); submap_addr.x += offsets_offset.x; submap_addr.y += offsets_offset.y; submap_addrs.push_back( submap_addr ); submap *sm = submaps[submap_addr]; if( sm != nullptr && !sm->is_uniform ) { all_uniform = false; } } if( all_uniform ) { // Nothing to save - this quad will be regenerated faster than it would be re-read if( delete_after_save ) { for( auto &submap_addr : submap_addrs ) { if( submaps.count( submap_addr ) > 0 && submaps[submap_addr] != nullptr ) { submaps_to_delete.push_back( submap_addr ); } } } return; } // Don't create the directory if it would be empty assure_dir_exist( dirname.c_str() ); std::ofstream fout; fopen_exclusive( fout, filename.c_str() ); if( !fout.is_open() ) { return; } JsonOut jsout( fout ); jsout.start_array(); for( auto &submap_addr : submap_addrs ) { if( submaps.count( submap_addr ) == 0 ) { continue; } submap *sm = submaps[submap_addr]; if( sm == nullptr ) { continue; } jsout.start_object(); jsout.member( "version", savegame_version); jsout.member( "coordinates" ); jsout.start_array(); jsout.write( submap_addr.x ); jsout.write( submap_addr.y ); jsout.write( submap_addr.z ); jsout.end_array(); jsout.member( "turn_last_touched", sm->turn_last_touched ); jsout.member( "temperature", sm->temperature ); jsout.member( "terrain" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save terrains jsout.write( sm->ter[i][j].obj().id ); } } jsout.end_array(); // Write out the radiation array in a simple RLE scheme. // written in intensity, count pairs jsout.member( "radiation" ); jsout.start_array(); int lastrad = -1; int count = 0; for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save radiation, re-examine this because it doesn't look like it works right int r = sm->get_radiation(i, j); if (r == lastrad) { count++; } else { if (count) { jsout.write( count ); } jsout.write( r ); lastrad = r; count = 1; } } } jsout.write( count ); jsout.end_array(); jsout.member("furniture"); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save furniture if( sm->get_furn( i, j ) != f_null ) { jsout.start_array(); jsout.write( i ); jsout.write( j ); jsout.write( sm->get_furn( i, j ).obj().id ); jsout.end_array(); } } } jsout.end_array(); jsout.member( "items" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { if( sm->itm[i][j].empty() ) { continue; } jsout.write( i ); jsout.write( j ); jsout.write( sm->itm[i][j] ); } } jsout.end_array(); jsout.member( "traps" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save traps if (sm->get_trap( i, j ) != tr_null) { jsout.start_array(); jsout.write( i ); jsout.write( j ); // TODO: jsout should support writting an id like jsout.write( trap_id ) jsout.write( sm->get_trap( i, j ).id().str() ); jsout.end_array(); } } } jsout.end_array(); jsout.member( "fields" ); jsout.start_array(); for(int j = 0; j < SEEY; j++) { for(int i = 0; i < SEEX; i++) { // Save fields if (sm->fld[i][j].fieldCount() > 0) { jsout.write( i ); jsout.write( j ); jsout.start_array(); for( auto &fld : sm->fld[i][j] ) { const field_entry &cur = fld.second; // We don't seem to have a string identifier for fields anywhere. jsout.write( cur.getFieldType() ); jsout.write( cur.getFieldDensity() ); jsout.write( cur.getFieldAge() ); } jsout.end_array(); } } } jsout.end_array(); jsout.member("cosmetics"); jsout.start_array(); for (int j = 0; j < SEEY; j++) { for (int i = 0; i < SEEX; i++) { if (sm->cosmetics[i][j].size() > 0) { jsout.start_array(); jsout.write(i); jsout.write(j); jsout.write(sm->cosmetics[i][j]); jsout.end_array(); } } } jsout.end_array(); // Output the spawn points jsout.member( "spawns" ); jsout.start_array(); for( auto &elem : sm->spawns ) { jsout.start_array(); jsout.write( elem.type.str() ); // TODO: json should know how to write string_ids jsout.write( elem.count ); jsout.write( elem.posx ); jsout.write( elem.posy ); jsout.write( elem.faction_id ); jsout.write( elem.mission_id ); jsout.write( elem.friendly ); jsout.write( elem.name ); jsout.end_array(); } jsout.end_array(); jsout.member( "vehicles" ); jsout.start_array(); for( auto &elem : sm->vehicles ) { // json lib doesn't know how to turn a vehicle * into a vehicle, // so we have to iterate manually. jsout.write( *elem ); } jsout.end_array(); // Output the computer if (sm->comp.name != "") { jsout.member( "computers", sm->comp.save_data() ); } // Output base camp if any if (sm->camp.is_valid()) { jsout.member( "camp" ); jsout.write( sm->camp.save_data() ); } if( delete_after_save ) { submaps_to_delete.push_back( submap_addr ); } jsout.end_object(); } jsout.end_array(); fclose_exclusive( fout, filename.c_str() ); }
void mapbuffer::save() { std::map<tripoint, submap*, pointcomp>::iterator it; std::ofstream fout; std::stringstream mapfile; mapfile << world_generator->active_world->world_path << "/maps.txt"; fout.open(mapfile.str().c_str()); fout << "# version " << savegame_version << std::endl; JsonOut jsout(fout); jsout.start_object(); jsout.member("listsize", (unsigned int)submap_list.size()); // To keep load speedy, we're saving ints, but since these are ints // that will change with revisions and loaded mods, we're also // including a rosetta stone. jsout.member("terrain_key"); jsout.start_array(); for (int i=0; i < terlist.size(); i++) { jsout.write(terlist[i].id); } jsout.end_array(); jsout.member("furniture_key"); jsout.start_array(); for (int i=0; i < furnlist.size(); i++) { jsout.write(furnlist[i].id); } jsout.end_array(); jsout.member("trap_key"); jsout.start_array(); for (int i=0; i < g->traps.size(); i++) { jsout.write(g->traps[i]->id); } jsout.end_array(); jsout.end_object(); fout << std::endl; int num_saved_submaps = 0; int num_total_submaps = submap_list.size(); for (it = submaps.begin(); it != submaps.end(); it++) { if (num_saved_submaps % 100 == 0) popup_nowait(_("Please wait as the map saves [%d/%d]"), num_saved_submaps, num_total_submaps); fout << it->first.x << " " << it->first.y << " " << it->first.z << std::endl; submap *sm = it->second; fout << sm->turn_last_touched << std::endl; fout << sm->temperature << std::endl; std::stringstream terout; std::stringstream radout; std::stringstream furnout; std::stringstream itemout; std::stringstream trapout; std::stringstream fieldout; std::stringstream graffout; int count = 0; int lastrad = -1; for(int j = 0; j < SEEY; j++){ for(int i = 0; i < SEEX; i++){ // Save terrains terout << int(sm->ter[i][j]) << " "; // Save radiation, re-examine this because it doesnt look like it works right int r = sm->rad[i][j]; if (r == lastrad) { count++; } else { if (count) { radout << count << " "; } radout << r << " "; lastrad = r; count = 1; } // Save furniture if (sm->frn[i][j] != f_null) { furnout << "f " << i << " " << j << " " << sm->frn[i][j] << std::endl; } // Save items item tmp; for (int k = 0; k < sm->itm[i][j].size(); k++) { tmp = sm->itm[i][j][k]; itemout << "I " << i << " " << j << std::endl; itemout << tmp.save_info() << std::endl; for (int l = 0; l < tmp.contents.size(); l++) { itemout << "C " << std::endl << tmp.contents[l].save_info() << std::endl; } } // Save traps if (sm->trp[i][j] != tr_null) { trapout << "T " << i << " " << j << " " << sm->trp[i][j] << std::endl; } // Save fields if (sm->fld[i][j].fieldCount() > 0){ for(std::map<field_id, field_entry*>::iterator it = sm->fld[i][j].getFieldStart(); it != sm->fld[i][j].getFieldEnd(); ++it){ if(it->second != NULL){ fieldout << "F " << i << " " << j << " " << int(it->second->getFieldType()) << " " << int(it->second->getFieldDensity()) << " " << (it->second->getFieldAge()) << std::endl; } } } // Save graffiti if (sm->graf[i][j].contents) { graffout << "G " << i << " " << j << *sm->graf[i][j].contents << std::endl; } } terout << std::endl; } radout << count << std::endl; fout << terout.str() << radout.str() << furnout.str() << itemout.str() << trapout.str() << fieldout.str() << graffout.str(); // Output the spawn points spawn_point tmpsp; for (int i = 0; i < sm->spawns.size(); i++) { tmpsp = sm->spawns[i]; fout << "S " << (tmpsp.type) << " " << tmpsp.count << " " << tmpsp.posx << " " << tmpsp.posy << " " << tmpsp.faction_id << " " << tmpsp.mission_id << (tmpsp.friendly ? " 1 " : " 0 ") << tmpsp.name << std::endl; } // Output the vehicles for (int i = 0; i < sm->vehicles.size(); i++) { fout << "V "; sm->vehicles[i]->save (fout); } // Output the computer if (sm->comp.name != "") fout << "c " << sm->comp.save_data() << std::endl; // Output base camp if any if (sm->camp.is_valid()) fout << "B " << sm->camp.save_data() << std::endl; fout << "----" << std::endl; num_saved_submaps++; } // Close the file; that's all we need. fout.close(); }
int main( int argc, char *argv[] ) { std::stringstream in; std::stringstream out; std::string filename; std::string header; char *gateway_var = getenv( "GATEWAY_INTERFACE" ); if( gateway_var == nullptr ) { // Expect a single filename for now. if( argc == 2 ) { filename = argv[1]; } else if( argc != 1 ) { std::cout << "Supply a filename to style or no arguments." << std::endl; exit( EXIT_FAILURE ); } if( filename.empty() ) { in << std::cin.rdbuf(); } else { std::ifstream fin( filename, std::ios::binary ); in << fin.rdbuf(); fin.close(); } } else { std::map<std::string, std::string> params; initializePost( params ); std::string data = params[ "data" ]; if( data.empty() ) { exit( -255 ); } in.str( data ); header = "Content-type: application/json\n\n"; } if( in.str().size() == 0 ) { std::cout << "Error, input empty." << std::endl; exit( EXIT_FAILURE ); } JsonOut jsout( out, true ); JsonIn jsin( in ); format( jsin, jsout ); out << std::endl; if( filename.empty() ) { std::cout << header; std::cout << out.str() << std::endl; } else { if( in.str() == out.str() ) { std::cout << "Unformatted " << filename << std::endl; exit( EXIT_SUCCESS ); } else { std::ofstream fout( filename, std::ios::binary | std::ios::trunc ); fout << out.str(); fout.close(); std::cout << "Formatted " << filename << std::endl; exit( EXIT_FAILURE ); } } }