OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../settings_type.h"
18 #include "../safeguards.h"
20 ScriptInfo::~ScriptInfo()
25 free(item.description);
26 if (item.labels !=
nullptr) {
27 for (
auto &lbl_map : *item.labels) {
33 this->config_list.clear();
68 static const char *
const required_functions[] = {
77 for (
size_t i = 0; i <
lengthof(required_functions); i++) {
78 if (!info->
CheckMethod(required_functions[i]))
return SQ_ERROR;
115 memset(&config, 0,
sizeof(config));
122 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
124 if (SQ_FAILED(sq_getstring(vm, -2, &key)))
return SQ_ERROR;
127 if (strcmp(key,
"name") == 0) {
128 const SQChar *sqvalue;
129 if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue)))
return SQ_ERROR;
136 while ((s = strchr(
name,
'=')) !=
nullptr) *s =
'_';
137 while ((s = strchr(
name,
',')) !=
nullptr) *s =
'_';
140 }
else if (strcmp(key,
"description") == 0) {
141 const SQChar *sqdescription;
142 if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription)))
return SQ_ERROR;
146 }
else if (strcmp(key,
"min_value") == 0) {
148 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
151 }
else if (strcmp(key,
"max_value") == 0) {
153 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
156 }
else if (strcmp(key,
"easy_value") == 0) {
158 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
161 }
else if (strcmp(key,
"medium_value") == 0) {
163 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
166 }
else if (strcmp(key,
"hard_value") == 0) {
168 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
171 }
else if (strcmp(key,
"random_deviation") == 0) {
173 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
176 }
else if (strcmp(key,
"custom_value") == 0) {
178 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
181 }
else if (strcmp(key,
"step_size") == 0) {
183 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
185 }
else if (strcmp(key,
"flags") == 0) {
187 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
221 this->config_list.push_back(config);
227 const SQChar *setting_name;
228 if (SQ_FAILED(sq_getstring(vm, -2, &setting_name)))
return SQ_ERROR;
232 for (
auto &item : this->config_list) {
233 if (strcmp(item.name, setting_name) == 0) config = &item;
236 if (config ==
nullptr) {
242 if (config->
labels !=
nullptr)
return SQ_ERROR;
248 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
249 const SQChar *key_string;
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;
255 int key = atoi(key_string + 1);
267 for (
int value = config->
min_value; value <= config->max_value; value++) {
284 for (
const auto &item : this->config_list) {
285 if (strcmp(item.name,
name) == 0)
return &item;
292 for (
const auto &item : this->config_list) {
293 if (strcmp(item.name,
name) != 0)
continue;
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();
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
std::string main_script
The full path of the script.
class Squirrel * engine
Engine used to register for Squirrel.
@ SP_HARD
Hard difficulty.
int step_size
The step size in the gui.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
std::string GetMainScript()
Get the current main script the ScanDir is currently tracking.
ScriptConfigFlags
Bitmask of flags for Script settings.
LabelMapping * labels
Text labels for the integer values.
static const int MAX_CREATEINSTANCE_OPS
Number of operations to create an instance of a script.
int medium_value
The default value on medium difficulty setting.
const char * description
Small description of the script.
static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
bool Insert(const T &key, const U &data)
Adds new item to this map.
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
int min_value
The minimal value this configuration setting can have.
int version
Version of the script.
@ SP_MEDIUM
Medium difficulty.
static GameSettings & GetGameSettings()
Get the settings-object applicable for the current situation: the newgame settings when we're in the ...
bool complete_labels
True if all values have a label.
int max_value
The maximal value this configuration setting can have.
std::list< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
void ThrowError(const char *error)
Throw a Squirrel error that will be nicely displayed to the user.
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).
class ScriptScanner * scanner
ScriptScanner object that was used to scan this script info.
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Scanner to help finding scripts.
@ SP_CUSTOM
No profile, special "custom" highscore.
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
bool CheckMethod(const char *name) const
Check if a given method exists.
SmallMap< int, char * > LabelMapping
Map-type used to map the setting numbers to labels.
const char * date
The date the script was written at.
const char * url
URL of the script.
class Squirrel * GetEngine()
Get the engine of the main squirrel handler (it indexes all available scripts).
bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance, in various flavors.
ScriptConfigItemList config_list
List of settings from this Script.
int easy_value
The default value on easy difficulty setting.
int custom_value
The default value on custom difficulty setting.
@ SP_EASY
Easy difficulty.
const char * short_name
Short name (4 chars) which uniquely identifies the script.
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
const char * description
The description of the configuration setting.
static const int MAX_GET_SETTING_OPS
Maximum number of operations allowed for getting a particular setting.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
const char * author
Author of the script.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
#define lengthof(x)
Return the length of an fixed size array.
@ SCRIPTCONFIG_RANDOM
When randomizing the Script, pick any value between min_value and max_value when on custom difficulty...
std::string tar_file
If, which tar file the script was in.
ScriptConfigFlags flags
Flags for the configuration setting.
HSQOBJECT * SQ_instance
The Squirrel instance created for this info.
const ScriptConfigItem * GetConfigItem(const char *name) const
Get the description of a certain Script config option.
const char * instance_name
Name of the main class in the script.
int GetSettingDefaultValue(const char *name) const
Get the default value for a setting.
int hard_value
The default value on hard difficulty setting.
const char * name
Full name of the script.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
std::string GetTarFile()
Get the current tar file the ScanDir is currently tracking.
All static information from an Script like name, version, etc.
bool GetSettings()
Get the settings of the Script.
Info about a single Script setting.
int random_deviation
The maximum random deviation from the default value.
const char * name
The name of the configuration setting.
#define lastof(x)
Get the last element of an fixed size array.
bool Contains(const T &key) const
Tests whether a key is assigned in this map.