11#include "../settings_type.h"
17#include "../core/string_consumer.hpp"
18#include "../3rdparty/fmt/format.h"
20#include "../safeguards.h"
25 this->
engine->
ThrowError(fmt::format(
"your info.nut/library.nut doesn't have the method '{}'",
name));
42 static const std::string_view required_functions[] = {
51 for (
const auto &required_function : required_functions) {
52 if (!info.
CheckMethod(required_function))
return SQ_ERROR;
66 if (info.
version < 0)
return SQ_ERROR;
87enum class ScriptConfigItemKey : uint8_t {
103 int medium_value = INT32_MIN;
107 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
108 std::string_view key_string;
109 if (SQ_FAILED(sq_getstring(vm, -2, key_string)))
return SQ_ERROR;
113 std::string_view sqvalue;
114 if (SQ_FAILED(sq_getstring(vm, -1, sqvalue)))
return SQ_ERROR;
118 auto replace_with_underscore = [](
auto c) {
return c ==
'=' || c ==
','; };
120 std::replace_if(config.
name.begin(), config.
name.end(), replace_with_underscore,
'_');
121 present.Set(ScriptConfigItemKey::Name);
122 }
else if (key ==
"description") {
123 std::string_view sqdescription;
124 if (SQ_FAILED(sq_getstring(vm, -1, sqdescription)))
return SQ_ERROR;
126 present.Set(ScriptConfigItemKey::Description);
127 }
else if (key ==
"min_value") {
129 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
130 config.
min_value = ClampTo<int32_t>(res);
131 present.Set(ScriptConfigItemKey::MinValue);
132 }
else if (key ==
"max_value") {
134 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
135 config.
max_value = ClampTo<int32_t>(res);
136 present.Set(ScriptConfigItemKey::MaxValue);
137 }
else if (key ==
"easy_value") {
139 }
else if (key ==
"medium_value") {
141 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
142 medium_value = ClampTo<int32_t>(res);
143 present.Set(ScriptConfigItemKey::MediumValue);
144 }
else if (key ==
"hard_value") {
146 }
else if (key ==
"custom_value") {
148 }
else if (key ==
"default_value") {
150 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
152 present.Set(ScriptConfigItemKey::DefaultValue);
153 }
else if (key ==
"random_deviation") {
155 }
else if (key ==
"step_size") {
157 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
158 config.
step_size = ClampTo<int32_t>(res);
159 }
else if (key ==
"flags") {
161 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
163 present.
Set(ScriptConfigItemKey::Flags);
165 this->
engine->
ThrowError(fmt::format(
"unknown setting property '{}'", key));
176 if (!present.Test(ScriptConfigItemKey::DefaultValue)) {
178 if (!present.Test(ScriptConfigItemKey::MediumValue)) {
179 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
184 present.Set(ScriptConfigItemKey::DefaultValue);
188 ScriptConfigItemKeys required = {ScriptConfigItemKey::Name, ScriptConfigItemKey::Description, ScriptConfigItemKey::DefaultValue, ScriptConfigItemKey::Flags};
191 if (!present.All(required)) {
192 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
202 std::string_view setting_name_view;
203 if (SQ_FAILED(sq_getstring(vm, -2, setting_name_view)))
return SQ_ERROR;
204 std::string setting_name =
StrMakeValid(setting_name_view);
208 if (item.name == setting_name) config = &item;
211 if (config ==
nullptr) {
212 this->
engine->
ThrowError(fmt::format(
"Trying to add labels for non-defined setting '{}'", setting_name));
215 if (!config->
labels.empty())
return SQ_ERROR;
219 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
220 std::string_view key_string;
221 std::string_view label;
222 if (SQ_FAILED(sq_getstring(vm, -2, key_string)))
return SQ_ERROR;
223 if (SQ_FAILED(sq_getstring(vm, -1, label)))
return SQ_ERROR;
226 key_string.remove_prefix(1);
228 if (key_string.starts_with(
'_')) {
231 key_string.remove_prefix(1);
233 auto key = ParseInteger<int>(key_string);
234 if (!key.has_value())
return SQ_ERROR;
244 if (config->
labels.find(value) == config->
labels.end()) {
261 if (item.name ==
name)
return &item;
269 if (item.name !=
name)
continue;
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
All static information from an Script like name, version, etc.
bool CheckMethod(std::string_view name) const
Check if a given method exists.
std::string short_name
Short name (4 chars) which uniquely identifies the script.
int version
Version of the script.
class ScriptScanner * scanner
ScriptScanner object that was used to scan this script info.
std::string description
Small description of the script.
ScriptConfigItemList config_list
List of settings from this Script.
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
class Squirrel * engine
Engine used to register for Squirrel.
std::string name
Full name of the script.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo &info)
Process the creation of a FileInfo object.
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
const ScriptConfigItem * GetConfigItem(std::string_view name) const
Get the description of a certain Script config option.
std::string main_script
The full path of the script.
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
std::string instance_name
Name of the main class in the script.
bool GetSettings()
Get the settings of the Script.
HSQOBJECT SQ_instance
The Squirrel instance created for this info.
std::string author
Author of the script.
int GetSettingDefaultValue(const std::string &name) const
Get the default value for a setting.
std::string tar_file
If, which tar file the script was in.
std::string url
URL of the script.
std::string date
The date the script was written at.
Scanner to help finding scripts.
std::string GetTarFile()
Get the current tar file the ScanDir is currently tracking.
class Squirrel * GetEngine()
Get the engine of the main squirrel handler (it indexes all available scripts).
std::string GetMainScript()
Get the current main script the ScanDir is currently tracking.
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
void ThrowError(std::string_view error)
Throw a Squirrel error that will be nicely displayed to the user.
static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
bool CallMethod(HSQOBJECT instance, std::string_view method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance, in various flavors.
bool MethodExists(HSQOBJECT instance, std::string_view method_name)
Check if a method exists in an instance.
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
@ Boolean
This value is a boolean (either 0 (false) or 1 (true) ).
ScriptInfo keeps track of all information of a script, like Author, Description, ....
static const int MAX_CREATEINSTANCE_OPS
Number of operations to create an instance of a script.
static const int MAX_GET_SETTING_OPS
Maximum number of operations allowed for getting a particular setting.
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Declarations of the class for the script scanner.
declarations and parts of the implementation of the class for convert code
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Info about a single Script setting.
ScriptConfigFlags flags
Flags for the configuration setting.
LabelMapping labels
Text labels for the integer values.
std::string name
The name of the configuration setting.
int default_value
The default value of this configuration setting.
int min_value
The minimal value this configuration setting can have.
int max_value
The maximal value this configuration setting can have.
int step_size
The step size in the gui.
std::string description
The description of the configuration setting.
bool complete_labels
True if all values have a label.