From 02d8ae018cbbd4913f9ded306340dce09ad562e3 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 11 May 2025 12:43:28 +0200 Subject: [PATCH] Codechange: simplify getting the value of a NewGRF property --- src/newgrf_debug_gui.cpp | 29 ++++++----------------------- src/table/newgrf_debug_data.h | 8 ++++---- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index d0723f4681..fe8ec226ff 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -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(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(this->value).Test(bit); } diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index b2ecb6a2b5..44fd14b40a 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -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(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(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(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(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(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(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(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(b)->GetAccepted(slot).cargo; }, prop, type } static const NIProperty _nip_industries[] = { NIP_PRODUCED_CARGO(0x25, Industry, 0, NIT_CARGO, "produced cargo 0"),