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; StringID str;
TextColour colour; TextColour colour;
uint idx = 0; uint idx = 0;
if (StrEmpty(config_item.description)) { if (config_item.description.empty()) {
str = STR_JUST_STRING; str = STR_JUST_STRING;
colour = TC_ORANGE; colour = TC_ORANGE;
} else { } else {
@ -211,9 +211,10 @@ struct GSConfigWindow : public Window {
} else { } 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); 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); SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_item.labels->Find(current_value)->second); SetDParamStr(idx++, config_iterator->second);
} else { } else {
SetDParam(idx++, STR_JUST_INT); SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value); SetDParam(idx++, current_value);
@ -311,7 +312,7 @@ struct GSConfigWindow : public Window {
DropDownList list; DropDownList list;
for (int i = config_item.min_value; i <= config_item.max_value; i++) { 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); 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->info = (name == nullptr) ? nullptr : this->FindInfo(this->name, version, force_exact_match);
this->version = (info == nullptr) ? -1 : info->GetVersion(); this->version = (info == nullptr) ? -1 : info->GetVersion();
this->is_random = is_random; this->is_random = is_random;
if (this->config_list != nullptr) delete this->config_list; this->config_list.reset();
this->config_list = (info == nullptr) ? nullptr : new ScriptConfigItemList();
this->to_load_data.reset(); this->to_load_data.reset();
this->ClearConfigList(); this->ClearConfigList();
@ -48,7 +47,6 @@ ScriptConfig::ScriptConfig(const ScriptConfig *config)
this->name = (config->name == nullptr) ? nullptr : stredup(config->name); this->name = (config->name == nullptr) ? nullptr : stredup(config->name);
this->info = config->info; this->info = config->info;
this->version = config->version; this->version = config->version;
this->config_list = nullptr;
this->is_random = config->is_random; this->is_random = config->is_random;
this->to_load_data.reset(); this->to_load_data.reset();
@ -64,7 +62,6 @@ ScriptConfig::~ScriptConfig()
{ {
free(this->name); free(this->name);
this->ResetSettings(); this->ResetSettings();
if (this->config_list != nullptr) delete this->config_list;
this->to_load_data.reset(); this->to_load_data.reset();
} }
@ -77,9 +74,9 @@ const ScriptConfigItemList *ScriptConfig::GetConfigList()
{ {
if (this->info != nullptr) return this->info->GetConfigList(); if (this->info != nullptr) return this->info->GetConfigList();
if (this->config_list == nullptr) { 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() 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); const auto it = this->settings.find(name);
if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name); if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
return (*it).second; 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. */ /* You can only set Script specific settings if an Script is selected. */
if (this->info == nullptr) return; if (this->info == nullptr) return;
@ -126,7 +123,7 @@ void ScriptConfig::ResetEditableSettings(bool yet_to_start)
if (this->info == nullptr) return ResetSettings(); if (this->info == nullptr) return ResetSettings();
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) { 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); assert(config_item != nullptr);
bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0; 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. 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. */ /** Info about a single Script setting. */
struct ScriptConfigItem { struct ScriptConfigItem {
const char *name; ///< The name of the configuration setting. std::string name; ///< The name of the configuration setting.
const char *description; ///< The description of the configuration setting. std::string description; ///< The description of the configuration setting.
int min_value; ///< The minimal value this configuration setting can have. int min_value = 0; ///< The minimal value this configuration setting can have.
int max_value; ///< The maximal value this configuration setting can have. int max_value = 1; ///< The maximal value this configuration setting can have.
int custom_value; ///< The default value on custom difficulty setting. int custom_value = 0; ///< The default value on custom difficulty setting.
int easy_value; ///< The default value on easy difficulty setting. int easy_value = 0; ///< The default value on easy difficulty setting.
int medium_value; ///< The default value on medium difficulty setting. int medium_value = 0; ///< The default value on medium difficulty setting.
int hard_value; ///< The default value on hard difficulty setting. int hard_value = 0; ///< The default value on hard difficulty setting.
int random_deviation; ///< The maximum random deviation from the default value. int random_deviation = 0; ///< The maximum random deviation from the default value.
int step_size; ///< The step size in the gui. int step_size = 1; ///< The step size in the gui.
ScriptConfigFlags flags; ///< Flags for the configuration setting. ScriptConfigFlags flags = SCRIPTCONFIG_NONE; ///< Flags for the configuration setting.
LabelMapping *labels; ///< Text labels for the integer values. LabelMapping labels; ///< Text labels for the integer values.
bool complete_labels; ///< True if all values have a label. 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. * Script settings.
@ -63,7 +63,6 @@ public:
name(nullptr), name(nullptr),
version(-1), version(-1),
info(nullptr), info(nullptr),
config_list(nullptr),
is_random(false), is_random(false),
to_load_data(nullptr) to_load_data(nullptr)
{} {}
@ -124,12 +123,12 @@ public:
* @return The (default) value of the setting, or -1 if the setting was not * @return The (default) value of the setting, or -1 if the setting was not
* found. * found.
*/ */
int GetSetting(const char *name) const; int GetSetting(const std::string &name) const;
/** /**
* Set the value of a setting for this config. * 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. * Reset all settings to their default value.
@ -195,7 +194,7 @@ protected:
int version; ///< Version of the Script int version; ///< Version of the Script
class ScriptInfo *info; ///< ScriptInfo object for related to this Script version 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 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. 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. 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; StringID str;
TextColour colour; TextColour colour;
uint idx = 0; uint idx = 0;
if (StrEmpty(config_item.description)) { if (config_item.description.empty()) {
str = STR_JUST_STRING; str = STR_JUST_STRING;
colour = TC_ORANGE; colour = TC_ORANGE;
} else { } else {
@ -396,9 +396,11 @@ struct ScriptSettingsWindow : public Window {
} else { } 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); 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); SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_item.labels->Find(current_value)->second); SetDParamStr(idx++, config_iterator->second);
} else { } else {
SetDParam(idx++, STR_JUST_INT); SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value); SetDParam(idx++, current_value);
@ -429,7 +431,7 @@ struct ScriptSettingsWindow : public Window {
VisibleSettingsList::const_iterator it = this->visible_settings.begin(); VisibleSettingsList::const_iterator it = this->visible_settings.begin();
for (int i = 0; i < num; i++) it++; 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->IsEditableItem(config_item)) return;
if (this->clicked_row != num) { if (this->clicked_row != num) {
@ -468,7 +470,7 @@ struct ScriptSettingsWindow : public Window {
DropDownList list; DropDownList list;
for (int i = config_item.min_value; i <= config_item.max_value; i++) { 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); 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(); VisibleSettingsList::const_iterator it = this->visible_settings.begin();
for (int i = 0; i < this->clicked_row; i++) it++; 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; 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->script_config->SetSetting(config_item.name, value);
this->SetDirty(); this->SetDirty();

View File

@ -19,19 +19,6 @@
ScriptInfo::~ScriptInfo() 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->author);
free(this->name); free(this->name);
free(this->short_name); free(this->short_name);
@ -112,9 +99,6 @@ bool ScriptInfo::GetSettings()
SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
{ {
ScriptConfigItem config; ScriptConfigItem config;
memset(&config, 0, sizeof(config));
config.max_value = 1;
config.step_size = 1;
uint items = 0; uint items = 0;
/* Read the table, and find all properties we care about */ /* Read the table, and find all properties we care about */
@ -127,21 +111,17 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
if (strcmp(key, "name") == 0) { if (strcmp(key, "name") == 0) {
const SQChar *sqvalue; const SQChar *sqvalue;
if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR; 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 /* Don't allow '=' and ',' in configure setting names, as we need those
* 2 chars to nicely store the settings as a string. */ * 2 chars to nicely store the settings as a string. */
while ((s = strchr(name, '=')) != nullptr) *s = '_'; auto replace_with_underscore = [](auto c) { return c == '=' || c == ','; };
while ((s = strchr(name, ',')) != nullptr) *s = '_'; config.name = StrMakeValid(sqvalue);
config.name = name; std::replace_if(config.name.begin(), config.name.end(), replace_with_underscore, '_');
items |= 0x001; items |= 0x001;
} else if (strcmp(key, "description") == 0) { } else if (strcmp(key, "description") == 0) {
const SQChar *sqdescription; const SQChar *sqdescription;
if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR; if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
config.description = stredup(sqdescription); config.description = StrMakeValid(sqdescription);
StrMakeValidInPlace(const_cast<char *>(config.description));
items |= 0x002; items |= 0x002;
} else if (strcmp(key, "min_value") == 0) { } else if (strcmp(key, "min_value") == 0) {
SQInteger res; SQInteger res;
@ -218,7 +198,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
return SQ_ERROR; return SQ_ERROR;
} }
this->config_list.push_back(config); this->config_list.emplace_back(config);
return 0; return 0;
} }
@ -230,7 +210,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
ScriptConfigItem *config = nullptr; ScriptConfigItem *config = nullptr;
for (auto &item : this->config_list) { 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) { if (config == nullptr) {
@ -239,9 +219,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
this->engine->ThrowError(error); this->engine->ThrowError(error);
return SQ_ERROR; return SQ_ERROR;
} }
if (config->labels != nullptr) return SQ_ERROR; if (!config->labels.empty()) return SQ_ERROR;
config->labels = new LabelMapping;
/* Read the table and find all labels */ /* Read the table and find all labels */
sq_pushnull(vm); sq_pushnull(vm);
@ -262,8 +240,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
int key = atoi(key_string) * sign; int key = atoi(key_string) * sign;
StrMakeValidInPlace(const_cast<char *>(label)); StrMakeValidInPlace(const_cast<char *>(label));
/* !Contains() prevents stredup from leaking. */ config->labels[key] = label;
if (!config->labels->Contains(key)) config->labels->Insert(key, stredup(label));
sq_pop(vm, 2); sq_pop(vm, 2);
} }
@ -272,7 +249,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
/* Check labels for completeness */ /* Check labels for completeness */
config->complete_labels = true; config->complete_labels = true;
for (int value = config->min_value; value <= config->max_value; value++) { 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; config->complete_labels = false;
break; break;
} }
@ -286,18 +263,18 @@ const ScriptConfigItemList *ScriptInfo::GetConfigList() const
return &this->config_list; 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) { for (const auto &item : this->config_list) {
if (strcmp(item.name, name) == 0) return &item; if (item.name == name) return &item;
} }
return nullptr; return nullptr;
} }
int ScriptInfo::GetSettingDefaultValue(const char *name) const int ScriptInfo::GetSettingDefaultValue(const std::string &name) const
{ {
for (const auto &item : this->config_list) { 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 */ /* The default value depends on the difficulty level */
switch (GetGameSettings().script.settings_profile) { switch (GetGameSettings().script.settings_profile) {
case SP_EASY: return item.easy_value; case SP_EASY: return item.easy_value;

View File

@ -122,7 +122,7 @@ public:
/** /**
* Get the description of a certain Script config option. * 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. * Set a setting.
@ -137,7 +137,7 @@ public:
/** /**
* Get the default value for a setting. * 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? * Can this script be selected by developers only?