Files
betterxen/tests/conformance/test_gameinput_script/main.cpp
T
ates 6e0b882691 Scripted input and assert stepping for unattended target-build runs
GameInput: BX_INPUT_SCRIPT presses gamepad buttons at given seconds,
without window focus, so an unattended run gets past screens that wait
for input. bx_runtime_veh: BX_CONTINUE_ASSERTS=1 steps over an int3 in
the title's own executable instead of letting it reach the crash
reporter, logging each address once and keeping the thread's last-error
value. Both are off unless set and documented with the other offline-run
diagnostics.

Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-11-30 04:24:10 +03:00

93 lines
3.5 KiB
C++

/*
* BX_INPUT_SCRIPT (GameInput diagnostic): scripted gamepad presses for unattended runs.
*
* The script "A@0.6,DOWN@0.6,B@1.6,BOGUS@1,X" asks for A and DOWN held from 0.6 s to 0.9 s and B
* from 1.6 s to 1.9 s after the first reading; the two unreadable entries are skipped. The test has
* no window, so the keyboard contributes nothing; readings taken at fixed times must carry exactly
* the scripted buttons inside each press and none outside. The times sit at least 0.2 s from every
* press boundary.
*/
#include <windows.h>
#include <unknwn.h>
#include <cstdint>
#include <cstdio>
namespace {
constexpr uint32_t gamepad_kind = 0x00040000;
constexpr uint32_t connected = 1;
struct State {
uint32_t buttons;
float lt, rt, lx, ly, rx, ry;
};
static_assert(sizeof(State) == 28);
template <typename Fn>
Fn slot(IUnknown* object, unsigned index) {
return reinterpret_cast<Fn>((*reinterpret_cast<void***>(object))[index]);
}
using Create = HRESULT(WINAPI*)(void**);
using Current = HRESULT(WINAPI*)(IUnknown*, uint32_t, IUnknown*, IUnknown**);
using DeviceCallback = void(WINAPI*)(uint64_t, void*, IUnknown*, uint64_t, uint32_t, uint32_t);
using Register = HRESULT(WINAPI*)(IUnknown*, IUnknown*, uint32_t, uint32_t, uint32_t, void*, DeviceCallback, uint64_t*);
using StateFn = bool(WINAPI*)(IUnknown*, State*);
void WINAPI note_device(uint64_t, void* context, IUnknown* device, uint64_t, uint32_t, uint32_t) {
*static_cast<IUnknown**>(context) = device;
}
IUnknown* g_input = nullptr;
IUnknown* g_pad = nullptr;
uint64_t g_start = 0;
// Waits until `seconds` after the first reading, then returns the buttons of a fresh reading.
uint32_t buttons_at(double seconds) {
while (GetTickCount64() - g_start < static_cast<uint64_t>(seconds * 1000.0)) Sleep(5);
IUnknown* reading = nullptr;
if (slot<Current>(g_input, 4)(g_input, gamepad_kind, g_pad, &reading) != S_OK || !reading) return 0xFFFFFFFF;
State state{};
const bool ok = slot<StateFn>(reading, 22)(reading, &state);
reading->Release();
return ok ? state.buttons : 0xFFFFFFFF;
}
int g_failed = 0;
void expect(const char* what, uint32_t got, uint32_t want) {
std::printf("%-34s buttons=0x%x expected=0x%x\n", what, got, want);
if (got != want) {
std::fprintf(stderr, "FAIL: test_gameinput_script: %s\n", what);
++g_failed;
}
}
} // namespace
int main() {
SetEnvironmentVariableA("BX_INPUT_SCRIPT", "A@0.6,DOWN@0.6,B@1.6,BOGUS@1,X");
HMODULE module = LoadLibraryW(L"GameInput.dll");
const auto create = module ? reinterpret_cast<Create>(reinterpret_cast<void*>(GetProcAddress(module, MAKEINTRESOURCEA(1)))) : nullptr;
if (!create || create(reinterpret_cast<void**>(&g_input)) != S_OK || !g_input) {
std::fprintf(stderr, "FAIL: test_gameinput_script: GameInput is not available\n");
return 1;
}
uint64_t token = 0;
if (slot<Register>(g_input, 9)(g_input, nullptr, gamepad_kind, connected, 2, &g_pad, note_device, &token) != S_OK || !g_pad) {
std::fprintf(stderr, "FAIL: test_gameinput_script: no gamepad device\n");
return 1;
}
g_start = GetTickCount64();
expect("first reading (0 s)", buttons_at(0.0), 0);
expect("0.3 s: before the first press", buttons_at(0.3), 0);
expect("0.75 s: A and DOWN held", buttons_at(0.75), 0x4 | 0x80);
expect("1.2 s: between presses", buttons_at(1.2), 0);
expect("1.75 s: B held", buttons_at(1.75), 0x8);
expect("2.2 s: after the script", buttons_at(2.2), 0);
if (g_failed) return 1;
std::puts("PASS: test_gameinput_script");
return 0;
}