Codechange: Use std::string for most of the user-settable custom names.
This commit is contained in:
@@ -645,20 +645,20 @@ bool AfterLoadGame()
|
||||
if (IsSavegameVersionBefore(SLV_84)) {
|
||||
for (Company *c : Company::Iterate()) {
|
||||
c->name = CopyFromOldName(c->name_1);
|
||||
if (c->name != nullptr) c->name_1 = STR_SV_UNNAMED;
|
||||
if (!c->name.empty()) c->name_1 = STR_SV_UNNAMED;
|
||||
c->president_name = CopyFromOldName(c->president_name_1);
|
||||
if (c->president_name != nullptr) c->president_name_1 = SPECSTR_PRESIDENT_NAME;
|
||||
if (!c->president_name.empty()) c->president_name_1 = SPECSTR_PRESIDENT_NAME;
|
||||
}
|
||||
|
||||
for (Station *st : Station::Iterate()) {
|
||||
st->name = CopyFromOldName(st->string_id);
|
||||
/* generating new name would be too much work for little effect, use the station name fallback */
|
||||
if (st->name != nullptr) st->string_id = STR_SV_STNAME_FALLBACK;
|
||||
if (!st->name.empty()) st->string_id = STR_SV_STNAME_FALLBACK;
|
||||
}
|
||||
|
||||
for (Town *t : Town::Iterate()) {
|
||||
t->name = CopyFromOldName(t->townnametype);
|
||||
if (t->name != nullptr) t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
|
||||
if (!t->name.empty()) t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2502,11 +2502,11 @@ bool AfterLoadGame()
|
||||
* highest possible number to get them numbered in the
|
||||
* order they have in the pool. */
|
||||
for (Waypoint *wp : Waypoint::Iterate()) {
|
||||
if (wp->name != nullptr) wp->town_cn = UINT16_MAX;
|
||||
if (!wp->name.empty()) wp->town_cn = UINT16_MAX;
|
||||
}
|
||||
|
||||
for (Waypoint* wp : Waypoint::Iterate()) {
|
||||
if (wp->name != nullptr) MakeDefaultName(wp);
|
||||
if (!wp->name.empty()) MakeDefaultName(wp);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -242,11 +242,11 @@ void AfterLoadCompanyStats()
|
||||
static const SaveLoad _company_desc[] = {
|
||||
SLE_VAR(CompanyProperties, name_2, SLE_UINT32),
|
||||
SLE_VAR(CompanyProperties, name_1, SLE_STRINGID),
|
||||
SLE_CONDSTR(CompanyProperties, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(CompanyProperties, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
|
||||
|
||||
SLE_VAR(CompanyProperties, president_name_1, SLE_STRINGID),
|
||||
SLE_VAR(CompanyProperties, president_name_2, SLE_UINT32),
|
||||
SLE_CONDSTR(CompanyProperties, president_name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(CompanyProperties, president_name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
|
||||
|
||||
SLE_VAR(CompanyProperties, face, SLE_UINT32),
|
||||
|
||||
@@ -519,7 +519,7 @@ static void Check_PLYR()
|
||||
}
|
||||
}
|
||||
|
||||
if (cprops->name == nullptr && !IsInsideMM(cprops->name_1, SPECSTR_COMPANY_NAME_START, SPECSTR_COMPANY_NAME_LAST + 1) &&
|
||||
if (cprops->name.empty() && !IsInsideMM(cprops->name_1, SPECSTR_COMPANY_NAME_START, SPECSTR_COMPANY_NAME_LAST + 1) &&
|
||||
cprops->name_1 != STR_GAME_SAVELOAD_NOT_AVAILABLE && cprops->name_1 != STR_SV_UNNAMED &&
|
||||
cprops->name_1 != SPECSTR_ANDCO_NAME && cprops->name_1 != SPECSTR_PRESIDENT_NAME &&
|
||||
cprops->name_1 != SPECSTR_SILLY_NAME) {
|
||||
|
@@ -23,7 +23,7 @@ static const SaveLoad _depot_desc[] = {
|
||||
SLEG_CONDVAR(_town_index, SLE_UINT16, SL_MIN_VERSION, SLV_141),
|
||||
SLE_CONDREF(Depot, town, REF_TOWN, SLV_141, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(Depot, town_cn, SLE_UINT16, SLV_141, SL_MAX_VERSION),
|
||||
SLE_CONDSTR(Depot, name, SLE_STR, 0, SLV_141, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Depot, name, SLE_STR, SLV_141, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(Depot, build_date, SLE_INT32, SLV_142, SL_MAX_VERSION),
|
||||
SLE_END()
|
||||
};
|
||||
|
@@ -39,7 +39,7 @@ static const SaveLoad _engine_desc[] = {
|
||||
SLE_CONDVAR(Engine, company_avail, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),
|
||||
SLE_CONDVAR(Engine, company_avail, SLE_UINT16, SLV_104, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(Engine, company_hidden, SLE_UINT16, SLV_193, SL_MAX_VERSION),
|
||||
SLE_CONDSTR(Engine, name, SLE_STR, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Engine, name, SLE_STR, SLV_84, SL_MAX_VERSION),
|
||||
|
||||
SLE_CONDNULL(16, SLV_2, SLV_144), // old reserved space
|
||||
|
||||
@@ -137,7 +137,7 @@ void CopyTempEngineData()
|
||||
e->preview_wait = se->preview_wait;
|
||||
e->company_avail = se->company_avail;
|
||||
e->company_hidden = se->company_hidden;
|
||||
if (se->name != nullptr) e->name = stredup(se->name);
|
||||
e->name = se->name;
|
||||
}
|
||||
|
||||
ResetTempEngineData();
|
||||
|
@@ -17,7 +17,7 @@
|
||||
|
||||
static const SaveLoad _group_desc[] = {
|
||||
SLE_CONDVAR(Group, name, SLE_NAME, SL_MIN_VERSION, SLV_84),
|
||||
SLE_CONDSTR(Group, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Group, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDNULL(2, SL_MIN_VERSION, SLV_164), // num_vehicle
|
||||
SLE_VAR(Group, owner, SLE_UINT8),
|
||||
SLE_VAR(Group, vehicle_type, SLE_UINT8),
|
||||
|
@@ -793,7 +793,7 @@ void WriteValue(void *ptr, VarType conv, int64 val)
|
||||
case SLE_VAR_U32: *(uint32*)ptr = val; break;
|
||||
case SLE_VAR_I64: *(int64 *)ptr = val; break;
|
||||
case SLE_VAR_U64: *(uint64*)ptr = val; break;
|
||||
case SLE_VAR_NAME: *(char**)ptr = CopyFromOldName(val); break;
|
||||
case SLE_VAR_NAME: *reinterpret_cast<std::string *>(ptr) = CopyFromOldName(val); break;
|
||||
case SLE_VAR_NULL: break;
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
@@ -1515,6 +1515,8 @@ static bool IsVariableSizeRight(const SaveLoad *sld)
|
||||
case SLE_VAR_I64:
|
||||
case SLE_VAR_U64:
|
||||
return sld->size == sizeof(int64);
|
||||
case SLE_VAR_NAME:
|
||||
return sld->size == sizeof(std::string);
|
||||
default:
|
||||
return sld->size == sizeof(void *);
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@
|
||||
|
||||
void InitializeOldNames();
|
||||
StringID RemapOldStringID(StringID s);
|
||||
char *CopyFromOldName(StringID id);
|
||||
std::string CopyFromOldName(StringID id);
|
||||
void ResetOldNames();
|
||||
|
||||
void ResetOldWaypoints();
|
||||
|
@@ -18,7 +18,7 @@
|
||||
/** Description of a sign within the savegame. */
|
||||
static const SaveLoad _sign_desc[] = {
|
||||
SLE_CONDVAR(Sign, name, SLE_NAME, SL_MIN_VERSION, SLV_84),
|
||||
SLE_CONDSTR(Sign, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Sign, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(Sign, x, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_5),
|
||||
SLE_CONDVAR(Sign, y, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_5),
|
||||
SLE_CONDVAR(Sign, x, SLE_INT32, SLV_5, SL_MAX_VERSION),
|
||||
|
@@ -62,8 +62,7 @@ void MoveBuoysToWaypoints()
|
||||
TileIndex xy = st->xy;
|
||||
Town *town = st->town;
|
||||
StringID string_id = st->string_id;
|
||||
char *name = st->name;
|
||||
st->name = nullptr;
|
||||
std::string name = st->name;
|
||||
Date build_date = st->build_date;
|
||||
/* TTDPatch could use "buoys with rail station" for rail waypoints */
|
||||
bool train = st->train_station.tile != INVALID_TILE;
|
||||
@@ -176,7 +175,7 @@ static const SaveLoad _old_station_desc[] = {
|
||||
SLE_CONDNULL(1, SL_MIN_VERSION, SLV_4), ///< alpha_order
|
||||
|
||||
SLE_VAR(Station, string_id, SLE_STRINGID),
|
||||
SLE_CONDSTR(Station, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Station, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(Station, indtype, SLE_UINT8, SLV_103, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(Station, had_vehicle_of_type, SLE_FILE_U16 | SLE_VAR_U8, SL_MIN_VERSION, SLV_122),
|
||||
SLE_CONDVAR(Station, had_vehicle_of_type, SLE_UINT8, SLV_122, SL_MAX_VERSION),
|
||||
@@ -389,7 +388,7 @@ static const SaveLoad _base_station_desc[] = {
|
||||
SLE_VAR(BaseStation, xy, SLE_UINT32),
|
||||
SLE_REF(BaseStation, town, REF_TOWN),
|
||||
SLE_VAR(BaseStation, string_id, SLE_STRINGID),
|
||||
SLE_STR(BaseStation, name, SLE_STR | SLF_ALLOW_CONTROL, 0),
|
||||
SLE_SSTR(BaseStation, name, SLE_STR | SLF_ALLOW_CONTROL),
|
||||
SLE_VAR(BaseStation, delete_ctr, SLE_UINT8),
|
||||
SLE_VAR(BaseStation, owner, SLE_UINT8),
|
||||
SLE_VAR(BaseStation, facilities, SLE_UINT8),
|
||||
|
@@ -11,6 +11,7 @@
|
||||
#include "../string_func.h"
|
||||
#include "../strings_func.h"
|
||||
#include "saveload_internal.h"
|
||||
#include <sstream>
|
||||
|
||||
#include "table/strings.h"
|
||||
|
||||
@@ -56,18 +57,17 @@ char *_old_name_array = nullptr;
|
||||
* @param id the StringID of the custom name to clone.
|
||||
* @return the clones custom name.
|
||||
*/
|
||||
char *CopyFromOldName(StringID id)
|
||||
std::string CopyFromOldName(StringID id)
|
||||
{
|
||||
/* Is this name an (old) custom name? */
|
||||
if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return nullptr;
|
||||
if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return std::string();
|
||||
|
||||
if (IsSavegameVersionBefore(SLV_37)) {
|
||||
/* Allow for expansion when converted to UTF-8. */
|
||||
char tmp[LEN_OLD_STRINGS * MAX_CHAR_LENGTH];
|
||||
uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9);
|
||||
const char *strfrom = &_old_name_array[offs];
|
||||
char *strto = tmp;
|
||||
|
||||
std::ostringstream tmp;
|
||||
std::ostreambuf_iterator<char> strto(tmp);
|
||||
for (; *strfrom != '\0'; strfrom++) {
|
||||
WChar c = (byte)*strfrom;
|
||||
|
||||
@@ -84,19 +84,13 @@ char *CopyFromOldName(StringID id)
|
||||
default: break;
|
||||
}
|
||||
|
||||
/* Check character will fit into our buffer. */
|
||||
if (strto + Utf8CharLen(c) > lastof(tmp)) break;
|
||||
|
||||
strto += Utf8Encode(strto, c);
|
||||
Utf8Encode(strto, c);
|
||||
}
|
||||
|
||||
/* Terminate the new string and copy it back to the name array */
|
||||
*strto = '\0';
|
||||
|
||||
return stredup(tmp);
|
||||
return tmp.str();
|
||||
} else {
|
||||
/* Name will already be in UTF-8. */
|
||||
return stredup(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]);
|
||||
return std::string(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -124,7 +124,7 @@ static const SaveLoad _town_desc[] = {
|
||||
SLE_CONDVAR(Town, townnamegrfid, SLE_UINT32, SLV_66, SL_MAX_VERSION),
|
||||
SLE_VAR(Town, townnametype, SLE_UINT16),
|
||||
SLE_VAR(Town, townnameparts, SLE_UINT32),
|
||||
SLE_CONDSTR(Town, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Town, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
|
||||
|
||||
SLE_VAR(Town, flags, SLE_UINT8),
|
||||
SLE_CONDVAR(Town, statues, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),
|
||||
@@ -167,7 +167,7 @@ static const SaveLoad _town_desc[] = {
|
||||
|
||||
SLE_CONDARR(Town, goal, SLE_UINT32, NUM_TE, SLV_165, SL_MAX_VERSION),
|
||||
|
||||
SLE_CONDSTR(Town, text, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_168, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Town, text, SLE_STR | SLF_ALLOW_CONTROL, SLV_168, SL_MAX_VERSION),
|
||||
|
||||
SLE_CONDVAR(Town, time_until_rebuild, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_54),
|
||||
SLE_CONDVAR(Town, grow_counter, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_54),
|
||||
|
@@ -585,7 +585,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
|
||||
|
||||
SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
|
||||
SLE_CONDVAR(Vehicle, name, SLE_NAME, SL_MIN_VERSION, SLV_84),
|
||||
SLE_CONDSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_8),
|
||||
SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, SLV_8, SL_MAX_VERSION),
|
||||
SLE_VAR(Vehicle, owner, SLE_UINT8),
|
||||
|
@@ -29,7 +29,7 @@ struct OldWaypoint {
|
||||
Town *town;
|
||||
uint16 town_cn;
|
||||
StringID string_id;
|
||||
char *name;
|
||||
std::string name;
|
||||
uint8 delete_ctr;
|
||||
Date build_date;
|
||||
uint8 localidx;
|
||||
@@ -172,7 +172,7 @@ static const SaveLoad _old_waypoint_desc[] = {
|
||||
SLE_CONDVAR(OldWaypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, SLV_12, SLV_89),
|
||||
SLE_CONDVAR(OldWaypoint, town_cn, SLE_UINT16, SLV_89, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(OldWaypoint, string_id, SLE_STRINGID, SL_MIN_VERSION, SLV_84),
|
||||
SLE_CONDSTR(OldWaypoint, name, SLE_STR, 0, SLV_84, SL_MAX_VERSION),
|
||||
SLE_CONDSSTR(OldWaypoint, name, SLE_STR, SLV_84, SL_MAX_VERSION),
|
||||
SLE_VAR(OldWaypoint, delete_ctr, SLE_UINT8),
|
||||
|
||||
SLE_CONDVAR(OldWaypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, SLV_3, SLV_31),
|
||||
@@ -194,7 +194,6 @@ static void Load_WAYP()
|
||||
while ((index = SlIterateArray()) != -1) {
|
||||
/*C++17: OldWaypoint *wp = &*/ _old_waypoints.emplace_back();
|
||||
OldWaypoint *wp = &_old_waypoints.back();
|
||||
memset(wp, 0, sizeof(*wp));
|
||||
|
||||
wp->index = index;
|
||||
SlObject(wp, _old_waypoint_desc);
|
||||
|
Reference in New Issue
Block a user