|
OpenTTD Source
14.0-beta3
|
Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../settings_type.h"
17 #include "../3rdparty/fmt/format.h"
19 #include "../safeguards.h"
24 this->
engine->
ThrowError(fmt::format(
"your info.nut/library.nut doesn't have the method '{}'",
name));
41 static const char *
const required_functions[] = {
50 for (
size_t i = 0; i <
lengthof(required_functions); i++) {
51 if (!info->
CheckMethod(required_functions[i]))
return SQ_ERROR;
90 int easy_value = INT32_MIN;
91 int medium_value = INT32_MIN;
92 int hard_value = INT32_MIN;
96 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
97 const SQChar *key_string;
98 if (SQ_FAILED(sq_getstring(vm, -2, &key_string)))
return SQ_ERROR;
102 const SQChar *sqvalue;
103 if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue)))
return SQ_ERROR;
107 auto replace_with_underscore = [](
auto c) {
return c ==
'=' || c ==
','; };
109 std::replace_if(config.
name.begin(), config.
name.end(), replace_with_underscore,
'_');
111 }
else if (key ==
"description") {
112 const SQChar *sqdescription;
113 if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription)))
return SQ_ERROR;
116 }
else if (key ==
"min_value") {
118 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
119 config.
min_value = ClampTo<int32_t>(res);
121 }
else if (key ==
"max_value") {
123 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
124 config.
max_value = ClampTo<int32_t>(res);
126 }
else if (key ==
"easy_value") {
128 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
129 easy_value = ClampTo<int32_t>(res);
131 }
else if (key ==
"medium_value") {
133 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
134 medium_value = ClampTo<int32_t>(res);
136 }
else if (key ==
"hard_value") {
138 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
139 hard_value = ClampTo<int32_t>(res);
141 }
else if (key ==
"custom_value") {
143 }
else if (key ==
"default_value") {
145 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
148 }
else if (key ==
"random_deviation") {
150 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
153 }
else if (key ==
"step_size") {
155 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
156 config.
step_size = ClampTo<int32_t>(res);
157 }
else if (key ==
"flags") {
159 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
163 this->
engine->
ThrowError(fmt::format(
"unknown setting property '{}'", key));
174 if ((items & 0x080) == 0) {
176 if ((items & 0x010) == 0 || (items & 0x020) == 0 || (items & 0x040) == 0) {
177 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
190 items |= 0x010 | 0x020 | 0x040;
196 this->
engine->
ThrowError(
"setting both random_deviation and CONFIG_BOOLEAN is not allowed");
206 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
216 const SQChar *setting_name_str;
217 if (SQ_FAILED(sq_getstring(vm, -2, &setting_name_str)))
return SQ_ERROR;
218 std::string setting_name =
StrMakeValid(setting_name_str);
222 if (item.name == setting_name) config = &item;
225 if (config ==
nullptr) {
226 this->
engine->
ThrowError(fmt::format(
"Trying to add labels for non-defined setting '{}'", setting_name));
229 if (!config->
labels.empty())
return SQ_ERROR;
233 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
234 const SQChar *key_string;
236 if (SQ_FAILED(sq_getstring(vm, -2, &key_string)))
return SQ_ERROR;
237 if (SQ_FAILED(sq_getstring(vm, -1, &label)))
return SQ_ERROR;
242 if (*key_string ==
'_') {
247 int key = atoi(key_string) * sign;
256 for (
int value = config->
min_value; value <= config->max_value; value++) {
257 if (config->
labels.find(value) == config->
labels.end()) {
274 if (item.name ==
name)
return &item;
282 if (item.name !=
name)
continue;
283 return item.default_value;
std::string short_name
Short name (4 chars) which uniquely identifies the script.
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
std::string main_script
The full path of the script.
std::string date
The date the script was written at.
class Squirrel * engine
Engine used to register for Squirrel.
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.
std::string name
The name of the configuration setting.
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
static const int MAX_CREATEINSTANCE_OPS
Number of operations to create an instance of a script.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
@ 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.
bool complete_labels
True if all values have a label.
int GetSettingDefaultValue(const std::string &name) const
Get the default value for a setting.
int max_value
The maximal value this configuration setting can have.
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
std::string description
Small description of the script.
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.
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
bool CheckMethod(const char *name) const
Check if a given method exists.
void ThrowError(const std::string_view error)
Throw a Squirrel error that will be nicely displayed to the user.
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.
std::string author
Author of 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.
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
static const int MAX_GET_SETTING_OPS
Maximum number of operations allowed for getting a particular setting.
std::string name
Full name of the script.
int default_value
The default value of this configuration setting.
#define lengthof(x)
Return the length of an fixed size array.
std::string tar_file
If, which tar file the script was in.
std::string description
The description of the configuration setting.
ScriptConfigFlags flags
Flags for the configuration setting.
std::string instance_name
Name of the main class in the script.
LabelMapping labels
Text labels for the integer values.
HSQOBJECT SQ_instance
The Squirrel instance created for this info.
std::string url
URL of the script.
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 ScriptConfigItem * GetConfigItem(const std::string_view name) const
Get the description of a certain Script config option.