OpenTTD Source  1.11.2
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include <squirrel.h>
12 
13 #include "../string_func.h"
14 #include "../strings_func.h"
15 
16 #include "../safeguards.h"
17 
18 /* The reason this exists in C++, is that a user can trash his ai/ or game/ dir,
19  * leaving no Scripts available. The complexity to solve this is insane, and
20  * therefore the alternative is used, and make sure there is always a Script
21  * available, no matter what the situation is. By defining it in C++, there
22  * is simply no way a user can delete it, and therefore safe to use. It has
23  * to be noted that this Script is complete invisible for the user, and impossible
24  * to select manual. It is a fail-over in case no Scripts are available.
25  */
26 
28 void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
29 {
30  char dummy_script[4096];
31  char *dp = dummy_script;
32  dp += seprintf(dp, lastof(dummy_script), "class Dummy%s extends %sInfo {\n", type, type);
33  dp += seprintf(dp, lastof(dummy_script), "function GetAuthor() { return \"OpenTTD Developers Team\"; }\n");
34  dp += seprintf(dp, lastof(dummy_script), "function GetName() { return \"Dummy%s\"; }\n", type);
35  dp += seprintf(dp, lastof(dummy_script), "function GetShortName() { return \"DUMM\"; }\n");
36  dp += seprintf(dp, lastof(dummy_script), "function GetDescription() { return \"A Dummy %s that is loaded when your %s/ dir is empty\"; }\n", type, dir);
37  dp += seprintf(dp, lastof(dummy_script), "function GetVersion() { return 1; }\n");
38  dp += seprintf(dp, lastof(dummy_script), "function GetDate() { return \"2008-07-26\"; }\n");
39  dp += seprintf(dp, lastof(dummy_script), "function CreateInstance() { return \"Dummy%s\"; }\n", type);
40  dp += seprintf(dp, lastof(dummy_script), "} RegisterDummy%s(Dummy%s());\n", type, type);
41 
42  const SQChar *sq_dummy_script = dummy_script;
43 
44  sq_pushroottable(vm);
45 
46  /* Load and run the script */
47  if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, strlen(sq_dummy_script), "dummy", SQTrue))) {
48  sq_push(vm, -2);
49  if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
50  sq_pop(vm, 1);
51  return;
52  }
53  }
54  NOT_REACHED();
55 }
56 
58 void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type)
59 {
60  /* We want to translate the error message.
61  * We do this in three steps:
62  * 1) We get the error message
63  */
64  char error_message[1024];
65  GetString(error_message, string, lastof(error_message));
66 
67  /* Make escapes for all quotes and slashes. */
68  char safe_error_message[1024];
69  char *q = safe_error_message;
70  for (const char *p = error_message; *p != '\0' && q < lastof(safe_error_message) - 2; p++, q++) {
71  if (*p == '"' || *p == '\\') *q++ = '\\';
72  *q = *p;
73  }
74  *q = '\0';
75 
76  /* 2) We construct the AI's code. This is done by merging a header, body and footer */
77  char dummy_script[4096];
78  char *dp = dummy_script;
79  dp += seprintf(dp, lastof(dummy_script), "class Dummy%s extends %sController {\n function Start()\n {\n", type, type);
80 
81  /* As special trick we need to split the error message on newlines and
82  * emit each newline as a separate error printing string. */
83  char *newline;
84  char *p = safe_error_message;
85  do {
86  newline = strchr(p, '\n');
87  if (newline != nullptr) *newline = '\0';
88 
89  dp += seprintf(dp, lastof(dummy_script), " %sLog.Error(\"%s\");\n", type, p);
90  p = newline + 1;
91  } while (newline != nullptr);
92 
93  strecpy(dp, " }\n}\n", lastof(dummy_script));
94 
95  /* 3) We translate the error message in the character format that Squirrel wants.
96  * We can use the fact that the wchar string printing also uses %s to print
97  * old style char strings, which is what was generated during the script generation. */
98  const SQChar *sq_dummy_script = dummy_script;
99 
100  /* And finally we load and run the script */
101  sq_pushroottable(vm);
102  if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, strlen(sq_dummy_script), "dummy", SQTrue))) {
103  sq_push(vm, -2);
104  if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
105  sq_pop(vm, 1);
106  return;
107  }
108  }
109  NOT_REACHED();
110 }
Script_CreateDummy
void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type)
Run the dummy AI and let it generate an error message.
Definition: script_info_dummy.cpp:58
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Script_CreateDummyInfo
void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir)
Run the dummy info.nut.
Definition: script_info_dummy.cpp:28
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:460
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:385