1
Fork 0
steamwrap/main.cpp

71 lines
1.7 KiB
C++
Raw Normal View History

#include <cstdlib>
2023-12-21 20:18:42 -05:00
#include <iostream>
#include <steam/steam_api.h>
#include <steam/steamclientpublic.h>
2023-12-21 20:18:42 -05:00
#include "httplib.h"
2023-12-21 20:18:42 -05:00
static bool isInitialized = false;
2023-12-21 20:18:42 -05:00
// 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];
}
2023-12-21 20:18:42 -05:00
int main(int argc, char *argv[]) {
httplib::Server server;
2023-12-21 20:18:42 -05:00
server.Post("/init", [] (const httplib::Request &req, httplib::Response &) {
if (isInitialized) {
return;
}
2023-12-21 20:18:42 -05:00
2025-05-07 17:20:03 -04:00
const bool freeTrial = req.get_param_value("ft") == "1";
if (freeTrial) {
putenv("SteamAppId=312060");
putenv("SteamGameId=312060");
} else {
putenv("SteamAppId=39210");
putenv("SteamGameId=39210");
}
if (!SteamAPI_Init()) {
std::cerr << "Failed to initialize Steam API!" << std::endl;
return;
}
2023-12-21 20:18:42 -05:00
std::cout << "Initialized Steam API..." << std::endl;
isInitialized = true;
});
server.Post("/shutdown", [] (const httplib::Request &, httplib::Response &) {
if (!isInitialized) {
return;
}
2023-12-21 20:18:42 -05:00
SteamAPI_Shutdown();
});
server.Get("/ticket", [] (const httplib::Request &, httplib::Response &res) {
auto user = SteamAPI_GetHSteamUser();
2023-12-21 20:18:42 -05:00
const int bufsize = 1024;
char* buf = new char[bufsize];
unsigned int length = 0;
HAuthTicket ret = SteamUser()->GetAuthSessionTicket(buf, bufsize, &length, nullptr);
char hexstr[length * 2 + 1];
btox(hexstr, buf, length * 2);
hexstr[length * 2] = 0;
res.set_content("{\"ticket\":\"" + std::string(hexstr) + "\",\"time\":" + std::to_string(SteamUtils()->GetServerRealTime()) + "}", "application/json");
});
2023-12-21 20:18:42 -05:00
server.listen("0.0.0.0", 50481);
2023-12-21 20:18:42 -05:00
return 0;
2023-12-21 20:18:42 -05:00
}