Run the dummy AI and let it generate an error message. */
| 79 | |
| 80 | /** Run the dummy AI and let it generate an error message. */ |
| 81 | void Script_CreateDummy(HSQUIRRELVM vm, StringID string, std::string_view type) |
| 82 | { |
| 83 | /* We want to translate the error message. |
| 84 | * We do this in three steps: |
| 85 | * 1) We get the error message, escape quotes and slashes, and split on |
| 86 | * newlines because Log.Error terminates passed strings at newlines. |
| 87 | */ |
| 88 | std::string error_message = GetString(string); |
| 89 | std::vector<std::string> messages = EscapeQuotesAndSlashesAndSplitOnNewLines(error_message); |
| 90 | |
| 91 | /* 2) We construct the AI's code. This is done by merging a header, body and footer */ |
| 92 | std::string dummy_script; |
| 93 | /* Just a rough ballpark estimate. */ |
| 94 | dummy_script.reserve(error_message.size() + 128 + 64 * messages.size()); |
| 95 | |
| 96 | format_append(dummy_script, "class Dummy{0} extends {0}Controller {{\n function Start()\n {{\n", type); |
| 97 | for (std::string &message : messages) { |
| 98 | format_append(dummy_script, " {}Log.Error(\"{}\");\n", type, message); |
| 99 | } |
| 100 | dummy_script += " }\n}\n"; |
| 101 | |
| 102 | /* 3) Finally we load and run the script */ |
| 103 | sq_pushroottable(vm); |
| 104 | if (SQ_SUCCEEDED(sq_compilebuffer(vm, dummy_script, "dummy", SQTrue))) { |
| 105 | sq_push(vm, -2); |
| 106 | if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { |
| 107 | sq_pop(vm, 1); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | NOT_REACHED(); |
| 112 | } |
no test coverage detected