MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / Script_CreateDummy

Function Script_CreateDummy

src/script/script_info_dummy.cpp:81–112  ·  view source on GitHub ↗

Run the dummy AI and let it generate an error message. */

Source from the content-addressed store, hash-verified

79
80/** Run the dummy AI and let it generate an error message. */
81void 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}

Callers 1

LoadDummyScriptMethod · 0.85

Calls 11

format_appendFunction · 0.85
sq_pushroottableFunction · 0.85
sq_compilebufferFunction · 0.85
sq_pushFunction · 0.85
sq_callFunction · 0.85
sq_popFunction · 0.85
NOT_REACHEDFunction · 0.85
reserveMethod · 0.80
GetStringFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected