/** * @brief Saves all the star planets. * * @return 0 on success. */ int dpl_saveAll (void) { int i; /*char file[PATH_MAX];*/ xmlDocPtr doc; xmlTextWriterPtr writer; int np; const Planet *p; const Planet **sorted_p; /* Create the writer. */ writer = xmlNewTextWriterDoc(&doc, 0); if (writer == NULL) { WARN("testXmlwriterDoc: Error creating the xml writer"); return -1; } /* Set the writer parameters. */ xmlw_setParams( writer ); /* Start writer. */ xmlw_start(writer); xmlw_startElem( writer, "Assets" ); /* Sort planets. */ p = planet_getAll( &np ); sorted_p = malloc( sizeof(Planet*) * np ); for (i=0; i<np; i++) sorted_p[i] = &p[i]; qsort( sorted_p, np, sizeof(Planet*), dpl_compPlanet ); /* Write planets. */ for (i=0; i<np; i++) dpl_savePlanet( writer, sorted_p[i] ); /* Clean up sorted planet.s */ free(sorted_p); /* End writer. */ xmlw_endElem( writer ); /* "Assets" */ xmlw_done( writer ); /* No need for writer anymore. */ xmlFreeTextWriter( writer ); /* Write data. */ xmlSaveFileEnc( "asset.xml", doc, "UTF-8" ); /* Clean up. */ xmlFreeDoc(doc); return 0; }
/** * @brief Saves the current game. * * @return 0 on success. */ int save_all (void) { char file[PATH_MAX]; xmlDocPtr doc; xmlTextWriterPtr writer; /* Create the writer. */ writer = xmlNewTextWriterDoc(&doc, conf.save_compress); if (writer == NULL) { ERR("testXmlwriterDoc: Error creating the xml writer"); return -1; } /* Set the writer parameters. */ xmlTextWriterSetIndentString(writer, (const xmlChar*)" "); xmlTextWriterSetIndent(writer, 1); /* Start element. */ xmlw_start(writer); xmlw_startElem(writer,"naev_save"); /* Save the version and such. */ xmlw_startElem(writer,"version"); xmlw_elem( writer, "naev", "%d.%d.%d", VMAJOR, VMINOR, VREV ); xmlw_elem( writer, "data", "%s", ndata_name() ); xmlw_endElem(writer); /* "version" */ /* Save the data. */ if (save_data(writer) < 0) { ERR("Trying to save game data"); goto err_writer; } /* Finish element. */ xmlw_endElem(writer); /* "naev_save" */ xmlw_done(writer); /* Write to file. */ if (nfile_dirMakeExist("%ssaves", nfile_basePath()) < 0) { WARN("Aborting save..."); goto err_writer; } snprintf(file, PATH_MAX, "%ssaves/%s.ns", nfile_basePath(), player_name); /* Back up old savegame. */ if (nfile_backupIfExists(file) < 0) { WARN("Aborting save..."); goto err_writer; } /* Critical section, if crashes here player's game gets corrupted. * Luckily we have a copy just in case... */ xmlFreeTextWriter(writer); if (xmlSaveFileEnc(file, doc, "UTF-8") < 0) { WARN("Failed to write savegame! You'll most likely have to restore it by copying your backup savegame over your current savegame."); goto err; } xmlFreeDoc(doc); return 0; err_writer: xmlFreeTextWriter(writer); err: xmlFreeDoc(doc); return -1; }
/** * @brief Saves a star system. * * @param writer Write to use for saving the star system. * @param sys Star system to save. * @return 0 on success. */ int dsys_saveSystem( StarSystem *sys ) { int i; xmlDocPtr doc; xmlTextWriterPtr writer; const Planet **sorted_planets; const JumpPoint **sorted_jumps, *jp; char file[PATH_MAX], *cleanName; /* Reconstruct jumps so jump pos are updated. */ system_reconstructJumps(sys); /* Create the writer. */ writer = xmlNewTextWriterDoc(&doc, 0); if (writer == NULL) { WARN("testXmlwriterDoc: Error creating the xml writer"); return -1; } /* Set the writer parameters. */ xmlw_setParams( writer ); /* Start writer. */ xmlw_start(writer); xmlw_startElem( writer, "ssys" ); /* Attributes. */ xmlw_attr( writer, "name", "%s", sys->name ); /* General. */ xmlw_startElem( writer, "general" ); if (sys->background != NULL) xmlw_elem( writer, "background", "%s", sys->background ); xmlw_elem( writer, "radius", "%f", sys->radius ); xmlw_elem( writer, "stars", "%d", sys->stars ); xmlw_elem( writer, "interference", "%f", sys->interference ); xmlw_startElem( writer, "nebula" ); xmlw_attr( writer, "volatility", "%f", sys->nebu_volatility ); xmlw_str( writer, "%f", sys->nebu_density ); xmlw_endElem( writer ); /* "nebula" */ xmlw_endElem( writer ); /* "general" */ /* Position. */ xmlw_startElem( writer, "pos" ); xmlw_elem( writer, "x", "%f", sys->pos.x ); xmlw_elem( writer, "y", "%f", sys->pos.y ); xmlw_endElem( writer ); /* "pos" */ /* Planets. */ sorted_planets = malloc( sizeof(Planet*) * sys->nplanets); memcpy( sorted_planets, sys->planets, sizeof(Planet*) * sys->nplanets ); qsort( sorted_planets, sys->nplanets, sizeof(Planet*), dsys_compPlanet ); xmlw_startElem( writer, "assets" ); for (i=0; i<sys->nplanets; i++) xmlw_elem( writer, "asset", "%s", sorted_planets[i]->name ); xmlw_endElem( writer ); /* "assets" */ free(sorted_planets); /* Jumps. */ sorted_jumps = malloc( sizeof(JumpPoint*) * sys->njumps ); for (i=0; i<sys->njumps; i++) sorted_jumps[i] = &sys->jumps[i]; qsort( sorted_jumps, sys->njumps, sizeof(JumpPoint*), dsys_compJump ); xmlw_startElem( writer, "jumps" ); for (i=0; i<sys->njumps; i++) { jp = sorted_jumps[i]; xmlw_startElem( writer, "jump" ); xmlw_attr( writer, "target", "%s", jp->target->name ); /* Position. */ if (!jp_isFlag( jp, JP_AUTOPOS )) { xmlw_startElem( writer, "pos" ); xmlw_attr( writer, "x", "%f", jp->pos.x ); xmlw_attr( writer, "y", "%f", jp->pos.y ); xmlw_endElem( writer ); /* "pos" */ } else xmlw_elemEmpty( writer, "autopos" ); /* Radius and misc properties. */ if (jp->radius != 200.) xmlw_elem( writer, "radius", "%f", jp->radius ); /* More flags. */ if (jp_isFlag( jp, JP_HIDDEN )) xmlw_elemEmpty( writer, "hidden" ); if (jp_isFlag( jp, JP_EXITONLY )) xmlw_elemEmpty( writer, "exitonly" ); xmlw_elem( writer, "hide", "%f", sqrt(jp->hide) ); xmlw_endElem( writer ); /* "jump" */ } xmlw_endElem( writer ); /* "jumps" */ free(sorted_jumps); xmlw_endElem( writer ); /** "ssys" */ xmlw_done(writer); /* No need for writer anymore. */ xmlFreeTextWriter(writer); /* Write data. */ cleanName = uniedit_nameFilter( sys->name ); nsnprintf( file, sizeof(file), "%s/%s.xml", conf.dev_save_sys, cleanName ); xmlSaveFileEnc( file, doc, "UTF-8" ); /* Clean up. */ xmlFreeDoc(doc); free(cleanName); return 0; }
/** * @brief Saves selected systems as a map outfit file. * * @return 0 on success. */ int dsys_saveMap (StarSystem **uniedit_sys, int uniedit_nsys) { int i, j, k; xmlDocPtr doc; xmlTextWriterPtr writer; StarSystem *s; char file[PATH_MAX], *cleanName; /* Create the writer. */ writer = xmlNewTextWriterDoc(&doc, 0); if (writer == NULL) { WARN("testXmlwriterDoc: Error creating the xml writer"); return -1; } /* Set the writer parameters. */ xmlw_setParams( writer ); /* Start writer. */ xmlw_start(writer); xmlw_startElem( writer, "outfit" ); /* Attributes. */ xmlw_attr( writer, "name", "Editor-generated Map" ); /* General. */ xmlw_startElem( writer, "general" ); xmlw_elem( writer, "mass", "%d", 0 ); xmlw_elem( writer, "price", "%d", 1000 ); xmlw_elem( writer, "description", "%s", "This map has been created by the universe editor." ); xmlw_elem( writer, "gfx_store", "%s", "map" ); xmlw_endElem( writer ); /* "general" */ xmlw_startElem( writer, "specific" ); xmlw_attr( writer, "type", "map" ); /* Iterate over all selected systems. Save said systems and any NORMAL jumps they might share. */ for (i = 0; i < uniedit_nsys; i++) { s = uniedit_sys[i]; xmlw_startElem( writer, "sys" ); xmlw_attr( writer, "name", "%s", s->name ); /* Iterate jumps and see if they lead to any other systems in our array. */ for (j = 0; j < s->njumps; j++) { /* Ignore hidden and exit-only jumps. */ if (jp_isFlag(&s->jumps[j], JP_EXITONLY )) continue; if (jp_isFlag(&s->jumps[j], JP_HIDDEN)) continue; /* This is a normal jump. */ for (k = 0; k < uniedit_nsys; k++) { if (s->jumps[j].target == uniedit_sys[k]) { xmlw_elem( writer, "jump", "%s", uniedit_sys[k]->name ); break; } } } /* Iterate assets and add them */ for (j = 0; j < s->nplanets; j++) { if (s->planets[j]->real) xmlw_elem( writer, "asset", "%s", s->planets[j]->name ); } xmlw_endElem( writer ); /* "sys" */ } xmlw_endElem( writer ); /* "specific" */ xmlw_endElem( writer ); /* "outfit" */ xmlw_done(writer); /* No need for writer anymore. */ xmlFreeTextWriter(writer); /* Write data. */ cleanName = uniedit_nameFilter( "saved map" ); nsnprintf( file, sizeof(file), "%s/%s.xml", conf.dev_save_map, cleanName ); xmlSaveFileEnc( file, doc, "UTF-8" ); /* Clean up. */ xmlFreeDoc(doc); free(cleanName); return 0; }