Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(SOURCEPP_USE_BSPPP "Build bsppp library" ON)
option(SOURCEPP_USE_DMXPP "Build dmxpp library" ON)
option(SOURCEPP_USE_FGDPP "Build fgdpp library" ON)
option(SOURCEPP_USE_GAMEPP "Build gamepp library" ON)
option(SOURCEPP_USE_KVPP "Build kvpp library" ON)
option(SOURCEPP_USE_MDLPP "Build mdlpp library" ON)
option(SOURCEPP_USE_STEAMPP "Build steampp library" ON)
Expand Down Expand Up @@ -88,6 +89,7 @@ endif()
add_sourcepp_library(bsppp NO_TEST)
add_sourcepp_library(dmxpp)
add_sourcepp_library(fgdpp)
add_sourcepp_library(gamepp)
add_sourcepp_library(kvpp)
add_sourcepp_library(mdlpp)
add_sourcepp_library(steampp C)
Expand Down
5 changes: 5 additions & 0 deletions FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
- Add write support
- `fgdpp`
- Perhaps add the ability to parse TeamSpen's additions to the format?
- `gamepp`
- Add a function to create instances of a game rather than just finding existing ones
- When creating an instance of the game, attaching a console might be easier, or enabling -condebug,
which would then allow reading output from the console
- Add a method to take a screenshot of the game and move the file to a user-specified location
- `kvpp`
- Add write support
- `mdlpp`
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<td align="center">✅</td>
<td align="center"></td>
</tr>
<tr>
<td><code>gamepp</code></td>
<td>
<ul>
<li>Get Source engine instance window title/position/size</li>
<li>Run commands in a Source engine instance remotely</li>
</ul>
</td>
<td align="center">n/a</td>
<td align="center">n/a</td>
<td align="center"></td>
</tr>
<tr>
<td><code>kvpp</code></td>
<td>
Expand Down Expand Up @@ -83,11 +95,11 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<td><code>steampp</code></td>
<td>
<ul>
<li>Finding Steam install folder</li>
<li>Finding installed Steam games</li>
<li>Find Steam install folder</li>
<li>Find installed Steam games</li>
</ul>
</td>
<td align="center"></td>
<td align="center">n/a</td>
<td align="center">n/a</td>
<td align="center">C</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion ext/bufferstream
89 changes: 89 additions & 0 deletions include/gamepp/gamepp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#pragma once

#include <optional>
#include <string>
#include <string_view>

#include <sourcepp/math/Vector.h>

namespace gamepp {

class GameInstance {
public:
/**
* Find a running instance of a Source engine game. Note that this function will only ever
* succeed on Windows, because Valve never bothered to expose this functionality on Linux.
* @param windowNameOverride The classname of the window to search for, e.g. "Valve001".
* If unspecified it will check for "Valve001" and "Strata001".
* @return A GameInstance if a game is located.
*/
[[nodiscard]] static std::optional<GameInstance> find(std::string_view windowNameOverride = "");

/**
* Get the human-readable window title.
* @return The window title.
*/
[[nodiscard]] std::string getWindowTitle() const;

/**
* Get the window position on-screen.
* @return The window position.
*/
[[nodiscard]] sourcepp::math::Vec2i getWindowPos() const;

/**
* Get the window size on-screen.
* @return The window size.
*/
[[nodiscard]] sourcepp::math::Vec2i getWindowSize() const;

/**
* Send a command to the engine (ran as if it was entered into the console by the user).
* @param command The command to run.
* @return This GameInstance.
*/
const GameInstance& command(std::string_view command) const;

/**
* Begin "pressing" an input such as forward or left.
* @param command The input to run (without the plus sign prefix).
* @return This GameInstance.
*/
const GameInstance& inputBegin(std::string_view input) const;

/**
* End "pressing" an input such as forward or left.
* @param command The input to run (without the minus sign prefix).
* @return This GameInstance.
*/
const GameInstance& inputEnd(std::string_view input) const;

/**
* Begin and end "pressing" an input in one tick, like tapping the use key.
* @param command The input to run (without any prefix).
* @return This GameInstance.
*/
const GameInstance& inputOnce(std::string_view input) const;

/**
* Begin and end "pressing" an input in the given timespan, like holding the use key.
* @param input The input to run (without any prefix).
* @param sec The time to hold the input for.
* @return This GameInstance.
*/
const GameInstance& inputHold(std::string_view input, double sec) const;

/**
* Sleep on the current thread for the given number of seconds.
* @param sec The number of seconds.
* @return This GameInstance.
*/
const GameInstance& wait(double sec) const;

protected:
GameInstance() = default;

void* hwnd;
};

} // namespace gamepp
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/fgdpp/fgdpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include <BufferStream.h>

