/* $Id$ */ /** @file console_cmds.cpp Implementation of the console hooks. */ #include "stdafx.h" #include "openttd.h" #include "console_internal.h" #include "debug.h" #include "engine_func.h" #include "landscape.h" #include "saveload.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_func.h" #include "settings_func.h" #include "fios.h" #include "fileio.h" #include "screenshot.h" #include "genworld.h" #include "network/network.h" #include "strings_func.h" #include "viewport_func.h" #include "window_func.h" #include "functions.h" #include "map_func.h" #include "date_func.h" #include "vehicle_func.h" #include "string_func.h" #include "player_func.h" #include "player_base.h" #include "settings_type.h" #ifdef ENABLE_NETWORK #include "table/strings.h" #endif /* ENABLE_NETWORK */ // ** 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() /* **************************** */ /* variable and command hooks */ /* **************************** */ #ifdef ENABLE_NETWORK static inline bool NetworkAvailable() { 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(CC_WARNING, "- %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) { if (argc == 0) { IConsoleHelp("Stops all vehicles in the game. For debugging only! Use at your own risk... Usage: 'stopall'"); return true; } StopAllVehicles(); 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(CC_ERROR, "Tile does not exist"); return true; } ScrollMainWindowToTile((TileIndex)result); return true; } } return false; } extern void BuildFileList(); 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 *filename = str_fmt("%s.sav", argv[1]); IConsolePrint(CC_DEFAULT, "Saving map..."); if (SaveOrLoad(filename, SL_SAVE, SAVE_DIR) != SL_OK) { IConsolePrint(CC_ERROR, "Saving map failed"); } else { IConsolePrintF(CC_DEFAULT, "Map sucessfully saved to %s", filename); } free(filename); 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(CC_DEFAULT, "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 IsInsideMM(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(CC_ERROR, "%s: Not a savegame.", file); } } else { IConsolePrintF(CC_ERROR, "%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(CC_ERROR, "%s: Failed to delete file", file); } else { IConsolePrintF(CC_ERROR, "%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(CC_DEFAULT, "%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(CC_ERROR, "%s: Not a directory.", file); } } else { IConsolePrintF(CC_ERROR, "%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(CC_DEFAULT, 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(CC_DEFAULT, "Client banned"); } else { IConsolePrint(CC_DEFAULT, "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(CC_DEFAULT, "IP unbanned."); return true; } } IConsolePrint(CC_DEFAULT, "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(CC_DEFAULT, "Banlist: "); for (i = 0; i < lengthof(_network_ban_list); i++) { if (_network_ban_list[i] != NULL) IConsolePrintF(CC_DEFAULT, " %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_game == 0) { DoCommandP(0, 1, 0, NULL, CMD_PAUSE); IConsolePrint(CC_DEFAULT, "Game paused."); } else { IConsolePrint(CC_DEFAULT, "Game is already paused."); } return true; } DEF_CONSOLE_CMD(ConUnPauseGame) { if (argc == 0) { IConsoleHelp("Unpause a network game. Usage: 'unpause'"); return true; } if (_pause_game != 0) { DoCommandP(0, 0, 0, NULL, CMD_PAUSE); IConsolePrint(CC_DEFAULT, "Game unpaused."); } else { IConsolePrint(CC_DEFAULT, "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; if (_network_server) { IConsoleCmdExec(argv[2]); } else { SEND_COMMAND(PACKET_CLIENT_RCON)(argv[1], argv[2]); } return true; } DEF_CONSOLE_CMD(ConStatus) { static const char* const stat_str[] = { "inactive", "authorizing", "authorized", "waiting", "loading map", "map done", "ready", "active" }; NetworkTCPSocketHandler *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 < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown"); IConsolePrintF(CC_INFO, "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(CC_DEFAULT, "Current/maximum clients: %2d/%2d", gi->clients_on, gi->clients_max); IConsolePrintF(CC_DEFAULT, "Current/maximum companies: %2d/%2d", ActivePlayerCount(), gi->companies_max); IConsolePrintF(CC_DEFAULT, "Current/maximum spectators: %2d/%2d", NetworkSpectatorCount(), gi->spectators_max); 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; NetworkTCPSocketHandler *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 = (PlayerID)(atoi(argv[1]) - 1); /* Check valid range */ if (!IsValidPlayer(index)) { IConsolePrintF(CC_ERROR, "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(CC_DEFAULT, "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(CC_INFO, "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(CC_DEFAULT, "Connecting to %s...", ip); if (player != NULL) { _network_playas = (PlayerID)atoi(player); IConsolePrintF(CC_DEFAULT, " 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(CC_DEFAULT, " 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