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