Merge branch 'save_ext' into jgrpp
# Conflicts: # config.lib # src/saveload/extended_ver_sl.cpp # src/saveload/saveload.cpp
This commit is contained in:
@@ -48,6 +48,38 @@
|
||||
/* scriptfile handling */
|
||||
static bool _script_running; ///< Script is running (used to abort execution when #ConReturn is encountered).
|
||||
|
||||
/** File list storage for the console, for caching the last 'ls' command. */
|
||||
class ConsoleFileList : public FileList {
|
||||
public:
|
||||
ConsoleFileList() : FileList()
|
||||
{
|
||||
this->file_list_valid = false;
|
||||
}
|
||||
|
||||
/** Declare the file storage cache as being invalid, also clears all stored files. */
|
||||
void InvalidateFileList()
|
||||
{
|
||||
this->Clear();
|
||||
this->file_list_valid = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* (Re-)validate the file storage cache. Only makes a change if the storage was invalid, or if \a force_reload.
|
||||
* @param Always reload the file storage cache.
|
||||
*/
|
||||
void ValidateFileList(bool force_reload = false)
|
||||
{
|
||||
if (force_reload || !this->file_list_valid) {
|
||||
this->BuildFileList(FT_SAVEGAME, SLO_LOAD);
|
||||
this->file_list_valid = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool file_list_valid; ///< If set, the file list is valid.
|
||||
};
|
||||
|
||||
static ConsoleFileList _console_file_list; ///< File storage cache for the console.
|
||||
|
||||
/* console command defines */
|
||||
#define DEF_CONSOLE_CMD(function) static bool function(byte argc, char *argv[])
|
||||
#define DEF_CONSOLE_HOOK(function) static ConsoleHookResult function(bool echo)
|
||||
@@ -288,7 +320,7 @@ DEF_CONSOLE_CMD(ConSave)
|
||||
char *filename = str_fmt("%s.sav", argv[1]);
|
||||
IConsolePrint(CC_DEFAULT, "Saving map...");
|
||||
|
||||
if (SaveOrLoad(filename, SL_SAVE, SAVE_DIR) != SL_OK) {
|
||||
if (SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, SAVE_DIR) != SL_OK) {
|
||||
IConsolePrint(CC_ERROR, "Saving map failed");
|
||||
} else {
|
||||
IConsolePrintF(CC_DEFAULT, "Map successfully saved to %s", filename);
|
||||
@@ -317,42 +349,6 @@ DEF_CONSOLE_CMD(ConSaveConfig)
|
||||
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();
|
||||
|
||||
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)
|
||||
{
|
||||
if (argc == 0) {
|
||||
@@ -363,24 +359,21 @@ DEF_CONSOLE_CMD(ConLoad)
|
||||
if (argc != 2) return false;
|
||||
|
||||
const char *file = argv[1];
|
||||
const FiosItem *item = GetFiosItem(file);
|
||||
_console_file_list.ValidateFileList();
|
||||
const FiosItem *item = _console_file_list.FindItem(file);
|
||||
if (item != NULL) {
|
||||
switch (item->type) {
|
||||
case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
|
||||
_switch_mode = SM_LOAD_GAME;
|
||||
SetFiosType(item->type);
|
||||
|
||||
strecpy(_file_to_saveload.name, FiosBrowseTo(item), lastof(_file_to_saveload.name));
|
||||
strecpy(_file_to_saveload.title, item->title, lastof(_file_to_saveload.title));
|
||||
break;
|
||||
}
|
||||
default: IConsolePrintF(CC_ERROR, "%s: Not a savegame.", file);
|
||||
if (GetAbstractFileType(item->type) == FT_SAVEGAME) {
|
||||
_switch_mode = SM_LOAD_GAME;
|
||||
_file_to_saveload.SetMode(item->type);
|
||||
_file_to_saveload.SetName(FiosBrowseTo(item));
|
||||
_file_to_saveload.SetTitle(item->title);
|
||||
} else {
|
||||
IConsolePrintF(CC_ERROR, "%s: Not a savegame.", file);
|
||||
}
|
||||
} else {
|
||||
IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
|
||||
}
|
||||
|
||||
FiosFreeSavegameList();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -395,7 +388,8 @@ DEF_CONSOLE_CMD(ConRemove)
|
||||
if (argc != 2) return false;
|
||||
|
||||
const char *file = argv[1];
|
||||
const FiosItem *item = GetFiosItem(file);
|
||||
_console_file_list.ValidateFileList();
|
||||
const FiosItem *item = _console_file_list.FindItem(file);
|
||||
if (item != NULL) {
|
||||
if (!FiosDelete(item->name)) {
|
||||
IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
|
||||
@@ -404,7 +398,7 @@ DEF_CONSOLE_CMD(ConRemove)
|
||||
IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
|
||||
}
|
||||
|
||||
FiosFreeSavegameList();
|
||||
_console_file_list.InvalidateFileList();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -417,13 +411,11 @@ DEF_CONSOLE_CMD(ConListFiles)
|
||||
return true;
|
||||
}
|
||||
|
||||
BuildFileList();
|
||||
|
||||
for (uint i = 0; i < _fios_items.Length(); i++) {
|
||||
IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title);
|
||||
_console_file_list.ValidateFileList(true);
|
||||
for (uint i = 0; i < _console_file_list.Length(); i++) {
|
||||
IConsolePrintF(CC_DEFAULT, "%d) %s", i, _console_file_list[i].title);
|
||||
}
|
||||
|
||||
FiosFreeSavegameList();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -438,7 +430,8 @@ DEF_CONSOLE_CMD(ConChangeDirectory)
|
||||
if (argc != 2) return false;
|
||||
|
||||
const char *file = argv[1];
|
||||
const FiosItem *item = GetFiosItem(file);
|
||||
_console_file_list.ValidateFileList(true);
|
||||
const FiosItem *item = _console_file_list.FindItem(file);
|
||||
if (item != NULL) {
|
||||
switch (item->type) {
|
||||
case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
|
||||
@@ -450,7 +443,7 @@ DEF_CONSOLE_CMD(ConChangeDirectory)
|
||||
IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
|
||||
}
|
||||
|
||||
FiosFreeSavegameList();
|
||||
_console_file_list.InvalidateFileList();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -464,8 +457,8 @@ DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
|
||||
}
|
||||
|
||||
/* XXX - Workaround for broken file handling */
|
||||
FiosGetSavegameList(SLD_LOAD_GAME);
|
||||
FiosFreeSavegameList();
|
||||
_console_file_list.ValidateFileList(true);
|
||||
_console_file_list.InvalidateFileList();
|
||||
|
||||
FiosGetDescText(&path, NULL);
|
||||
IConsolePrint(CC_DEFAULT, path);
|
||||
|
Reference in New Issue
Block a user