Added bounds checking to various creature name functions in the Units module.

develop
Rose 2023-01-18 20:52:58 -08:00
parent 87c54c8716
commit f9ac987367
1 changed files with 30 additions and 15 deletions

@ -1025,9 +1025,12 @@ df::unit_misc_trait *Units::getMiscTrait(df::unit *unit, df::misc_trait_type typ
// get race name by id or unit pointer
string Units::getRaceNameById(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->creature_id;
if (id >= 0 && id < world->raws.creatures.all.size())
{
df::creature_raw* raw = world->raws.creatures.all[id];
if (raw)
return raw->creature_id;
}
return "";
}
string Units::getRaceName(df::unit* unit)
@ -1039,9 +1042,12 @@ string Units::getRaceName(df::unit* unit)
// get human-readable race name by id or unit pointer
string Units::getRaceReadableNameById(int32_t id)
{
df::creature_raw* raw = world->raws.creatures.all[id];
if (raw)
return raw->name[0];
if (id >= 0 && id < world->raws.creatures.all.size())
{
df::creature_raw* raw = world->raws.creatures.all[id];
if (raw)
return raw->name[0];
}
return "";
}
string Units::getRaceReadableName(df::unit* unit)
@ -1072,9 +1078,12 @@ string Units::getPhysicalDescription(df::unit* unit)
// get plural of race name (used for display in autobutcher UI and for sorting the watchlist)
string Units::getRaceNamePluralById(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->name[1]; // second field is plural of race name
if (id >= 0 && id < world->raws.creatures.all.size())
{
df::creature_raw* raw = world->raws.creatures.all[id];
if (raw)
return raw->name[1]; // second field is plural of race name
}
return "";
}
@ -1086,9 +1095,12 @@ string Units::getRaceNamePlural(df::unit* unit)
string Units::getRaceBabyNameById(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->general_baby_name[0];
if (id >= 0 && id < world->raws.creatures.all.size())
{
df::creature_raw* raw = world->raws.creatures.all[id];
if (raw)
return raw->general_baby_name[0];
}
return "";
}
@ -1100,9 +1112,12 @@ string Units::getRaceBabyName(df::unit* unit)
string Units::getRaceChildNameById(int32_t id)
{
df::creature_raw *raw = world->raws.creatures.all[id];
if (raw)
return raw->general_child_name[0];
if (id >= 0 && id < world->raws.creatures.all.size())
{
df::creature_raw* raw = world->raws.creatures.all[id];
if (raw)
return raw->general_child_name[0];
}
return "";
}