Files
@ r28572:dd3e6f760743
Branch filter:
Location: cpp/openttd-patchpack/source/src/script/script_info.cpp - annotation
r28572:dd3e6f760743
9.0 KiB
text/x-c
Update: nlohmann/json to 3.11.3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r18508:f893337ab5c7 r11380:8edd3e28a734 r11380:8edd3e28a734 r18510:9d33e9643f80 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r27357:d987424862c7 r11380:8edd3e28a734 r21383:942c32fb8b0e r21383:942c32fb8b0e r18508:f893337ab5c7 r11380:8edd3e28a734 r27369:d93808e810cd r27357:d987424862c7 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r18508:f893337ab5c7 r11380:8edd3e28a734 r11380:8edd3e28a734 r27369:d93808e810cd r11380:8edd3e28a734 r27369:d93808e810cd r11380:8edd3e28a734 r18508:f893337ab5c7 r18508:f893337ab5c7 r18508:f893337ab5c7 r18508:f893337ab5c7 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r11380:8edd3e28a734 r18508:f893337ab5c7 r24529:3dec691db49a r24529:3dec691db49a r11380:8edd3e28a734 r11380:8edd3e28a734 r27369:d93808e810cd r27369:d93808e810cd r27369:d93808e810cd r27369:d93808e810cd r27369:d93808e810cd r27369:d93808e810cd r27369:d93808e810cd r11380:8edd3e28a734 r11702:a9dd3b59c546 r27369:d93808e810cd r27369:d93808e810cd r11702:a9dd3b59c546 r11702:a9dd3b59c546 r18510:9d33e9643f80 r27369:d93808e810cd r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r11380:8edd3e28a734 r11380:8edd3e28a734 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27369:d93808e810cd r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27551:ea0213f40bc7 r27551:ea0213f40bc7 r27551:ea0213f40bc7 r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r20531:6a7aeb0059d3 r18510:9d33e9643f80 r18510:9d33e9643f80 r27310:97eaae2636e8 r27310:97eaae2636e8 r27310:97eaae2636e8 r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27310:97eaae2636e8 r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r18510:9d33e9643f80 r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27271:e0993573a0ba r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27357:d987424862c7 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27357:d987424862c7 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27357:d987424862c7 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27310:97eaae2636e8 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27551:ea0213f40bc7 r27551:ea0213f40bc7 r27551:ea0213f40bc7 r18510:9d33e9643f80 r23607:36c15679007d r24500:deda806a71a7 r27310:97eaae2636e8 r18510:9d33e9643f80 r18510:9d33e9643f80 r23607:36c15679007d r27357:d987424862c7 r18510:9d33e9643f80 r18510:9d33e9643f80 r27310:97eaae2636e8 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r21639:04805e592ebb r21639:04805e592ebb r21639:04805e592ebb r21639:04805e592ebb r18510:9d33e9643f80 r18510:9d33e9643f80 r26902:f61861f69e3d r26902:f61861f69e3d r26902:f61861f69e3d r26902:f61861f69e3d r26902:f61861f69e3d r26902:f61861f69e3d r26902:f61861f69e3d r26902:f61861f69e3d r27551:ea0213f40bc7 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r19400:b409042e8924 r19400:b409042e8924 r19400:b409042e8924 r27310:97eaae2636e8 r19400:b409042e8924 r19400:b409042e8924 r19400:b409042e8924 r19400:b409042e8924 r19400:b409042e8924 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r27359:9505d4407412 r18510:9d33e9643f80 r24500:deda806a71a7 r27310:97eaae2636e8 r18510:9d33e9643f80 r23607:36c15679007d r18510:9d33e9643f80 r18510:9d33e9643f80 r27310:97eaae2636e8 r18510:9d33e9643f80 r24500:deda806a71a7 r27310:97eaae2636e8 r18510:9d33e9643f80 r19834:0a601872b900 r24500:deda806a71a7 r24500:deda806a71a7 r24500:deda806a71a7 r24500:deda806a71a7 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 r18510:9d33e9643f80 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file script_info.cpp Implementation of ScriptInfo. */
#include "../stdafx.h"
#include "../settings_type.h"
#include "squirrel_helper.hpp"
#include "script_info.hpp"
#include "script_scanner.hpp"
#include "../3rdparty/fmt/format.h"
#include "../safeguards.h"
bool ScriptInfo::CheckMethod(const char *name) const
{
if (!this->engine->MethodExists(this->SQ_instance, name)) {
this->engine->ThrowError(fmt::format("your info.nut/library.nut doesn't have the method '{}'", name));
return false;
}
return true;
}
/* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
{
/* Set some basic info from the parent */
Squirrel::GetInstance(vm, &info->SQ_instance, 2);
/* Make sure the instance stays alive over time */
sq_addref(vm, &info->SQ_instance);
info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
info->engine = info->scanner->GetEngine();
/* Ensure the mandatory functions exist */
static const char * const required_functions[] = {
"GetAuthor",
"GetName",
"GetShortName",
"GetDescription",
"GetVersion",
"GetDate",
"CreateInstance",
};
for (size_t i = 0; i < lengthof(required_functions); i++) {
if (!info->CheckMethod(required_functions[i])) return SQ_ERROR;
}
/* Get location information of the scanner */
info->main_script = info->scanner->GetMainScript();
info->tar_file = info->scanner->GetTarFile();
/* Cache the data the info file gives us. */
if (!info->engine->CallStringMethod(info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
if (!info->engine->CallStringMethod(info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
if (!info->engine->CallStringMethod(info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
if (!info->engine->CallStringMethod(info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
if (!info->engine->CallStringMethod(info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR;
if (!info->engine->CallIntegerMethod(info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
if (!info->engine->CallStringMethod(info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
/* The GetURL function is optional. */
if (info->engine->MethodExists(info->SQ_instance, "GetURL")) {
if (!info->engine->CallStringMethod(info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
}
/* Check if we have settings */
if (info->engine->MethodExists(info->SQ_instance, "GetSettings")) {
if (!info->GetSettings()) return SQ_ERROR;
}
return 0;
}
bool ScriptInfo::GetSettings()
{
return this->engine->CallMethod(this->SQ_instance, "GetSettings", nullptr, MAX_GET_SETTING_OPS);
}
SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
{
ScriptConfigItem config;
uint items = 0;
/* Read the table, and find all properties we care about */
sq_pushnull(vm);
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
const SQChar *key_string;
if (SQ_FAILED(sq_getstring(vm, -2, &key_string))) return SQ_ERROR;
std::string key = StrMakeValid(key_string);
if (key == "name") {
const SQChar *sqvalue;
if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
/* Don't allow '=' and ',' in configure setting names, as we need those
* 2 chars to nicely store the settings as a string. */
auto replace_with_underscore = [](auto c) { return c == '=' || c == ','; };
config.name = StrMakeValid(sqvalue);
std::replace_if(config.name.begin(), config.name.end(), replace_with_underscore, '_');
items |= 0x001;
} else if (key == "description") {
const SQChar *sqdescription;
if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
config.description = StrMakeValid(sqdescription);
items |= 0x002;
} else if (key == "min_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.min_value = ClampTo<int32_t>(res);
items |= 0x004;
} else if (key == "max_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.max_value = ClampTo<int32_t>(res);
items |= 0x008;
} else if (key == "easy_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.easy_value = ClampTo<int32_t>(res);
items |= 0x010;
} else if (key == "medium_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.medium_value = ClampTo<int32_t>(res);
items |= 0x020;
} else if (key == "hard_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.hard_value = ClampTo<int32_t>(res);
items |= 0x040;
} else if (key == "random_deviation") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.random_deviation = ClampTo<int32_t>(abs(res));
items |= 0x200;
} else if (key == "custom_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.custom_value = ClampTo<int32_t>(res);
items |= 0x080;
} else if (key == "step_size") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.step_size = ClampTo<int32_t>(res);
} else if (key == "flags") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.flags = (ScriptConfigFlags)res;
items |= 0x100;
} else {
this->engine->ThrowError(fmt::format("unknown setting property '{}'", key));
return SQ_ERROR;
}
sq_pop(vm, 2);
}
sq_pop(vm, 1);
/* Don't allow both random_deviation and SCRIPTCONFIG_RANDOM to
* be set for the same config item. */
if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_RANDOM) != 0) {
this->engine->ThrowError("Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed");
return SQ_ERROR;
}
/* Reset the bit for random_deviation as it's optional. */
items &= ~0x200;
/* Make sure all properties are defined */
uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
if (items != mask) {
this->engine->ThrowError("please define all properties of a setting (min/max not allowed for booleans)");
return SQ_ERROR;
}
this->config_list.emplace_back(config);
return 0;
}
SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
{
const SQChar *setting_name_str;
if (SQ_FAILED(sq_getstring(vm, -2, &setting_name_str))) return SQ_ERROR;
std::string setting_name = StrMakeValid(setting_name_str);
ScriptConfigItem *config = nullptr;
for (auto &item : this->config_list) {
if (item.name == setting_name) config = &item;
}
if (config == nullptr) {
this->engine->ThrowError(fmt::format("Trying to add labels for non-defined setting '{}'", setting_name));
return SQ_ERROR;
}
if (!config->labels.empty()) return SQ_ERROR;
/* Read the table and find all labels */
sq_pushnull(vm);
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
const SQChar *key_string;
const SQChar *label;
if (SQ_FAILED(sq_getstring(vm, -2, &key_string))) return SQ_ERROR;
if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
/* Because squirrel doesn't support identifiers starting with a digit,
* we skip the first character. */
key_string++;
int sign = 1;
if (*key_string == '_') {
/* When the second character is '_', it indicates the value is negative. */
sign = -1;
key_string++;
}
int key = atoi(key_string) * sign;
config->labels[key] = StrMakeValid(label);
sq_pop(vm, 2);
}
sq_pop(vm, 1);
/* Check labels for completeness */
config->complete_labels = true;
for (int value = config->min_value; value <= config->max_value; value++) {
if (config->labels.find(value) == config->labels.end()) {
config->complete_labels = false;
break;
}
}
return 0;
}
const ScriptConfigItemList *ScriptInfo::GetConfigList() const
{
return &this->config_list;
}
const ScriptConfigItem *ScriptInfo::GetConfigItem(const std::string_view name) const
{
for (const auto &item : this->config_list) {
if (item.name == name) return &item;
}
return nullptr;
}
int ScriptInfo::GetSettingDefaultValue(const std::string &name) const
{
for (const auto &item : this->config_list) {
if (item.name != name) continue;
/* The default value depends on the difficulty level */
switch (GetGameSettings().script.settings_profile) {
case SP_EASY: return item.easy_value;
case SP_MEDIUM: return item.medium_value;
case SP_HARD: return item.hard_value;
case SP_CUSTOM: return item.custom_value;
default: NOT_REACHED();
}
}
/* There is no such setting */
return -1;
}
|