Codechange: use std::map over SmallMap and std::string of stredup(char*)

This commit is contained in:
Rubidium 2023-05-05 16:48:53 +02:00 committed by rubidium42
parent 12085d088c
commit 1ae7eb1594
6 changed files with 52 additions and 76 deletions

View File

@ -193,7 +193,7 @@ struct GSConfigWindow : public Window {
StringID str;
TextColour colour;
uint idx = 0;
if (StrEmpty(config_item.description)) {
if (config_item.description.empty()) {
str = STR_JUST_STRING;
colour = TC_ORANGE;
} else {
@ -211,9 +211,10 @@ struct GSConfigWindow : public Window {
} else {
DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
}
if (config_item.labels != nullptr && config_item.labels->Contains(current_value)) {
auto config_iterator = config_item.labels.find(current_value);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value);
@ -311,7 +312,7 @@ struct GSConfigWindow : public Window {
DropDownList list;
for (int i = config_item.min_value; i <= config_item.max_value; i++) {
list.emplace_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false));
list.emplace_back(new DropDownListCharStringItem(config_item.labels.find(i)->second, i, false));
}
ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE);

View File

@ -24,8 +24,7 @@ void ScriptConfig::Change(const char *name, int version, bool force_exact_match,
this->info = (name == nullptr) ? nullptr : this->FindInfo(this->name, version, force_exact_match);
this->version = (info == nullptr) ? -1 : info->GetVersion();
this->is_random = is_random;
if (this->config_list != nullptr) delete this->config_list;
this->config_list = (info == nullptr) ? nullptr : new ScriptConfigItemList();
this->config_list.reset();
this->to_load_data.reset();
this->ClearConfigList();
@ -48,7 +47,6 @@ ScriptConfig::ScriptConfig(const ScriptConfig *config)
this->name = (config->name == nullptr) ? nullptr : stredup(config->name);
this->info = config->info;
this->version = config->version;
this->config_list = nullptr;
this->is_random = config->is_random;
this->to_load_data.reset();
@ -64,7 +62,6 @@ ScriptConfig::~ScriptConfig()
{
free(this->name);
this->ResetSettings();
if (this->config_list != nullptr) delete this->config_list;
this->to_load_data.reset();
}
@ -77,9 +74,9 @@ const ScriptConfigItemList *ScriptConfig::GetConfigList()
{
if (this->info != nullptr) return this->info->GetConfigList();
if (this->config_list == nullptr) {
this->config_list = new ScriptConfigItemList();
this->config_list = std::make_unique<ScriptConfigItemList>();
}
return this->config_list;
return this->config_list.get();
}
void ScriptConfig::ClearConfigList()
@ -96,14 +93,14 @@ void ScriptConfig::AnchorUnchangeableSettings()
}
}
int ScriptConfig::GetSetting(const char *name) const
int ScriptConfig::GetSetting(const std::string &name) const
{
const auto it = this->settings.find(name);
if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
return (*it).second;
}
void ScriptConfig::SetSetting(const char *name, int value)
void ScriptConfig::SetSetting(const std::string &name, int value)
{
/* You can only set Script specific settings if an Script is selected. */
if (this->info == nullptr) return;
@ -126,7 +123,7 @@ void ScriptConfig::ResetEditableSettings(bool yet_to_start)
if (this->info == nullptr) return ResetSettings();
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first.c_str());
const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
assert(config_item != nullptr);
bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;

View File

