Skip to content

Commit

Permalink
Merge remote-tracking branch 'blattersturm/feature/win32k-usercrit-ch…
Browse files Browse the repository at this point in the history
…ecks' into master-pub
  • Loading branch information
blattersturm committed Aug 14, 2021
2 parents 56b0b6a + 4f3737e commit 7f18e00
Showing 1 changed file with 92 additions and 4 deletions.
96 changes: 92 additions & 4 deletions code/components/citizen-devtools/src/ResourceTimeWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <chrono>

#include <CfxLocale.h>

#include "ResourceMonitor.h"

DLL_IMPORT ImFont* GetConsoleFontTiny();
Expand Down Expand Up @@ -67,7 +69,73 @@ constexpr auto tuple_slice(Cont&& t)
return detail::slice_impl<I1>(std::forward<Cont>(t),
std::make_index_sequence<I2 - I1>{});
}

static std::chrono::microseconds lastHitch;

#ifdef GTA_FIVE
#include <Hooking.h>
#include <InputHook.h>

static decltype(&SetThreadExecutionState) origSetThreadExecutionState;
static decltype(&PeekMessageW) origPeekMessageW;

static std::chrono::microseconds currentTotal;
static bool currentWasFocusEvent = false;

static EXECUTION_STATE WINAPI SetThreadExecutionState_Track(EXECUTION_STATE esFlags)
{
if (esFlags == 3)
{
currentTotal = std::chrono::microseconds{ 0 };
currentWasFocusEvent = false;
}

return origSetThreadExecutionState(esFlags);
}

static BOOL WINAPI PeekMessageW_Track(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
{
// a focus event may take long, so we ignore it
if (currentWasFocusEvent)
{
return origPeekMessageW(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
}

// track just the PeekMessage call
// this will include internal wndproc invocations as well once other events run out
auto thisStart = usec();
auto rv = origPeekMessageW(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
auto thisEnd = usec();

currentTotal += (thisEnd - thisStart);

// if we're out of events, and didn't get a focus event generated anyway
if (!rv && !currentWasFocusEvent)
{
if (currentTotal > 30ms)
{
lastHitch = thisEnd;
}
}

return rv;
}

static HookFunction hookFunctionGameTime([]()
{
InputHook::DeprecatedOnWndProc.Connect([](HWND, UINT uMsg, WPARAM, LPARAM, bool&, LRESULT&)
{
// we want to ignore both focus-in and focus-out events
if (uMsg == WM_ACTIVATEAPP)
{
currentWasFocusEvent = true;
}
}, INT32_MIN);

origSetThreadExecutionState = hook::iat("kernel32.dll", SetThreadExecutionState_Track, "SetThreadExecutionState");
origPeekMessageW = hook::iat("user32.dll", PeekMessageW_Track, "PeekMessageW");
});
#endif

static InitFunction initFunction([]()
{
Expand Down Expand Up @@ -119,7 +187,7 @@ static InitFunction initFunction([]()
ConHost::OnDrawGui.Connect([]()
{
#ifndef IS_FXSERVER
if ((usec() - warningLastShown) < 20s)
auto displayWarningDialog = [](auto cb)
{
ImGui::PushFont(GetConsoleFontTiny());

Expand All @@ -130,15 +198,35 @@ static InitFunction initFunction([]()

ImGui::SetNextWindowBgAlpha(0.3f); // Transparent background
if (ImGui::Begin("Time Warning", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav))
{
cb();
}

ImGui::End();
ImGui::PopFont();
};

if ((usec() - warningLastShown) < 20s)
{
displayWarningDialog([]
{
ImGui::Text("/!\\ Resource time warning");
ImGui::Separator();
ImGui::Text(resourceTimeWarningText.c_str());
ImGui::Separator();
ImGui::Text("Please contact the server owner to resolve this issue.");
}
ImGui::End();
ImGui::PopFont();
});
}
else if ((usec() - lastHitch) < 5s)
{
displayWarningDialog([]
{
ImGui::Text(va("/!\\ %s", gettext("Slow system performance detected")));
ImGui::Separator();
ImGui::Text("%s", gettext("A call into the Windows API took too long recently and led to a game stutter.").c_str());
ImGui::Separator();
ImGui::Text("%s", gettext("Please close any software you have running in the background (including Windows apps such as File Explorer or Task Manager).").c_str());
});
}
#endif

Expand Down

0 comments on commit 7f18e00

Please sign in to comment.