#include <sourcepp/fs/FS.h>
#include <sourcepp/parser/Text.h>
#include <sourcepp/string/String.h>
#include <sourcepp/FS.h>
#include <sourcepp/String.h>

using namespace fgdpp;
using namespace sourcepp;
Expand Down
4 changes: 4 additions & 0 deletions src/gamepp/_gamepp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_pretty_parser(gamepp
SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/include/gamepp/gamepp.h"
"${CMAKE_CURRENT_LIST_DIR}/gamepp.cpp")
112 changes: 112 additions & 0 deletions src/gamepp/gamepp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <gamepp/gamepp.h>

#include <string>
#include <thread>

using namespace gamepp;
using namespace sourcepp;

// All this stuff is very Windows-dependent, sorry!
#ifdef _WIN32

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

std::optional<GameInstance> GameInstance::find(std::string_view windowNameOverride) {
GameInstance instance;

if (!windowNameOverride.empty()) {
instance.hwnd = FindWindowA(windowNameOverride.data(), nullptr);
} else {
instance.hwnd = FindWindowA("Valve001", nullptr);
if (!instance.hwnd) {
instance.hwnd = FindWindowA("Strata001", nullptr);
}
// are any other names used in other branches known?
}

if (!instance.hwnd) {
return std::nullopt;
}
return instance;
}

std::string GameInstance::getWindowTitle() const {
// This should be large enough
std::string title(512, '\0');
if (auto size = GetWindowTextA(reinterpret_cast<HWND>(this->hwnd), title.data(), title.length())) {
title.resize(size);
return title;
}
return "";
}

math::Vec2i GameInstance::getWindowPos() const {
RECT rect;
GetWindowRect(reinterpret_cast<HWND>(this->hwnd), &rect);
return {rect.left, rect.top};
}

sourcepp::math::Vec2i GameInstance::getWindowSize() const {
RECT rect;
GetWindowRect(reinterpret_cast<HWND>(this->hwnd), &rect);
return {rect.right - rect.left, rect.bottom - rect.top};
}

const GameInstance& GameInstance::command(std::string_view command) const {
COPYDATASTRUCT data;
data.dwData = 0;
data.cbData = command.length() + 1;
data.lpData = reinterpret_cast<void*>(const_cast<char*>(command.data()));
SendMessageTimeoutA(reinterpret_cast<HWND>(this->hwnd), WM_COPYDATA, 0, reinterpret_cast<LPARAM>(&data), SMTO_ABORTIFHUNG, 0, nullptr);
return *this;
}

#else

std::optional<GameInstance> GameInstance::find(std::string_view) {
return std::nullopt;
}

std::string GameInstance::getWindowTitle() const {
return "";
}

math::Vec2i GameInstance::getWindowPos() const {
return {};
}

math::Vec2i GameInstance::getWindowSize() const {
return {};
}

const GameInstance& GameInstance::command(std::string_view) const {
return *this;
}

#endif

const GameInstance& GameInstance::inputBegin(std::string_view input) const {
this->command("+" + std::string{input});
return *this;
}

const GameInstance& GameInstance::inputEnd(std::string_view input) const {
this->command("-" + std::string{input});
return *this;
}

const GameInstance& GameInstance::inputOnce(std::string_view input) const {
this->inputBegin(input).inputEnd(input);
return *this;
}

const GameInstance& GameInstance::inputHold(std::string_view input, double sec) const {
this->inputBegin(input).wait(sec).inputEnd(input);
return *this;
}

