Codechange: Modernise NewGRF debug helper definitions. (#13410)

* Replace pointers and null-terminated lists with spans.
* Replace raw (undeleted) pointer with unique_ptr.
* Replace const char * with string_view.
This commit is contained in:
Peter Nelson 2025-01-30 18:50:53 +00:00 committed by GitHub
parent 56b1e9df1f
commit a252ba3146
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 98 deletions

View File

@ -86,7 +86,7 @@ typedef const void *NIOffsetProc(const void *b);
/** Representation of the data from a NewGRF property. */
struct NIProperty {
const char *name; ///< A (human readable) name for the property
std::string_view name; ///< A (human readable) name for the property
NIOffsetProc *offset_proc; ///< Callback proc to get the actual variable address in memory
uint8_t read_size; ///< Number of bytes (i.e. byte, word, dword etc)
uint8_t prop; ///< The number of the property
@ -99,7 +99,7 @@ struct NIProperty {
* information on when they actually apply.
*/
struct NICallback {
const char *name; ///< The human readable name of the callback
std::string_view name; ///< The human readable name of the callback
NIOffsetProc *offset_proc; ///< Callback proc to get the actual variable address in memory
uint8_t read_size; ///< The number of bytes (i.e. byte, word, dword etc) to read
uint8_t cb_bit; ///< The bit that needs to be set for this callback to be enabled
@ -110,7 +110,7 @@ static const int CBM_NO_BIT = UINT8_MAX;
/** Representation on the NewGRF variables. */
struct NIVariable {
const char *name;
std::string_view name;
uint8_t var;
};
@ -222,10 +222,10 @@ protected:
/** Container for all information for a given feature. */
struct NIFeature {
const NIProperty *properties; ///< The properties associated with this feature.
const NICallback *callbacks; ///< The callbacks associated with this feature.
const NIVariable *variables; ///< The variables associated with this feature.
const NIHelper *helper; ///< The class container all helper functions.
std::span<const NIProperty> properties; ///< The properties associated with this feature.
std::span<const NICallback> callbacks; ///< The callbacks associated with this feature.
std::span<const NIVariable> variables; ///< The variables associated with this feature.
std::unique_ptr<const NIHelper> helper; ///< The class container all helper functions.
};
/* Load all the NewGRF debug data; externalised as it is just a huge bunch of tables. */
@ -258,9 +258,9 @@ static inline const NIFeature *GetFeature(uint window_number)
* @pre GetFeature(window_number) != nullptr
* @return the NIHelper
*/
static inline const NIHelper *GetFeatureHelper(uint window_number)
static inline const NIHelper &GetFeatureHelper(uint window_number)
{
return GetFeature(window_number)->helper;
return *GetFeature(window_number)->helper;
}
/** Window used for inspecting NewGRFs. */
@ -345,7 +345,7 @@ struct NewGRFInspectWindow : Window {
this->FinishInitNested(wno);
this->vscroll->SetCount(0);
this->SetWidgetDisabledState(WID_NGRFI_PARENT, GetFeatureHelper(this->window_number)->GetParent(this->GetFeatureIndex()) == UINT32_MAX);
this->SetWidgetDisabledState(WID_NGRFI_PARENT, GetFeatureHelper(this->window_number).GetParent(this->GetFeatureIndex()) == UINT32_MAX);
this->OnInvalidateData(0, true);
}
@ -354,7 +354,7 @@ struct NewGRFInspectWindow : Window {
{
if (widget != WID_NGRFI_CAPTION) return;
GetFeatureHelper(this->window_number)->SetStringParameters(this->GetFeatureIndex());
GetFeatureHelper(this->window_number).SetStringParameters(this->GetFeatureIndex());
}
void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
@ -439,31 +439,31 @@ struct NewGRFInspectWindow : Window {
{
uint index = this->GetFeatureIndex();
const NIFeature *nif = GetFeature(this->window_number);
const NIHelper *nih = nif->helper;
const void *base = nih->GetInstance(index);
const void *base_spec = nih->GetSpec(index);
const NIHelper &nih = *nif->helper;
const void *base = nih.GetInstance(index);
const void *base_spec = nih.GetSpec(index);
uint i = 0;
if (nif->variables != nullptr) {
if (!nif->variables.empty()) {
this->DrawString(r, i++, "Variables:");
for (const NIVariable *niv = nif->variables; niv->name != nullptr; niv++) {
for (const NIVariable &niv : nif->variables) {
bool avail = true;
uint param = HasVariableParameter(niv->var) ? NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][niv->var - 0x60] : 0;
uint value = nih->Resolve(index, niv->var, param, avail);
uint param = HasVariableParameter(niv.var) ? NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][niv.var - 0x60] : 0;
uint value = nih.Resolve(index, niv.var, param, avail);
if (!avail) continue;
if (HasVariableParameter(niv->var)) {
this->DrawString(r, i++, fmt::format(" {:02x}[{:02x}]: {:08x} ({})", niv->var, param, value, niv->name));
if (HasVariableParameter(niv.var)) {
this->DrawString(r, i++, fmt::format(" {:02x}[{:02x}]: {:08x} ({})", niv.var, param, value, niv.name));
} else {
this->DrawString(r, i++, fmt::format(" {:02x}: {:08x} ({})", niv->var, value, niv->name));
this->DrawString(r, i++, fmt::format(" {:02x}: {:08x} ({})", niv.var, value, niv.name));
}
}
}
auto psa = nih->GetPSA(index, this->caller_grfid);
auto psa = nih.GetPSA(index, this->caller_grfid);
if (!psa.empty()) {
if (nih->PSAWithParameter()) {
if (nih.PSAWithParameter()) {
this->DrawString(r, i++, fmt::format("Persistent storage [{:08X}]:", std::byteswap(this->caller_grfid)));
} else {
this->DrawString(r, i++, "Persistent storage:");
@ -474,12 +474,12 @@ struct NewGRFInspectWindow : Window {
}
}
if (nif->properties != nullptr) {
if (!nif->properties.empty()) {
this->DrawString(r, i++, "Properties:");
for (const NIProperty *nip = nif->properties; nip->name != nullptr; nip++) {
const void *ptr = nip->offset_proc(base);
for (const NIProperty &nip : nif->properties) {
const void *ptr = nip.offset_proc(base);
uint value;
switch (nip->read_size) {
switch (nip.read_size) {
case 1: value = *(const uint8_t *)ptr; break;
case 2: value = *(const uint16_t *)ptr; break;
case 4: value = *(const uint32_t *)ptr; break;
@ -488,7 +488,7 @@ struct NewGRFInspectWindow : Window {
StringID string;
SetDParam(0, value);
switch (nip->type) {
switch (nip.type) {
case NIT_INT:
string = STR_JUST_INT;
break;
@ -501,27 +501,27 @@ struct NewGRFInspectWindow : Window {
NOT_REACHED();
}
this->DrawString(r, i++, fmt::format(" {:02x}: {} ({})", nip->prop, GetString(string), nip->name));
this->DrawString(r, i++, fmt::format(" {:02x}: {} ({})", nip.prop, GetString(string), nip.name));
}
}
if (nif->callbacks != nullptr) {
if (!nif->callbacks.empty()) {
this->DrawString(r, i++, "Callbacks:");
for (const NICallback *nic = nif->callbacks; nic->name != nullptr; nic++) {
if (nic->cb_bit != CBM_NO_BIT) {
const void *ptr = nic->offset_proc(base_spec);
for (const NICallback &nic : nif->callbacks) {
if (nic.cb_bit != CBM_NO_BIT) {
const void *ptr = nic.offset_proc(base_spec);
uint value;
switch (nic->read_size) {
switch (nic.read_size) {
case 1: value = *(const uint8_t *)ptr; break;
case 2: value = *(const uint16_t *)ptr; break;
case 4: value = *(const uint32_t *)ptr; break;
default: NOT_REACHED();
}
if (!HasBit(value, nic->cb_bit)) continue;
this->DrawString(r, i++, fmt::format(" {:03x}: {}", nic->cb_id, nic->name));
if (!HasBit(value, nic.cb_bit)) continue;
this->DrawString(r, i++, fmt::format(" {:03x}: {}", nic.cb_id, nic.name));
} else {
this->DrawString(r, i++, fmt::format(" {:03x}: {} (unmasked)", nic->cb_id, nic->name));
this->DrawString(r, i++, fmt::format(" {:03x}: {} (unmasked)", nic.cb_id, nic.name));
}
}
}
@ -549,9 +549,9 @@ struct NewGRFInspectWindow : Window {
{
switch (widget) {
case WID_NGRFI_PARENT: {
const NIHelper *nih = GetFeatureHelper(this->window_number);
uint index = nih->GetParent(this->GetFeatureIndex());
::ShowNewGRFInspectWindow(GetFeatureNum(index), ::GetFeatureIndex(index), nih->GetGRFID(this->GetFeatureIndex()));
const NIHelper &nih = GetFeatureHelper(this->window_number);
uint index = nih.GetParent(this->GetFeatureIndex());
::ShowNewGRFInspectWindow(GetFeatureNum(index), ::GetFeatureIndex(index), nih.GetGRFID(this->GetFeatureIndex()));
break;
}
@ -576,19 +576,19 @@ struct NewGRFInspectWindow : Window {
case WID_NGRFI_MAINPANEL: {
/* Does this feature have variables? */
const NIFeature *nif = GetFeature(this->window_number);
if (nif->variables == nullptr) return;
if (nif->variables.empty()) return;
/* Get the line, make sure it's within the boundaries. */
int32_t line = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NGRFI_MAINPANEL, WidgetDimensions::scaled.frametext.top);
if (line == INT32_MAX) return;
/* Find the variable related to the line */
for (const NIVariable *niv = nif->variables; niv->name != nullptr; niv++, line--) {
if (line != 1) continue; // 1 because of the "Variables:" line
for (const NIVariable &niv : nif->variables) {
if (--line != 0) continue; // 0 because of the "Variables:" line
if (!HasVariableParameter(niv->var)) break;
if (!HasVariableParameter(niv.var)) break;
this->current_edit_param = niv->var;
this->current_edit_param = niv.var;
ShowQueryString(STR_EMPTY, STR_NEWGRF_INSPECT_QUERY_CAPTION, 9, this, CS_HEXADECIMAL, QSF_NONE);
}
}

View File

@ -14,15 +14,12 @@
/* Helper for filling property tables */
#define NIP(prop, base, variable, type, name) { name, [] (const void *b) -> const void * { return std::addressof(static_cast<const base *>(b)->variable); }, cpp_sizeof(base, variable), prop, type }
#define NIP_END() { nullptr, 0, 0, 0, 0 }
/* Helper for filling callback tables */
#define NIC(cb_id, base, variable, bit) { #cb_id, [] (const void *b) -> const void * { return std::addressof(static_cast<const base *>(b)->variable); }, cpp_sizeof(base, variable), bit, cb_id }
#define NIC_END() { nullptr, 0, 0, 0, 0 }
/* Helper for filling variable tables */
#define NIV(var, name) { name, var }
#define NIV_END() { nullptr, 0 }
/*** NewGRF Vehicles ***/
@ -44,7 +41,6 @@ static const NICallback _nic_vehicles[] = {
NICV(CBID_VEHICLE_AUTOREPLACE_SELECTION, CBM_NO_BIT),
NICV(CBID_VEHICLE_MODIFY_PROPERTY, CBM_NO_BIT),
NICV(CBID_VEHICLE_NAME, CBM_VEHICLE_NAME),
NIC_END()
};
@ -67,7 +63,6 @@ static const NIVariable _niv_vehicles[] = {
// 0x61 not useful, since it requires register 0x10F
NIV(0x62, "curvature/position difference to other vehicle"),
NIV(0x63, "tile compatibility wrt. track-type"),
NIV_END()
};
class NIHVehicle : public NIHelper {
@ -87,10 +82,10 @@ class NIHVehicle : public NIHelper {
};
static const NIFeature _nif_vehicle = {
nullptr,
{},
_nic_vehicles,
_niv_vehicles,
new NIHVehicle(),
std::make_unique<NIHVehicle>(),
};
@ -105,7 +100,6 @@ static const NICallback _nic_stations[] = {
NICS(CBID_STATION_ANIM_NEXT_FRAME, CBM_STATION_ANIMATION_NEXT_FRAME),
NICS(CBID_STATION_ANIMATION_SPEED, CBM_STATION_ANIMATION_SPEED),
NICS(CBID_STATION_LAND_SLOPE_CHECK, CBM_STATION_SLOPE_CHECK),
NIC_END()
};
static const NIVariable _niv_stations[] = {
@ -132,7 +126,6 @@ static const NIVariable _niv_stations[] = {
NIV(0x69, "information about cargo accepted in the past"),
NIV(0x6A, "GRFID of nearby station tiles"),
NIV(0x6B, "station ID of nearby tiles"),
NIV_END()
};
class NIHStation : public NIHelper {
@ -152,10 +145,10 @@ class NIHStation : public NIHelper {
};
static const NIFeature _nif_station = {
nullptr,
{},
_nic_stations,
_niv_stations,
new NIHStation(),
std::make_unique<NIHStation>(),
};
@ -178,7 +171,6 @@ static const NICallback _nic_house[] = {
NICH(CBID_HOUSE_CUSTOM_NAME, CBM_NO_BIT),
NICH(CBID_HOUSE_DRAW_FOUNDATIONS, CBM_HOUSE_DRAW_FOUNDATIONS),
NICH(CBID_HOUSE_AUTOSLOPE, CBM_HOUSE_AUTOSLOPE),
NIC_END()
};
static const NIVariable _niv_house[] = {
@ -198,7 +190,6 @@ static const NIVariable _niv_house[] = {
NIV(0x65, "distance of nearest house matching a given criterion"),
NIV(0x66, "class and ID of nearby house tile"),
NIV(0x67, "GRFID of nearby house tile"),
NIV_END()
};
class NIHHouse : public NIHelper {
@ -218,10 +209,10 @@ class NIHHouse : public NIHelper {
};
static const NIFeature _nif_house = {
nullptr,
{},
_nic_house,
_niv_house,
new NIHHouse(),
std::make_unique<NIHHouse>(),
};
@ -237,7 +228,6 @@ static const NICallback _nic_industrytiles[] = {
NICIT(CBID_INDTILE_SHAPE_CHECK, CBM_INDT_SHAPE_CHECK),
NICIT(CBID_INDTILE_DRAW_FOUNDATIONS, CBM_INDT_DRAW_FOUNDATIONS),
NICIT(CBID_INDTILE_AUTOSLOPE, CBM_INDT_AUTOSLOPE),
NIC_END()
};
static const NIVariable _niv_industrytiles[] = {
@ -249,7 +239,6 @@ static const NIVariable _niv_industrytiles[] = {
NIV(0x60, "land info of nearby tiles"),
NIV(0x61, "animation stage of nearby tiles"),
NIV(0x62, "get industry or airport tile ID at offset"),
NIV_END()
};
class NIHIndustryTile : public NIHelper {
@ -269,10 +258,10 @@ class NIHIndustryTile : public NIHelper {
};
static const NIFeature _nif_industrytile = {
nullptr,
{},
_nic_industrytiles,
_niv_industrytiles,
new NIHIndustryTile(),
std::make_unique<NIHIndustryTile>(),
};
@ -313,7 +302,6 @@ static const NIProperty _nip_industries[] = {
NIP_ACCEPTED_CARGO(0x26, Industry, 13, NIT_CARGO, "accepted cargo 13"),
NIP_ACCEPTED_CARGO(0x26, Industry, 14, NIT_CARGO, "accepted cargo 14"),
NIP_ACCEPTED_CARGO(0x26, Industry, 15, NIT_CARGO, "accepted cargo 15"),
NIP_END()
};
#undef NIP_PRODUCED_CARGO
@ -334,7 +322,6 @@ static const NICallback _nic_industries[] = {
NICI(CBID_INDUSTRY_INPUT_CARGO_TYPES, CBM_IND_INPUT_CARGO_TYPES),
NICI(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, CBM_IND_OUTPUT_CARGO_TYPES),
NICI(CBID_INDUSTRY_PROD_CHANGE_BUILD, CBM_IND_PROD_CHANGE_BUILD),
NIC_END()
};
static const NIVariable _niv_industries[] = {
@ -363,7 +350,6 @@ static const NIVariable _niv_industries[] = {
NIV(0x6F, "waiting input cargo"),
NIV(0x70, "production rate"),
NIV(0x71, "percentage of cargo transported last month"),
NIV_END()
};
class NIHIndustry : public NIHelper {
@ -393,7 +379,7 @@ static const NIFeature _nif_industry = {
_nip_industries,
_nic_industries,
_niv_industries,
new NIHIndustry(),
std::make_unique<NIHIndustry>(),
};
@ -408,7 +394,6 @@ static const NICallback _nic_objects[] = {
NICO(CBID_OBJECT_COLOUR, CBM_OBJ_COLOUR),
NICO(CBID_OBJECT_FUND_MORE_TEXT, CBM_OBJ_FUND_MORE_TEXT),
NICO(CBID_OBJECT_AUTOSLOPE, CBM_OBJ_AUTOSLOPE),
NIC_END()
};
static const NIVariable _niv_objects[] = {
@ -426,7 +411,6 @@ static const NIVariable _niv_objects[] = {
NIV(0x62, "land info of nearby tiles"),
NIV(0x63, "animation stage of nearby tiles"),
NIV(0x64, "distance on nearest object with given type"),
NIV_END()
};
class NIHObject : public NIHelper {
@ -446,10 +430,10 @@ class NIHObject : public NIHelper {
};
static const NIFeature _nif_object = {
nullptr,
{},
_nic_objects,
_niv_objects,
new NIHObject(),
std::make_unique<NIHObject>(),
};
@ -461,7 +445,6 @@ static const NIVariable _niv_railtypes[] = {
NIV(0x42, "level crossing status"),
NIV(0x43, "construction date"),
NIV(0x44, "town zone"),
NIV_END()
};
class NIHRailType : public NIHelper {
@ -482,10 +465,10 @@ class NIHRailType : public NIHelper {
};
static const NIFeature _nif_railtype = {
nullptr,
nullptr,
{},
{},
_niv_railtypes,
new NIHRailType(),
std::make_unique<NIHRailType>(),
};
@ -497,7 +480,6 @@ static const NICallback _nic_airporttiles[] = {
NICAT(CBID_AIRPTILE_ANIM_START_STOP, CBM_NO_BIT),
NICAT(CBID_AIRPTILE_ANIM_NEXT_FRAME, CBM_AIRT_ANIM_NEXT_FRAME),
NICAT(CBID_AIRPTILE_ANIMATION_SPEED, CBM_AIRT_ANIM_SPEED),
NIC_END()
};
class NIHAirportTile : public NIHelper {
@ -517,10 +499,10 @@ class NIHAirportTile : public NIHelper {
};
static const NIFeature _nif_airporttile = {
nullptr,
{},
_nic_airporttiles,
_niv_industrytiles, // Yes, they share this (at least now)
new NIHAirportTile(),
std::make_unique<NIHAirportTile>(),
};
@ -539,7 +521,6 @@ static const NIVariable _niv_airports[] = {
NIV(0xF1, "type of the airport"),
NIV(0xF6, "airport block status"),
NIV(0xFA, "built date"),
NIV_END()
};
class NIHAirport : public NIHelper {
@ -566,10 +547,10 @@ class NIHAirport : public NIHelper {
};
static const NIFeature _nif_airport = {
nullptr,
nullptr,
{},
{},
_niv_airports,
new NIHAirport(),
std::make_unique<NIHAirport>(),
};
@ -585,7 +566,6 @@ static const NIVariable _niv_towns[] = {
NIV(0x9A, "zone radius 3"),
NIV(0x9C, "zone radius 4"),
NIV(0xB6, "number of buildings"),
NIV_END()
};
class NIHTown : public NIHelper {
@ -616,10 +596,10 @@ class NIHTown : public NIHelper {
};
static const NIFeature _nif_town = {
nullptr,
nullptr,
{},
{},
_niv_towns,
new NIHTown(),
std::make_unique<NIHTown>(),
};
/*** NewGRF road types ***/
@ -630,7 +610,6 @@ static const NIVariable _niv_roadtypes[] = {
NIV(0x42, "level crossing status"),
NIV(0x43, "construction date"),
NIV(0x44, "town zone"),
NIV_END()
};
class NIHRoadType : public NIHelper {
@ -651,17 +630,17 @@ class NIHRoadType : public NIHelper {
};
static const NIFeature _nif_roadtype = {
nullptr,
nullptr,
{},
{},
_niv_roadtypes,
new NIHRoadType(),
std::make_unique<NIHRoadType>(),
};
static const NIFeature _nif_tramtype = {
nullptr,
nullptr,
{},
{},
_niv_roadtypes,
new NIHRoadType(),
std::make_unique<NIHRoadType>(),
};
#define NICRS(cb_id, bit) NIC(cb_id, RoadStopSpec, callback_mask, bit)
@ -670,7 +649,6 @@ static const NICallback _nic_roadstops[] = {
NICRS(CBID_STATION_ANIM_START_STOP, CBM_NO_BIT),
NICRS(CBID_STATION_ANIM_NEXT_FRAME, CBM_ROAD_STOP_ANIMATION_NEXT_FRAME),
NICRS(CBID_STATION_ANIMATION_SPEED, CBM_ROAD_STOP_ANIMATION_SPEED),
NIC_END()
};
static const NIVariable _nif_roadstops[] = {
@ -696,7 +674,6 @@ static const NIVariable _nif_roadstops[] = {
NIV(0x69, "information about cargo accepted in the past"),
NIV(0x6A, "GRFID of nearby road stop tiles"),
NIV(0x6B, "road stop ID of nearby tiles"),
NIV_END(),
};
class NIHRoadStop : public NIHelper {
@ -717,10 +694,10 @@ class NIHRoadStop : public NIHelper {
};
static const NIFeature _nif_roadstop = {
nullptr,
{},
_nic_roadstops,
_nif_roadstops,
new NIHRoadStop(),
std::make_unique<NIHRoadStop>(),
};
/** Table with all NIFeatures. */