SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Toggle main menu visibility
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
6
using namespace
gamepp
;
7
using 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
15
std::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
34
std::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
44
math::Vec2i
GameInstance::getWindowPos
()
const
{
45
RECT rect;
46
GetWindowRect(
reinterpret_cast<
HWND
>
(this->
hwnd
), &rect);
47
return
{rect.left, rect.top};
48
}
49
50
sourcepp::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
56
const
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
67
std::optional<GameInstance>
GameInstance::find
(std::string_view) {
68
return
std::nullopt;
69
}
70
71
std::string
GameInstance::getWindowTitle
()
const
{
72
return
""
;
73
}
74
75
math::Vec2i
GameInstance::getWindowPos
()
const
{
76
return
{};
77
}
78
79
math::Vec2i
GameInstance::getWindowSize
()
const
{
80
return
{};
81
}
82
83
const
GameInstance
&
GameInstance::command
(std::string_view)
const
{
84
return
*
this
;
85
}
86
87
#endif
88
89
const
GameInstance
&
GameInstance::inputBegin
(std::string_view input)
const
{
90
this->
command
(
"+"
+ std::string{input});
91
return
*
this
;
92
}
93
94
const
GameInstance
&
GameInstance::inputEnd
(std::string_view input)
const
{
95
this->
command
(
"-"
+ std::string{input});
96
return
*
this
;
97
}
98
99
const
GameInstance
&
GameInstance::inputOnce
(std::string_view input)
const
{
100
this->
inputBegin
(input).
inputEnd
(input);
101
return
*
this
;
102
}
103
104
const
GameInstance
&
GameInstance::inputHold
(std::string_view input,
double
sec)
const
{
105
this->
inputBegin
(input).
wait
(sec).
inputEnd
(input);
106
return
*
this
;
107
}
108
109
const
GameInstance
&
GameInstance::wait
(
double
sec)
const
{
110
std::this_thread::sleep_for(std::chrono::duration<double>(sec));
111
return
*
this
;
112
}
gamepp::GameInstance
Definition
gamepp.h:11
gamepp::GameInstance::inputHold
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
gamepp::GameInstance::inputBegin
const GameInstance & inputBegin(std::string_view input) const
Begin "pressing" an input such as forward or left.
Definition
gamepp.cpp:89
gamepp::GameInstance::getWindowSize
sourcepp::math::Vec2i getWindowSize() const
Get the window size on-screen.
Definition
gamepp.cpp:79
gamepp::GameInstance::wait
const GameInstance & wait(double sec) const
Sleep on the current thread for the given number of seconds.
Definition
gamepp.cpp:109
gamepp::GameInstance::command
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
gamepp::GameInstance::GameInstance
GameInstance()=default
gamepp::GameInstance::find
static std::optional< GameInstance > find(std::string_view windowNameOverride="")
Find a running instance of a Source engine game.
Definition
gamepp.cpp:67
gamepp::GameInstance::hwnd
void * hwnd
Definition
gamepp.h:86
gamepp::GameInstance::inputOnce
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
gamepp::GameInstance::inputEnd
const GameInstance & inputEnd(std::string_view input) const
End "pressing" an input such as forward or left.
Definition
gamepp.cpp:94
gamepp::GameInstance::getWindowTitle
std::string getWindowTitle() const
Get the human-readable window title.
Definition
gamepp.cpp:71
gamepp::GameInstance::getWindowPos
sourcepp::math::Vec2i getWindowPos() const
Get the window position on-screen.
Definition
gamepp.cpp:75
gamepp.h
gamepp
Definition
gamepp.h:9
sourcepp
Definition
LZMA.h:11
src
gamepp
gamepp.cpp
Generated on
for SourcePP by
1.17.0