Use label dumper wrapper for printing NewGRF labels

This commit is contained in:
Jonathan G Rennison 2024-09-01 11:26:28 +01:00
parent 7b6781b953
commit a6537accf9
4 changed files with 39 additions and 35 deletions

View File

@ -3027,10 +3027,10 @@ DEF_CONSOLE_CMD(ConDumpRoadTypes)
grfid = grf->grfid;
grfs.insert(std::pair<uint32_t, const GRFFile *>(grfid, grf));
}
IConsolePrintF(CC_DEFAULT, " %02u %s %c%c%c%c, Flags: %c%c%c%c%c, Extra Flags: %c%c%c%c, GRF: %08X, %s",
IConsolePrintF(CC_DEFAULT, " %02u %s %s, Flags: %c%c%c%c%c, Extra Flags: %c%c%c%c, GRF: %08X, %s",
(uint) rt,
RoadTypeIsTram(rt) ? "Tram" : "Road",
rti->label >> 24, rti->label >> 16, rti->label >> 8, rti->label,
NewGRFLabelDumper().Label(rti->label),
HasBit(rti->flags, ROTF_CATENARY) ? 'c' : '-',
HasBit(rti->flags, ROTF_NO_LEVEL_CROSSING) ? 'l' : '-',
HasBit(rti->flags, ROTF_NO_HOUSES) ? 'X' : '-',
@ -3085,9 +3085,9 @@ DEF_CONSOLE_CMD(ConDumpRailTypes)
grfid = grf->grfid;
grfs.insert(std::pair<uint32_t, const GRFFile *>(grfid, grf));
}
IConsolePrintF(CC_DEFAULT, " %02u %c%c%c%c, Flags: %c%c%c%c%c%c, Ctrl Flags: %c%c%c%c, GRF: %08X, %s",
IConsolePrintF(CC_DEFAULT, " %02u %s, Flags: %c%c%c%c%c%c, Ctrl Flags: %c%c%c%c, GRF: %08X, %s",
(uint) rt,
rti->label >> 24, rti->label >> 16, rti->label >> 8, rti->label,
NewGRFLabelDumper().Label(rti->label),
HasBit(rti->flags, RTF_CATENARY) ? 'c' : '-',
HasBit(rti->flags, RTF_NO_LEVEL_CROSSING) ? 'l' : '-',
HasBit(rti->flags, RTF_HIDDEN) ? 'h' : '-',
@ -3197,10 +3197,10 @@ DEF_CONSOLE_CMD(ConDumpCargoTypes)
grfid = grf->grfid;
grfs.insert(std::pair<uint32_t, const GRFFile *>(grfid, grf));
}
IConsolePrintF(CC_DEFAULT, " %02u Bit: %2u, Label: %c%c%c%c, Callback mask: 0x%02X, Cargo class: %c%c%c%c%c%c%c%c%c%c%c, GRF: %08X, %s",
IConsolePrintF(CC_DEFAULT, " %02u Bit: %2u, Label: %s, Callback mask: 0x%02X, Cargo class: %c%c%c%c%c%c%c%c%c%c%c, GRF: %08X, %s",
(uint) i,
spec->bitnum,
spec->label.base() >> 24, spec->label.base() >> 16, spec->label.base() >> 8, spec->label.base(),
NewGRFLabelDumper().Label(spec->label.base()),
spec->callback_mask,
(spec->classes & CC_PASSENGERS) != 0 ? 'p' : '-',
(spec->classes & CC_MAIL) != 0 ? 'm' : '-',
@ -3314,10 +3314,10 @@ DEF_CONSOLE_CMD(ConDumpGrfCargoTables)
char *b = buffer;
for (const CargoSpec *cs : CargoSpec::Iterate()) {
if (grf->cargo_map[cs->Index()] == i) {
b += seprintf(b, lastof(buffer), "%s%02u[%c%c%c%c]", (b == buffer) ? ": " : ", ", cs->Index(), GB(cs->label.base(), 24, 8), GB(cs->label.base(), 16, 8), GB(cs->label.base(), 8, 8), GB(cs->label.base(), 0, 8));
b += seprintf(b, lastof(buffer), "%s%02u[%s]", (b == buffer) ? ": " : ", ", cs->Index(), NewGRFLabelDumper().Label(cs->label.base()));
}
}
IConsolePrintF(CC_DEFAULT, " %c%c%c%c%s", GB(cl.base(), 24, 8), GB(cl.base(), 16, 8), GB(cl.base(), 8, 8), GB(cl.base(), 0, 8), buffer);
IConsolePrintF(CC_DEFAULT, " %s%s", NewGRFLabelDumper().Label(cl.base()), buffer);
i++;
}
}

View File

@ -11941,3 +11941,22 @@ const char *GetExtendedVariableNameById(int id)
return nullptr;
}
static bool IsLabelPrintable(uint32_t l)
{
for (uint i = 0; i < 4; i++) {
if ((l & 0xFF) < 0x20 || (l & 0xFF) > 0x7F) return false;
l >>= 8;
}
return true;
}
const char *NewGRFLabelDumper::Label(uint32_t label)
{
if (IsLabelPrintable(label)) {
seprintf(this->buffer, lastof(this->buffer), "%c%c%c%c", label >> 24, label >> 16, label >> 8, label);
} else {
seprintf(this->buffer, lastof(this->buffer), "0x%08X", BSWAP32(label));
}
return this->buffer;
}

View File

@ -459,4 +459,11 @@ void InitGRFGlobalVars();
const char *GetExtendedVariableNameById(int id);
struct NewGRFLabelDumper {
const char *Label(uint32_t label);
private:
char buffer[12];
};
#endif /* NEWGRF_H */

View File

@ -49,26 +49,7 @@ static uint GetTownInspectWindowNumber(const Town *town)
return GetInspectWindowNumber(GSF_FAKE_TOWNS, town->index);
}
static bool IsLabelPrintable(uint32_t l)
{
for (uint i = 0; i < 4; i++) {
if ((l & 0xFF) < 0x20 || (l & 0xFF) > 0x7F) return false;
l >>= 8;
}
return true;
}
struct label_dumper {
inline const char *Label(uint32_t label)
{
if (IsLabelPrintable(label)) {
seprintf(this->buffer, lastof(this->buffer), "%c%c%c%c", label >> 24, label >> 16, label >> 8, label);
} else {
seprintf(this->buffer, lastof(this->buffer), "0x%08X", BSWAP32(label));
}
return this->buffer;
}
struct label_dumper : public NewGRFLabelDumper {
inline const char *RailTypeLabel(RailType rt)
{
return this->Label(GetRailTypeInfo(rt)->label);
@ -78,9 +59,6 @@ struct label_dumper {
{
return this->Label(GetRoadTypeInfo(rt)->label);
}
private:
char buffer[64];
};
static void DumpRailTypeList(NIExtraInfoOutput &output, const char *prefix, RailTypes rail_types, RailTypes mark = RAILTYPES_NONE)
@ -1364,9 +1342,9 @@ class NIHCargo : public NIHelper {
output.print(buffer);
const CargoSpec *spec = CargoSpec::Get(index);
seprintf(buffer, lastof(buffer), " Bit: %2u, Label: %c%c%c%c, Callback mask: 0x%02X",
seprintf(buffer, lastof(buffer), " Bit: %2u, Label: %s, Callback mask: 0x%02X",
spec->bitnum,
spec->label.base() >> 24, spec->label.base() >> 16, spec->label.base() >> 8, spec->label.base(),
label_dumper().Label(spec->label.base()),
spec->callback_mask);
output.print(buffer);
int written = seprintf(buffer, lastof(buffer), " Cargo class: %s%s%s%s%s%s%s%s%s%s%s",
@ -1626,7 +1604,7 @@ class NIHObject : public NIHelper {
}
if (spec->class_index != INVALID_OBJECT_CLASS) {
uint class_id = ObjectClass::Get(spec->class_index)->global_id;
b += seprintf(b, lastof(buffer), ", class ID: %c%c%c%c", class_id >> 24, class_id >> 16, class_id >> 8, class_id);
b += seprintf(b, lastof(buffer), ", class ID: %s", label_dumper().Label(class_id));
}
output.print(buffer);
seprintf(buffer, lastof(buffer), " view: %u, colour: %u, effective foundation: %u", obj->view, obj->colour, GetObjectEffectiveFoundationType(index));
@ -2492,7 +2470,7 @@ class NIHRoadStop : public NIHelper {
const RoadStopSpec *spec = GetRoadStopSpec(index);
if (spec != nullptr) {
uint class_id = RoadStopClass::Get(spec->class_index)->global_id;
char *b = buffer + seprintf(buffer, lastof(buffer), " class ID: %c%c%c%c", class_id >> 24, class_id >> 16, class_id >> 8, class_id);
char *b = buffer + seprintf(buffer, lastof(buffer), " class ID: %s", label_dumper().Label(class_id));
if (spec->grf_prop.grffile != nullptr) {
b += seprintf(b, lastof(buffer), " (local ID: %u)", spec->grf_prop.local_id);
}