const GameInstance& GameInstance::wait(double sec) const {
std::this_thread::sleep_for(std::chrono::duration<double>(sec));
return *this;
}
2 changes: 1 addition & 1 deletion src/kvpp/kvpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <BufferStream.h>

#include <sourcepp/parser/Text.h>
#include <sourcepp/string/String.h>
#include <sourcepp/String.h>

using namespace kvpp;
using namespace sourcepp;
Expand Down
2 changes: 1 addition & 1 deletion src/sourcepp/fs/FS.cpp → src/sourcepp/FS.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <sourcepp/fs/FS.h>
#include <sourcepp/FS.h>

#include <filesystem>

Expand Down
2 changes: 1 addition & 1 deletion src/sourcepp/string/String.cpp → src/sourcepp/String.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <sourcepp/string/String.h>
#include <sourcepp/String.h>

#include <algorithm>
#include <cctype>
Expand Down
8 changes: 4 additions & 4 deletions src/sourcepp/_sourcepp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ list(APPEND ${PROJECT_NAME}_HEADERS
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/crypto/MD5.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/crypto/RSA.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/crypto/String.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/fs/FS.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Angles.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Float.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Integer.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Matrix.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/math/Vector.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/parser/Binary.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/parser/Text.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/string/String.h")
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/FS.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sourcepp/String.h")

add_library(${PROJECT_NAME} STATIC
${${PROJECT_NAME}_HEADERS}
Expand All @@ -21,10 +21,10 @@ add_library(${PROJECT_NAME} STATIC
"${CMAKE_CURRENT_LIST_DIR}/crypto/MD5.cpp"
"${CMAKE_CURRENT_LIST_DIR}/crypto/RSA.cpp"
"${CMAKE_CURRENT_LIST_DIR}/crypto/String.cpp"
"${CMAKE_CURRENT_LIST_DIR}/fs/FS.cpp"
"${CMAKE_CURRENT_LIST_DIR}/parser/Binary.cpp"
"${CMAKE_CURRENT_LIST_DIR}/parser/Text.cpp"
"${CMAKE_CURRENT_LIST_DIR}/string/String.cpp")
"${CMAKE_CURRENT_LIST_DIR}/FS.cpp"
"${CMAKE_CURRENT_LIST_DIR}/String.cpp")

target_precompile_headers(${PROJECT_NAME} PUBLIC ${${PROJECT_NAME}_HEADERS})

Expand Down
2 changes: 1 addition & 1 deletion src/steampp/steampp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#endif

#include <kvpp/kvpp.h>
#include <sourcepp/fs/FS.h>
#include <sourcepp/FS.h>

using namespace kvpp;
using namespace sourcepp;
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/PackFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <FileStream.h>

#include <sourcepp/crypto/CRC32.h>
#include <sourcepp/fs/FS.h>
#include <sourcepp/string/String.h>
#include <sourcepp/FS.h>
#include <sourcepp/String.h>
#include <vpkpp/format/BSP.h>
#include <vpkpp/format/FPX.h>
#include <vpkpp/format/GCF.h>
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/BSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <mz_zip.h>
#include <mz_zip_rw.h>

#include <sourcepp/fs/FS.h>
#include <sourcepp/string/String.h>
#include <sourcepp/FS.h>
#include <sourcepp/String.h>

using namespace sourcepp;
using namespace vpkpp;
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/GCF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include <sourcepp/crypto/Adler32.h>
#include <sourcepp/crypto/CRC32.h>
#include <sourcepp/fs/FS.h>
#include <sourcepp/string/String.h>
#include <sourcepp/FS.h>
#include <sourcepp/String.h>

using namespace sourcepp;
using namespace vpkpp;
Expand Down
4 changes: 2 additions & 2 deletions src/vpkpp/format/GMA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <FileStream.h>

#include <sourcepp/crypto/CRC32.h>
#include <sourcepp/fs/FS.h>
#include <sourcepp/string/String.h>
#include <sourcepp/FS.h>
#include <sourcepp/String.h>

using namespace sourcepp;
using namespace vpkpp;
Expand Down
Loading