From f4ed77d21f7c5ebe945dcea8b205de070c7cf78f Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 4 May 2025 16:38:49 -0400 Subject: [PATCH] Only keep the Steam API initialized once --- main.cpp | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 408aa12..5090e3d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,25 @@ #include #include #include +#include +#include #include #include #include #include +static bool isInitialized = false; + int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QHttpServer server; server.route("/init", QHttpServerRequest::Method::Post, [] (const QHttpServerRequest &request) { + if (isInitialized) { + return ""; + } + const bool freeTrial = request.query().queryItemValue(QStringLiteral("ft")) == QStringLiteral("1"); if (freeTrial) { @@ -28,9 +36,15 @@ int main(int argc, char *argv[]) { qInfo() << "Initialized Steam API..."; + isInitialized = true; + return ""; }); server.route("/shutdown", QHttpServerRequest::Method::Post, [] () { + if (!isInitialized) { + return ""; + } + SteamAPI_Shutdown(); qInfo() << "Shutting down Steam API!"; @@ -38,18 +52,21 @@ int main(int argc, char *argv[]) { return ""; }); server.route("/ticket", QHttpServerRequest::Method::Get, [] () { - auto user = SteamAPI_GetHSteamUser(); + auto user = SteamAPI_GetHSteamUser(); - const int bufsize = 1024; - char* buf = new char[bufsize]; + 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, nullptr); - unsigned int length = 0; - HAuthTicket ret = SteamUser()->GetAuthSessionTicket(buf, bufsize, &length, &snid); + QJsonObject response + { + {QStringLiteral("ticket"), QString::fromLatin1(QByteArray::fromRawData(buf, length).toHex())}, + {QStringLiteral("time"), QString::number(SteamUtils()->GetServerRealTime())} + }; - return QString::fromUtf8(buf, length); + return QJsonDocument(response).toJson(QJsonDocument::Compact); }); auto tcpserver = new QTcpServer();