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;
104 int medium_value = INT32_MIN;
108 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
109 std::string_view key_string;
110 if (SQ_FAILED(sq_getstring(vm, -2, key_string)))
return SQ_ERROR;
114 std::string_view sqvalue;
115 if (SQ_FAILED(sq_getstring(vm, -1, sqvalue)))
return SQ_ERROR;
119 auto replace_with_underscore = [](
auto c) {
return c ==
'=' || c ==
','; };
121 std::replace_if(config.
name.begin(), config.
name.end(), replace_with_underscore,
'_');
123 }
else if (key ==
"description") {
124 std::string_view sqdescription;
125 if (SQ_FAILED(sq_getstring(vm, -1, sqdescription)))
return SQ_ERROR;
128 }
else if (key ==
"min_value") {
130 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
131 config.
min_value = ClampTo<int32_t>(res);
133 }
else if (key ==
"max_value") {
135 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
136 config.
max_value = ClampTo<int32_t>(res);
138 }
else if (key ==
"easy_value") {
140 }
else if (key ==
"medium_value") {
142 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
143 medium_value = ClampTo<int32_t>(res);
145 }
else if (key ==
"hard_value") {
147 }
else if (key ==
"custom_value") {
149 }
else if (key ==
"default_value") {
151 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
154 }
else if (key ==
"random_deviation") {
156 }
else if (key ==
"step_size") {
158 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
159 config.
step_size = ClampTo<int32_t>(res);
160 }
else if (key ==
"flags") {
162 if (SQ_FAILED(sq_getinteger(vm, -1, &res)))
return SQ_ERROR;
166 this->
engine->
ThrowError(fmt::format(
"unknown setting property '{}'", key));
180 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
192 if (!present.All(required)) {
193 this->
engine->
ThrowError(
"please define all properties of a setting (min/max not allowed for booleans)");
203 std::string_view setting_name_view;
204 if (SQ_FAILED(sq_getstring(vm, -2, setting_name_view)))
return SQ_ERROR;
205 std::string setting_name =
StrMakeValid(setting_name_view);
209 if (item.name == setting_name) config = &item;
212 if (config ==
nullptr) {
213 this->
engine->
ThrowError(fmt::format(
"Trying to add labels for non-defined setting '{}'", setting_name));
216 if (!config->
labels.empty())
return SQ_ERROR;
220 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
221 std::string_view key_string;
222 std::string_view label;
223 if (SQ_FAILED(sq_getstring(vm, -2, key_string)))
return SQ_ERROR;
224 if (SQ_FAILED(sq_getstring(vm, -1, label)))
return SQ_ERROR;
227 key_string.remove_prefix(1);
229 if (key_string.starts_with(
'_')) {
232 key_string.remove_prefix(1);
234 auto key = ParseInteger<int>(key_string);
235 if (!key.has_value())
return SQ_ERROR;
245 if (config->
labels.find(value) == config->
labels.end()) {
262 if (item.name ==
name)
return &item;
270 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) ).
ScriptConfigItemKey
Configuration items for a script.
@ DefaultValue
Default value when nothing is entered.
@ Name
Name of the configuration.
@ Flags
ScriptConfigFlags defining how/when to use this configuration.
@ MediumValue
Used for reading the old medium difficulty setting, which is used as default when that does not exist...
@ Description
Description.
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.