From 9e33b2275ecec8711fe7882dd451ea3f51632773 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 4 May 2025 13:23:59 -0400 Subject: [PATCH] Refactor project, use HTTP for IPC Now Astra can actually fetch Steam tickets from the Steamworks API! --- CMakeLists.txt | 4 ++- main.cpp | 74 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c20cb59..70a7684 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,13 +3,15 @@ project(steamwrap) set(CMAKE_CXX_STANDARD 17) +find_package(Qt6 REQUIRED HttpServer Network Core) + add_library(Steamworks IMPORTED SHARED) set_target_properties(Steamworks PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${STEAMWORKS_INCLUDE_DIR} IMPORTED_LOCATION ${STEAMWORKS_LIBRARIES}) add_executable(steamwrap main.cpp) -target_link_libraries(steamwrap PRIVATE Steamworks) +target_link_libraries(steamwrap PRIVATE Steamworks Qt6::HttpServer Qt6::Network Qt6::Core) set_target_properties(steamwrap PROPERTIES BUILD_RPATH "$ORIGIN" diff --git a/main.cpp b/main.cpp index 80fa9b5..408aa12 100644 --- a/main.cpp +++ b/main.cpp @@ -1,36 +1,64 @@ +#include +#include +#include +#include #include #include -#include +#include int main(int argc, char *argv[]) { - putenv("SteamAppId=39210"); - putenv("SteamGameId=39210"); + QCoreApplication app(argc, argv); - std::cout << "Previous LD_LIBRARY_PATH:" << getenv("LD_LIBRARY_PATH") << std::endl; + QHttpServer server; - std::string newEnv = std::string("PREVIOUS_LD_LIBRARY_PATH=") + getenv("LD_LIBRARY_PATH"); + server.route("/init", QHttpServerRequest::Method::Post, [] (const QHttpServerRequest &request) { + const bool freeTrial = request.query().queryItemValue(QStringLiteral("ft")) == QStringLiteral("1"); - putenv(const_cast(newEnv.c_str())); + if (freeTrial) { + putenv("SteamAppId=312060"); + putenv("SteamGameId=312060"); + } else { + putenv("SteamAppId=39210"); + putenv("SteamGameId=39210"); + } - putenv("LD_LIBRARY_PATH="); + if (!SteamAPI_Init()) { + qFatal() << "Failed to initialize Steam API!"; + } - if (!SteamAPI_Init()) { - throw std::runtime_error("Failed to initialize steam api!"); + qInfo() << "Initialized Steam API..."; + + return ""; + }); + server.route("/shutdown", QHttpServerRequest::Method::Post, [] () { + SteamAPI_Shutdown(); + + qInfo() << "Shutting down Steam API!"; + + return ""; + }); + server.route("/ticket", QHttpServerRequest::Method::Get, [] () { + auto user = SteamAPI_GetHSteamUser(); + + const int bufsize = 1024; + char* buf = new char[bufsize]; + + SteamNetworkingIdentity snid; + snid.SetSteamID(SteamUser()->GetSteamID()); + + unsigned int length = 0; + HAuthTicket ret = SteamUser()->GetAuthSessionTicket(buf, bufsize, &length, &snid); + + return QString::fromUtf8(buf, length); + }); + + auto tcpserver = new QTcpServer(); + if (!tcpserver->listen(QHostAddress::Any, 50481) || !server.bind(tcpserver)) { + delete tcpserver; + return -1; } - std::cout << "Initialized Steam API..." << std::endl; + qInfo() << "Listening on port" << tcpserver->serverPort(); - std::string args; - for (int i = 1; i < argc; i++) { - args += std::string(argv[i]) + " "; - } - - std::cout << args.c_str() << std::endl; - system(args.c_str()); - - SteamAPI_Shutdown(); - - std::cout << "Shutdown!" << std::endl; - - return 0; + return QCoreApplication::exec(); }