| 198 | -------------------------------------------------------------------------------*/ |
| 199 | |
| 200 | void consoleCommand(char const* const command_str) |
| 201 | { |
| 202 | if (!command_str || command_str[0] == '\0') |
| 203 | { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | char buf[1024]; |
| 208 | size_t size = strlen(command_str); |
| 209 | size = std::min(size, sizeof(buf) - 1); |
| 210 | memcpy(buf, command_str, size); |
| 211 | buf[size] = '\0'; |
| 212 | |
| 213 | std::vector<const char*> tokens; |
| 214 | auto token = strtok(buf, " "); |
| 215 | auto command = token; |
| 216 | while (token) |
| 217 | { |
| 218 | tokens.push_back(token); |
| 219 | token = strtok(nullptr, " "); |
| 220 | } |
| 221 | |
| 222 | auto& map = getConsoleCommands(); |
| 223 | auto find = map.find(command); |
| 224 | if (find == map.end()) |
| 225 | { |
| 226 | // invalid command |
| 227 | if (intro || !initialized) |
| 228 | { |
| 229 | printlog("Unknown command: '%s'\n", command_str); |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | messagePlayer(clientnum, MESSAGE_MISC, Language::get(305), command_str); |
| 234 | } |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | auto& ccmd = find->second; |
| 239 | ccmd(tokens.size(), tokens.data()); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | const char* FindConsoleCommand(const char* str, int index) { |
| 244 | if (!str || str[0] == '\0') { |
no test coverage detected