1
Fork 0

Port to httplib, update build instructions

This commit is contained in:
Joshua Goins 2025-05-04 17:40:04 -04:00
parent d0242bd943
commit 619e64159c
4 changed files with 10582 additions and 78 deletions

View file

@ -3,15 +3,13 @@ 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 Qt6::HttpServer Qt6::Network Qt6::Core)
target_link_libraries(steamwrap PRIVATE Steamworks pthread)
set_target_properties(steamwrap
PROPERTIES
BUILD_RPATH "$ORIGIN"

View file

@ -9,8 +9,8 @@ Due to restrictions on how the Steamworks SDK is distributed, I have to build it
Remember that newer glibc/++ won't work when it's inside the Steam Linux Runtime, so I recommend using distrobox to build steamwrap:
```shell
$ distrobox create --name test --init --image debian:10 --additional-packages "systemd libpam-systemd" # Debian 10 is the base of Steam Linux Runtime 2.0
$ distrobox enter test
$ distrobox create --name steamwrap --init --image debian:10 --additional-packages "systemd libpam-systemd cmake build-essential" # Debian 10 is the base of Steam Linux Runtime 2.0
$ distrobox enter steamwrap
$ mkdir build
$ cd build
$ cmake -DSTEAMWORKS_LIBRARIES=<location of steam sdk>/redistributable_bin/linux64/libsteam_api.so -DSTEAMWORKS_INCLUDE_DIR=<location of steam sdk>/public/ ..

10518
httplib.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,26 +1,28 @@
#include <QHttpServer>
#include <QTcpServer>
#include <QCoreApplication>
#include <QJsonObject>
#include <QJsonDocument>
#include <cstdlib>
#include <iostream>
#include <steam/steam_api.h>
#include <steam/steamclientpublic.h>
#include "httplib.h"
static bool isInitialized = false;
// from https://stackoverflow.com/a/53966346
void btox(char *xp, const char *bb, int n)
{
const char xx[]= "0123456789ABCDEF";
while (--n >= 0) xp[n] = xx[(bb[n>>1] >> ((1 - (n&1)) << 2)) & 0xF];
}
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
httplib::Server server;
QHttpServer server;
server.route("/init", QHttpServerRequest::Method::Post, [] (const QHttpServerRequest &request) {
server.Post("/init", [] (const httplib::Request &req, httplib::Response &) {
if (isInitialized) {
return "";
return;
}
const bool freeTrial = request.query().queryItemValue(QStringLiteral("ft")) == QStringLiteral("1");
const bool freeTrial = req.get_param_value_count("ft") == 1;
if (freeTrial) {
putenv("SteamAppId=312060");
@ -31,27 +33,21 @@ int main(int argc, char *argv[]) {
}
if (!SteamAPI_Init()) {
qFatal() << "Failed to initialize Steam API!";
std::cerr << "Failed to initialize Steam API!" << std::endl;
}
qInfo() << "Initialized Steam API...";
std::cout << "Initialized Steam API..." << std::endl;
isInitialized = true;
return "";
});
server.route("/shutdown", QHttpServerRequest::Method::Post, [] () {
server.Post("/shutdown", [] (const httplib::Request &, httplib::Response &) {
if (!isInitialized) {
return "";
return;
}
SteamAPI_Shutdown();
qInfo() << "Shutting down Steam API!";
return "";
});
server.route("/ticket", QHttpServerRequest::Method::Get, [] () {
server.Get("/ticket", [] (const httplib::Request &, httplib::Response &res) {
auto user = SteamAPI_GetHSteamUser();
const int bufsize = 1024;
@ -60,22 +56,14 @@ int main(int argc, char *argv[]) {
unsigned int length = 0;
HAuthTicket ret = SteamUser()->GetAuthSessionTicket(buf, bufsize, &length, nullptr);
QJsonObject response
{
{QStringLiteral("ticket"), QString::fromLatin1(QByteArray::fromRawData(buf, length).toHex())},
{QStringLiteral("time"), static_cast<qint64>(SteamUtils()->GetServerRealTime())}
};
char hexstr[length * 2 + 1];
btox(hexstr, buf, length * 2);
hexstr[length * 2] = 0;
return QJsonDocument(response).toJson(QJsonDocument::Compact);
res.set_content("{\"ticket\":\"" + std::string(hexstr) + "\",\"time\":" + std::to_string(SteamUtils()->GetServerRealTime()) + "}", "application/json");
});
auto tcpserver = new QTcpServer();
if (!tcpserver->listen(QHostAddress::Any, 50481) || !server.bind(tcpserver)) {
delete tcpserver;
return -1;
}
server.listen("0.0.0.0", 50481);
qInfo() << "Listening on port" << tcpserver->serverPort();
return QCoreApplication::exec();
return 0;
}