Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / script / script_info.cpp
blobd381ae9c2cfbf7b04226b825d4aecbd922b2937c
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file script_info.cpp Implementation of ScriptInfo. */
10 #include "../stdafx.h"
11 #include "../settings_type.h"
13 #include "squirrel_helper.hpp"
15 #include "script_info.hpp"
16 #include "script_scanner.hpp"
18 #include "../safeguards.h"
20 ScriptInfo::~ScriptInfo()
22 /* Free all allocated strings */
23 for (const auto &item : this->config_list) {
24 free(item.name);
25 free(item.description);
26 if (item.labels != nullptr) {
27 for (auto &lbl_map : *item.labels) {
28 free(lbl_map.second);
30 delete item.labels;
33 this->config_list.clear();
35 free(this->author);
36 free(this->name);
37 free(this->short_name);
38 free(this->description);
39 free(this->date);
40 free(this->instance_name);
41 free(this->url);
42 free(this->SQ_instance);
45 bool ScriptInfo::CheckMethod(const char *name) const
47 if (!this->engine->MethodExists(*this->SQ_instance, name)) {
48 char error[1024];
49 seprintf(error, lastof(error), "your info.nut/library.nut doesn't have the method '%s'", name);
50 this->engine->ThrowError(error);
51 return false;
53 return true;
56 /* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
58 /* Set some basic info from the parent */
59 info->SQ_instance = MallocT<SQObject>(1);
60 Squirrel::GetInstance(vm, info->SQ_instance, 2);
61 /* Make sure the instance stays alive over time */
62 sq_addref(vm, info->SQ_instance);
64 info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
65 info->engine = info->scanner->GetEngine();
67 /* Ensure the mandatory functions exist */
68 static const char * const required_functions[] = {
69 "GetAuthor",
70 "GetName",
71 "GetShortName",
72 "GetDescription",
73 "GetVersion",
74 "GetDate",
75 "CreateInstance",
77 for (size_t i = 0; i < lengthof(required_functions); i++) {
78 if (!info->CheckMethod(required_functions[i])) return SQ_ERROR;
81 /* Get location information of the scanner */
82 info->main_script = info->scanner->GetMainScript();
83 info->tar_file = info->scanner->GetTarFile();
85 /* Cache the data the info file gives us. */
86 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
87 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
88 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
89 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
90 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR;
91 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
92 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
94 /* The GetURL function is optional. */
95 if (info->engine->MethodExists(*info->SQ_instance, "GetURL")) {
96 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
99 /* Check if we have settings */
100 if (info->engine->MethodExists(*info->SQ_instance, "GetSettings")) {
101 if (!info->GetSettings()) return SQ_ERROR;
104 return 0;
107 bool ScriptInfo::GetSettings()
109 return this->engine->CallMethod(*this->SQ_instance, "GetSettings", nullptr, MAX_GET_SETTING_OPS);
112 SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
114 ScriptConfigItem config;
115 memset(&config, 0, sizeof(config));
116 config.max_value = 1;
117 config.step_size = 1;
118 uint items = 0;
120 /* Read the table, and find all properties we care about */
121 sq_pushnull(vm);
122 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
123 const SQChar *key;
124 if (SQ_FAILED(sq_getstring(vm, -2, &key))) return SQ_ERROR;
125 StrMakeValidInPlace(const_cast<char *>(key));
127 if (strcmp(key, "name") == 0) {
128 const SQChar *sqvalue;
129 if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
130 char *name = stredup(sqvalue);
131 char *s;
132 StrMakeValidInPlace(name);
134 /* Don't allow '=' and ',' in configure setting names, as we need those
135 * 2 chars to nicely store the settings as a string. */
136 while ((s = strchr(name, '=')) != nullptr) *s = '_';
137 while ((s = strchr(name, ',')) != nullptr) *s = '_';
138 config.name = name;
139 items |= 0x001;
140 } else if (strcmp(key, "description") == 0) {
141 const SQChar *sqdescription;
142 if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
143 config.description = stredup(sqdescription);
144 StrMakeValidInPlace(const_cast<char *>(config.description));
145 items |= 0x002;
146 } else if (strcmp(key, "min_value") == 0) {
147 SQInteger res;
148 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
149 config.min_value = res;
150 items |= 0x004;
151 } else if (strcmp(key, "max_value") == 0) {
152 SQInteger res;
153 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
154 config.max_value = res;
155 items |= 0x008;
156 } else if (strcmp(key, "easy_value") == 0) {
157 SQInteger res;
158 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
159 config.easy_value = res;
160 items |= 0x010;
161 } else if (strcmp(key, "medium_value") == 0) {
162 SQInteger res;
163 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
164 config.medium_value = res;
165 items |= 0x020;
166 } else if (strcmp(key, "hard_value") == 0) {
167 SQInteger res;
168 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
169 config.hard_value = res;
170 items |= 0x040;
171 } else if (strcmp(key, "random_deviation") == 0) {
172 SQInteger res;
173 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
174 config.random_deviation = res;
175 items |= 0x200;
176 } else if (strcmp(key, "custom_value") == 0) {
177 SQInteger res;
178 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
179 config.custom_value = res;
180 items |= 0x080;
181 } else if (strcmp(key, "step_size") == 0) {
182 SQInteger res;
183 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
184 config.step_size = res;
185 } else if (strcmp(key, "flags") == 0) {
186 SQInteger res;
187 if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
188 config.flags = (ScriptConfigFlags)res;
189 items |= 0x100;
190 } else {
191 char error[1024];
192 seprintf(error, lastof(error), "unknown setting property '%s'", key);
193 this->engine->ThrowError(error);
194 return SQ_ERROR;
197 sq_pop(vm, 2);
199 sq_pop(vm, 1);
201 /* Don't allow both random_deviation and SCRIPTCONFIG_RANDOM to
202 * be set for the same config item. */
203 if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_RANDOM) != 0) {
204 char error[1024];
205 seprintf(error, lastof(error), "Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed");
206 this->engine->ThrowError(error);
207 return SQ_ERROR;
209 /* Reset the bit for random_deviation as it's optional. */
210 items &= ~0x200;
212 /* Make sure all properties are defined */
213 uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
214 if (items != mask) {
215 char error[1024];
216 seprintf(error, lastof(error), "please define all properties of a setting (min/max not allowed for booleans)");
217 this->engine->ThrowError(error);
218 return SQ_ERROR;
221 this->config_list.push_back(config);
222 return 0;
225 SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
227 const SQChar *setting_name;
228 if (SQ_FAILED(sq_getstring(vm, -2, &setting_name))) return SQ_ERROR;
229 StrMakeValidInPlace(const_cast<char *>(setting_name));
231 ScriptConfigItem *config = nullptr;
232 for (auto &item : this->config_list) {
233 if (strcmp(item.name, setting_name) == 0) config = &item;
236 if (config == nullptr) {
237 char error[1024];
238 seprintf(error, lastof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
239 this->engine->ThrowError(error);
240 return SQ_ERROR;
242 if (config->labels != nullptr) return SQ_ERROR;
244 config->labels = new LabelMapping;
246 /* Read the table and find all labels */
247 sq_pushnull(vm);
248 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
249 const SQChar *key_string;
250 const SQChar *label;
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;
253 /* Because squirrel doesn't support identifiers starting with a digit,
254 * we skip the first character. */
255 int key = atoi(key_string + 1);
256 StrMakeValidInPlace(const_cast<char *>(label));
258 /* !Contains() prevents stredup from leaking. */
259 if (!config->labels->Contains(key)) config->labels->Insert(key, stredup(label));
261 sq_pop(vm, 2);
263 sq_pop(vm, 1);
265 /* Check labels for completeness */
266 config->complete_labels = true;
267 for (int value = config->min_value; value <= config->max_value; value++) {
268 if (!config->labels->Contains(value)) {
269 config->complete_labels = false;
270 break;
274 return 0;
277 const ScriptConfigItemList *ScriptInfo::GetConfigList() const
279 return &this->config_list;
282 const ScriptConfigItem *ScriptInfo::GetConfigItem(const char *name) const
284 for (const auto &item : this->config_list) {
285 if (strcmp(item.name, name) == 0) return &item;
287 return nullptr;
290 int ScriptInfo::GetSettingDefaultValue(const char *name) const
292 for (const auto &item : this->config_list) {
293 if (strcmp(item.name, name) != 0) continue;
294 /* The default value depends on the difficulty level */
295 switch (GetGameSettings().script.settings_profile) {
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();
304 /* There is no such setting */
305 return -1;