| 403 | } |
| 404 | |
| 405 | void MapResource::saveFile(const MapFormat& format, const scene::IMapRootNodePtr& root, |
| 406 | const GraphTraversalFunc& traverse, const std::string& filename) |
| 407 | { |
| 408 | // Actual output file paths |
| 409 | fs::path outFile = filename; |
| 410 | fs::path auxFile = outFile; |
| 411 | auxFile.replace_extension(game::current::getInfoFileExtension()); |
| 412 | |
| 413 | // Check writeability of the primary output file |
| 414 | throwIfNotWriteable(outFile); |
| 415 | |
| 416 | // Test opening the output file |
| 417 | rMessage() << "Opening file " << outFile.string(); |
| 418 | |
| 419 | // Open the stream to the primary output file |
| 420 | std::ofstream outFileStream(outFile.string()); |
| 421 | std::unique_ptr<std::ofstream> auxFileStream; // aux stream is optional |
| 422 | |
| 423 | // Check writeability of the auxiliary output file if necessary |
| 424 | if (format.allowInfoFileCreation()) |
| 425 | { |
| 426 | rMessage() << " and auxiliary file " << auxFile.string(); |
| 427 | |
| 428 | throwIfNotWriteable(auxFile); |
| 429 | |
| 430 | auxFileStream.reset(new std::ofstream(auxFile.string())); |
| 431 | } |
| 432 | |
| 433 | rMessage() << " for writing... "; |
| 434 | |
| 435 | if (!outFileStream.is_open()) |
| 436 | { |
| 437 | throw OperationException(fmt::format(_("Could not open file for writing: {0}"), outFile.string())); |
| 438 | } |
| 439 | |
| 440 | if (auxFileStream && !auxFileStream->is_open()) |
| 441 | { |
| 442 | throw OperationException(fmt::format(_("Could not open file for writing: {0}"), auxFile.string())); |
| 443 | } |
| 444 | |
| 445 | rMessage() << "success" << std::endl; |
| 446 | |
| 447 | // Check the total count of nodes to traverse |
| 448 | NodeCounter counter; |
| 449 | traverse(root, counter); |
| 450 | |
| 451 | // Create our main MapExporter walker, and pass the desired |
| 452 | // format to it. The constructor will prepare the scene |
| 453 | // and the destructor will clean it up afterwards. That way |
| 454 | // we ensure a nice and tidy scene when exceptions are thrown. |
| 455 | MapExporterPtr exporter; |
| 456 | auto mapWriter = format.getMapWriter(); |
| 457 | |
| 458 | if (format.allowInfoFileCreation()) |
| 459 | { |
| 460 | exporter.reset(new MapExporter(*mapWriter, root, outFileStream, *auxFileStream, counter.getCount())); |
| 461 | } |
| 462 | else |
nothing calls this directly
no test coverage detected