Debug: Allow setting parameters for extended vars in debug window

Reafctor handling of set parameters
pull/532/head
Jonathan G Rennison 1 year ago
parent a10c5a62c9
commit e3bd8966fc

@ -109,10 +109,17 @@ struct NICallback {
/** Mask to show no bit needs to be enabled for the callback. */
static const int CBM_NO_BIT = UINT8_MAX;
enum NIVariableFlags : uint16 {
NIVF_NONE = 0,
NIVF_SHOW_PARAMS = 1 << 0,
};
DECLARE_ENUM_AS_BIT_SET(NIVariableFlags)
/** Representation on the NewGRF variables. */
struct NIVariable {
const char *name;
uint16 var;
NIVariableFlags flags;
};
struct NIExtraInfoOutput {
@ -295,7 +302,7 @@ static inline const NIHelper *GetFeatureHelper(uint window_number)
/** Window used for inspecting NewGRFs. */
struct NewGRFInspectWindow : Window {
/** The value for the variable 60 parameters. */
static uint32 var60params[GSF_FAKE_END][0x20];
btree::btree_map<uint16, uint32> var60params;
/** GRFID of the caller of this window, 0 if it has no caller. */
uint32 caller_grfid;
@ -304,7 +311,7 @@ struct NewGRFInspectWindow : Window {
uint chain_index;
/** The currently edited parameter, to update the right one. */
byte current_edit_param;
uint16 current_edit_param;
Scrollbar *vscroll;
@ -331,9 +338,9 @@ struct NewGRFInspectWindow : Window {
* @param variable the variable to check.
* @return true iff the variable has a parameter.
*/
static bool HasVariableParameter(uint variable)
static bool HasVariableParameter(const NIVariable *niv)
{
return IsInsideBS(variable, 0x60, 0x20);
return IsInsideBS(niv->var, 0x60, 0x20) || (niv->flags & NIVF_SHOW_PARAMS);
}
/**
@ -646,46 +653,64 @@ struct NewGRFInspectWindow : Window {
if (nif->variables != nullptr) {
this->DrawString(r, i++, "Variables:");
uint prefix_width = 0;
int prefix_width = 0;
uint widest_num = 0;
for (const NIVariable *niv = nif->variables; niv->name != nullptr; niv++) {
if (niv->var >= 0x100) {
const char *name = GetExtendedVariableNameById(niv->var);
if (name != nullptr) {
char buf[512];
seprintf(buf, lastof(buf), " %s: ", name);
prefix_width = std::max<uint>(prefix_width, GetStringBoundingBox(buf).width);
if (HasVariableParameter(niv)) {
if (widest_num == 0) widest_num = GetBroadestDigitsValue(2);
seprintf(buf, lastof(buf), " %s [%u]: ", name, widest_num);
} else {
seprintf(buf, lastof(buf), " %s: ", name);
}
prefix_width = std::max<int>(prefix_width, GetStringBoundingBox(buf).width);
}
}
}
for (const NIVariable *niv = nif->variables; niv->name != nullptr; niv++) {
GetVariableExtra extra;
uint param = HasVariableParameter(niv->var) ? NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][niv->var - 0x60] : 0;
const bool has_param = HasVariableParameter(niv);
uint param = 0;
if (has_param) {
auto iter = this->var60params.find(niv->var);
if (iter != this->var60params.end()) param = iter->second;
}
uint value = nih->Resolve(index, niv->var, param, &extra);
if (!extra.available) continue;
if (HasVariableParameter(niv->var)) {
this->DrawString(r, i++, " %02x[%02x]: %08x (%s)", niv->var, param, value, niv->name);
} else if (niv->var >= 0x100) {
if (niv->var >= 0x100) {
const char *name = GetExtendedVariableNameById(niv->var);
if (name != nullptr) {
char buf[512];
if (has_param) {
seprintf(buf, lastof(buf), " %s [%02X]: ", name, param);
} else {
seprintf(buf, lastof(buf), " %s: ", name);
}
if (_current_text_dir == TD_RTL) {
this->DrawString(r, i++, " %s: %08x (%s)", name, value, niv->name);
this->DrawString(r, i++, "%s%08x (%s)", buf, value, niv->name);
} else {
if (this->log_console) DEBUG(misc, 0, " %s: %08x (%s)", name, value, niv->name);
if (this->log_console) DEBUG(misc, 0, " %s%08x (%s)", buf, value, niv->name);
int offset = i - this->vscroll->GetPosition();
i++;
if (offset >= 0 && offset < this->vscroll->GetCapacity()) {
Rect sr = r.Shrink(WidgetDimensions::scaled.frametext).Shrink(0, offset * this->resize.step_height, 0, 0);
char buf[512];
seprintf(buf, lastof(buf), " %s: ", name);
::DrawString(sr.left, sr.right, sr.top, buf, TC_BLACK);
int edge = ::DrawString(sr.left, sr.right, sr.top, buf, TC_BLACK);
seprintf(buf, lastof(buf), "%08x (%s)", value, niv->name);
::DrawString(sr.left + prefix_width, sr.right, sr.top, buf, TC_BLACK);
::DrawString(std::max(edge, sr.left + prefix_width), sr.right, sr.top, buf, TC_BLACK);
}
}
}
continue;
}
if (has_param) {
this->DrawString(r, i++, " %02x[%02x]: %08x (%s)", niv->var, param, value, niv->name);
} else {
this->DrawString(r, i++, " %02x: %08x (%s)", niv->var, value, niv->name);
}
@ -889,7 +914,7 @@ struct NewGRFInspectWindow : Window {
for (const NIVariable *niv = nif->variables; niv->name != nullptr; niv++, line--) {
if (line != 1) continue; // 1 because of the "Variables:" line
if (!HasVariableParameter(niv->var)) break;
if (!HasVariableParameter(niv)) break;
this->current_edit_param = niv->var;
ShowQueryString(STR_EMPTY, STR_NEWGRF_INSPECT_QUERY_CAPTION, 9, this, CS_HEXADECIMAL, QSF_NONE);
@ -974,7 +999,7 @@ struct NewGRFInspectWindow : Window {
this->SetWidgetDirty(WID_NGRFI_SCROLLBAR);
}
} else if (this->current_edit_param != 0 && !this->sprite_dump) {
NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][this->current_edit_param - 0x60] = std::strtol(str, nullptr, 16);
this->var60params[this->current_edit_param] = std::strtol(str, nullptr, 16);
this->SetDirty();
}
}
@ -1013,8 +1038,6 @@ struct NewGRFInspectWindow : Window {
}
};
/* static */ uint32 NewGRFInspectWindow::var60params[GSF_FAKE_END][0x20] = { {0} }; // Use spec to have 0s in whole array
static const NWidgetPart _nested_newgrf_inspect_chain_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_GREY),

@ -35,8 +35,9 @@
#define NIC_END() { nullptr, 0, 0, 0, 0 }
/* Helper for filling variable tables */
#define NIV(var, name) { name, var }
#define NIV_END() { nullptr, 0 }
#define NIV(var, name) { name, var, NIVF_NONE }
#define NIVF(var, name, flags) { name, var, flags }
#define NIV_END() { nullptr, 0, NIVF_NONE }
/*** NewGRF Vehicles ***/
@ -584,8 +585,8 @@ static const NIVariable _niv_stations[] = {
NIV(0x68, "station info of nearby tiles"),
NIV(0x69, "information about cargo accepted in the past"),
NIV(0x6A, "GRFID of nearby station tiles"),
NIV(A2VRI_STATION_INFO_NEARBY_TILES_EXT, "station info of nearby tiles ext"),
NIV(A2VRI_STATION_INFO_NEARBY_TILES_V2, "station info of nearby tiles v2"),
NIVF(A2VRI_STATION_INFO_NEARBY_TILES_EXT, "station info of nearby tiles ext", NIVF_SHOW_PARAMS),
NIVF(A2VRI_STATION_INFO_NEARBY_TILES_V2, "station info of nearby tiles v2", NIVF_SHOW_PARAMS),
NIV_END()
};
@ -1947,8 +1948,8 @@ 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 info of nearby plain road tiles"),
NIV(A2VRI_ROADSTOP_INFO_NEARBY_TILES_EXT, "road stop info of nearby tiles ext"),
NIV(A2VRI_ROADSTOP_INFO_NEARBY_TILES_V2, "road stop info of nearby tiles v2"),
NIVF(A2VRI_ROADSTOP_INFO_NEARBY_TILES_EXT, "road stop info of nearby tiles ext", NIVF_SHOW_PARAMS),
NIVF(A2VRI_ROADSTOP_INFO_NEARBY_TILES_V2, "road stop info of nearby tiles v2", NIVF_SHOW_PARAMS),
NIV_END(),
};

Loading…
Cancel
Save