#include "stdafx.h" #include "ttd.h" #include "console.h" #include "debug.h" #include "engine.h" #include "functions.h" #include "string.h" #include "variables.h" #include "network_data.h" #include "network_client.h" #include "network_server.h" #include "network_udp.h" #include "command.h" #include "settings.h" #include "hal.h" /* for file list */ // ** 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; } #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])) { 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", _path.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; } 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 (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); strcpy(_file_to_saveload.name, FiosBrowseTo(item)); break; default: IConsolePrintF(_icolour_err, "%s: Not a savegame.", 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[0] != '\0') ? item->title : item->name); } 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(&_fios_num, 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; uint32 index; if (argc == 0) { IConsoleHelp("Ban a player from a network game. Usage: 'ban '"); IConsoleHelp("For client-id's, see the command 'clients'"); return true; } if (argc != 2) return false; index = atoi(argv[1]); if (index == NETWORK_SERVER_INDEX) { IConsolePrint(_icolour_def, "Silly boy, you can not ban yourself!"); return true; } if (index == 0) { IConsoleError("Invalid Client-ID"); return true; } ci = NetworkFindClientInfoFromIndex(index); if (ci != NULL) { uint i; /* Add user to ban-list */ for (i = 0; i < lengthof(_network_ban_list); i++) { if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') { _network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ci->client_ip)); break; } } SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED); } else IConsoleError("Client-ID not found"); return true; } DEF_CONSOLE_CMD(ConUnBan) { uint i; if (argc == 0) { IConsoleHelp("Unban a player from a network game. Usage: 'unban '"); return true; } if (argc != 2) return false; for (i = 0; i < lengthof(_network_ban_list); i++) { if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') continue; if (strncmp(_network_ban_list[i], argv[1], strlen(_network_ban_list[i])) == 0) { _network_ban_list[i][0] = '\0'; 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 || _network_ban_list[i][0] == '\0') continue; 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 *stat_str[] = {"inactive", "authorized", "waiting", "loading map", "map done", "ready", "active"}; const char *status; 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); status = (cs->status <= STATUS_ACTIVE) ? stat_str[cs->status] : "unknown"; IConsolePrintF(8, "Client #%d/%s status: %s frame-lag: %d play-as: %d unique-id: %s", cs->index, ci->client_name, status, lag, ci->client_playas, ci->unique_id); } 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; index = atoi(argv[1]); if (index == NETWORK_SERVER_INDEX) { IConsolePrint(_icolour_def, "Silly boy, you can not kick yourself!"); return true; } if (index == 0) { IConsoleError("Invalid client-id"); return true; } ci = NetworkFindClientInfoFromIndex(index); if (ci != NULL) { SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED); } else IConsoleError("Client-id not found"); return true; } DEF_CONSOLE_CMD(ConResetCompany) { Player *p; NetworkClientState *cs; NetworkClientInfo *ci; byte 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]); /* Check valid range */ if (index < 1 || index > MAX_PLAYERS) { IConsolePrintF(_icolour_err, "Company does not exist. Company-id must be between 1 and %d.", MAX_PLAYERS); return true; } /* Check if company does exist */ index--; p = DEREF_PLAYER(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 - 1 == index) { IConsoleError("Cannot remove company: a client is connected to that company."); return true; } } ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); if (ci->client_playas - 1 == 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, and company-id. Usage: 'clients'"); return true; } for (ci = _network_client_info; ci != &_network_client_info[MAX_CLIENT_INFO]; ci++) { if (ci->client_index != NETWORK_EMPTY_INDEX) { IConsolePrintF(8, "Client #%d name: %s play-as company: %d", ci->client_index, ci->client_name, ci->client_playas); } } 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'"); return true; } if (argc < 2) return false; if (_networking) // We are in network-mode, first close it! NetworkDisconnect(); ip = argv[1]; rport = NETWORK_DEFAULT_PORT; ParseConnectionString(&player, &port, ip); IConsolePrintF(_icolour_def, "Connecting to %s...", ip); if (player != NULL) { _network_playas = atoi(player); IConsolePrintF(_icolour_def, " player-no: %s", player); } 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