diff options
| author | Sc4ros <353102343+Sc4ros@users.noreply.github.com> | 2026-11-22 17:59:38 +0100 |
|---|---|---|
| committer | Sc4ros <353102343+Sc4ros@users.noreply.github.com> | 2026-11-22 21:20:03 +0100 |
| commit | 37e6d482865ce7266cb198163c049121f46caeb0 (patch) | |
| tree | 8185667723be2bd0031f65dc86c20f24b43afceb /runtime/test/gameinput_host_test.cpp | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
Tests: the host GameInput.dll path from mapped code
docs/gameinput.md says that without the SDL3 shim the loader binds
GameInputCreate to the host's GameInput.dll, but nothing tested it.
gameinput_host_test maps a CRT-free fixture DLL built against the SDK's
gameinput.lib and checks that the import binds to the host copy (the test links
no shim table), that GameInputCreate and GetCurrentTimestamp work from mapped
code, that GetCurrentReading answers, and, with a pad attached, that
GetGamepadState succeeds. It skips (77) where there is no GameInput.dll, and is
only built when gameinput.lib is found.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/gameinput_host_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_host_test.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/runtime/test/gameinput_host_test.cpp b/runtime/test/gameinput_host_test.cpp new file mode 100644 index 000000000..5673965a9 --- /dev/null +++ b/runtime/test/gameinput_host_test.cpp | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | // The host GameInput path: mapped code that imports GameInputCreate from GameInput.dll, with no GameInput shim. | ||
| 2 | // | ||
| 3 | // docs/gameinput.md: without the SDL3 shim, the loader binds GameInputCreate to the host's GameInput.dll. This checks | ||
| 4 | // that path with a synthetic DLL (test/fixtures/gameinput_host.cpp) mapped by rt::Loader: | ||
| 5 | // - its GameInput.dll import binds to the host's copy (this test links no shim table, so it cannot be a shim); | ||
| 6 | // - GameInputCreate succeeds from mapped code, and the library answers GetCurrentTimestamp; | ||
| 7 | // - GetCurrentReading says either "reading" or "no reading found": no crash, no other error. With a gamepad attached | ||
| 8 | // GetGamepadState must succeed on the reading; the test passes with or without one. | ||
| 9 | // Skips (exit 77) where there is no GameInput.dll (the import is then unimplemented). No game files. | ||
| 10 | #include <windows.h> | ||
| 11 | |||
| 12 | #include <cstdio> | ||
| 13 | #include <filesystem> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <string> | ||
| 16 | |||
| 17 | #include "pe_loader.h" | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | using ProbeFn = int (*)(long*, unsigned long long*, long*, int*); | ||
| 22 | constexpr unsigned long kReadingNotFound = 0x838A0003UL; // GAMEINPUT_E_READING_NOT_FOUND | ||
| 23 | |||
| 24 | int g_failures = 0; | ||
| 25 | |||
| 26 | void check(bool ok, const std::string& what) { | ||
| 27 | std::printf("%s %s\n", ok ? "PASS" : "FAIL", what.c_str()); | ||
| 28 | std::fflush(stdout); | ||
| 29 | if (!ok) ++g_failures; | ||
| 30 | } | ||
| 31 | |||
| 32 | } // namespace | ||
| 33 | |||
| 34 | int main(int argc, char** argv) { | ||
| 35 | if (argc != 2) { | ||
| 36 | std::fprintf(stderr, "usage: gameinput_host_test <gameinput_host.dll>\n"); | ||
| 37 | return 2; | ||
| 38 | } | ||
| 39 | const std::filesystem::path fixture = argv[1]; | ||
| 40 | try { | ||
| 41 | rt::Loader loader(fixture.parent_path()); | ||
| 42 | rt::Module& m = loader.load(fixture); | ||
| 43 | |||
| 44 | // The SDK's import library imports by ordinal, so match the DLL name, not the function name. | ||
| 45 | rt::Binding binding = rt::Binding::Unimplemented; | ||
| 46 | int seen = 0; | ||
| 47 | for (const auto& r : m.imports) | ||
| 48 | if (rt::lower(r.dll) == "gameinput.dll") { | ||
| 49 | binding = r.binding; | ||
| 50 | ++seen; | ||
| 51 | } | ||
| 52 | if (seen == 1 && binding == rt::Binding::Unimplemented) { | ||
| 53 | std::printf("SKIP: no GameInput.dll on this machine\n"); | ||
| 54 | return 77; | ||
| 55 | } | ||
| 56 | check(seen == 1 && binding == rt::Binding::Host, "the GameInput.dll import binds to the host's copy"); | ||
| 57 | |||
| 58 | loader.initialise(); | ||
| 59 | const auto probe = reinterpret_cast<ProbeFn>(m.exports.at("gameinput_host_probe")); | ||
| 60 | long create_hr = -1, reading_hr = -1; | ||
| 61 | unsigned long long timestamp = 0; | ||
| 62 | int state_ok = 0; | ||
| 63 | const int created = probe(&create_hr, ×tamp, &reading_hr, &state_ok); | ||
| 64 | std::printf(" create hr=0x%08lX, timestamp=%llu, reading hr=0x%08lX\n", static_cast<unsigned long>(create_hr), | ||
| 65 | timestamp, static_cast<unsigned long>(reading_hr)); | ||
| 66 | check(created == 1 && create_hr == 0, "GameInputCreate succeeds from mapped code"); | ||
| 67 | check(timestamp != 0, "GetCurrentTimestamp returns a time"); | ||
| 68 | const bool pad = reading_hr == 0; | ||
| 69 | check(pad || static_cast<unsigned long>(reading_hr) == kReadingNotFound, | ||
| 70 | "GetCurrentReading says either 'reading' or 'no reading found'"); | ||
| 71 | std::printf(" gamepad reading: %s\n", pad ? "yes (a gamepad is attached)" : "none (no gamepad attached)"); | ||
| 72 | if (pad) check(state_ok == 1, "GetGamepadState succeeds on a real reading"); | ||
| 73 | } catch (const std::exception& e) { | ||
| 74 | std::printf("FAIL unexpected exception: %s\n", e.what()); | ||
| 75 | ++g_failures; | ||
| 76 | } | ||
| 77 | std::printf("gameinput_host_test: %s (%d failures)\n", g_failures ? "FAILED" : "OK", g_failures); | ||
| 78 | return g_failures ? 1 : 0; | ||
| 79 | } | ||