Update to 14.0-beta1
This commit is contained in:
+93
-118
@@ -14,40 +14,14 @@
|
||||
|
||||
#include "script_info.hpp"
|
||||
#include "script_scanner.hpp"
|
||||
#include "../3rdparty/fmt/format.h"
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
ScriptInfo::~ScriptInfo()
|
||||
{
|
||||
/* Free all allocated strings */
|
||||
for (const auto &item : this->config_list) {
|
||||
free(item.name);
|
||||
free(item.description);
|
||||
if (item.labels != nullptr) {
|
||||
for (auto &lbl_map : *item.labels) {
|
||||
free(lbl_map.second);
|
||||
}
|
||||
delete item.labels;
|
||||
}
|
||||
}
|
||||
this->config_list.clear();
|
||||
|
||||
free(this->author);
|
||||
free(this->name);
|
||||
free(this->short_name);
|
||||
free(this->description);
|
||||
free(this->date);
|
||||
free(this->instance_name);
|
||||
free(this->url);
|
||||
free(this->SQ_instance);
|
||||
}
|
||||
|
||||
bool ScriptInfo::CheckMethod(const char *name) const
|
||||
{
|
||||
if (!this->engine->MethodExists(*this->SQ_instance, name)) {
|
||||
char error[1024];
|
||||
seprintf(error, lastof(error), "your info.nut/library.nut doesn't have the method '%s'", name);
|
||||
this->engine->ThrowError(error);
|
||||
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;
|
||||
@@ -56,10 +30,9 @@ bool ScriptInfo::CheckMethod(const char *name) const
|
||||
/* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
|
||||
{
|
||||
/* Set some basic info from the parent */
|
||||
info->SQ_instance = MallocT<SQObject>(1);
|
||||
Squirrel::GetInstance(vm, info->SQ_instance, 2);
|
||||
Squirrel::GetInstance(vm, &info->SQ_instance, 2);
|
||||
/* Make sure the instance stays alive over time */
|
||||
sq_addref(vm, info->SQ_instance);
|
||||
sq_addref(vm, &info->SQ_instance);
|
||||
|
||||
info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
|
||||
info->engine = info->scanner->GetEngine();
|
||||
@@ -83,21 +56,21 @@ bool ScriptInfo::CheckMethod(const char *name) const
|
||||
info->tar_file = info->scanner->GetTarFile();
|
||||
|
||||
/* Cache the data the info file gives us. */
|
||||
if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
|
||||
if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
|
||||
if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
|
||||
if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
|
||||
if (!info->engine->CallStringMethodStrdup(*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->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
|
||||
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->CallStringMethodStrdup(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
|
||||
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->engine->MethodExists(info->SQ_instance, "GetSettings")) {
|
||||
if (!info->GetSettings()) return SQ_ERROR;
|
||||
}
|
||||
|
||||
@@ -106,91 +79,88 @@ bool ScriptInfo::CheckMethod(const char *name) const
|
||||
|
||||
bool ScriptInfo::GetSettings()
|
||||
{
|
||||
return this->engine->CallMethod(*this->SQ_instance, "GetSettings", nullptr, MAX_GET_SETTING_OPS);
|
||||
return this->engine->CallMethod(this->SQ_instance, "GetSettings", nullptr, MAX_GET_SETTING_OPS);
|
||||
}
|
||||
|
||||
SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
||||
{
|
||||
ScriptConfigItem config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
config.max_value = 1;
|
||||
config.step_size = 1;
|
||||
uint items = 0;
|
||||
|
||||
int easy_value = INT32_MIN;
|
||||
int medium_value = INT32_MIN;
|
||||
int hard_value = INT32_MIN;
|
||||
|
||||
/* Read the table, and find all properties we care about */
|
||||
sq_pushnull(vm);
|
||||
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
|
||||
const SQChar *key;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &key))) return SQ_ERROR;
|
||||
StrMakeValidInPlace(const_cast<char *>(key));
|
||||
const SQChar *key_string;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &key_string))) return SQ_ERROR;
|
||||
std::string key = StrMakeValid(key_string);
|
||||
|
||||
if (strcmp(key, "name") == 0) {
|
||||
if (key == "name") {
|
||||
const SQChar *sqvalue;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
|
||||
char *name = stredup(sqvalue);
|
||||
char *s;
|
||||
StrMakeValidInPlace(name);
|
||||
|
||||
/* Don't allow '=' and ',' in configure setting names, as we need those
|
||||
* 2 chars to nicely store the settings as a string. */
|
||||
while ((s = strchr(name, '=')) != nullptr) *s = '_';
|
||||
while ((s = strchr(name, ',')) != nullptr) *s = '_';
|
||||
config.name = name;
|
||||
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 (strcmp(key, "description") == 0) {
|
||||
} else if (key == "description") {
|
||||
const SQChar *sqdescription;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
|
||||
config.description = stredup(sqdescription);
|
||||
StrMakeValidInPlace(const_cast<char *>(config.description));
|
||||
config.description = StrMakeValid(sqdescription);
|
||||
items |= 0x002;
|
||||
} else if (strcmp(key, "min_value") == 0) {
|
||||
} else if (key == "min_value") {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.min_value = ClampToI32(res);
|
||||
config.min_value = ClampTo<int32_t>(res);
|
||||
items |= 0x004;
|
||||
} else if (strcmp(key, "max_value") == 0) {
|
||||
} else if (key == "max_value") {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.max_value = ClampToI32(res);
|
||||
config.max_value = ClampTo<int32_t>(res);
|
||||
items |= 0x008;
|
||||
} else if (strcmp(key, "easy_value") == 0) {
|
||||
} else if (key == "easy_value") {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.easy_value = ClampToI32(res);
|
||||
easy_value = ClampTo<int32_t>(res);
|
||||
items |= 0x010;
|
||||
} else if (strcmp(key, "medium_value") == 0) {
|
||||
} else if (key == "medium_value") {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.medium_value = ClampToI32(res);
|
||||
medium_value = ClampTo<int32_t>(res);
|
||||
items |= 0x020;
|
||||
} else if (strcmp(key, "hard_value") == 0) {
|
||||
} else if (key == "hard_value") {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.hard_value = ClampToI32(res);
|
||||
hard_value = ClampTo<int32_t>(res);
|
||||
items |= 0x040;
|
||||
} else if (strcmp(key, "random_deviation") == 0) {
|
||||
} else if (key == "custom_value") {
|
||||
// No longer parsed.
|
||||
} else if (key == "default_value") {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.random_deviation = ClampToI32(abs(res));
|
||||
items |= 0x200;
|
||||
} else if (strcmp(key, "custom_value") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.custom_value = ClampToI32(res);
|
||||
config.default_value = ClampTo<int32_t>(res);
|
||||
items |= 0x080;
|
||||
} else if (strcmp(key, "step_size") == 0) {
|
||||
} else if (key == "random_deviation") {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.step_size = ClampToI32(res);
|
||||
} else if (strcmp(key, "flags") == 0) {
|
||||
config.random_deviation = ClampTo<int32_t>(abs(res));
|
||||
items |= 0x200;
|
||||
} 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 {
|
||||
char error[1024];
|
||||
seprintf(error, lastof(error), "unknown setting property '%s'", key);
|
||||
this->engine->ThrowError(error);
|
||||
this->engine->ThrowError(fmt::format("unknown setting property '{}'", key));
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
@@ -198,50 +168,65 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
||||
}
|
||||
sq_pop(vm, 1);
|
||||
|
||||
/* Don't allow both random_deviation and SCRIPTCONFIG_RANDOM to
|
||||
/* Check if default_value is set. Although required, this was changed with
|
||||
* 14.0, and as such, older AIs don't use it yet. So we convert the older
|
||||
* values into a default_value. */
|
||||
if ((items & 0x080) == 0) {
|
||||
/* Easy/medium/hard should all three be defined. */
|
||||
if ((items & 0x010) == 0 || (items & 0x020) == 0 || (items & 0x040) == 0) {
|
||||
this->engine->ThrowError("please define all properties of a setting (min/max not allowed for booleans)");
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
config.default_value = medium_value;
|
||||
/* If not boolean and no random deviation set, calculate it based on easy/hard difference. */
|
||||
if ((config.flags & SCRIPTCONFIG_BOOLEAN) == 0 && (items & 0x200) == 0) {
|
||||
config.random_deviation = abs(hard_value - easy_value) / 2;
|
||||
items |= 0x200;
|
||||
}
|
||||
items |= 0x080;
|
||||
} else {
|
||||
/* For compatibility, also act like the default sets the easy/medium/hard. */
|
||||
items |= 0x010 | 0x020 | 0x040;
|
||||
}
|
||||
|
||||
/* Don't allow both random_deviation and SCRIPTCONFIG_BOOLEAN to
|
||||
* be set for the same config item. */
|
||||
if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_RANDOM) != 0) {
|
||||
char error[1024];
|
||||
seprintf(error, lastof(error), "Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed");
|
||||
this->engine->ThrowError(error);
|
||||
if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
|
||||
this->engine->ThrowError("setting both random_deviation and CONFIG_BOOLEAN 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) {
|
||||
char error[1024];
|
||||
seprintf(error, lastof(error), "please define all properties of a setting (min/max not allowed for booleans)");
|
||||
this->engine->ThrowError(error);
|
||||
this->engine->ThrowError("please define all properties of a setting (min/max not allowed for booleans)");
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
this->config_list.push_back(config);
|
||||
this->config_list.emplace_back(config);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
|
||||
{
|
||||
const SQChar *setting_name;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &setting_name))) return SQ_ERROR;
|
||||
StrMakeValidInPlace(const_cast<char *>(setting_name));
|
||||
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 (strcmp(item.name, setting_name) == 0) config = &item;
|
||||
if (item.name == setting_name) config = &item;
|
||||
}
|
||||
|
||||
if (config == nullptr) {
|
||||
char error[1024];
|
||||
seprintf(error, lastof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
|
||||
this->engine->ThrowError(error);
|
||||
this->engine->ThrowError(fmt::format("Trying to add labels for non-defined setting '{}'", setting_name));
|
||||
return SQ_ERROR;
|
||||
}
|
||||
if (config->labels != nullptr) return SQ_ERROR;
|
||||
|
||||
config->labels = new LabelMapping;
|
||||
if (!config->labels.empty()) return SQ_ERROR;
|
||||
|
||||
/* Read the table and find all labels */
|
||||
sq_pushnull(vm);
|
||||
@@ -260,10 +245,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
|
||||
key_string++;
|
||||
}
|
||||
int key = atoi(key_string) * sign;
|
||||
StrMakeValidInPlace(const_cast<char *>(label));
|
||||
|
||||
/* !Contains() prevents stredup from leaking. */
|
||||
if (!config->labels->Contains(key)) config->labels->Insert(key, stredup(label));
|
||||
config->labels[key] = StrMakeValid(label);
|
||||
|
||||
sq_pop(vm, 2);
|
||||
}
|
||||
@@ -272,7 +254,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
|
||||
/* Check labels for completeness */
|
||||
config->complete_labels = true;
|
||||
for (int value = config->min_value; value <= config->max_value; value++) {
|
||||
if (!config->labels->Contains(value)) {
|
||||
if (config->labels.find(value) == config->labels.end()) {
|
||||
config->complete_labels = false;
|
||||
break;
|
||||
}
|
||||
@@ -286,26 +268,19 @@ const ScriptConfigItemList *ScriptInfo::GetConfigList() const
|
||||
return &this->config_list;
|
||||
}
|
||||
|
||||
const ScriptConfigItem *ScriptInfo::GetConfigItem(const char *name) const
|
||||
const ScriptConfigItem *ScriptInfo::GetConfigItem(const std::string_view name) const
|
||||
{
|
||||
for (const auto &item : this->config_list) {
|
||||
if (strcmp(item.name, name) == 0) return &item;
|
||||
if (item.name == name) return &item;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int ScriptInfo::GetSettingDefaultValue(const char *name) const
|
||||
int ScriptInfo::GetSettingDefaultValue(const std::string &name) const
|
||||
{
|
||||
for (const auto &item : this->config_list) {
|
||||
if (strcmp(item.name, name) != 0) 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();
|
||||
}
|
||||
if (item.name != name) continue;
|
||||
return item.default_value;
|
||||
}
|
||||
|
||||
/* There is no such setting */
|
||||
|
||||
Reference in New Issue
Block a user