diff -r 1608018c5ff2 -r 55c8267c933f src/console_cmds.c --- a/src/console_cmds.c Thu Jan 11 13:16:26 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1622 +0,0 @@ -/* $Id$ */ - -#include "stdafx.h" -#include "openttd.h" -#include "console.h" -#include "debug.h" -#include "engine.h" -#include "functions.h" -#include "saveload.h" -#include "string.h" -#include "variables.h" -#include "network/network_data.h" -#include "network/network_client.h" -#include "network/network_server.h" -#include "network/network_udp.h" -#include "command.h" -#include "settings.h" -#include "fios.h" -#include "vehicle.h" -#include "station.h" -#include "strings.h" -#include "screenshot.h" -#include "genworld.h" -#include "date.h" -#include "network/network.h" - -// ** scriptfile handling ** // -static FILE *_script_file; -static bool _script_running; - -// ** console command / variable defines ** // -#define DEF_CONSOLE_CMD(function) static bool function(byte argc, char *argv[]) -#define DEF_CONSOLE_HOOK(function) static bool function(void) - - -/* **************************** */ -/* variable and command hooks */ -/* **************************** */ - -#ifdef ENABLE_NETWORK - -static inline bool NetworkAvailable(void) -{ - if (!_network_available) { - IConsoleError("You cannot use this command because there is no network available."); - return false; - } - return true; -} - -DEF_CONSOLE_HOOK(ConHookServerOnly) -{ - if (!NetworkAvailable()) return false; - - if (!_network_server) { - IConsoleError("This command/variable is only available to a network server."); - return false; - } - return true; -} - -DEF_CONSOLE_HOOK(ConHookClientOnly) -{ - if (!NetworkAvailable()) return false; - - if (_network_server) { - IConsoleError("This command/variable is not available to a network server."); - return false; - } - return true; -} - -DEF_CONSOLE_HOOK(ConHookNeedNetwork) -{ - if (!NetworkAvailable()) return false; - - if (!_networking) { - IConsoleError("Not connected. This command/variable is only available in multiplayer."); - return false; - } - return true; -} - -DEF_CONSOLE_HOOK(ConHookNoNetwork) -{ - if (_networking) { - IConsoleError("This command/variable is forbidden in multiplayer."); - return false; - } - return true; -} - -#endif /* ENABLE_NETWORK */ - -static void IConsoleHelp(const char *str) -{ - IConsolePrintF(_icolour_warn, "- %s", str); -} - -DEF_CONSOLE_CMD(ConResetEngines) -{ - if (argc == 0) { - IConsoleHelp("Reset status data of all engines. This might solve some issues with 'lost' engines. Usage: 'resetengines'"); - return true; - } - - StartupEngines(); - return true; -} - -#ifdef _DEBUG -DEF_CONSOLE_CMD(ConResetTile) -{ - if (argc == 0) { - IConsoleHelp("Reset a tile to bare land. Usage: 'resettile '"); - IConsoleHelp("Tile can be either decimal (34161) or hexadecimal (0x4a5B)"); - return true; - } - - if (argc == 2) { - uint32 result; - if (GetArgumentInteger(&result, argv[1])) { - DoClearSquare((TileIndex)result); - return true; - } - } - - return false; -} - -DEF_CONSOLE_CMD(ConStopAllVehicles) -{ - Vehicle* v; - if (argc == 0) { - IConsoleHelp("Stops all vehicles in the game. For debugging only! Use at your own risk... Usage: 'stopall'"); - return true; - } - - FOR_ALL_VEHICLES(v) { - /* Code ripped from CmdStartStopTrain. Can't call it, because of - * ownership problems, so we'll duplicate some code, for now */ - if (v->type == VEH_Train) - v->u.rail.days_since_order_progr = 0; - v->vehstatus |= VS_STOPPED; - InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); - InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); - } - return true; -} -#endif /* _DEBUG */ - -DEF_CONSOLE_CMD(ConScrollToTile) -{ - if (argc == 0) { - IConsoleHelp("Center the screen on a given tile. Usage: 'scrollto '"); - IConsoleHelp("Tile can be either decimal (34161) or hexadecimal (0x4a5B)"); - return true; - } - - if (argc == 2) { - uint32 result; - if (GetArgumentInteger(&result, argv[1])) { - if (result >= MapSize()) { - IConsolePrint(_icolour_err, "Tile does not exist"); - return true; - } - ScrollMainWindowToTile((TileIndex)result); - return true; - } - } - - return false; -} - -extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm); -extern void BuildFileList(void); -extern void SetFiosType(const byte fiostype); - -/* Save the map to a file */ -DEF_CONSOLE_CMD(ConSave) -{ - if (argc == 0) { - IConsoleHelp("Save the current game. Usage: 'save '"); - return true; - } - - if (argc == 2) { - char buf[200]; - - snprintf(buf, lengthof(buf), "%s%s%s.sav", _paths.save_dir, PATHSEP, argv[1]); - IConsolePrint(_icolour_def, "Saving map..."); - - if (SaveOrLoad(buf, SL_SAVE) != SL_OK) { - IConsolePrint(_icolour_err, "SaveMap failed"); - } else { - IConsolePrintF(_icolour_def, "Map sucessfully saved to %s", buf); - } - return true; - } - - return false; -} - -/* Explicitly save the configuration */ -DEF_CONSOLE_CMD(ConSaveConfig) -{ - if (argc == 0) { - IConsoleHelp("Saves the current config, typically to 'openttd.cfg'."); - return true; - } - - SaveToConfig(); - IConsolePrint(_icolour_def, "Saved config."); - return true; -} - -static const FiosItem* GetFiosItem(const char* file) -{ - int i; - - _saveload_mode = SLD_LOAD_GAME; - BuildFileList(); - - for (i = 0; i < _fios_num; i++) { - if (strcmp(file, _fios_list[i].name) == 0) break; - if (strcmp(file, _fios_list[i].title) == 0) break; - } - - if (i == _fios_num) { /* If no name matches, try to parse it as number */ - char* endptr; - - i = strtol(file, &endptr, 10); - if (file == endptr || *endptr != '\0') i = -1; - } - - return IS_INT_INSIDE(i, 0, _fios_num) ? &_fios_list[i] : NULL; -} - - -DEF_CONSOLE_CMD(ConLoad) -{ - const FiosItem *item; - const char *file; - - if (argc == 0) { - IConsoleHelp("Load a game by name or index. Usage: 'load '"); - return true; - } - - if (argc != 2) return false; - - file = argv[1]; - item = GetFiosItem(file); - if (item != NULL) { - switch (item->type) { - case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: { - _switch_mode = SM_LOAD; - SetFiosType(item->type); - - ttd_strlcpy(_file_to_saveload.name, FiosBrowseTo(item), sizeof(_file_to_saveload.name)); - ttd_strlcpy(_file_to_saveload.title, item->title, sizeof(_file_to_saveload.title)); - } break; - default: IConsolePrintF(_icolour_err, "%s: Not a savegame.", file); - } - } else { - IConsolePrintF(_icolour_err, "%s: No such file or directory.", file); - } - - FiosFreeSavegameList(); - return true; -} - - -DEF_CONSOLE_CMD(ConRemove) -{ - const FiosItem* item; - const char* file; - - if (argc == 0) { - IConsoleHelp("Remove a savegame by name or index. Usage: 'rm '"); - return true; - } - - if (argc != 2) return false; - - file = argv[1]; - item = GetFiosItem(file); - if (item != NULL) { - if (!FiosDelete(item->name)) - IConsolePrintF(_icolour_err, "%s: Failed to delete file", file); - } else { - IConsolePrintF(_icolour_err, "%s: No such file or directory.", file); - } - - FiosFreeSavegameList(); - return true; -} - - -/* List all the files in the current dir via console */ -DEF_CONSOLE_CMD(ConListFiles) -{ - int i; - - if (argc == 0) { - IConsoleHelp("List all loadable savegames and directories in the current dir via console. Usage: 'ls | dir'"); - return true; - } - - BuildFileList(); - - for (i = 0; i < _fios_num; i++) { - const FiosItem *item = &_fios_list[i]; - IConsolePrintF(_icolour_def, "%d) %s", i, item->title); - } - - FiosFreeSavegameList(); - return true; -} - -/* Change the dir via console */ -DEF_CONSOLE_CMD(ConChangeDirectory) -{ - const FiosItem *item; - const char *file; - - if (argc == 0) { - IConsoleHelp("Change the dir via console. Usage: 'cd '"); - return true; - } - - if (argc != 2) return false; - - 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; - default: IConsolePrintF(_icolour_err, "%s: Not a directory.", file); - } - } else { - IConsolePrintF(_icolour_err, "%s: No such file or directory.", file); - } - - FiosFreeSavegameList(); - return true; -} - -DEF_CONSOLE_CMD(ConPrintWorkingDirectory) -{ - const char *path; - - if (argc == 0) { - IConsoleHelp("Print out the current working directory. Usage: 'pwd'"); - return true; - } - - // XXX - Workaround for broken file handling - FiosGetSavegameList(SLD_LOAD_GAME); - FiosFreeSavegameList(); - - FiosGetDescText(&path, NULL); - IConsolePrint(_icolour_def, path); - return true; -} - -DEF_CONSOLE_CMD(ConClearBuffer) -{ - if (argc == 0) { - IConsoleHelp("Clear the console buffer. Usage: 'clear'"); - return true; - } - - IConsoleClearBuffer(); - InvalidateWindow(WC_CONSOLE, 0); - return true; -} - - -// ********************************* // -// * Network Core Console Commands * // -// ********************************* // -#ifdef ENABLE_NETWORK - -DEF_CONSOLE_CMD(ConBan) -{ - NetworkClientInfo *ci; - const char *banip = NULL; - uint32 index; - - if (argc == 0) { - IConsoleHelp("Ban a player from a network game. Usage: 'ban '"); - IConsoleHelp("For client-id's, see the command 'clients'"); - IConsoleHelp("If the client is no longer online, you can still ban his/her IP"); - return true; - } - - if (argc != 2) return false; - - if (strchr(argv[1], '.') == NULL) { // banning with ID - index = atoi(argv[1]); - ci = NetworkFindClientInfoFromIndex(index); - } else { // banning IP - ci = NetworkFindClientInfoFromIP(argv[1]); - if (ci == NULL) { - banip = argv[1]; - index = (uint32)-1; - } else { - index = ci->client_index; - } - } - - if (index == NETWORK_SERVER_INDEX) { - IConsoleError("Silly boy, you can not ban yourself!"); - return true; - } - - if (index == 0 || (ci == NULL && index != (uint32)-1)) { - IConsoleError("Invalid client"); - return true; - } - - if (ci != NULL) { - banip = inet_ntoa(*(struct in_addr *)&ci->client_ip); - SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED); - IConsolePrint(_icolour_def, "Client banned"); - } else { - IConsolePrint(_icolour_def, "Client not online, banned IP"); - } - - /* Add user to ban-list */ - for (index = 0; index < lengthof(_network_ban_list); index++) { - if (_network_ban_list[index] == NULL) { - _network_ban_list[index] = strdup(banip); - break; - } - } - - return true; -} - -DEF_CONSOLE_CMD(ConUnBan) -{ - uint i, index; - - if (argc == 0) { - IConsoleHelp("Unban a player from a network game. Usage: 'unban '"); - IConsoleHelp("For a list of banned IP's, see the command 'banlist'"); - return true; - } - - if (argc != 2) return false; - - index = (strchr(argv[1], '.') == NULL) ? atoi(argv[1]) : 0; - index--; - - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] == NULL) continue; - - if (strcmp(_network_ban_list[i], argv[1]) == 0 || index == i) { - free(_network_ban_list[i]); - _network_ban_list[i] = NULL; - IConsolePrint(_icolour_def, "IP unbanned."); - return true; - } - } - - IConsolePrint(_icolour_def, "IP not in ban-list."); - return true; -} - -DEF_CONSOLE_CMD(ConBanList) -{ - uint i; - - if (argc == 0) { - IConsoleHelp("List the IP's of banned clients: Usage 'banlist'"); - return true; - } - - IConsolePrint(_icolour_def, "Banlist: "); - - for (i = 0; i < lengthof(_network_ban_list); i++) { - if (_network_ban_list[i] != NULL) - IConsolePrintF(_icolour_def, " %d) %s", i + 1, _network_ban_list[i]); - } - - return true; -} - -DEF_CONSOLE_CMD(ConPauseGame) -{ - if (argc == 0) { - IConsoleHelp("Pause a network game. Usage: 'pause'"); - return true; - } - - if (_pause == 0) { - DoCommandP(0, 1, 0, NULL, CMD_PAUSE); - IConsolePrint(_icolour_def, "Game paused."); - } else { - IConsolePrint(_icolour_def, "Game is already paused."); - } - - return true; -} - -DEF_CONSOLE_CMD(ConUnPauseGame) -{ - if (argc == 0) { - IConsoleHelp("Unpause a network game. Usage: 'unpause'"); - return true; - } - - if (_pause != 0) { - DoCommandP(0, 0, 0, NULL, CMD_PAUSE); - IConsolePrint(_icolour_def, "Game unpaused."); - } else { - IConsolePrint(_icolour_def, "Game is already unpaused."); - } - - return true; -} - -DEF_CONSOLE_CMD(ConRcon) -{ - if (argc == 0) { - IConsoleHelp("Remote control the server from another client. Usage: 'rcon '"); - IConsoleHelp("Remember to enclose the command in quotes, otherwise only the first parameter is sent"); - return true; - } - - if (argc < 3) return false; - - SEND_COMMAND(PACKET_CLIENT_RCON)(argv[1], argv[2]); - return true; -} - -DEF_CONSOLE_CMD(ConStatus) -{ - static const char* const stat_str[] = { - "inactive", - "authorized", - "waiting", - "loading map", - "map done", - "ready", - "active" - }; - - const NetworkClientState *cs; - - if (argc == 0) { - IConsoleHelp("List the status of all clients connected to the server. Usage 'status'"); - return true; - } - - FOR_ALL_CLIENTS(cs) { - int lag = NetworkCalculateLag(cs); - const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); - const char* status; - - status = (cs->status < lengthof(stat_str) ? stat_str[cs->status] : "unknown"); - IConsolePrintF(8, "Client #%1d name: '%s' status: '%s' frame-lag: %3d company: %1d IP: %s unique-id: '%s'", - cs->index, ci->client_name, status, lag, - ci->client_playas + (IsValidPlayer(ci->client_playas) ? 1 : 0), - GetPlayerIP(ci), ci->unique_id); - } - - return true; -} - -DEF_CONSOLE_CMD(ConServerInfo) -{ - const NetworkGameInfo *gi; - - if (argc == 0) { - IConsoleHelp("List current and maximum client/player limits. Usage 'server_info'"); - IConsoleHelp("You can change these values by setting the variables 'max_clients', 'max_companies' and 'max_spectators'"); - return true; - } - - gi = &_network_game_info; - IConsolePrintF(_icolour_def, "Current/maximum clients: %2d/%2d", gi->clients_on, gi->clients_max); - IConsolePrintF(_icolour_def, "Current/maximum companies: %2d/%2d", ActivePlayerCount(), gi->companies_max); - IConsolePrintF(_icolour_def, "Current/maximum spectators: %2d/%2d", NetworkSpectatorCount(), gi->spectators_max); - - return true; -} - -DEF_CONSOLE_HOOK(ConHookValidateMaxClientsCount) -{ - /* XXX - hardcoded, string limiation -- TrueLight - * XXX - also see network.c:NetworkStartup ~1356 */ - if (_network_game_info.clients_max > 10) { - _network_game_info.clients_max = 10; - IConsoleError("Maximum clients out of bounds, truncating to limit."); - } - - return true; -} - -DEF_CONSOLE_HOOK(ConHookValidateMaxCompaniesCount) -{ - if (_network_game_info.companies_max > MAX_PLAYERS) { - _network_game_info.companies_max = MAX_PLAYERS; - IConsoleError("Maximum companies out of bounds, truncating to limit."); - } - - return true; -} - -DEF_CONSOLE_HOOK(ConHookValidateMaxSpectatorsCount) -{ - /* XXX @see ConHookValidateMaxClientsCount */ - if (_network_game_info.spectators_max > 10) { - _network_game_info.spectators_max = 10; - IConsoleError("Maximum spectators out of bounds, truncating to limit."); - } - - return true; -} - -DEF_CONSOLE_HOOK(ConHookCheckMinPlayers) -{ - CheckMinPlayers(); - return true; -} - -DEF_CONSOLE_CMD(ConKick) -{ - NetworkClientInfo *ci; - uint32 index; - - if (argc == 0) { - IConsoleHelp("Kick a player from a network game. Usage: 'kick '"); - IConsoleHelp("For client-id's, see the command 'clients'"); - return true; - } - - if (argc != 2) return false; - - if (strchr(argv[1], '.') == NULL) { - index = atoi(argv[1]); - ci = NetworkFindClientInfoFromIndex(index); - } else { - ci = NetworkFindClientInfoFromIP(argv[1]); - index = (ci == NULL) ? 0 : ci->client_index; - } - - if (index == NETWORK_SERVER_INDEX) { - IConsoleError("Silly boy, you can not kick yourself!"); - return true; - } - - if (index == 0) { - IConsoleError("Invalid client"); - return true; - } - - if (ci != NULL) { - SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED); - } else { - IConsoleError("Client not found"); - } - - return true; -} - -DEF_CONSOLE_CMD(ConResetCompany) -{ - const Player *p; - const NetworkClientState *cs; - const NetworkClientInfo *ci; - PlayerID index; - - if (argc == 0) { - IConsoleHelp("Remove an idle company from the game. Usage: 'reset_company '"); - IConsoleHelp("For company-id's, see the list of companies from the dropdown menu. Player 1 is 1, etc."); - return true; - } - - if (argc != 2) return false; - - index = atoi(argv[1]) - 1; - - /* Check valid range */ - if (!IsValidPlayer(index)) { - IConsolePrintF(_icolour_err, "Company does not exist. Company-id must be between 1 and %d.", MAX_PLAYERS); - return true; - } - - /* Check if company does exist */ - p = GetPlayer(index); - if (!p->is_active) { - IConsoleError("Company does not exist."); - return true; - } - - if (p->is_ai) { - IConsoleError("Company is owned by an AI."); - return true; - } - - /* Check if the company has active players */ - FOR_ALL_CLIENTS(cs) { - ci = DEREF_CLIENT_INFO(cs); - if (ci->client_playas == index) { - IConsoleError("Cannot remove company: a client is connected to that company."); - return true; - } - } - ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); - if (ci->client_playas == index) { - IConsoleError("Cannot remove company: the server is connected to that company."); - return true; - } - - /* It is safe to remove this company */ - DoCommandP(0, 2, index, NULL, CMD_PLAYER_CTRL); - IConsolePrint(_icolour_def, "Company deleted."); - - return true; -} - -DEF_CONSOLE_CMD(ConNetworkClients) -{ - NetworkClientInfo *ci; - - if (argc == 0) { - IConsoleHelp("Get a list of connected clients including their ID, name, company-id, and IP. Usage: 'clients'"); - return true; - } - - FOR_ALL_ACTIVE_CLIENT_INFOS(ci) { - IConsolePrintF(8, "Client #%1d name: '%s' company: %1d IP: %s", - ci->client_index, ci->client_name, - ci->client_playas + (IsValidPlayer(ci->client_playas) ? 1 : 0), - GetPlayerIP(ci)); - } - - return true; -} - -DEF_CONSOLE_CMD(ConNetworkConnect) -{ - char *ip; - const char *port = NULL; - const char *player = NULL; - uint16 rport; - - if (argc == 0) { - IConsoleHelp("Connect to a remote OTTD server and join the game. Usage: 'connect '"); - IConsoleHelp("IP can contain port and player: 'IP[[#Player]:Port]', eg: 'server.ottd.org#2:443'"); - IConsoleHelp("Player #255 is spectator all others are a certain company with Company 1 being #1"); - return true; - } - - if (argc < 2) return false; - if (_networking) NetworkDisconnect(); // we are in network-mode, first close it! - - ip = argv[1]; - /* Default settings: default port and new company */ - rport = NETWORK_DEFAULT_PORT; - _network_playas = PLAYER_NEW_COMPANY; - - ParseConnectionString(&player, &port, ip); - - IConsolePrintF(_icolour_def, "Connecting to %s...", ip); - if (player != NULL) { - _network_playas = atoi(player); - IConsolePrintF(_icolour_def, " player-no: %d", _network_playas); - - /* From a user pov 0 is a new player, internally it's different and all - * players are offset by one to ease up on users (eg players 1-8 not 0-7) */ - if (_network_playas != PLAYER_SPECTATOR) { - _network_playas--; - if (!IsValidPlayer(_network_playas)) return false; - } - } - if (port != NULL) { - rport = atoi(port); - IConsolePrintF(_icolour_def, " port: %s", port); - } - - NetworkClientConnectGame(ip, rport); - - return true; -} - -#endif /* ENABLE_NETWORK */ - -/* ******************************** */ -/* script file console commands */ -/* ******************************** */ - -DEF_CONSOLE_CMD(ConExec) -{ - char cmdline[ICON_CMDLN_SIZE]; - char *cmdptr; - - if (argc == 0) { - IConsoleHelp("Execute a local script file. Usage: 'exec