Add: list_[scenario|heightmap] and load_[scenario|height] console commands (#11867)
This commit is contained in:
33
src/fios.cpp
33
src/fios.cpp
@@ -65,8 +65,9 @@ bool FiosItem::operator< (const FiosItem &other) const
|
||||
* Construct a file list with the given kind of files, for the stated purpose.
|
||||
* @param abstract_filetype Kind of files to collect.
|
||||
* @param fop Purpose of the collection, either #SLO_LOAD or #SLO_SAVE.
|
||||
* @param show_dirs Whether to show directories.
|
||||
*/
|
||||
void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
|
||||
void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop, bool show_dirs)
|
||||
{
|
||||
this->clear();
|
||||
|
||||
@@ -76,15 +77,15 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati
|
||||
break;
|
||||
|
||||
case FT_SAVEGAME:
|
||||
FiosGetSavegameList(fop, *this);
|
||||
FiosGetSavegameList(fop, show_dirs, *this);
|
||||
break;
|
||||
|
||||
case FT_SCENARIO:
|
||||
FiosGetScenarioList(fop, *this);
|
||||
FiosGetScenarioList(fop, show_dirs, *this);
|
||||
break;
|
||||
|
||||
case FT_HEIGHTMAP:
|
||||
FiosGetHeightmapList(fop, *this);
|
||||
FiosGetHeightmapList(fop, show_dirs, *this);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -337,11 +338,12 @@ bool FiosFileScanner::AddFile(const std::string &filename, size_t, const std::st
|
||||
/**
|
||||
* Fill the list of the files in a directory, according to some arbitrary rule.
|
||||
* @param fop Purpose of collecting the list.
|
||||
* @param show_dirs Whether to list directories.
|
||||
* @param callback_proc The function that is called where you need to do the filtering.
|
||||
* @param subdir The directory from where to start (global) searching.
|
||||
* @param file_list Destination of the found files.
|
||||
*/
|
||||
static void FiosGetFileList(SaveLoadOperation fop, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
|
||||
static void FiosGetFileList(SaveLoadOperation fop, bool show_dirs, FiosGetTypeAndNameProc *callback_proc, Subdirectory subdir, FileList &file_list)
|
||||
{
|
||||
struct stat sb;
|
||||
struct dirent *dirent;
|
||||
@@ -354,7 +356,7 @@ static void FiosGetFileList(SaveLoadOperation fop, FiosGetTypeAndNameProc *callb
|
||||
assert(_fios_path != nullptr);
|
||||
|
||||
/* A parent directory link exists if we are not in the root directory */
|
||||
if (!FiosIsRoot(*_fios_path)) {
|
||||
if (show_dirs && !FiosIsRoot(*_fios_path)) {
|
||||
fios = &file_list.emplace_back();
|
||||
fios->type = FIOS_TYPE_PARENT;
|
||||
fios->mtime = 0;
|
||||
@@ -364,7 +366,7 @@ static void FiosGetFileList(SaveLoadOperation fop, FiosGetTypeAndNameProc *callb
|
||||
}
|
||||
|
||||
/* Show subdirectories */
|
||||
if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
|
||||
if (show_dirs && (dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
|
||||
while ((dirent = readdir(dir)) != nullptr) {
|
||||
std::string d_name = FS2OTTD(dirent->d_name);
|
||||
|
||||
@@ -384,7 +386,7 @@ static void FiosGetFileList(SaveLoadOperation fop, FiosGetTypeAndNameProc *callb
|
||||
}
|
||||
|
||||
/* Sort the subdirs always by name, ascending, remember user-sorting order */
|
||||
{
|
||||
if (show_dirs) {
|
||||
SortingBits order = _savegame_sort_order;
|
||||
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
|
||||
std::sort(file_list.begin(), file_list.end());
|
||||
@@ -464,10 +466,11 @@ std::tuple<FiosType, std::string> FiosGetSavegameListCallback(SaveLoadOperation
|
||||
/**
|
||||
* Get a list of savegames.
|
||||
* @param fop Purpose of collecting the list.
|
||||
* @param show_dirs Whether to show directories.
|
||||
* @param file_list Destination of the found files.
|
||||
* @see FiosGetFileList
|
||||
*/
|
||||
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
|
||||
void FiosGetSavegameList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
|
||||
{
|
||||
static std::optional<std::string> fios_save_path;
|
||||
|
||||
@@ -475,7 +478,7 @@ void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
|
||||
|
||||
_fios_path = &(*fios_save_path);
|
||||
|
||||
FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
|
||||
FiosGetFileList(fop, show_dirs, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -510,10 +513,11 @@ std::tuple<FiosType, std::string> FiosGetScenarioListCallback(SaveLoadOperation
|
||||
/**
|
||||
* Get a list of scenarios.
|
||||
* @param fop Purpose of collecting the list.
|
||||
* @param show_dirs Whether to show directories.
|
||||
* @param file_list Destination of the found files.
|
||||
* @see FiosGetFileList
|
||||
*/
|
||||
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
|
||||
void FiosGetScenarioList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
|
||||
{
|
||||
static std::optional<std::string> fios_scn_path;
|
||||
|
||||
@@ -524,7 +528,7 @@ void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
|
||||
|
||||
std::string base_path = FioFindDirectory(SCENARIO_DIR);
|
||||
Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
|
||||
FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
|
||||
FiosGetFileList(fop, show_dirs, &FiosGetScenarioListCallback, subdir, file_list);
|
||||
}
|
||||
|
||||
std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation, const std::string &file, const std::string_view ext)
|
||||
@@ -570,9 +574,10 @@ std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation
|
||||
/**
|
||||
* Get a list of heightmaps.
|
||||
* @param fop Purpose of collecting the list.
|
||||
* @param show_dirs Whether to show directories.
|
||||
* @param file_list Destination of the found files.
|
||||
*/
|
||||
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
|
||||
void FiosGetHeightmapList(SaveLoadOperation fop, bool show_dirs, FileList &file_list)
|
||||
{
|
||||
static std::optional<std::string> fios_hmap_path;
|
||||
|
||||
@@ -582,7 +587,7 @@ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
|
||||
|
||||
std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
|
||||
Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
|
||||
FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
|
||||
FiosGetFileList(fop, show_dirs, &FiosGetHeightmapListCallback, subdir, file_list);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user