OpenTTD Source
20240919-master-gdf0233f4c2
|
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 (
const auto &required_function : required_functions) {
51 if (!info->
CheckMethod(required_function))
return SQ_ERROR;
90 int medium_value = INT32_MIN;
94 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
95 const SQChar *key_string;
96 if (SQ_FAILED(sq_getstring(vm, -2, &key_string)))
return SQ_ERROR;
100 const SQChar *sqvalue;
101 if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue)))
return SQ_ERROR;
105 auto replace_with_underscore = [](
auto c) {
return c ==
'=' || c ==
','; };
107 std::replace_if(config.
name.begin(), config.
name.end(), replace_with_underscore,
'_');
109 }
else if (key ==
"description") {
110 const SQChar *sqdescription;
111 if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription)))
return SQ_ERROR;
114 }
else if (key ==
"min_value") {
116 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
117 config.
min_value = ClampTo<int32_t>(res);
119 }
else if (key ==
"max_value") {
121 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
122 config.
max_value = ClampTo<int32_t>(res);
124 }
else if (key ==
"easy_value") {
127 }
else if (key ==
"medium_value") {
129 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
130 medium_value = ClampTo<int32_t>(res);
132 }
else if (key ==
"hard_value") {
135 }
else if (key ==
"custom_value") {
137 }
else if (key ==
"default_value") {
139 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
142 }
else if (key ==
"random_deviation") {
144 }
else if (key ==
"step_size") {
146 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
147 config.
step_size = ClampTo<int32_t>(res);
148 }
else if (key ==
"flags") {
150 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
154 this->
engine->
ThrowError(fmt::format(
"unknown setting property '{}'", key));
165 if ((items & 0x080) == 0) {
167 if ((items & 0x010) == 0 || (items & 0x020) == 0 || (items & 0x040) == 0) {
168 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
176 items |= 0x010 | 0x020 | 0x040;
182 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
192 const SQChar *setting_name_str;
193 if (SQ_FAILED(sq_getstring(vm, -2, &setting_name_str)))
return SQ_ERROR;
194 std::string setting_name =
StrMakeValid(setting_name_str);
198 if (item.name == setting_name) config = &item;
201 if (config ==
nullptr) {
202 this->
engine->
ThrowError(fmt::format(
"Trying to add labels for non-defined setting '{}'", setting_name));
205 if (!config->
labels.empty())
return SQ_ERROR;
209 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
210 const SQChar *key_string;
212 if (SQ_FAILED(sq_getstring(vm, -2, &key_string)))
return SQ_ERROR;
213 if (SQ_FAILED(sq_getstring(vm, -1, &label)))
return SQ_ERROR;
218 if (*key_string ==
'_') {
223 int key = atoi(key_string) * sign;
232 for (
int value = config->
min_value; value <= config->max_value; value++) {
233 if (config->
labels.find(value) == config->
labels.end()) {
250 if (item.name ==
name)
return &item;
258 if (item.name !=
name)
continue;
259 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.
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.
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.
const ScriptConfigItem * GetConfigItem(const std::string_view name) const
Get the description of a certain Script config option.