SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
gamepp.cpp
Go to the documentation of this file.
1#include <gamepp/gamepp.h>
2
3#include <string>
4#include <thread>
5
6using namespace gamepp;
7using namespace sourcepp;
8
9// All this stuff is very Windows-dependent, sorry!
10#ifdef _WIN32
11
12#define WIN32_LEAN_AND_MEAN
13#include <Windows.h>
14
15std::optional<GameInstance> GameInstance::find(std::string_view windowNameOverride) {
16 GameInstance instance{};
17
18 if (!windowNameOverride.empty()) {
19 instance.hwnd = FindWindowA(windowNameOverride.data(), nullptr);
20 } else {
21 instance.hwnd = FindWindowA("Valve001", nullptr);
22 if (!instance.hwnd) {
23 instance.hwnd = FindWindowA("Strata001", nullptr);
24 }
25 // are any other names used in other branches known?
26 }
27
28 if (!instance.hwnd) {
29 return std::nullopt;
30 }
31 return instance;
32}
33
34std::string GameInstance::getWindowTitle() const {
35 // This should be large enough
36 std::string title(512, '\0');
37 if (auto size = GetWindowTextA(reinterpret_cast<HWND>(this->hwnd), title.data(), static_cast<int>(title.length()))) {
38 title.resize(size);
39 return title;
40 }
41 return "";
42}
43
44math::Vec2i GameInstance::getWindowPos() const {
45 RECT rect;
46 GetWindowRect(reinterpret_cast<HWND>(this->hwnd), &rect);
47 return {rect.left, rect.top};
48}
49
50sourcepp::math::Vec2i GameInstance::getWindowSize() const {
51 RECT rect;
52 GetWindowRect(reinterpret_cast<HWND>(this->hwnd), &rect);
53 return {rect.right - rect.left, rect.bottom - rect.top};
54}
55
56const GameInstance& GameInstance::command(std::string_view command) const {
57 COPYDATASTRUCT data;
58 data.dwData = 0;
59 data.cbData = command.length() + 1;
60 data.lpData = reinterpret_cast<void*>(const_cast<char*>(command.data()));
61 SendMessageTimeoutA(reinterpret_cast<HWND>(this->hwnd), WM_COPYDATA, 0, reinterpret_cast<LPARAM>(&data), SMTO_ABORTIFHUNG, 0, nullptr);
62 return *this;
63}
64
65#else
66
67std::optional<GameInstance> GameInstance::find(std::string_view) {
68 return std::nullopt;
69}
70
71std::string GameInstance::getWindowTitle() const {
72 return "";
73}
74
75math::Vec2i GameInstance::getWindowPos() const {
76 return {};
77}
78
79math::Vec2i GameInstance::getWindowSize() const {
80 return {};
81}
82
83const GameInstance& GameInstance::command(std::string_view) const {
84 return *this;
85}
86
87#endif
88
89const GameInstance& GameInstance::inputBegin(std::string_view input) const {
90 this->command("+" + std::string{input});
91 return *this;
92}
93
94const GameInstance& GameInstance::inputEnd(std::string_view input) const {
95 this->command("-" + std::string{input});
96 return *this;
97}
98
99const GameInstance& GameInstance::inputOnce(std::string_view input) const {
100 this->inputBegin(input).inputEnd(input);
101 return *this;
102}
103
104const GameInstance& GameInstance::inputHold(std::string_view input, double sec) const {
105 this->inputBegin(input).wait(sec).inputEnd(input);
106 return *this;
107}
108
109const GameInstance& GameInstance::wait(double sec) const {
110 std::this_thread::sleep_for(std::chrono::duration<double>(sec));
111 return *this;
112}
const GameInstance & inputHold(std::string_view input, double sec) const
Begin and end "pressing" an input in the given timespan, like holding the use key.
Definition: gamepp.cpp:104
const GameInstance & inputBegin(std::string_view input) const
Begin "pressing" an input such as forward or left.
Definition: gamepp.cpp:89
sourcepp::math::Vec2i getWindowSize() const
Get the window size on-screen.
Definition: gamepp.cpp:79
const GameInstance & wait(double sec) const
Sleep on the current thread for the given number of seconds.
Definition: gamepp.cpp:109
const GameInstance & command(std::string_view command) const
Send a command to the engine (ran as if it was entered into the console by the user).
Definition: gamepp.cpp:83
static std::optional< GameInstance > find(std::string_view windowNameOverride="")
Find a running instance of a Source engine game.
Definition: gamepp.cpp:67
const GameInstance & inputOnce(std::string_view input) const
Begin and end "pressing" an input in one tick, like tapping the use key.
Definition: gamepp.cpp:99
const GameInstance & inputEnd(std::string_view input) const
End "pressing" an input such as forward or left.
Definition: gamepp.cpp:94
std::string getWindowTitle() const
Get the human-readable window title.
Definition: gamepp.cpp:71
sourcepp::math::Vec2i getWindowPos() const
Get the window position on-screen.
Definition: gamepp.cpp:75
Definition: gamepp.h:9
Definition: LZMA.h:11