1
0
Fork 0

Codechange: simplify getting the value of a NewGRF property

pull/14252/head
Rubidium 2025-05-11 12:43:28 +02:00 committed by rubidium42
parent f2b48bad79
commit 02d8ae018c
2 changed files with 10 additions and 27 deletions

View File

@ -86,13 +86,12 @@ enum NIType : uint8_t {
NIT_CARGO, ///< The property is a cargo
};
typedef const void *NIOffsetProc(const void *b);
using NIReadProc = uint32_t(const void *b);
/** Representation of the data from a NewGRF property. */
struct NIProperty {
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)
NIReadProc *read_proc; ///< Callback proc to get the actual variable from memory
uint8_t prop; ///< The number of the property
uint8_t type;
};
@ -104,8 +103,7 @@ struct NIProperty {
*/
struct NICallback {
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
NIReadProc *read_proc; ///< Callback proc to get the actual variable from memory
std::variant<
std::monostate,
VehicleCallbackMask,
@ -488,15 +486,7 @@ struct NewGRFInspectWindow : Window {
if (!nif->properties.empty()) {
this->DrawString(r, i++, "Properties:");
for (const NIProperty &nip : nif->properties) {
const void *ptr = nip.offset_proc(base);
uint value;
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;
default: NOT_REACHED();
}
uint32_t value = nip.read_proc(base);
this->DrawString(r, i++, fmt::format(" {:02x}: {} ({})", nip.prop, this->GetPropertyString(nip, value), nip.name));
}
}
@ -505,17 +495,10 @@ struct NewGRFInspectWindow : Window {
this->DrawString(r, i++, "Callbacks:");
for (const NICallback &nic : nif->callbacks) {
if (!std::holds_alternative<std::monostate>(nic.cb_bit)) {
const void *ptr = nic.offset_proc(base_spec);
uint value;
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();
}
uint32_t value = nic.read_proc(base_spec);
struct visitor {
uint value;
uint32_t value;
bool operator()(const std::monostate &) { return false; }
bool operator()(const VehicleCallbackMask &bit) { return static_cast<VehicleCallbackMasks>(this->value).Test(bit); }

View File

@ -13,10 +13,10 @@
#include "../newgrf_roadstop.h"
/* 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(prop, base_class, variable, type, name) { name, [] (const void *b) -> uint32_t { return static_cast<const base_class *>(b)->variable; }, prop, type }
/* 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(cb_id, base_class, variable, bit) { #cb_id, [] (const void *b) -> uint32_t { return static_cast<const base_class *>(b)->variable.base(); }, bit, cb_id }
/* Helper for filling variable tables */
#define NIV(var, name) { name, var }
@ -270,8 +270,8 @@ static const NIFeature _nif_industrytile = {
/*** NewGRF industries ***/
#define NIP_PRODUCED_CARGO(prop, base, slot, type, name) { name, [] (const void *b) -> const void * { return std::addressof(static_cast<const base *>(b)->GetProduced(slot).cargo); }, sizeof(CargoType), prop, type }
#define NIP_ACCEPTED_CARGO(prop, base, slot, type, name) { name, [] (const void *b) -> const void * { return std::addressof(static_cast<const base *>(b)->GetAccepted(slot).cargo); }, sizeof(CargoType), prop, type }
#define NIP_PRODUCED_CARGO(prop, base_class, slot, type, name) { name, [] (const void *b) -> uint32_t { return static_cast<const base_class *>(b)->GetProduced(slot).cargo; }, prop, type }
#define NIP_ACCEPTED_CARGO(prop, base_class, slot, type, name) { name, [] (const void *b) -> uint32_t { return static_cast<const base_class *>(b)->GetAccepted(slot).cargo; }, prop, type }
static const NIProperty _nip_industries[] = {
NIP_PRODUCED_CARGO(0x25, Industry, 0, NIT_CARGO, "produced cargo 0"),