* Constructs FiosNumberedSaveName. Initial number is the most recent save, or -1 if not found. * @param prefix The prefix to use to generate a filename. */
| 699 | * @param prefix The prefix to use to generate a filename. |
| 700 | */ |
| 701 | FiosNumberedSaveName::FiosNumberedSaveName(const std::string &prefix) : prefix(prefix), number(-1) |
| 702 | { |
| 703 | static std::optional<std::string> _autosave_path; |
| 704 | if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR); |
| 705 | |
| 706 | static std::string _prefix; ///< Static as the lambda needs access to it. |
| 707 | |
| 708 | /* Callback for FiosFileScanner. */ |
| 709 | static FiosGetTypeAndNameProc *const proc = [](SaveLoadOperation, std::string_view file, std::string_view ext) { |
| 710 | if (StrEqualsIgnoreCase(ext, ".sav") && file.starts_with(_prefix)) return std::tuple(FIOS_TYPE_FILE, std::string{}); |
| 711 | return std::tuple(FIOS_TYPE_INVALID, std::string{}); |
| 712 | }; |
| 713 | |
| 714 | /* Prefix to check in the callback. */ |
| 715 | _prefix = *_autosave_path + this->prefix; |
| 716 | |
| 717 | /* Get the save list. */ |
| 718 | FileList list; |
| 719 | FiosFileScanner scanner(SLO_SAVE, proc, list); |
| 720 | scanner.Scan(".sav", *_autosave_path, false); |
| 721 | |
| 722 | /* Find the number for the most recent save, if any. */ |
| 723 | if (list.begin() != list.end()) { |
| 724 | SortingBits order = _savegame_sort_order; |
| 725 | _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING; |
| 726 | std::sort(list.begin(), list.end()); |
| 727 | _savegame_sort_order = order; |
| 728 | |
| 729 | std::string name = list.begin()->title.GetDecodedString(); |
| 730 | std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Generate a savegame name and number according to _settings_client.gui.max_num_autosaves. |
nothing calls this directly
no test coverage detected