diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -21,23 +21,13 @@ #include "textfile_gui.h" #include "thread.h" #include "newgrf_config.h" +#include "newgrf_text.h" #include "fileio_func.h" #include "fios.h" #include "safeguards.h" -/** Create a new GRFTextWrapper. */ -GRFTextWrapper::GRFTextWrapper() : - text(nullptr) -{ -} - -/** Cleanup a GRFTextWrapper object. */ -GRFTextWrapper::~GRFTextWrapper() -{ - CleanUpGRFText(this->text); -} /** * Create a new GRFConfig. @@ -45,15 +35,9 @@ GRFTextWrapper::~GRFTextWrapper() * is copied so the original string isn't needed after the constructor. */ GRFConfig::GRFConfig(const char *filename) : - name(new GRFTextWrapper()), - info(new GRFTextWrapper()), - url(new GRFTextWrapper()), num_valid_params(lengthof(param)) { if (filename != nullptr) this->filename = stredup(filename); - this->name->AddRef(); - this->info->AddRef(); - this->url->AddRef(); } /** @@ -79,9 +63,6 @@ GRFConfig::GRFConfig(const GRFConfig &co MemCpyT(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum)); MemCpyT(this->param, config.param, lengthof(this->param)); if (config.filename != nullptr) this->filename = stredup(config.filename); - this->name->AddRef(); - this->info->AddRef(); - this->url->AddRef(); if (config.error != nullptr) this->error = new GRFError(*config.error); for (uint i = 0; i < config.param_info.size(); i++) { if (config.param_info[i] == nullptr) { @@ -100,9 +81,6 @@ GRFConfig::~GRFConfig() free(this->filename); delete this->error; } - this->name->Release(); - this->info->Release(); - this->url->Release(); for (uint i = 0; i < this->param_info.size(); i++) delete this->param_info[i]; } @@ -125,7 +103,7 @@ void GRFConfig::CopyParams(const GRFConf */ const char *GRFConfig::GetName() const { - const char *name = GetGRFStringFromGRFText(this->name->text); + const char *name = GetGRFStringFromGRFText(this->name); return StrEmpty(name) ? this->filename : name; } @@ -135,7 +113,7 @@ const char *GRFConfig::GetName() const */ const char *GRFConfig::GetDescription() const { - return GetGRFStringFromGRFText(this->info->text); + return GetGRFStringFromGRFText(this->info); } /** @@ -144,7 +122,7 @@ const char *GRFConfig::GetDescription() */ const char *GRFConfig::GetURL() const { - return GetGRFStringFromGRFText(this->url->text); + return GetGRFStringFromGRFText(this->url); } /** Set the default value for all parameters as specified by action14. */ @@ -232,8 +210,8 @@ GRFError::~GRFError() * @param nr The newgrf parameter that is changed. */ GRFParameterInfo::GRFParameterInfo(uint nr) : - name(nullptr), - desc(nullptr), + name(), + desc(), type(PTYPE_UINT_ENUM), min_value(0), max_value(UINT32_MAX), @@ -241,6 +219,7 @@ GRFParameterInfo::GRFParameterInfo(uint param_nr(nr), first_bit(0), num_bit(32), + value_names(), complete_labels(false) {} @@ -250,8 +229,8 @@ GRFParameterInfo::GRFParameterInfo(uint * @param info The GRFParameterInfo object to make a copy of. */ GRFParameterInfo::GRFParameterInfo(GRFParameterInfo &info) : - name(DuplicateGRFText(info.name)), - desc(DuplicateGRFText(info.desc)), + name(info.name), + desc(info.desc), type(info.type), min_value(info.min_value), max_value(info.max_value), @@ -259,23 +238,9 @@ GRFParameterInfo::GRFParameterInfo(GRFPa param_nr(info.param_nr), first_bit(info.first_bit), num_bit(info.num_bit), + value_names(info.value_names), complete_labels(info.complete_labels) { - for (uint i = 0; i < info.value_names.size(); i++) { - std::pair *data = info.value_names.data() + i; - this->value_names.Insert(data->first, DuplicateGRFText(data->second)); - } -} - -/** Cleanup all parameter info. */ -GRFParameterInfo::~GRFParameterInfo() -{ - CleanUpGRFText(this->name); - CleanUpGRFText(this->desc); - for (uint i = 0; i < this->value_names.size(); i++) { - std::pair *data = this->value_names.data() + i; - CleanUpGRFText(data->second); - } } /** @@ -598,12 +563,8 @@ compatible_grf: free(c->filename); c->filename = stredup(f->filename); memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum)); - c->name->Release(); c->name = f->name; - c->name->AddRef(); - c->info->Release(); c->info = f->name; - c->info->AddRef(); c->error = nullptr; c->version = f->version; c->min_loadable_version = f->min_loadable_version; @@ -686,7 +647,7 @@ bool GRFFileScanner::AddFile(const char _modal_progress_paint_mutex.lock(); const char *name = nullptr; - if (c->name != nullptr) name = GetGRFStringFromGRFText(c->name->text); + if (c->name != nullptr) name = GetGRFStringFromGRFText(c->name); if (name == nullptr) name = c->filename; UpdateNewGRFScanStatus(this->num_scanned, name); @@ -820,8 +781,12 @@ const GRFConfig *FindGRFConfig(uint32 gr /** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */ struct UnknownGRF : public GRFIdentifier { - UnknownGRF *next; ///< The next unknown GRF. - GRFTextWrapper *name; ///< Name of the GRF. + GRFTextWrapper name; ///< Name of the GRF. + + UnknownGRF() = default; + UnknownGRF(const UnknownGRF &other) = default; + UnknownGRF(UnknownGRF &&other) = default; + UnknownGRF(uint32 grfid, const uint8 *_md5sum) : GRFIdentifier(grfid, _md5sum), name(new GRFTextList) {} }; /** @@ -841,30 +806,24 @@ struct UnknownGRF : public GRFIdentifier * and MD5 checksum or nullptr when it does not exist and create is false. * This value must NEVER be freed by the caller. */ -GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create) +GRFTextWrapper FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create) { - UnknownGRF *grf; - static UnknownGRF *unknown_grfs = nullptr; + static std::vector unknown_grfs; - for (grf = unknown_grfs; grf != nullptr; grf = grf->next) { - if (grf->grfid == grfid) { - if (memcmp(md5sum, grf->md5sum, sizeof(grf->md5sum)) == 0) return grf->name; + for (const auto &grf : unknown_grfs) { + if (grf.grfid == grfid) { + if (memcmp(md5sum, grf.md5sum, sizeof(grf.md5sum)) == 0) return grf.name; } } if (!create) return nullptr; - grf = CallocT(1); - grf->grfid = grfid; - grf->next = unknown_grfs; - grf->name = new GRFTextWrapper(); - grf->name->AddRef(); + unknown_grfs.emplace_back(grfid, md5sum); + UnknownGRF &grf = unknown_grfs.back(); - AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER); - memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum)); + AddGRFTextToList(grf.name, UNKNOWN_GRF_NAME_PLACEHOLDER); - unknown_grfs = grf; - return grf->name; + return grf.name; } /**