(svn r2085) Improve browsing via console:
- change directory/load map via filename (number is also possible) - add command "pwd" to Print the current Working Directory - rename "list_files" to "ls" (and add alias "dir") - rename "goto_dir" to "cd" - loading of TTD maps via "load" is now possible - switching of drives via "cd" (only applies to systems which have a drive concept) is now possible - "scan_files" is now obsolete and was removed This is still far from perfect, mostly because the way OTTD handles files is Broken By Design(tm), but should make file navigation via console a bit easier
This commit is contained in:
		
							
								
								
									
										191
									
								
								console_cmds.c
									
									
									
									
									
								
							
							
						
						
									
										191
									
								
								console_cmds.c
									
									
									
									
									
								
							@@ -174,152 +174,127 @@ DEF_CONSOLE_CMD(ConSave)
 | 
				
			|||||||
	return NULL;
 | 
						return NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Load a file-number from current dir */
 | 
					
 | 
				
			||||||
static void LoadMap(uint no)
 | 
					static const FiosItem* GetFiosItem(const char* file)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	/* Build file list */
 | 
						int i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	BuildFileList();
 | 
						BuildFileList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Check if in range */
 | 
						for (i = 0; i < _fios_num; i++) {
 | 
				
			||||||
	if (no != 0 && no <= (uint)_fios_num) {
 | 
							if (strcmp(file, _fios_list[i].name) == 0) break;
 | 
				
			||||||
		const FiosItem *item = &_fios_list[no - 1];
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (item->type == FIOS_TYPE_FILE) {
 | 
						if (i == _fios_num) { /* If no name matches, try to parse it as number */
 | 
				
			||||||
			/* Load the file */
 | 
							char* endptr;
 | 
				
			||||||
			_switch_mode = SM_LOAD;
 | 
					 | 
				
			||||||
			SetFiosType(item->type);
 | 
					 | 
				
			||||||
			strcpy(_file_to_saveload.name, FiosBrowseTo(item));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
			IConsolePrint(_iconsole_color_default, "Loading map...");
 | 
							i = strtol(file, &endptr, 10);
 | 
				
			||||||
		} else
 | 
							if (file == endptr || *endptr != '\0') i = -1;
 | 
				
			||||||
			IConsolePrint(_iconsole_color_error, "That is not a map.");
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	} else /* Show usages */
 | 
						return IS_INT_INSIDE(i, 0, _fios_num) ? &_fios_list[i] : NULL;
 | 
				
			||||||
		IConsolePrint(_iconsole_color_error, "Unknown map. Use 'list_files' and 'goto_dir' to find the numbers of the savegame.");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/* Free the file-list */
 | 
					 | 
				
			||||||
	FiosFreeSavegameList();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Load a file from a map */
 | 
					
 | 
				
			||||||
DEF_CONSOLE_CMD(ConLoad)
 | 
					DEF_CONSOLE_CMD(ConLoad)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	/* We need 1 argument */
 | 
						const FiosItem* item;
 | 
				
			||||||
	if (argc == 2) {
 | 
						const char* file;
 | 
				
			||||||
		/* Load the map */
 | 
					
 | 
				
			||||||
		LoadMap(atoi(argv[1]));
 | 
						if (argc != 2) {
 | 
				
			||||||
 | 
							IConsolePrint(_iconsole_color_default, "Usage: load <file | number>");
 | 
				
			||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Give usage */
 | 
						file = argv[1];
 | 
				
			||||||
	IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: load <file-no>");
 | 
						item = GetFiosItem(file);
 | 
				
			||||||
 | 
						if (item != NULL) {
 | 
				
			||||||
 | 
							switch (item->type) {
 | 
				
			||||||
 | 
								case FIOS_TYPE_FILE:
 | 
				
			||||||
 | 
								case FIOS_TYPE_OLDFILE:
 | 
				
			||||||
 | 
									_switch_mode = SM_LOAD;
 | 
				
			||||||
 | 
									SetFiosType(item->type);
 | 
				
			||||||
 | 
									strcpy(_file_to_saveload.name, FiosBrowseTo(item));
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									IConsolePrintF(_iconsole_color_error, "%s: Not a map.", file);
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							IConsolePrintF(_iconsole_color_error, "%s: No such file or directory.",
 | 
				
			||||||
 | 
								file);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						FiosFreeSavegameList();
 | 
				
			||||||
	return NULL;
 | 
						return NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* List all the files in the current dir via console */
 | 
					/* List all the files in the current dir via console */
 | 
				
			||||||
DEF_CONSOLE_CMD(ConListFiles)
 | 
					DEF_CONSOLE_CMD(ConListFiles)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	const FiosItem *item;
 | 
						int i;
 | 
				
			||||||
	int pos = 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Build the file-list */
 | 
					 | 
				
			||||||
	BuildFileList();
 | 
						BuildFileList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* As long as we have files */
 | 
						for (i = 0; i < _fios_num; i++) {
 | 
				
			||||||
	while (pos < _fios_num) {
 | 
							const FiosItem* item = &_fios_list[i];
 | 
				
			||||||
		item = _fios_list + pos;
 | 
					
 | 
				
			||||||
		pos++;
 | 
							IConsolePrintF(_iconsole_color_default, "%d) %s",
 | 
				
			||||||
		/* Show them */
 | 
								i, item->title[0] != '\0' ? item->title : item->name);
 | 
				
			||||||
		IConsolePrintF(_iconsole_color_default, "%d) %s", pos, item->title[0] ? item->title : item->name);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Destroy the file list */
 | 
					 | 
				
			||||||
	FiosFreeSavegameList();
 | 
						FiosFreeSavegameList();
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return NULL;
 | 
						return NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Get an Specific file */
 | 
					 | 
				
			||||||
DEF_CONSOLE_CMD(ConScanFiles)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	const FiosItem *item;
 | 
					 | 
				
			||||||
	int pos = 0;
 | 
					 | 
				
			||||||
	_iconsole_var* result;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	result = IConsoleVarAlloc(ICONSOLE_VAR_STRING);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (argc <= 1) {
 | 
					 | 
				
			||||||
		IConsoleVarSetString(result, "0");
 | 
					 | 
				
			||||||
		return result; // return an zero
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/* Build the file-list */
 | 
					 | 
				
			||||||
	BuildFileList();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/* As long as we have files */
 | 
					 | 
				
			||||||
	while (pos < _fios_num) {
 | 
					 | 
				
			||||||
		item = _fios_list + pos;
 | 
					 | 
				
			||||||
		pos++;
 | 
					 | 
				
			||||||
		if (strcmp(argv[1], "..") == 0) {
 | 
					 | 
				
			||||||
			if (item->type == FIOS_TYPE_PARENT) {
 | 
					 | 
				
			||||||
				// huh we are searching for the parent directory
 | 
					 | 
				
			||||||
				char buffer[10];
 | 
					 | 
				
			||||||
				sprintf(buffer, "%d", pos);
 | 
					 | 
				
			||||||
				IConsoleVarSetString(result, buffer);
 | 
					 | 
				
			||||||
				return result;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		} else
 | 
					 | 
				
			||||||
			// file records ?
 | 
					 | 
				
			||||||
			if (item->type == FIOS_TYPE_FILE) {
 | 
					 | 
				
			||||||
				if (strcmp(argv[1], item->name) == 0) {
 | 
					 | 
				
			||||||
					char buffer[10];
 | 
					 | 
				
			||||||
					sprintf(buffer, "%d", pos);
 | 
					 | 
				
			||||||
					IConsoleVarSetString(result, buffer);
 | 
					 | 
				
			||||||
					return result;
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/* Destroy the file list */
 | 
					 | 
				
			||||||
	FiosFreeSavegameList();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return NULL;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Change the dir via console */
 | 
					/* Change the dir via console */
 | 
				
			||||||
DEF_CONSOLE_CMD(ConGotoDir)
 | 
					DEF_CONSOLE_CMD(ConChangeDirectory)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	char *file;
 | 
						const FiosItem* item;
 | 
				
			||||||
	int no;
 | 
						const char* file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* We need 1 argument */
 | 
					 | 
				
			||||||
	if (argc != 2) {
 | 
						if (argc != 2) {
 | 
				
			||||||
		IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: goto_dir <dir-no>");
 | 
							IConsolePrint(_iconsole_color_default, "Usage: cd <directory | number>");
 | 
				
			||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	no = atoi(argv[1]);
 | 
						file = argv[1];
 | 
				
			||||||
 | 
						item = GetFiosItem(file);
 | 
				
			||||||
 | 
						if (item != NULL) {
 | 
				
			||||||
 | 
							switch (item->type) {
 | 
				
			||||||
 | 
								case FIOS_TYPE_DIR:
 | 
				
			||||||
 | 
								case FIOS_TYPE_DRIVE:
 | 
				
			||||||
 | 
								case FIOS_TYPE_PARENT:
 | 
				
			||||||
 | 
									FiosBrowseTo(item);
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Make the file list */
 | 
								default:
 | 
				
			||||||
	BuildFileList();
 | 
									IConsolePrintF(_iconsole_color_error, "%s: Not a directory.", file);
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
	/* Check if we are in range */
 | 
					 | 
				
			||||||
	if (no != 0 && no <= _fios_num) {
 | 
					 | 
				
			||||||
		const FiosItem *item = &_fios_list[no - 1];
 | 
					 | 
				
			||||||
		/* Only DIR and PARENT we do allow here */
 | 
					 | 
				
			||||||
		if (item->type == FIOS_TYPE_DIR || item->type == FIOS_TYPE_PARENT) {
 | 
					 | 
				
			||||||
			/* Goto the map */
 | 
					 | 
				
			||||||
			file = FiosBrowseTo(item);
 | 
					 | 
				
			||||||
			FiosFreeSavegameList();
 | 
					 | 
				
			||||||
			return NULL;
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							IConsolePrintF(_iconsole_color_error, "%s: No such file or directory.",
 | 
				
			||||||
 | 
								file);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Report error */
 | 
						FiosFreeSavegameList();
 | 
				
			||||||
	IConsolePrint(_iconsole_color_default, "That number is no directory.");
 | 
						return NULL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					DEF_CONSOLE_CMD(ConPrintWorkingDirectory)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char* path;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// XXX Workaround for broken file handling
 | 
				
			||||||
 | 
						FiosGetSavegameList(&_fios_num, SLD_LOAD_GAME);
 | 
				
			||||||
	FiosFreeSavegameList();
 | 
						FiosFreeSavegameList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						FiosGetDescText(&path);
 | 
				
			||||||
 | 
						IConsolePrint(_iconsole_color_default, path);
 | 
				
			||||||
	return NULL;
 | 
						return NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1270,13 +1245,13 @@ void IConsoleStdLibRegister(void)
 | 
				
			|||||||
	IConsoleCmdRegister("alias",		ConAlias);
 | 
						IConsoleCmdRegister("alias",		ConAlias);
 | 
				
			||||||
	IConsoleCmdRegister("load",			ConLoad);
 | 
						IConsoleCmdRegister("load",			ConLoad);
 | 
				
			||||||
	IConsoleCmdRegister("save",			ConSave);
 | 
						IConsoleCmdRegister("save",			ConSave);
 | 
				
			||||||
	IConsoleCmdRegister("list_files", ConListFiles);
 | 
						IConsoleCmdRegister("ls", ConListFiles);
 | 
				
			||||||
	IConsoleCmdRegister("scan_files", ConScanFiles);
 | 
						IConsoleCmdRegister("cd", ConChangeDirectory);
 | 
				
			||||||
	IConsoleCmdRegister("goto_dir", ConGotoDir);
 | 
						IConsoleCmdRegister("pwd", ConPrintWorkingDirectory);
 | 
				
			||||||
 | 
						IConsoleAliasRegister("dir", "ls");
 | 
				
			||||||
	IConsoleAliasRegister("new_game", "newgame");
 | 
						IConsoleAliasRegister("new_game", "newgame");
 | 
				
			||||||
	IConsoleAliasRegister("newmap", "newgame");
 | 
						IConsoleAliasRegister("newmap", "newgame");
 | 
				
			||||||
	IConsoleAliasRegister("new_map", "newgame");
 | 
						IConsoleAliasRegister("new_map", "newgame");
 | 
				
			||||||
	IConsoleAliasRegister("load_game", "temp_string << scan_files %!;load temp_string");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	IConsoleVarRegister("developer", &_stdlib_developer, ICONSOLE_VAR_BYTE);
 | 
						IConsoleVarRegister("developer", &_stdlib_developer, ICONSOLE_VAR_BYTE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user