@ -29,26 +29,26 @@ enum ScriptConfigFlags {
SCRIPTCONFIG_DEVELOPER = 0x8, ///< This setting will only be visible when the Script development tools are active.
};
typedef SmallMap<int, char *> LabelMapping; ///< Map-type used to map the setting numbers to labels.
typedef std::map<int, std::string> LabelMapping; ///< Map-type used to map the setting numbers to labels.
/** Info about a single Script setting. */
struct ScriptConfigItem {
const char *name; ///< The name of the configuration setting.
const char *description; ///< The description of the 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 custom_value; ///< The default value on custom difficulty setting.
int easy_value; ///< The default value on easy difficulty setting.
int medium_value; ///< The default value on medium difficulty setting.
int hard_value; ///< The default value on hard difficulty setting.
int random_deviation; ///< The maximum random deviation from the default value.
int step_size; ///< The step size in the gui.
ScriptConfigFlags flags; ///< Flags for the configuration setting.
LabelMapping *labels; ///< Text labels for the integer values.
bool complete_labels; ///< True if all values have a label.
std::string name; ///< The name of the configuration setting.
std::string description; ///< The description of the configuration setting.
int min_value = 0; ///< The minimal value this configuration setting can have.
int max_value = 1; ///< The maximal value this configuration setting can have.
int custom_value = 0; ///< The default value on custom difficulty setting.
int easy_value = 0; ///< The default value on easy difficulty setting.
int medium_value = 0; ///< The default value on medium difficulty setting.
int hard_value = 0; ///< The default value on hard difficulty setting.
int random_deviation = 0; ///< The maximum random deviation from the default value.
int step_size = 1; ///< The step size in the gui.
ScriptConfigFlags flags = SCRIPTCONFIG_NONE; ///< Flags for the configuration setting.
LabelMapping labels; ///< Text labels for the integer values.
bool complete_labels = false; ///< True if all values have a label.
};
typedef std::list<ScriptConfigItem> ScriptConfigItemList; ///< List of ScriptConfig items.
typedef std::vector<ScriptConfigItem> ScriptConfigItemList; ///< List of ScriptConfig items.
/**
* Script settings.
@ -63,7 +63,6 @@ public:
name(nullptr),
version(-1),
info(nullptr),
config_list(nullptr),
is_random(false),
to_load_data(nullptr)
{}
@ -124,12 +123,12 @@ public:
* @return The (default) value of the setting, or -1 if the setting was not
* found.
*/
int GetSetting(const char *name) const;
int GetSetting(const std::string &name) const;
/**
* Set the value of a setting for this config.
*/
void SetSetting(const char *name, int value);
void SetSetting(const std::string &name, int value);
/**
* Reset all settings to their default value.
@ -195,7 +194,7 @@ protected:
int version; ///< Version of the Script
class ScriptInfo *info; ///< ScriptInfo object for related to this Script version
SettingValueList settings; ///< List with all setting=>value pairs that are configure for this Script
ScriptConfigItemList *config_list; ///< List with all settings defined by this Script
std::unique_ptr<ScriptConfigItemList> config_list; ///< List with all settings defined by this Script
bool is_random; ///< True if the AI in this slot was randomly chosen.
std::unique_ptr<ScriptInstance::ScriptData> to_load_data; ///< Data to load after the Script start.

View File

@ -378,7 +378,7 @@ struct ScriptSettingsWindow : public Window {
StringID str;
TextColour colour;
uint idx = 0;
if (StrEmpty(config_item.description)) {
if (config_item.description.empty()) {
str = STR_JUST_STRING;
colour = TC_ORANGE;
} else {
@ -396,9 +396,11 @@ struct ScriptSettingsWindow : public Window {
} else {
DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
}
if (config_item.labels != nullptr && config_item.labels->Contains(current_value)) {
auto config_iterator = config_item.labels.find(current_value);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_item.labels->Find(current_value)->second);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value);
@ -429,7 +431,7 @@ struct ScriptSettingsWindow : public Window {
VisibleSettingsList::const_iterator it = this->visible_settings.begin();
for (int i = 0; i < num; i++) it++;
const ScriptConfigItem config_item = **it;
const ScriptConfigItem &config_item = **it;
if (!this->IsEditableItem(config_item)) return;
if (this->clicked_row != num) {
@ -468,7 +470,7 @@ struct ScriptSettingsWindow : public Window {
DropDownList list;
for (int i = config_item.min_value; i <= config_item.max_value; i++) {
list.emplace_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false));
list.emplace_back(new DropDownListCharStringItem(config_item.labels.find(i)->second, i, false));
}
ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE);
@ -577,7 +579,7 @@ private:
{
VisibleSettingsList::const_iterator it = this->visible_settings.begin();
for (int i = 0; i < this->clicked_row; i++) it++;
const ScriptConfigItem config_item = **it;
const ScriptConfigItem &config_item = **it;
if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
this->script_config->SetSetting(config_item.name, value);
this->SetDirty();

View File

@ -19,19 +19,6 @@
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);
@ -112,9 +99,6 @@ bool ScriptInfo::GetSettings()
SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
{
ScriptConfigItem config;
memset(&config, 0, sizeof(config));
config.max_value = 1;
config.step_size = 1;
uint items = 0;
/* Read the table, and find all properties we care about */
@ -127,21 +111,17 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
if (strcmp(key, "name") == 0) {
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) {
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) {
SQInteger res;
@ -218,7 +198,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
return SQ_ERROR;
}
this->config_list.push_back(config);
this->config_list.emplace_back(config);
return 0;
}
@ -230,7 +210,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
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) {
@ -239,9 +219,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
this->engine->ThrowError(error);
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);
@ -262,8 +240,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
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] = label;
sq_pop(vm, 2);
}
@ -272,7 +249,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,18 +263,18 @@ const ScriptConfigItemList *ScriptInfo::GetConfigList() const
return &this->config_list;
}
const ScriptConfigItem *ScriptInfo::GetConfigItem(const char *name) const
const ScriptConfigItem *ScriptInfo::GetConfigItem(const std::string &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;
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;

View File

@ -122,7 +122,7 @@ public:
/**
* Get the description of a certain Script config option.
*/
const ScriptConfigItem *GetConfigItem(const char *name) const;
const ScriptConfigItem *GetConfigItem(const std::string &name) const;
/**
* Set a setting.
@ -137,7 +137,7 @@ public:
/**
* Get the default value for a setting.
*/
int GetSettingDefaultValue(const char *name) const;
int GetSettingDefaultValue(const std::string &name) const;
/**
* Can this script be selected by developers only?