Skip to content

Commit b799dd6

Browse files
committed
remove use of Materials module from probe
also remove UB use of a union and clean up headers (at least somewhat)
1 parent 2e2b921 commit b799dd6

1 file changed

Lines changed: 43 additions & 53 deletions

File tree

plugins/probe.cpp

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
#include <ios>
2+
#include <string>
3+
#include <vector>
4+
15
#include "LuaTools.h"
6+
#include "MiscUtils.h"
27
#include "PluginManager.h"
38
#include "TileTypes.h"
49

510
#include "modules/Gui.h"
6-
#include "modules/Materials.h"
711
#include "modules/MapCache.h"
812
#include "modules/Maps.h"
13+
#include "modules/Materials.h"
914

1015
#include "df/block_square_event_grassst.h"
1116
#include "df/block_square_event_world_constructionst.h"
@@ -15,6 +20,8 @@
1520
#include "df/civzone_type.h"
1621
#include "df/construction_type.h"
1722
#include "df/furnace_type.h"
23+
#include "df/global_objects.h"
24+
#include "df/inorganic_raw.h"
1825
#include "df/item.h"
1926
#include "df/map_block.h"
2027
#include "df/region_map_entry.h"
@@ -86,10 +93,8 @@ static void describeTile(color_ostream &out, df::tiletype tiletype) {
8693
}
8794

