OpenTTD Source: src/script/script_info_dummy.cpp Source File

OpenTTD Source 15.3

script_info_dummy.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include <squirrel.h>
12
13#include "../string_func.h"
14#include "../strings_func.h"
15#include "../core/format.hpp"
16
17#include "../safeguards.h"
18
19/* The reason this exists in C++, is that a user can trash their ai/ or game/ dir,
20 * leaving no Scripts available. The complexity to solve this is insane, and
21 * therefore the alternative is used, and make sure there is always a Script
22 * available, no matter what the situation is. By defining it in C++, there
23 * is simply no way a user can delete it, and therefore safe to use. It has
24 * to be noted that this Script is complete invisible for the user, and impossible
25 * to select manual. It is a fail-over in case no Scripts are available.
26 */
27
29 void Script_CreateDummyInfo(HSQUIRRELVM vm, std::string_view type, std::string_view dir)
30{
31 std::string dummy_script = fmt::format(
32 "class Dummy{0} extends {0}Info {{\n"
33 "function GetAuthor() {{ return \"OpenTTD Developers Team\"; }}\n"
34 "function GetName() {{ return \"Dummy{0}\"; }}\n"
35 "function GetShortName() {{ return \"DUMM\"; }}\n"
36 "function GetDescription() {{ return \"A Dummy {0} that is loaded when your {1}/ dir is empty\"; }}\n"
37 "function GetVersion() {{ return 1; }}\n"
38 "function GetDate() {{ return \"2008-07-26\"; }}\n"
39 "function CreateInstance() {{ return \"Dummy{0}\"; }}\n"
40 "}} RegisterDummy{0}(Dummy{0}());\n", type, dir);
41
42 sq_pushroottable(vm);
43
44 /* Load and run the script */
45 if (SQ_SUCCEEDED(sq_compilebuffer(vm, dummy_script, "dummy", SQTrue))) {
46 sq_push(vm, -2);
47 if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
48 sq_pop(vm, 1);
49 return;
50 }
51 }
52 NOT_REACHED();
53}
54
61 static std::vector<std::string> EscapeQuotesAndSlashesAndSplitOnNewLines(const std::string &message)
62{
63 std::vector<std::string> messages;
64
65 std::string safe_message;
66 for (auto c : message) {
67 if (c == '\n') {
68 messages.emplace_back(std::move(safe_message));
69 safe_message.clear(); // std::move leaves safe_message in a "valid but unspecified state" according to the specification.
70 continue;
71 }
72
73 if (c == '"' || c == '\\') safe_message.push_back('\\');
74 safe_message.push_back(c);
75 }
76 messages.emplace_back(std::move(safe_message));
77 return messages;
78}
79
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}
EscapeQuotesAndSlashesAndSplitOnNewLines
static std::vector< std::string > EscapeQuotesAndSlashesAndSplitOnNewLines(const std::string &message)
Split the given message on newlines (' ') and escape quotes and (back)slashes, so they can be properl...
Definition script_info_dummy.cpp:61
Script_CreateDummy
void Script_CreateDummy(HSQUIRRELVM vm, StringID string, std::string_view type)
Run the dummy AI and let it generate an error message.
Definition script_info_dummy.cpp:81
Script_CreateDummyInfo
void Script_CreateDummyInfo(HSQUIRRELVM vm, std::string_view type, std::string_view dir)
Run the dummy info.nut.
Definition script_info_dummy.cpp:29
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19