Skip to content

Commit 93520b4

Browse files
committed
dwarfmonitor prefs: fix segfault if item_subtype is null for some item types
1 parent b343d00 commit 93520b4

2 files changed

Lines changed: 73 additions & 15 deletions

File tree

docs/changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
3333

3434
# Future
3535

36+
## Fixes
37+
- `dwarfmonitor`: fixed a crash when opening the ``prefs`` screen if units have vague preferences
38+
3639
# 0.47.04-r3
3740

3841
## New Plugins

plugins/dwarfmonitor.cpp

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,52 +1155,107 @@ struct preference_map
11551155

11561156
string getItemLabel()
11571157
{
1158-
df::world_raws::T_itemdefs &defs = world->raws.itemdefs;
11591158
label = ENUM_ATTR_STR(item_type, caption, pref.item_type);
11601159
switch (pref.item_type)
11611160
{
11621161
case (df::item_type::WEAPON):
1163-
label = defs.weapons[pref.item_subtype]->name_plural;
1162+
{
1163+
auto *def = vector_get(world->raws.itemdefs.weapons, pref.item_subtype);
1164+
if (def)
1165+
label = def->name_plural;
11641166
break;
1167+
}
11651168
case (df::item_type::TRAPCOMP):
1166-
label = defs.trapcomps[pref.item_subtype]->name_plural;
1169+
{
1170+
auto *def = vector_get(world->raws.itemdefs.trapcomps, pref.item_subtype);
1171+
if (def)
1172+
label = def->name_plural;
11671173
break;
1174+
}
11681175
case (df::item_type::TOY):
1169-
label = defs.toys[pref.item_subtype]->name_plural;
1176+
{
1177+
auto *def = vector_get(world->raws.itemdefs.toys, pref.item_subtype);
1178+
if (def)
1179+
label = def->name_plural;
11701180
break;
1181+
}
11711182
case (df::item_type::TOOL):
1172-
label = defs.tools[pref.item_subtype]->name_plural;
1183+
{
1184+
auto *def = vector_get(world->raws.itemdefs.tools, pref.item_subtype);
1185+
if (def)
1186+
label = def->name_plural;
11731187
break;
1188+
}
11741189
case (df::item_type::INSTRUMENT):
1175-
label = defs.instruments[pref.item_subtype]->name_plural;
1190+
{
1191+
auto *def = vector_get(world->raws.itemdefs.instruments, pref.item_subtype);
1192+
if (def)
1193+
label = def->name_plural;
11761194
break;
1195+
}
11771196
case (df::item_type::ARMOR):
1178-
label = defs.armor[pref.item_subtype]->name_plural;
1197+
{
1198+
auto *def = vector_get(world->raws.itemdefs.armor, pref.item_subtype);
1199+
if (def)
1200+
label = def->name_plural;
11791201
break;
1202+
}
11801203
case (df::item_type::AMMO):
1181-
label = defs.ammo[pref.item_subtype]->name_plural;
1204+
{
1205+
auto *def = vector_get(world->raws.itemdefs.ammo, pref.item_subtype);
1206+
if (def)
1207+
label = def->name_plural;
11821208
break;
1209+
}
11831210
case (df::item_type::SIEGEAMMO):
1184-
label = defs.siege_ammo[pref.item_subtype]->name_plural;
1211+
{
1212+
auto *def = vector_get(world->raws.itemdefs.siege_ammo, pref.item_subtype);
1213+
if (def)
1214+
label = def->name_plural;
11851215
break;
1216+
}
11861217
case (df::item_type::GLOVES):
1187-
label = defs.gloves[pref.item_subtype]->name_plural;
1218+
{
1219+
auto *def = vector_get(world->raws.itemdefs.gloves, pref.item_subtype);
1220+
if (def)
1221+
label = def->name_plural;
11881222
break;
1223+
}
11891224
case (df::item_type::SHOES):
1190-
label = defs.shoes[pref.item_subtype]->name_plural;
1225+
{
1226+
auto *def = vector_get(world->raws.itemdefs.shoes, pref.item_subtype);
1227+
if (def)
1228+
label = def->name_plural;
11911229
break;
1230+
}
11921231
case (df::item_type::SHIELD):
1193-
label = defs.shields[pref.item_subtype]->name_plural;
1232+
{
1233+
auto *def = vector_get(world->raws.itemdefs.shields, pref.item_subtype);
1234+
if (def)
1235+
label = def->name_plural;
11941236
break;
1237+
}
11951238
case (df::item_type::HELM):
1196-
label = defs.helms[pref.item_subtype]->name_plural;
1239+
{
1240+
auto *def = vector_get(world->raws.itemdefs.helms, pref.item_subtype);
1241+
if (def)
1242+
label = def->name_plural;
11971243
break;
1244+
}
11981245
case (df::item_type::PANTS):
1199-
label = defs.pants[pref.item_subtype]->name_plural;
1246+
{
1247+
auto *def = vector_get(world->raws.itemdefs.pants, pref.item_subtype);
1248+
if (def)
1249+
label = def->name_plural;
12001250
break;
1251+
}
12011252
case (df::item_type::FOOD):
1202-
label = defs.food[pref.item_subtype]->name;
1253+
{
1254+
auto *def = vector_get(world->raws.itemdefs.food, pref.item_subtype);
1255+
if (def)
1256+
label = def->name;
12031257
break;
1258+
}
12041259

12051260
default:
12061261
label = ENUM_ATTR_STR(item_type, caption, pref.item_type);

0 commit comments

Comments
 (0)