mirror of
https://github.com/JGRennison/OpenTTD-patches.git
synced 2024-11-11 13:10:45 +00:00
Codechange: use fmt::format to create dummy scripts
This commit is contained in:
parent
9f2fc860ad
commit
3453c03a17
@ -12,6 +12,7 @@
|
||||
|
||||
#include "../string_func.h"
|
||||
#include "../strings_func.h"
|
||||
#include "../3rdparty/fmt/format.h"
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
@ -27,24 +28,21 @@
|
||||
/** Run the dummy info.nut. */
|
||||
void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
|
||||
{
|
||||
char dummy_script[4096];
|
||||
char *dp = dummy_script;
|
||||
dp += seprintf(dp, lastof(dummy_script), "class Dummy%s extends %sInfo {\n", type, type);
|
||||
dp += seprintf(dp, lastof(dummy_script), "function GetAuthor() { return \"OpenTTD Developers Team\"; }\n");
|
||||
dp += seprintf(dp, lastof(dummy_script), "function GetName() { return \"Dummy%s\"; }\n", type);
|
||||
dp += seprintf(dp, lastof(dummy_script), "function GetShortName() { return \"DUMM\"; }\n");
|
||||
dp += seprintf(dp, lastof(dummy_script), "function GetDescription() { return \"A Dummy %s that is loaded when your %s/ dir is empty\"; }\n", type, dir);
|
||||
dp += seprintf(dp, lastof(dummy_script), "function GetVersion() { return 1; }\n");
|
||||
dp += seprintf(dp, lastof(dummy_script), "function GetDate() { return \"2008-07-26\"; }\n");
|
||||
dp += seprintf(dp, lastof(dummy_script), "function CreateInstance() { return \"Dummy%s\"; }\n", type);
|
||||
dp += seprintf(dp, lastof(dummy_script), "} RegisterDummy%s(Dummy%s());\n", type, type);
|
||||
|
||||
const SQChar *sq_dummy_script = dummy_script;
|
||||
std::string dummy_script = fmt::format(
|
||||
"class Dummy{0} extends {0}Info {{\n"
|
||||
"function GetAuthor() {{ return \"OpenTTD Developers Team\"; }}\n"
|
||||
"function GetName() {{ return \"Dummy{0}\"; }}\n"
|
||||
"function GetShortName() {{ return \"DUMM\"; }}\n"
|
||||
"function GetDescription() {{ return \"A Dummy {0} that is loaded when your {1}/ dir is empty\"; }}\n"
|
||||
"function GetVersion() {{ return 1; }}\n"
|
||||
"function GetDate() {{ return \"2008-07-26\"; }}\n"
|
||||
"function CreateInstance() {{ return \"Dummy{0}\"; }}\n"
|
||||
"}} RegisterDummy{0}(Dummy{0}());\n", type, dir);
|
||||
|
||||
sq_pushroottable(vm);
|
||||
|
||||
/* Load and run the script */
|
||||
if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, strlen(sq_dummy_script), "dummy", SQTrue))) {
|
||||
if (SQ_SUCCEEDED(sq_compilebuffer(vm, dummy_script.c_str(), dummy_script.size(), "dummy", SQTrue))) {
|
||||
sq_push(vm, -2);
|
||||
if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
|
||||
sq_pop(vm, 1);
|
||||
@ -54,52 +52,56 @@ void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
|
||||
NOT_REACHED();
|
||||
}
|
||||
|
||||
/**
|
||||
* Split the given message on newlines ('\n') and escape quotes and (back)slashes,
|
||||
* so they can be properly interpreted as string constants by the Squirrel compiler.
|
||||
* @param message The message that we want to sanitize for use in Squirrel code.
|
||||
* @return Vector with sanitized strings to use as string constant in Squirrel code.
|
||||
*/
|
||||
static std::vector<std::string> EscapeQuotesAndSlashesAndSplitOnNewLines(const std::string &message)
|
||||
{
|
||||
std::vector<std::string> messages;
|
||||
|
||||
std::string safe_message;
|
||||
for (auto c : message) {
|
||||
if (c == '\n') {
|
||||
messages.emplace_back(std::move(safe_message));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '"' || c == '\\') safe_message.push_back('\\');
|
||||
safe_message.push_back(c);
|
||||
}
|
||||
messages.emplace_back(std::move(safe_message));
|
||||
return messages;
|
||||
}
|
||||
|
||||
/** Run the dummy AI and let it generate an error message. */
|
||||
void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type)
|
||||
{
|
||||
/* We want to translate the error message.
|
||||
* We do this in three steps:
|
||||
* 1) We get the error message
|
||||
* 1) We get the error message, escape quotes and slashes, and split on
|
||||
* newlines because Log.Error terminates passed strings at newlines.
|
||||
*/
|
||||
char error_message[1024];
|
||||
GetString(error_message, string, lastof(error_message));
|
||||
|
||||
/* Make escapes for all quotes and slashes. */
|
||||
char safe_error_message[1024];
|
||||
char *q = safe_error_message;
|
||||
for (const char *p = error_message; *p != '\0' && q < lastof(safe_error_message) - 2; p++, q++) {
|
||||
if (*p == '"' || *p == '\\') *q++ = '\\';
|
||||
*q = *p;
|
||||
}
|
||||
*q = '\0';
|
||||
std::string error_message = GetString(string);
|
||||
std::vector<std::string> messages = EscapeQuotesAndSlashesAndSplitOnNewLines(error_message);
|
||||
|
||||
/* 2) We construct the AI's code. This is done by merging a header, body and footer */
|
||||
char dummy_script[4096];
|
||||
char *dp = dummy_script;
|
||||
dp += seprintf(dp, lastof(dummy_script), "class Dummy%s extends %sController {\n function Start()\n {\n", type, type);
|
||||
std::string dummy_script;
|
||||
auto back_inserter = std::back_inserter(dummy_script);
|
||||
/* Just a rough ballpark estimate. */
|
||||
dummy_script.reserve(error_message.size() + 128 + 64 * messages.size());
|
||||
|
||||
/* As special trick we need to split the error message on newlines and
|
||||
* emit each newline as a separate error printing string. */
|
||||
char *newline;
|
||||
char *p = safe_error_message;
|
||||
do {
|
||||
newline = strchr(p, '\n');
|
||||
if (newline != nullptr) *newline = '\0';
|
||||
fmt::format_to(back_inserter, "class Dummy{0} extends {0}Controller {{\n function Start()\n {{\n", type);
|
||||
for (std::string &message : messages) {
|
||||
fmt::format_to(back_inserter, " {}Log.Error(\"{}\");\n", type, message);
|
||||
}
|
||||
dummy_script += " }\n}\n";
|
||||
|
||||
dp += seprintf(dp, lastof(dummy_script), " %sLog.Error(\"%s\");\n", type, p);
|
||||
p = newline + 1;
|
||||
} while (newline != nullptr);
|
||||
|
||||
strecpy(dp, " }\n}\n", lastof(dummy_script));
|
||||
|
||||
/* 3) We translate the error message in the character format that Squirrel wants.
|
||||
* We can use the fact that the wchar string printing also uses %s to print
|
||||
* old style char strings, which is what was generated during the script generation. */
|
||||
const SQChar *sq_dummy_script = dummy_script;
|
||||
|
||||
/* And finally we load and run the script */
|
||||
/* 3) Finally we load and run the script */
|
||||
sq_pushroottable(vm);
|
||||
if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, strlen(sq_dummy_script), "dummy", SQTrue))) {
|
||||
if (SQ_SUCCEEDED(sq_compilebuffer(vm, dummy_script.c_str(), dummy_script.size(), "dummy", SQTrue))) {
|
||||
sq_push(vm, -2);
|
||||
if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
|
||||
sq_pop(vm, 1);
|
||||
|
Loading…
Reference in New Issue
Block a user