Rise
The Vieneo Province
Loading...
Searching...
No Matches
HudDiagnostics.cpp
Go to the documentation of this file.
2#include "HudDiagnostics.h"
3#include "../Logger.h"
4#ifdef ChatEnter
5#include "../GameClass.h"
7#include "DialogBase.h"
8#endif
9
10#include <cstdint>
11#include <cstring>
12#include <new>
13
14namespace
15{
16 class TracedEditBox final : public RiseEditBox
17 {
18 public:
19 TracedEditBox(RiseDialog& hud, int dialogId, Logger& logger)
20 : RiseEditBox(&hud), dialogId(dialogId), logger(logger)
21 {
22 }
23
24#ifdef ChatEnter
25 void OnFocusIn() override
26 {
27 const bool hadFocus = m_bHasFocus;
29 if (!hadFocus)
30 LogInputState("focus gained");
31 }
32
33 void OnFocusOut() override
34 {
35 const bool hadFocus = m_bHasFocus;
37 if (hadFocus)
38 LogInputState("focus lost");
39 }
40
41 void SetVisible(bool visible) override
42 {
43 const bool wasVisible = m_bVisible;
45 if (wasVisible != visible)
46 LogInputState(visible ? "shown" : "hidden");
47 }
48
49 bool MsgProc(UINT message, WPARAM key, LPARAM flags) override
50 {
51 if (message == WM_CHAR && key == VK_RETURN)
52 LogInputState("Enter offered to edit control");
53 return RiseEditBox::MsgProc(message, key, flags);
54 }
55
56#endif
57 void Render(float elapsedTime) override
58 {
59 char message[CrashBreadcrumbs::EntrySize];
60 std::snprintf(message, sizeof message, "HUD %d edit %d Inspect v%d focus%d ptr%p",
61 dialogId, GetID(), m_bVisible, m_bHasFocus, static_cast<void*>(this));
62 logger.AddToCallStack(message);
63 if (m_bVisible)
64 {
65 std::snprintf(message, sizeof message, "HUD %d edit %d caret%d sel%d first%d len%d",
66 dialogId, GetID(), m_nCaret, m_nSelStart, m_nFirstVisible, GetTextLength());
67 logger.AddToCallStack(message);
68 std::snprintf(message, sizeof message, "HUD %d edit %d size%dx%d elements%d buffer%p",
69 dialogId, GetID(), m_width, m_height, m_Elements.GetSize(),
70 static_cast<const void*>(GetText()));
71 logger.AddToCallStack(message);
72 }
73 std::snprintf(message, sizeof message, "HUD %d edit %d Render", dialogId, GetID());
74 logger.AddToCallStack(message);
75 RiseEditBox::Render(elapsedTime);
76 std::snprintf(message, sizeof message, "HUD %d edit %d Render DONE", dialogId, GetID());
77 logger.AddToCallStack(message);
78 }
79
80 private:
81#ifdef ChatEnter
82 void LogInputState(const char* event)
83 {
84 char message[200];
85 std::snprintf(message, sizeof message,
86 "ChatInput: %s dialog=%d control=%d visible=%d enabled=%d focus=%d",
87 event, dialogId, GetID(), m_bVisible, m_bEnabled, m_bHasFocus);
88 logger.Log(message); // Info stays in the session trace; never log edit contents.
89 }
90
91#endif
92 int dialogId;
93 Logger& logger;
94 };
95
96 int RecordNativeException(EXCEPTION_POINTERS* exception, int dialogId, Logger& logger)
97 {
98 const EXCEPTION_RECORD& record = *exception->ExceptionRecord;
99 // Leave ordinary C++ exception handling and debugger traps alone.
100 if (record.ExceptionCode == 0xe06d7363 || record.ExceptionCode == EXCEPTION_BREAKPOINT ||
101 record.ExceptionCode == EXCEPTION_SINGLE_STEP)
102 return EXCEPTION_CONTINUE_SEARCH;
103
104 char message[400];
105 std::snprintf(message, sizeof message, "HUD %d native exception 0x%08lX at %p",
106 dialogId, record.ExceptionCode, record.ExceptionAddress);
107 logger.Log(message);
108 logger.AddToCallStack(message);
109
110 HMODULE module = nullptr;
111 if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
112 reinterpret_cast<LPCSTR>(record.ExceptionAddress), &module))
113 {
114 char modulePath[MAX_PATH] = {};
115 GetModuleFileNameA(module, modulePath, MAX_PATH);
116 modulePath[MAX_PATH - 1] = '\0';
117 const char* moduleName = strrchr(modulePath, '\\');
118 std::snprintf(message, sizeof message, "HUD fault module %s base %p RVA 0x%llX",
119 moduleName ? moduleName + 1 : modulePath, static_cast<void*>(module),
120 static_cast<unsigned long long>(reinterpret_cast<std::uintptr_t>(record.ExceptionAddress) -
121 reinterpret_cast<std::uintptr_t>(module)));
122 logger.Log(message);
123 }
124 if ((record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION || record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) &&
125 record.NumberParameters >= 2)
126 {
127 std::snprintf(message, sizeof message, "HUD fault access %llu (0=read 1=write 8=execute) address %p",
128 static_cast<unsigned long long>(record.ExceptionInformation[0]),
129 reinterpret_cast<void*>(record.ExceptionInformation[1]));
130 logger.Log(message);
131 }
132
133 // Observe the fault before unwinding, then retain the existing fatal handler.
134 return EXCEPTION_CONTINUE_SEARCH;
135 }
136
137 HRESULT RenderWithBreadcrumbs(RiseDialog& hud, float elapsedTime, int dialogId, Logger& logger)
138 {
139 char message[CrashBreadcrumbs::EntrySize];
140 std::snprintf(message, sizeof message, "HUD %d Inspect resources", dialogId);
141 logger.AddToCallStack(message);
142 RiseDialogResourceManager* manager = hud.GetManager();
143 std::snprintf(message, sizeof message, "HUD %d manager%p device%p", dialogId,
144 static_cast<void*>(manager), manager ? static_cast<void*>(manager->GetRenderer()) : nullptr);
145 logger.AddToCallStack(message);
146 std::snprintf(message, sizeof message, "HUD %d sprite%p state%p", dialogId,
147 manager ? static_cast<void*>(manager->m_pSprite) : nullptr,
148 manager ? static_cast<void*>(manager->m_pStateBlock) : nullptr);
149 logger.AddToCallStack(message);
150 std::snprintf(message, sizeof message, "HUD %d visible%d minimized%d foreground%d dt%.4f", dialogId,
151 hud.GetVisible(), hud.GetMinimized(), GetForegroundWindow() == Rendering::ClientWindow(), elapsedTime);
152 logger.AddToCallStack(message);
153 std::snprintf(message, sizeof message, "HUD %d OnRender", dialogId);
154 logger.AddToCallStack(message);
155 const HRESULT result = hud.OnRender(elapsedTime);
156 std::snprintf(message, sizeof message, "HUD %d OnRender DONE hr%08lX", dialogId, result);
157 logger.AddToCallStack(message);
158 return result;
159 }
160}
161
162#ifdef ChatEnter
163bool HudDiagnostics::IsEnterMessage(UINT message, WPARAM key)
164{
165 return key == VK_RETURN &&
166 (message == WM_KEYDOWN || message == WM_KEYUP || message == WM_CHAR ||
167 message == WM_SYSKEYDOWN || message == WM_SYSKEYUP || message == WM_SYSCHAR);
168}
169
170void HudDiagnostics::LogChatInput(HMI& gui, const char* event, int handledByDialog)
171{
172 char message[600];
173 std::snprintf(message, sizeof message,
174 "ChatInput: %s stage=%d textMode=%d editing=%d edited=%d hover=%d leftClick=%d foreground=%d handledByDialog=%d",
175 event, static_cast<int>(gui.game->displaystage), static_cast<int>(g_bTextInput),
176 gui.Editing, gui.Edited, gui.cursoroverwindow, gui.g_bLeftClick,
177 GetForegroundWindow() == Rendering::ClientWindow(), handledByDialog);
178 gui.game->logger->Log(message);
179
180 // Dialogs are only valid in game. Include other editors even when their HUD
181 // is skipped by the mouse-over routing, and the receiver before/after dispatch.
182 if (gui.game->displaystage != GameState::STATUS_GAME)
183 return;
184 for (int id = 0; id < D_ENUMERATION; ++id)
185 {
186 auto* dialog = DialogBase::dialogs[id];
187 if (!dialog || (id != D_COMMUNICATIONS && id != handledByDialog &&
188 dialog->editline == -1 && dialog->editfield == -1 && dialog->edittab == -1))
189 continue;
190 std::snprintf(message, sizeof message,
191 "ChatInput: dialog=%d active=%d minimized=%d layer=%d editLine=%ld editField=%ld editTab=%ld "
192 "hudVisible=%d hudMinimized=%d keyboard=%d position=%.0f,%.0f size=%d,%d",
193 id, dialog->active, dialog->minimized, dialog->layer, dialog->editline,
194 dialog->editfield, dialog->edittab, dialog->HUD.GetVisible(), dialog->HUD.GetMinimized(),
195 dialog->HUD.IsKeyboardInputEnabled(), dialog->windowlocation.x, dialog->windowlocation.y,
196 dialog->width, dialog->height);
197 gui.game->logger->Log(message);
198 for (int controlId : { IDC_TRANSMIT, IDC_FIELDEDIT, IDC_TABEDIT })
199 {
200 auto* control = dialog->HUD.GetEditBox(controlId);
201 if (!control)
202 continue;
203 std::snprintf(message, sizeof message,
204 "ChatInput: dialog=%d control=%d visible=%d enabled=%d focus=%d",
205 id, controlId, control->GetVisible(), control->GetEnabled(), control->m_bHasFocus);
206 gui.game->logger->Log(message);
207 }
208 }
209}
210
211#endif
212HRESULT HudDiagnostics::Render(RiseDialog& hud, float elapsedTime, int dialogId, Logger& logger)
213{
214 __try
215 {
216 return RenderWithBreadcrumbs(hud, elapsedTime, dialogId, logger);
217 }
218 __except (RecordNativeException(GetExceptionInformation(), dialogId, logger))
219 {
220 return E_FAIL; // The filter always continues the exception search.
221 }
222}
223
224HRESULT HudDiagnostics::AddEditBox(RiseDialog& hud, int dialogId, int controlId, bool isDefault, Logger& logger)
225{
226 auto* edit = new (std::nothrow) TracedEditBox(hud, dialogId, logger);
227 if (!edit)
228 return E_OUTOFMEMORY;
229 const HRESULT result = hud.AddControl(edit);
230 if (FAILED(result))
231 {
232 delete edit;
233 return result;
234 }
235 // Match the original AddEditBox construction; subsequent layout and input still use DXUT.
236 edit->SetID(controlId);
237 edit->SetLocation(1, 1);
238 edit->SetSize(1, 1);
239 edit->m_bIsDefault = isDefault;
240 edit->m_bIsPassword = false;
241 edit->SetText(L"");
242 return S_OK;
243}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
@ D_COMMUNICATIONS
Definition DialogBase.h:12
@ D_ENUMERATION
Definition DialogBase.h:15
@ STATUS_GAME
Definition GameState.h:9
static constexpr std::size_t EntrySize
static DialogBase * dialogs[D_ENUMERATION]
Definition DialogBase.h:41
Definition gui.h:1012
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
void AddToCallStack(const char *msg)
Definition Logger.cpp:355
virtual void OnFocusOut()
Definition Controls.h:554
virtual void SetVisible(bool bVisible)
Definition Controls.h:583
bool GetVisible()
Definition Controls.h:239
HRESULT OnRender(float fElapsedTime)
Definition Controls.cpp:526
HRESULT AddControl(RiseControl *pControl)
RiseDialogResourceManager * GetManager()
Definition Controls.h:346
bool GetMinimized()
Definition Controls.h:247
Rendering::RenderDevice * GetRenderer()
Definition Controls.h:457
Rendering::RenderSprite * m_pSprite
Definition Controls.h:490
Rendering::RenderStateSnapshot * m_pStateBlock
Definition Controls.h:489
virtual void Render(float fElapsedTime)
virtual bool MsgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
virtual void OnFocusIn()
char g_bTextInput
Definition globals.cpp:61
@ IDC_TABEDIT
Definition gui.h:973
@ IDC_FIELDEDIT
Definition gui.h:972
@ IDC_TRANSMIT
Definition gui.h:974
HRESULT Render(RiseDialog &hud, float elapsedTime, int dialogId, Logger &logger)
HRESULT AddEditBox(RiseDialog &hud, int dialogId, int controlId, bool isDefault, Logger &logger)
HWND ClientWindow()
gui(open_browser=True)
Definition erin.py:108