OpenTTD Source  12.0-beta2
script_info.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 "../settings_type.h"
12 
13 #include "squirrel_helper.hpp"
14 
15 #include "script_info.hpp"
16 #include "script_scanner.hpp"
17 
18 #include "../safeguards.h"
19 
20 ScriptInfo::~ScriptInfo()
21 {
22  /* Free all allocated strings */
23  for (const auto &item : this->config_list) {
24  free(item.name);
25  free(item.description);
26  if (item.labels != nullptr) {
27  for (auto &lbl_map : *item.labels) {
28  free(lbl_map.second);
29  }
30  delete item.labels;
31  }
32  }
33  this->config_list.clear();
34 
35  free(this->author);
36  free(this->name);
37  free(this->short_name);
38  free(this->description);
39  free(this->date);
40  free(this->instance_name);
41  free(this->url);
42  free(this->SQ_instance);
43 }
44 
45 bool ScriptInfo::CheckMethod(const char *name) const
46 {
47  if (!this->engine->MethodExists(*this->SQ_instance, name)) {
48  char error[1024];
49  seprintf(error, lastof(error), "your info.nut/library.nut doesn't have the method '%s'", name);
50  this->engine->ThrowError(error);
51  return false;
52  }
53  return true;
54 }
55 
56 /* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
57 {
58  /* Set some basic info from the parent */
59  info->SQ_instance = MallocT<SQObject>(1);
60  Squirrel::GetInstance(vm, info->SQ_instance, 2);
61  /* Make sure the instance stays alive over time */
62  sq_addref(vm, info->SQ_instance);
63 
65  info->engine = info->scanner->GetEngine();
66 
67  /* Ensure the mandatory functions exist */
68  static const char * const required_functions[] = {
69  "GetAuthor",
70  "GetName",
71  "GetShortName",
72  "GetDescription",
73  "GetVersion",
74  "GetDate",
75  "CreateInstance",
76  };
77  for (size_t i = 0; i < lengthof(required_functions); i++) {
78  if (!info->CheckMethod(required_functions[i])) return SQ_ERROR;
79  }
80 
81  /* Get location information of the scanner */
82  info->main_script = info->scanner->GetMainScript();
83  info->tar_file = info->scanner->GetTarFile();
84 
85  /* Cache the data the info file gives us. */
86  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
87  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
88  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
89  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
90  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR;
91  if (!info->engine->CallIntegerMethod(*info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
92  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
93 
94  /* The GetURL function is optional. */
95  if (info->engine->MethodExists(*info->SQ_instance, "GetURL")) {
96  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
97  }
98 
99  /* Check if we have settings */
100  if (info->engine->MethodExists(*info->SQ_instance, "GetSettings")) {
101  if (!info->GetSettings()) return SQ_ERROR;
102  }
103 
104  return 0;
105 }
106 
108 {
109  return this->engine->CallMethod(*this->SQ_instance, "GetSettings", nullptr, MAX_GET_SETTING_OPS);
110 }
111 
112 SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
113 {
114  ScriptConfigItem config;
115  memset(&config, 0, sizeof(config));
116  config.max_value = 1;
117  config.step_size = 1;
118  uint items = 0;
119 
120  /* Read the table, and find all properties we care about */
121  sq_pushnull(vm);
122  while (SQ_SUCCEEDED(sq_next(vm, -2))) {
123  const SQChar *key;
124  if (SQ_FAILED(sq_getstring(vm, -2, &key))) return SQ_ERROR;
125  StrMakeValidInPlace(const_cast<char *>(key));
126 
127  if (strcmp(key, "name") == 0) {
128  const SQChar *sqvalue;
129  if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
130  char *name = stredup(sqvalue);
131  char *s;
133 
134  /* Don't allow '=' and ',' in configure setting names, as we need those
135  * 2 chars to nicely store the settings as a string. */
136  while ((s = strchr(name, '=')) != nullptr) *s = '_';
137  while ((s = strchr(name, ',')) != nullptr) *s = '_';
138  config.name = name;
139  items |= 0x001;
140  } else if (strcmp(key, "description") == 0) {
141  const SQChar *sqdescription;
142  if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
143  config.description = stredup(sqdescription);
144  StrMakeValidInPlace(const_cast<char *>(config.description));
145  items |= 0x002;
146  } else if (strcmp(key, "min_value") == 0) {
147  SQInteger res;
148  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
149  config.min_value = res;
150  items |= 0x004;
151  } else if (strcmp(key, "max_value") == 0) {
152  SQInteger res;
153  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
154  config.max_value = res;
155  items |= 0x008;
156  } else if (strcmp(key, "easy_value") == 0) {
157  SQInteger res;
158  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
159  config.easy_value = res;
160  items |= 0x010;
161  } else if (strcmp(key, "medium_value") == 0) {
162  SQInteger res;
163  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
164  config.medium_value = res;
165  items |= 0x020;
166  } else if (strcmp(key, "hard_value") == 0) {
167  SQInteger res;
168  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
169  config.hard_value = res;
170  items |= 0x040;
171  } else if (strcmp(key, "random_deviation") == 0) {
172  SQInteger res;
173  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
174  config.random_deviation = res;
175  items |= 0x200;
176  } else if (strcmp(key, "custom_value") == 0) {
177  SQInteger res;
178  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
179  config.custom_value = res;
180  items |= 0x080;
181  } else if (strcmp(key, "step_size") == 0) {
182  SQInteger res;
183  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
184  config.step_size = res;
185  } else if (strcmp(key, "flags") == 0) {
186  SQInteger res;
187  if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
188  config.flags = (ScriptConfigFlags)res;
189  items |= 0x100;
190  } else {
191  char error[1024];
192  seprintf(error, lastof(error), "unknown setting property '%s'", key);
193  this->engine->ThrowError(error);
194  return SQ_ERROR;
195  }
196 
197  sq_pop(vm, 2);
198  }
199  sq_pop(vm, 1);
200 
201  /* Don't allow both random_deviation and SCRIPTCONFIG_RANDOM to
202  * be set for the same config item. */
203  if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_RANDOM) != 0) {
204  char error[1024];
205  seprintf(error, lastof(error), "Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed");
206  this->engine->ThrowError(error);
207  return SQ_ERROR;
208  }
209  /* Reset the bit for random_deviation as it's optional. */
210  items &= ~0x200;
211 
212  /* Make sure all properties are defined */
213  uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
214  if (items != mask) {
215  char error[1024];
216  seprintf(error, lastof(error), "please define all properties of a setting (min/max not allowed for booleans)");
217  this->engine->ThrowError(error);
218  return SQ_ERROR;
219  }
220 
221  this->config_list.push_back(config);
222  return 0;
223 }
224 
225 SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
226 {
227  const SQChar *setting_name;
228  if (SQ_FAILED(sq_getstring(vm, -2, &setting_name))) return SQ_ERROR;
229  StrMakeValidInPlace(const_cast<char *>(setting_name));
230 
231  ScriptConfigItem *config = nullptr;
232  for (auto &item : this->config_list) {
233  if (strcmp(item.name, setting_name) == 0) config = &item;
234  }
235 
236  if (config == nullptr) {
237  char error[1024];
238  seprintf(error, lastof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
239  this->engine->ThrowError(error);
240  return SQ_ERROR;
241  }
242  if (config->labels != nullptr) return SQ_ERROR;
243 
244  config->labels = new LabelMapping;
245 
246  /* Read the table and find all labels */
247  sq_pushnull(vm);
248  while (SQ_SUCCEEDED(sq_next(vm, -2))) {
249  const SQChar *key_string;
250  const SQChar *label;
251  if (SQ_FAILED(sq_getstring(vm, -2, &key_string))) return SQ_ERROR;
252  if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
253  /* Because squirrel doesn't support identifiers starting with a digit,
254  * we skip the first character. */
255  int key = atoi(key_string + 1);
256  StrMakeValidInPlace(const_cast<char *>(label));
257 
258  /* !Contains() prevents stredup from leaking. */
259  if (!config->labels->Contains(key)) config->labels->Insert(key, stredup(label));
260 
261  sq_pop(vm, 2);
262  }
263  sq_pop(vm, 1);
264 
265  /* Check labels for completeness */
266  config->complete_labels = true;
267  for (int value = config->min_value; value <= config->max_value; value++) {
268  if (!config->labels->Contains(value)) {
269  config->complete_labels = false;
270  break;
271  }
272  }
273 
274  return 0;
275 }
276 
278 {
279  return &this->config_list;
280 }
281 
282 const ScriptConfigItem *ScriptInfo::GetConfigItem(const char *name) const
283 {
284  for (const auto &item : this->config_list) {
285  if (strcmp(item.name, name) == 0) return &item;
286  }
287  return nullptr;
288 }
289 
290 int ScriptInfo::GetSettingDefaultValue(const char *name) const
291 {
292  for (const auto &item : this->config_list) {
293  if (strcmp(item.name, name) != 0) continue;
294  /* The default value depends on the difficulty level */
295  switch (GetGameSettings().script.settings_profile) {
296  case SP_EASY: return item.easy_value;
297  case SP_MEDIUM: return item.medium_value;
298  case SP_HARD: return item.hard_value;
299  case SP_CUSTOM: return item.custom_value;
300  default: NOT_REACHED();
301  }
302  }
303 
304  /* There is no such setting */
305  return -1;
306 }
ScriptInfo::AddSetting
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
Definition: script_info.cpp:112
ScriptInfo::main_script
std::string main_script
The full path of the script.
Definition: script_info.hpp:153
ScriptInfo::engine
class Squirrel * engine
Engine used to register for Squirrel.
Definition: script_info.hpp:148
SP_HARD
@ SP_HARD
Hard difficulty.
Definition: settings_type.h:39
ScriptConfigItem::step_size
int step_size
The step size in the gui.
Definition: script_config.hpp:42
ScriptInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
Definition: script_info.cpp:56
ScriptScanner::GetMainScript
std::string GetMainScript()
Get the current main script the ScanDir is currently tracking.
Definition: script_scanner.hpp:35
ScriptConfigFlags
ScriptConfigFlags
Bitmask of flags for Script settings.
Definition: script_config.hpp:21
ScriptConfigItem::labels
LabelMapping * labels
Text labels for the integer values.
Definition: script_config.hpp:44
MAX_CREATEINSTANCE_OPS
static const int MAX_CREATEINSTANCE_OPS
Number of operations to create an instance of a script.
Definition: script_info.hpp:23
ScriptConfigItem::medium_value
int medium_value
The default value on medium difficulty setting.
Definition: script_config.hpp:39
ScriptInfo::description
const char * description
Small description of the script.
Definition: script_info.hpp:158
Squirrel::GetInstance
static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
Definition: squirrel.hpp:200
SmallMap::Insert
bool Insert(const T &key, const U &data)
Adds new item to this map.
Definition: smallmap_type.hpp:127
squirrel_helper.hpp
SCRIPTCONFIG_BOOLEAN
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
Definition: script_config.hpp:24
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition: script_config.hpp:35
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:161
SP_MEDIUM
@ SP_MEDIUM
Medium difficulty.
Definition: settings_type.h:38
GetGameSettings
static GameSettings & GetGameSettings()
Get the settings-object applicable for the current situation: the newgame settings when we're in the ...
Definition: settings_type.h:617
ScriptConfigItem::complete_labels
bool complete_labels
True if all values have a label.
Definition: script_config.hpp:45
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition: script_config.hpp:36
ScriptConfigItemList
std::list< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition: script_config.hpp:48
ScriptInfo::GetConfigList
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
Definition: script_info.cpp:277
Squirrel::ThrowError
void ThrowError(const char *error)
Throw a Squirrel error that will be nicely displayed to the user.
Definition: squirrel.hpp:236
StrMakeValidInPlace
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:255
ScriptInfo::scanner
class ScriptScanner * scanner
ScriptScanner object that was used to scan this script info.
Definition: script_info.hpp:164
MAX_GET_OPS
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Definition: script_info.hpp:25
ScriptScanner
Scanner to help finding scripts.
Definition: script_scanner.hpp:20
SP_CUSTOM
@ SP_CUSTOM
No profile, special "custom" highscore.
Definition: settings_type.h:43
Squirrel::GetGlobalPointer
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
Definition: squirrel.hpp:226
ScriptInfo::CheckMethod
bool CheckMethod(const char *name) const
Check if a given method exists.
Definition: script_info.cpp:45
LabelMapping
SmallMap< int, char * > LabelMapping
Map-type used to map the setting numbers to labels.
Definition: script_config.hpp:29
ScriptInfo::date
const char * date
The date the script was written at.
Definition: script_info.hpp:159
ScriptInfo::url
const char * url
URL of the script.
Definition: script_info.hpp:162
ScriptScanner::GetEngine
class Squirrel * GetEngine()
Get the engine of the main squirrel handler (it indexes all available scripts).
Definition: script_scanner.hpp:30
Squirrel::CallMethod
bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance, in various flavors.
Definition: squirrel.cpp:411
ScriptInfo::config_list
ScriptConfigItemList config_list
List of settings from this Script.
Definition: script_info.hpp:150
ScriptConfigItem::easy_value
int easy_value
The default value on easy difficulty setting.
Definition: script_config.hpp:38
ScriptConfigItem::custom_value
int custom_value
The default value on custom difficulty setting.
Definition: script_config.hpp:37
SP_EASY
@ SP_EASY
Easy difficulty.
Definition: settings_type.h:37
ScriptInfo::short_name
const char * short_name
Short name (4 chars) which uniquely identifies the script.
Definition: script_info.hpp:157
Squirrel::MethodExists
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:358
ScriptInfo::AddLabels
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
Definition: script_info.cpp:225
ScriptConfigItem::description
const char * description
The description of the configuration setting.
Definition: script_config.hpp:34
MAX_GET_SETTING_OPS
static const int MAX_GET_SETTING_OPS
Maximum number of operations allowed for getting a particular setting.
Definition: script_info.hpp:27
script_info.hpp
script_scanner.hpp
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
ScriptInfo::author
const char * author
Author of the script.
Definition: script_info.hpp:155
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:132
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
SCRIPTCONFIG_RANDOM
@ SCRIPTCONFIG_RANDOM
When randomizing the Script, pick any value between min_value and max_value when on custom difficulty...
Definition: script_config.hpp:23
ScriptInfo::tar_file
std::string tar_file
If, which tar file the script was in.
Definition: script_info.hpp:154
ScriptConfigItem::flags
ScriptConfigFlags flags
Flags for the configuration setting.
Definition: script_config.hpp:43
ScriptInfo::SQ_instance
HSQOBJECT * SQ_instance
The Squirrel instance created for this info.
Definition: script_info.hpp:149
ScriptInfo::GetConfigItem
const ScriptConfigItem * GetConfigItem(const char *name) const
Get the description of a certain Script config option.
Definition: script_info.cpp:282
ScriptInfo::instance_name
const char * instance_name
Name of the main class in the script.
Definition: script_info.hpp:160
ScriptInfo::GetSettingDefaultValue
int GetSettingDefaultValue(const char *name) const
Get the default value for a setting.
Definition: script_info.cpp:290
ScriptConfigItem::hard_value
int hard_value
The default value on hard difficulty setting.
Definition: script_config.hpp:40
ScriptInfo::name
const char * name
Full name of the script.
Definition: script_info.hpp:156
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
ScriptScanner::GetTarFile
std::string GetTarFile()
Get the current tar file the ScanDir is currently tracking.
Definition: script_scanner.hpp:40
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
ScriptInfo::GetSettings
bool GetSettings()
Get the settings of the Script.
Definition: script_info.cpp:107
ScriptConfigItem
Info about a single Script setting.
Definition: script_config.hpp:32
ScriptConfigItem::random_deviation
int random_deviation
The maximum random deviation from the default value.
Definition: script_config.hpp:41
ScriptConfigItem::name
const char * name
The name of the configuration setting.
Definition: script_config.hpp:33
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
SmallMap::Contains
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:79