| 455 | } |
| 456 | |
| 457 | void execute(const ConsoleArgList& args, const char* inputText) |
| 458 | { |
| 459 | char errorMsg[CSTR_LEN]; |
| 460 | |
| 461 | const size_t count = s_cmd.size(); |
| 462 | const ConsoleCommand* cmd = s_cmd.data(); |
| 463 | const s32 argCount = (s32)args.size() - 1; |
| 464 | for (size_t i = 0; i < count; i++, cmd++) |
| 465 | { |
| 466 | if (strcasecmp(cmd->name.c_str(), args[0].c_str()) == 0) |
| 467 | { |
| 468 | if (argCount < (s32)cmd->argCount) |
| 469 | { |
| 470 | sprintf(errorMsg, "Too few arguments (%d) for console command \"%s\"", argCount, cmd->name.c_str()); |
| 471 | s_history.push_back({ c_historyErrorColor, errorMsg }); |
| 472 | } |
| 473 | else if (!cmd->func) |
| 474 | { |
| 475 | sprintf(errorMsg, "Console command has no implementation - \"%s\"", cmd->name.c_str()); |
| 476 | s_history.push_back({ c_historyErrorColor, errorMsg }); |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | if (cmd->repeat) { s_history.push_back({ c_historyCmdColor, inputText }); } |
| 481 | cmd->func(args); |
| 482 | } |
| 483 | return; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // If no function is called, we might be directly accessing a variable (shortcut for get/set). |
| 488 | const size_t varCount = s_var.size(); |
| 489 | CVar* var = s_var.data(); |
| 490 | |
| 491 | ConsoleArgList varArgs; |
| 492 | if (argCount < 1) |
| 493 | { |
| 494 | varArgs.push_back("get"); |
| 495 | varArgs.push_back(args[0]); |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | varArgs.push_back("set"); |
| 500 | varArgs.push_back(args[0]); |
| 501 | varArgs.push_back(args[1]); |
| 502 | } |
| 503 | |
| 504 | for (size_t i = 0; i < varCount; i++, var++) |
| 505 | { |
| 506 | if (strcasecmp(var->name.c_str(), args[0].c_str()) == 0) |
| 507 | { |
| 508 | if (argCount < 1) { c_get(varArgs); } |
| 509 | else { c_set(varArgs); } |
| 510 | return; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | sprintf(errorMsg, "Invalid command \"%s\"", args[0].c_str()); |