mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-12 01:24:54 +00:00
(svn r27644) -Codechange: Split GetFiosItem into BuildFileList and FindItem, and move both to FileList.
This commit is contained in:
parent
95bb103a23
commit
d6cd3b1605
@ -315,42 +315,6 @@ DEF_CONSOLE_CMD(ConSaveConfig)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get savegame file informations.
|
|
||||||
* @param file The savegame filename to return information about. Can be the actual name
|
|
||||||
* or a numbered entry into the filename list.
|
|
||||||
* @return FiosItem The information on the file.
|
|
||||||
*/
|
|
||||||
static const FiosItem *GetFiosItem(const char *file)
|
|
||||||
{
|
|
||||||
_saveload_mode = SLD_LOAD_GAME;
|
|
||||||
BuildFileList(_saveload_mode);
|
|
||||||
|
|
||||||
for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
|
|
||||||
if (strcmp(file, item->name) == 0) return item;
|
|
||||||
if (strcmp(file, item->title) == 0) return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If no name matches, try to parse it as number */
|
|
||||||
char *endptr;
|
|
||||||
int i = strtol(file, &endptr, 10);
|
|
||||||
if (file == endptr || *endptr != '\0') i = -1;
|
|
||||||
|
|
||||||
if (IsInsideMM(i, 0, _fios_items.Length())) return _fios_items.Get(i);
|
|
||||||
|
|
||||||
/* As a last effort assume it is an OpenTTD savegame and
|
|
||||||
* that the ".sav" part was not given. */
|
|
||||||
char long_file[MAX_PATH];
|
|
||||||
seprintf(long_file, lastof(long_file), "%s.sav", file);
|
|
||||||
for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
|
|
||||||
if (strcmp(long_file, item->name) == 0) return item;
|
|
||||||
if (strcmp(long_file, item->title) == 0) return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
DEF_CONSOLE_CMD(ConLoad)
|
DEF_CONSOLE_CMD(ConLoad)
|
||||||
{
|
{
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
@ -361,7 +325,8 @@ DEF_CONSOLE_CMD(ConLoad)
|
|||||||
if (argc != 2) return false;
|
if (argc != 2) return false;
|
||||||
|
|
||||||
const char *file = argv[1];
|
const char *file = argv[1];
|
||||||
const FiosItem *item = GetFiosItem(file);
|
_fios_items.BuildFileList(SLD_LOAD_GAME);
|
||||||
|
const FiosItem *item = _fios_items.FindItem(file);
|
||||||
if (item != NULL) {
|
if (item != NULL) {
|
||||||
switch (item->type) {
|
switch (item->type) {
|
||||||
case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
|
case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
|
||||||
@ -393,7 +358,8 @@ DEF_CONSOLE_CMD(ConRemove)
|
|||||||
if (argc != 2) return false;
|
if (argc != 2) return false;
|
||||||
|
|
||||||
const char *file = argv[1];
|
const char *file = argv[1];
|
||||||
const FiosItem *item = GetFiosItem(file);
|
_fios_items.BuildFileList(SLD_LOAD_GAME);
|
||||||
|
const FiosItem *item = _fios_items.FindItem(file);
|
||||||
if (item != NULL) {
|
if (item != NULL) {
|
||||||
if (!FiosDelete(item->name)) {
|
if (!FiosDelete(item->name)) {
|
||||||
IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
|
IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
|
||||||
@ -415,8 +381,7 @@ DEF_CONSOLE_CMD(ConListFiles)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BuildFileList(_saveload_mode);
|
_fios_items.BuildFileList(_saveload_mode);
|
||||||
|
|
||||||
for (uint i = 0; i < _fios_items.Length(); i++) {
|
for (uint i = 0; i < _fios_items.Length(); i++) {
|
||||||
IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title);
|
IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title);
|
||||||
}
|
}
|
||||||
@ -436,7 +401,8 @@ DEF_CONSOLE_CMD(ConChangeDirectory)
|
|||||||
if (argc != 2) return false;
|
if (argc != 2) return false;
|
||||||
|
|
||||||
const char *file = argv[1];
|
const char *file = argv[1];
|
||||||
const FiosItem *item = GetFiosItem(file);
|
_fios_items.BuildFileList(SLD_LOAD_GAME);
|
||||||
|
const FiosItem *item = _fios_items.FindItem(file);
|
||||||
if (item != NULL) {
|
if (item != NULL) {
|
||||||
switch (item->type) {
|
switch (item->type) {
|
||||||
case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
|
case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
|
||||||
|
52
src/fios.cpp
52
src/fios.cpp
@ -69,6 +69,58 @@ FileList::~FileList()
|
|||||||
this->Clear();
|
this->Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a file list containing file appropriate for the specified \a mode.
|
||||||
|
* @param mode Kind of files required in the list.
|
||||||
|
*/
|
||||||
|
void FileList::BuildFileList(SaveLoadDialogMode mode)
|
||||||
|
{
|
||||||
|
this->Clear();
|
||||||
|
|
||||||
|
switch (mode) {
|
||||||
|
case SLD_LOAD_SCENARIO:
|
||||||
|
case SLD_SAVE_SCENARIO:
|
||||||
|
FiosGetScenarioList(mode, *this); break;
|
||||||
|
case SLD_SAVE_HEIGHTMAP:
|
||||||
|
case SLD_LOAD_HEIGHTMAP:
|
||||||
|
FiosGetHeightmapList(mode, *this); break;
|
||||||
|
|
||||||
|
default: FiosGetSavegameList(mode, *this); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find file information of a file by its name from the file list.
|
||||||
|
* @param file The filename to return information about. Can be the actual name
|
||||||
|
* or a numbered entry into the filename list.
|
||||||
|
* @return The information on the file, or \c NULL if the file is not available.
|
||||||
|
*/
|
||||||
|
const FiosItem *FileList::FindItem(const char *file)
|
||||||
|
{
|
||||||
|
for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
|
||||||
|
if (strcmp(file, item->name) == 0) return item;
|
||||||
|
if (strcmp(file, item->title) == 0) return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If no name matches, try to parse it as number */
|
||||||
|
char *endptr;
|
||||||
|
int i = strtol(file, &endptr, 10);
|
||||||
|
if (file == endptr || *endptr != '\0') i = -1;
|
||||||
|
|
||||||
|
if (IsInsideMM(i, 0, this->Length())) return this->Get(i);
|
||||||
|
|
||||||
|
/* As a last effort assume it is an OpenTTD savegame and
|
||||||
|
* that the ".sav" part was not given. */
|
||||||
|
char long_file[MAX_PATH];
|
||||||
|
seprintf(long_file, lastof(long_file), "%s.sav", file);
|
||||||
|
for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
|
||||||
|
if (strcmp(long_file, item->name) == 0) return item;
|
||||||
|
if (strcmp(long_file, item->title) == 0) return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get descriptive texts. Returns the path and free space
|
* Get descriptive texts. Returns the path and free space
|
||||||
* left on the device
|
* left on the device
|
||||||
|
@ -200,6 +200,9 @@ public:
|
|||||||
this->files.Compact();
|
this->files.Compact();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BuildFileList(SaveLoadDialogMode mode);
|
||||||
|
const FiosItem *FindItem(const char *file);
|
||||||
|
|
||||||
SmallVector<FiosItem, 32> files; ///< The list of files.
|
SmallVector<FiosItem, 32> files; ///< The list of files.
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -235,6 +238,4 @@ int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b);
|
|||||||
|
|
||||||
extern const TextColour _fios_colours[];
|
extern const TextColour _fios_colours[];
|
||||||
|
|
||||||
void BuildFileList(SaveLoadDialogMode mode);
|
|
||||||
|
|
||||||
#endif /* FIOS_H */
|
#endif /* FIOS_H */
|
||||||
|
@ -193,26 +193,6 @@ const TextColour _fios_colours[] = {
|
|||||||
TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW
|
TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW
|
||||||
};
|
};
|
||||||
|
|
||||||
void BuildFileList(SaveLoadDialogMode mode)
|
|
||||||
{
|
|
||||||
_fios_path_changed = true;
|
|
||||||
_fios_items.Clear();
|
|
||||||
|
|
||||||
switch (mode) {
|
|
||||||
case SLD_LOAD_SCENARIO:
|
|
||||||
case SLD_SAVE_SCENARIO:
|
|
||||||
FiosGetScenarioList(mode, _fios_items); break;
|
|
||||||
case SLD_SAVE_HEIGHTMAP:
|
|
||||||
case SLD_LOAD_HEIGHTMAP:
|
|
||||||
FiosGetHeightmapList(mode, _fios_items); break;
|
|
||||||
|
|
||||||
default: FiosGetSavegameList(mode, _fios_items); break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Invalidate saveload window */
|
|
||||||
InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MakeSortedSaveGameList()
|
static void MakeSortedSaveGameList()
|
||||||
{
|
{
|
||||||
uint sort_start = 0;
|
uint sort_start = 0;
|
||||||
@ -683,7 +663,10 @@ public:
|
|||||||
this->selected = NULL;
|
this->selected = NULL;
|
||||||
_load_check_data.Clear();
|
_load_check_data.Clear();
|
||||||
if (!gui_scope) break;
|
if (!gui_scope) break;
|
||||||
BuildFileList(_saveload_mode);
|
|
||||||
|
_fios_path_changed = true;
|
||||||
|
_fios_items.BuildFileList(_saveload_mode);
|
||||||
|
InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
|
||||||
/* FALL THROUGH */
|
/* FALL THROUGH */
|
||||||
case 1:
|
case 1:
|
||||||
/* Selection changes */
|
/* Selection changes */
|
||||||
|
Loading…
Reference in New Issue
Block a user