8895
static command_result df_probe(color_ostream &out, vector<string> & parameters) {
89-
DFHack::Materials *Materials = Core::getInstance().getMaterials();
9096

91-
vector<t_matglossInorganic> inorganic;
92-
bool hasmats = Materials->CopyInorganicMaterials(inorganic);
97+
auto& inorganic = world->raws.inorganics.all;
9398

9499
if (!Maps::IsValid()) {
95100
out.printerr("Map is not available!\n");
@@ -173,29 +178,25 @@ static command_result df_probe(color_ostream &out, vector<string> & parameters)
173178
out << "geolayer: " << des.bits.geolayer_index
174179
<< std::endl;
175180
int16_t base_rock = mc.layerMaterialAt(cursor);
176-
if (base_rock != -1) {
181+
if (base_rock != -1)
182+
{
177183
out << "Layer material: " << std::dec << base_rock;
178-
if(hasmats)
179-
out << " / " << inorganic[base_rock].id
180-
<< " / "
181-
<< inorganic[base_rock].name
182-
<< std::endl;
183-
else
184-
out << std::endl;
184+
out << " / " << inorganic[base_rock]->id
185+
<< " / "
186+
<< inorganic[base_rock]->material.stone_name
187+
<< std::endl;
185188
}
186189
int16_t vein_rock = mc.veinMaterialAt(cursor);
187-
if (vein_rock != -1) {
190+
if (vein_rock != -1)
191+
{
188192
out << "Vein material (final): " << std::dec << vein_rock;
189-
if(hasmats)
190-
out << " / " << inorganic[vein_rock].id
191-
<< " / "
192-
<< inorganic[vein_rock].name
193-
<< " ("
194-
<< ENUM_KEY_STR(inclusion_type,b->veinTypeAt(cursor))
195-
<< ")"
196-
<< std::endl;
197-
else
198-
out << std::endl;
193+
out << " / " << inorganic[vein_rock]->id
194+
<< " / "
195+
<< inorganic[vein_rock]->material.stone_name
196+
<< " ("
197+
<< ENUM_KEY_STR(inclusion_type, b->veinTypeAt(cursor))
198+
<< ")"
199+
<< std::endl;
199200
}
200201
MaterialInfo minfo(mc.baseMaterialAt(cursor));
201202
if (minfo.isValid())
@@ -309,17 +310,6 @@ static command_result df_probe(color_ostream &out, vector<string> & parameters)
309310
return CR_OK;
310311
}
311312

312-
union Subtype {
313-
int16_t subtype;
314-
df::civzone_type civzone_type;
315-
df::furnace_type furnace_type;
316-
df::workshop_type workshop_type;
317-
df::construction_type construction_type;
318-
df::shop_type shop_type;
319-
df::siegeengine_type siegeengine_type;
320-
df::trap_type trap_type;
321-
};
322-
323313
static command_result df_bprobe(color_ostream &out, vector<string> & parameters) {
324314
auto bld = Gui::getSelectedBuilding(out);
325315
if (!bld)
@@ -329,7 +319,7 @@ static command_result df_bprobe(color_ostream &out, vector<string> & parameters)
329319
bld->getName(&name);
330320

331321
auto bld_type = bld->getType();
332-
Subtype subtype{bld->getSubtype()};
322+
int16_t subtype{bld->getSubtype()};
333323
int32_t custom = bld->getCustomType();
334324

335325
out.print("Building {:<4}, \"{}\", type {} ({})",
@@ -342,46 +332,46 @@ static command_result df_bprobe(color_ostream &out, vector<string> & parameters)
342332
switch (bld_type) {
343333
case df::building_type::Civzone:
344334
out.print(", subtype {} ({})",
345-
ENUM_KEY_STR(civzone_type, subtype.civzone_type),
346-
subtype.subtype);
335+
ENUM_KEY_STR(civzone_type, static_cast<df::civzone_type>(subtype)),
336+
subtype);
347337
break;
348338
case df::building_type::Furnace:
349339
out.print(", subtype {} ({})",
350-
ENUM_KEY_STR(furnace_type, subtype.furnace_type),
351-
subtype.subtype);
352-
if (subtype.furnace_type == df::furnace_type::Custom)
340+
ENUM_KEY_STR(furnace_type, static_cast<df::furnace_type>(subtype)),
341+
subtype);
342+
if (static_cast<df::furnace_type>(subtype) == df::furnace_type::Custom)
353343
out.print(", custom type {} ({})",
354344
world->raws.buildings.all[custom]->code,
355345
custom);
356346
break;
357347
case df::building_type::Workshop:
358348
out.print(", subtype {} ({})",
359-
ENUM_KEY_STR(workshop_type, subtype.workshop_type),
360-
subtype.subtype);
361-
if (subtype.workshop_type == df::workshop_type::Custom)
349+
ENUM_KEY_STR(workshop_type, static_cast<df::workshop_type>(subtype)),
350+
subtype);
351+
if (subtype == static_cast<int16_t>(df::workshop_type::Custom))
362352
out.print(", custom type {} ({})",
363353
world->raws.buildings.all[custom]->code,
364354
custom);
365355
break;
366356
case df::building_type::Construction:
367357
out.print(", subtype {} ({})",
368-
ENUM_KEY_STR(construction_type, subtype.construction_type),
369-
subtype.subtype);
358+
ENUM_KEY_STR(construction_type, static_cast<df::construction_type>(subtype)),
359+
subtype);
370360
break;
371361
case df::building_type::Shop:
372362
out.print(", subtype {} ({})",
373-
ENUM_KEY_STR(shop_type, subtype.shop_type),
374-
subtype.subtype);
363+
ENUM_KEY_STR(shop_type, static_cast<df::shop_type>(subtype)),
364+
subtype);
375365
break;
376366
case df::building_type::SiegeEngine:
377367
out.print(", subtype {} ({})",
378-
ENUM_KEY_STR(siegeengine_type, subtype.siegeengine_type),
379-
subtype.subtype);
368+
ENUM_KEY_STR(siegeengine_type, static_cast<df::siegeengine_type>(subtype)),
369+
subtype);
380370
break;
381371
case df::building_type::Trap:
382372
out.print(", subtype {} ({})",
383-
ENUM_KEY_STR(trap_type, subtype.trap_type),
384-
subtype.subtype);
373+
ENUM_KEY_STR(trap_type, static_cast<df::trap_type>(subtype)),
374+
subtype);
385375
break;
386376
case df::building_type::NestBox:
387377
{
@@ -391,8 +381,8 @@ static command_result df_bprobe(color_ostream &out, vector<string> & parameters)
391381
break;
392382
}
393383
default:
394-
if (subtype.subtype != -1)
395-
out.print(", subtype {}", subtype.subtype);
384+
if (subtype != -1)
385+
out.print(", subtype {}", subtype);
396386
break;
397387
}
398388

0 commit comments

Comments
 (0)