mirror of https://github.com/OpenTTD/OpenTTD
Codechange: simplify getting the value of a NewGRF property
parent
f2b48bad79
commit
02d8ae018c
|
@ -86,13 +86,12 @@ enum NIType : uint8_t {
|
||||||
NIT_CARGO, ///< The property is a cargo
|
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. */
|
/** Representation of the data from a NewGRF property. */
|
||||||
struct NIProperty {
|
struct NIProperty {
|
||||||
std::string_view 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
|
NIReadProc *read_proc; ///< Callback proc to get the actual variable from memory
|
||||||
uint8_t read_size; ///< Number of bytes (i.e. byte, word, dword etc)
|
|
||||||
uint8_t prop; ///< The number of the property
|
uint8_t prop; ///< The number of the property
|
||||||
uint8_t type;
|
uint8_t type;
|
||||||
};
|
};
|
||||||
|
@ -104,8 +103,7 @@ struct NIProperty {
|
||||||
*/
|
*/
|
||||||
struct NICallback {
|
struct NICallback {
|
||||||
std::string_view 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
|
NIReadProc *read_proc; ///< Callback proc to get the actual variable from memory
|
||||||
uint8_t read_size; ///< The number of bytes (i.e. byte, word, dword etc) to read
|
|
||||||
std::variant<
|
std::variant<
|
||||||
std::monostate,
|
std::monostate,
|
||||||
VehicleCallbackMask,
|
VehicleCallbackMask,
|
||||||
|
@ -488,15 +486,7 @@ struct NewGRFInspectWindow : Window {
|
||||||
if (!nif->properties.empty()) {
|
if (!nif->properties.empty()) {
|
||||||
this->DrawString(r, i++, "Properties:");
|
this->DrawString(r, i++, "Properties:");
|
||||||
for (const NIProperty &nip : nif->properties) {
|
for (const NIProperty &nip : nif->properties) {
|
||||||
const void *ptr = nip.offset_proc(base);
|
uint32_t value = nip.read_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();
|
|
||||||
}
|
|
||||||
|
|
||||||
this->DrawString(r, i++, fmt::format(" {:02x}: {} ({})", nip.prop, this->GetPropertyString(nip, value), nip.name));
|
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:");
|
this->DrawString(r, i++, "Callbacks:");
|
||||||
for (const NICallback &nic : nif->callbacks) {
|
for (const NICallback &nic : nif->callbacks) {
|
||||||
if (!std::holds_alternative<std::monostate>(nic.cb_bit)) {
|
if (!std::holds_alternative<std::monostate>(nic.cb_bit)) {
|
||||||
const void *ptr = nic.offset_proc(base_spec);
|
uint32_t value = nic.read_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();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct visitor {
|
struct visitor {
|
||||||
uint value;
|
uint32_t value;
|
||||||
|
|
||||||
bool operator()(const std::monostate &) { return false; }
|
bool operator()(const std::monostate &) { return false; }
|
||||||
bool operator()(const VehicleCallbackMask &bit) { return static_cast<VehicleCallbackMasks>(this->value).Test(bit); }
|
bool operator()(const VehicleCallbackMask &bit) { return static_cast<VehicleCallbackMasks>(this->value).Test(bit); }
|
||||||
|
|
|
@ -13,10 +13,10 @@
|
||||||
#include "../newgrf_roadstop.h"
|
#include "../newgrf_roadstop.h"
|
||||||
|
|
||||||
/* Helper for filling property tables */
|
/* 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 */
|
/* 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 */
|
/* Helper for filling variable tables */
|
||||||
#define NIV(var, name) { name, var }
|
#define NIV(var, name) { name, var }
|
||||||
|
@ -270,8 +270,8 @@ static const NIFeature _nif_industrytile = {
|
||||||
|
|
||||||
|
|
||||||
/*** NewGRF industries ***/
|
/*** 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_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, 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_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[] = {
|
static const NIProperty _nip_industries[] = {
|
||||||
NIP_PRODUCED_CARGO(0x25, Industry, 0, NIT_CARGO, "produced cargo 0"),
|
NIP_PRODUCED_CARGO(0x25, Industry, 0, NIT_CARGO, "produced cargo 0"),
|
||||||
|
|
Loading…
Reference in New Issue