Codechange: use std::string as std::map key, instead of stredup string
This commit is contained in:
@@ -76,16 +76,6 @@ ScriptController::ScriptController(CompanyID company) :
|
||||
ScriptObject::SetCompany(company);
|
||||
}
|
||||
|
||||
ScriptController::~ScriptController()
|
||||
{
|
||||
for (const auto &item : this->loaded_library) {
|
||||
free(item.second);
|
||||
free(item.first);
|
||||
}
|
||||
|
||||
this->loaded_library.clear();
|
||||
}
|
||||
|
||||
/* static */ uint ScriptController::GetTick()
|
||||
{
|
||||
return ScriptObject::GetActiveInstance()->GetController()->ticks;
|
||||
@@ -120,24 +110,22 @@ ScriptController::~ScriptController()
|
||||
}
|
||||
|
||||
/* Internally we store libraries as 'library.version' */
|
||||
char library_name[1024];
|
||||
seprintf(library_name, lastof(library_name), "%s.%d", library, version);
|
||||
strtolower(library_name);
|
||||
std::string library_name = fmt::format("{}.{}", library, version);
|
||||
|
||||
/* Get the current table/class we belong to */
|
||||
HSQOBJECT parent;
|
||||
sq_getstackobj(vm, 1, &parent);
|
||||
|
||||
char fake_class[1024];
|
||||
std::string fake_class;
|
||||
|
||||
LoadedLibraryList::iterator it = controller->loaded_library.find(library_name);
|
||||
if (it != controller->loaded_library.end()) {
|
||||
strecpy(fake_class, (*it).second, lastof(fake_class));
|
||||
fake_class = (*it).second;
|
||||
} else {
|
||||
int next_number = ++controller->loaded_library_count;
|
||||
|
||||
/* Create a new fake internal name */
|
||||
seprintf(fake_class, lastof(fake_class), "_internalNA%d", next_number);
|
||||
fake_class = fmt::format("_internalNA{}", next_number);
|
||||
|
||||
/* Load the library in a 'fake' namespace, so we can link it to the name the user requested */
|
||||
sq_pushroottable(vm);
|
||||
@@ -153,7 +141,7 @@ ScriptController::~ScriptController()
|
||||
sq_newslot(vm, -3, SQFalse);
|
||||
sq_pop(vm, 1);
|
||||
|
||||
controller->loaded_library[stredup(library_name)] = stredup(fake_class);
|
||||
controller->loaded_library[library_name] = fake_class;
|
||||
}
|
||||
|
||||
/* Find the real class inside the fake class (like 'sets.Vector') */
|
||||
|
@@ -55,11 +55,6 @@ public:
|
||||
*/
|
||||
ScriptController(CompanyID company);
|
||||
|
||||
/**
|
||||
* Destructor of the ScriptController.
|
||||
*/
|
||||
~ScriptController();
|
||||
|
||||
#else
|
||||
/**
|
||||
* This function is called to start your script. Your script starts here. If you
|
||||
@@ -210,7 +205,7 @@ public:
|
||||
static HSQOBJECT Import(const char *library, const char *class_name, int version);
|
||||
|
||||
private:
|
||||
typedef std::map<const char *, const char *, StringCompare> LoadedLibraryList; ///< The type for loaded libraries.
|
||||
typedef std::map<std::string, std::string, CaseInsensitiveComparator> LoadedLibraryList; ///< The type for loaded libraries.
|
||||
|
||||
uint ticks; ///< The amount of ticks we're sleeping.
|
||||
LoadedLibraryList loaded_library; ///< The libraries we loaded.
|
||||
|
@@ -53,7 +53,7 @@ ScriptConfig::ScriptConfig(const ScriptConfig *config)
|
||||
this->to_load_data.reset();
|
||||
|
||||
for (const auto &item : config->settings) {
|
||||
this->settings[stredup(item.first)] = item.second;
|
||||
this->settings[item.first] = item.second;
|
||||
}
|
||||
|
||||
/* Virtual functions get called statically in constructors, so make it explicit to remove any confusion. */
|
||||
@@ -84,9 +84,6 @@ const ScriptConfigItemList *ScriptConfig::GetConfigList()
|
||||
|
||||
void ScriptConfig::ClearConfigList()
|
||||
{
|
||||
for (const auto &item : this->settings) {
|
||||
free(item.first);
|
||||
}
|
||||
this->settings.clear();
|
||||
}
|
||||
|
||||
@@ -116,19 +113,11 @@ void ScriptConfig::SetSetting(const char *name, int value)
|
||||
|
||||
value = Clamp(value, config_item->min_value, config_item->max_value);
|
||||
|
||||
const auto it = this->settings.find(name);
|
||||
if (it != this->settings.end()) {
|
||||
(*it).second = value;
|
||||
} else {
|
||||
this->settings[stredup(name)] = value;
|
||||
}
|
||||
this->settings[name] = value;
|
||||
}
|
||||
|
||||
void ScriptConfig::ResetSettings()
|
||||
{
|
||||
for (const auto &item : this->settings) {
|
||||
free(item.first);
|
||||
}
|
||||
this->settings.clear();
|
||||
}
|
||||
|
||||
@@ -137,14 +126,13 @@ 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);
|
||||
const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first.c_str());
|
||||
assert(config_item != nullptr);
|
||||
|
||||
bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
|
||||
bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
|
||||
|
||||
if (editable && visible) {
|
||||
free(it->first);
|
||||
it = this->settings.erase(it);
|
||||
} else {
|
||||
it++;
|
||||
@@ -209,29 +197,16 @@ void ScriptConfig::StringToSettings(const std::string &value)
|
||||
|
||||
std::string ScriptConfig::SettingsToString() const
|
||||
{
|
||||
char string[1024];
|
||||
char *last = lastof(string);
|
||||
char *s = string;
|
||||
*s = '\0';
|
||||
if (this->settings.empty()) return {};
|
||||
|
||||
std::string result;
|
||||
for (const auto &item : this->settings) {
|
||||
char no[INT32_DIGITS_WITH_SIGN_AND_TERMINATION];
|
||||
seprintf(no, lastof(no), "%d", item.second);
|
||||
|
||||
/* Check if the string would fit in the destination */
|
||||
size_t needed_size = strlen(item.first) + 1 + strlen(no);
|
||||
/* If it doesn't fit, skip the next settings */
|
||||
if (s + needed_size > last) break;
|
||||
|
||||
s = strecat(s, item.first, last);
|
||||
s = strecat(s, "=", last);
|
||||
s = strecat(s, no, last);
|
||||
s = strecat(s, ",", last);
|
||||
fmt::format_to(std::back_inserter(result), "{}={},", item.first, item.second);
|
||||
}
|
||||
|
||||
/* Remove the last ',', but only if at least one setting was saved. */
|
||||
if (s != string) s[-1] = '\0';
|
||||
|
||||
return string;
|
||||
/* Remove the last ','. */
|
||||
result.resize(result.size() - 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
|
||||
|
@@ -57,7 +57,7 @@ typedef std::list<ScriptConfigItem> ScriptConfigItemList; ///< List of ScriptCon
|
||||
class ScriptConfig {
|
||||
protected:
|
||||
/** List with name=>value pairs of all script-specific settings */
|
||||
typedef std::map<const char *, int, StringCompare> SettingValueList;
|
||||
typedef std::map<std::string, int> SettingValueList;
|
||||
|
||||
public:
|
||||
ScriptConfig() :
|
||||
|
@@ -84,12 +84,8 @@ void ScriptScanner::RescanDir()
|
||||
void ScriptScanner::Reset()
|
||||
{
|
||||
for (const auto &item : this->info_list) {
|
||||
free(item.first);
|
||||
delete item.second;
|
||||
}
|
||||
for (const auto &item : this->info_single_list) {
|
||||
free(item.first);
|
||||
}
|
||||
|
||||
this->info_list.clear();
|
||||
this->info_single_list.clear();
|
||||
@@ -128,15 +124,16 @@ void ScriptScanner::RegisterScript(ScriptInfo *info)
|
||||
return;
|
||||
}
|
||||
|
||||
this->info_list[stredup(script_name)] = info;
|
||||
this->info_list[script_name] = info;
|
||||
|
||||
if (!info->IsDeveloperOnly() || _settings_client.gui.ai_developer_tools) {
|
||||
/* Add the script to the 'unique' script list, where only the highest version
|
||||
* of the script is registered. */
|
||||
if (this->info_single_list.find(script_original_name) == this->info_single_list.end()) {
|
||||
this->info_single_list[stredup(script_original_name)] = info;
|
||||
} else if (this->info_single_list[script_original_name]->GetVersion() < info->GetVersion()) {
|
||||
auto it = this->info_single_list.find(script_original_name);
|
||||
if (it == this->info_single_list.end()) {
|
||||
this->info_single_list[script_original_name] = info;
|
||||
} else if (it->second->GetVersion() < info->GetVersion()) {
|
||||
it->second = info;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,9 +12,9 @@
|
||||
|
||||
#include <map>
|
||||
#include "../fileio_func.h"
|
||||
#include "../core/string_compare_type.hpp"
|
||||
#include "../string_func.h"
|
||||
|
||||
typedef std::map<const char *, class ScriptInfo *, StringCompare> ScriptInfoList; ///< Type for the list of scripts.
|
||||
typedef std::map<std::string, class ScriptInfo *, CaseInsensitiveComparator> ScriptInfoList; ///< Type for the list of scripts.
|
||||
|
||||
/** Scanner to help finding scripts. */
|
||||
class ScriptScanner : public FileScanner {
|
||||
|
Reference in New Issue
Block a user