src/console_cmds.cpp
changeset 10808 d42b140d24f2
parent 10792 95b6eb9fcc9e
child 10818 95a5bba4e812
equal deleted inserted replaced
10807:e4e343679ed6 10808:d42b140d24f2
   213 	return true;
   213 	return true;
   214 }
   214 }
   215 
   215 
   216 static const FiosItem* GetFiosItem(const char* file)
   216 static const FiosItem* GetFiosItem(const char* file)
   217 {
   217 {
   218 	int i;
       
   219 
       
   220 	_saveload_mode = SLD_LOAD_GAME;
   218 	_saveload_mode = SLD_LOAD_GAME;
   221 	BuildFileList();
   219 	BuildFileList();
   222 
   220 
   223 	for (i = 0; i < _fios_num; i++) {
   221 	for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
   224 		if (strcmp(file, _fios_list[i].name) == 0) break;
   222 		if (strcmp(file, item->name) == 0) return item;
   225 		if (strcmp(file, _fios_list[i].title) == 0) break;
   223 		if (strcmp(file, item->title) == 0) return item;
   226 	}
   224 	}
   227 
   225 
   228 	if (i == _fios_num) { // If no name matches, try to parse it as number
   226 	/* If no name matches, try to parse it as number */
   229 		char* endptr;
   227 	char *endptr;
   230 
   228 	int i = strtol(file, &endptr, 10);
   231 		i = strtol(file, &endptr, 10);
   229 	if (file == endptr || *endptr != '\0') i = -1;
   232 		if (file == endptr || *endptr != '\0') i = -1;
   230 
   233 	}
   231 	return IsInsideMM(i, 0, _fios_items.Length()) ? _fios_items.Get(i) : NULL;
   234 
       
   235 	return IsInsideMM(i, 0, _fios_num) ? &_fios_list[i] : NULL;
       
   236 }
   232 }
   237 
   233 
   238 
   234 
   239 DEF_CONSOLE_CMD(ConLoad)
   235 DEF_CONSOLE_CMD(ConLoad)
   240 {
   236 {
   241 	const FiosItem *item;
       
   242 	const char *file;
       
   243 
       
   244 	if (argc == 0) {
   237 	if (argc == 0) {
   245 		IConsoleHelp("Load a game by name or index. Usage: 'load <file | number>'");
   238 		IConsoleHelp("Load a game by name or index. Usage: 'load <file | number>'");
   246 		return true;
   239 		return true;
   247 	}
   240 	}
   248 
   241 
   249 	if (argc != 2) return false;
   242 	if (argc != 2) return false;
   250 
   243 
   251 	file = argv[1];
   244 	const char *file = argv[1];
   252 	item = GetFiosItem(file);
   245 	const FiosItem *item = GetFiosItem(file);
   253 	if (item != NULL) {
   246 	if (item != NULL) {
   254 		switch (item->type) {
   247 		switch (item->type) {
   255 			case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
   248 			case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
   256 				_switch_mode = SM_LOAD;
   249 				_switch_mode = SM_LOAD;
   257 				SetFiosType(item->type);
   250 				SetFiosType(item->type);
   270 }
   263 }
   271 
   264 
   272 
   265 
   273 DEF_CONSOLE_CMD(ConRemove)
   266 DEF_CONSOLE_CMD(ConRemove)
   274 {
   267 {
   275 	const FiosItem* item;
       
   276 	const char* file;
       
   277 
       
   278 	if (argc == 0) {
   268 	if (argc == 0) {
   279 		IConsoleHelp("Remove a savegame by name or index. Usage: 'rm <file | number>'");
   269 		IConsoleHelp("Remove a savegame by name or index. Usage: 'rm <file | number>'");
   280 		return true;
   270 		return true;
   281 	}
   271 	}
   282 
   272 
   283 	if (argc != 2) return false;
   273 	if (argc != 2) return false;
   284 
   274 
   285 	file = argv[1];
   275 	const char *file = argv[1];
   286 	item = GetFiosItem(file);
   276 	const FiosItem *item = GetFiosItem(file);
   287 	if (item != NULL) {
   277 	if (item != NULL) {
   288 		if (!FiosDelete(item->name))
   278 		if (!FiosDelete(item->name))
   289 			IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
   279 			IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
   290 	} else {
   280 	} else {
   291 		IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
   281 		IConsolePrintF(CC_ERROR, "%s: No such file or directory.", file);
   297 
   287 
   298 
   288 
   299 /* List all the files in the current dir via console */
   289 /* List all the files in the current dir via console */
   300 DEF_CONSOLE_CMD(ConListFiles)
   290 DEF_CONSOLE_CMD(ConListFiles)
   301 {
   291 {
   302 	int i;
       
   303 
       
   304 	if (argc == 0) {
   292 	if (argc == 0) {
   305 		IConsoleHelp("List all loadable savegames and directories in the current dir via console. Usage: 'ls | dir'");
   293 		IConsoleHelp("List all loadable savegames and directories in the current dir via console. Usage: 'ls | dir'");
   306 		return true;
   294 		return true;
   307 	}
   295 	}
   308 
   296 
   309 	BuildFileList();
   297 	BuildFileList();
   310 
   298 
   311 	for (i = 0; i < _fios_num; i++) {
   299 	for (uint i = 0; i < _fios_items.Length(); i++) {
   312 		const FiosItem *item = &_fios_list[i];
   300 		IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title);
   313 		IConsolePrintF(CC_DEFAULT, "%d) %s", i, item->title);
       
   314 	}
   301 	}
   315 
   302 
   316 	FiosFreeSavegameList();
   303 	FiosFreeSavegameList();
   317 	return true;
   304 	return true;
   318 }
   305 }
   319 
   306 
   320 /* Change the dir via console */
   307 /* Change the dir via console */
   321 DEF_CONSOLE_CMD(ConChangeDirectory)
   308 DEF_CONSOLE_CMD(ConChangeDirectory)
   322 {
   309 {
   323 	const FiosItem *item;
       
   324 	const char *file;
       
   325 
       
   326 	if (argc == 0) {
   310 	if (argc == 0) {
   327 		IConsoleHelp("Change the dir via console. Usage: 'cd <directory | number>'");
   311 		IConsoleHelp("Change the dir via console. Usage: 'cd <directory | number>'");
   328 		return true;
   312 		return true;
   329 	}
   313 	}
   330 
   314 
   331 	if (argc != 2) return false;
   315 	if (argc != 2) return false;
   332 
   316 
   333 	file = argv[1];
   317 	const char *file = argv[1];
   334 	item = GetFiosItem(file);
   318 	const FiosItem *item = GetFiosItem(file);
   335 	if (item != NULL) {
   319 	if (item != NULL) {
   336 		switch (item->type) {
   320 		switch (item->type) {
   337 			case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
   321 			case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
   338 				FiosBrowseTo(item);
   322 				FiosBrowseTo(item);
   339 				break;
   323 				break;