steamunlock/main.cpp
2025-02-26 20:29:11 -05:00

137 lines
3.3 KiB
C++

#include <thread>
#include <chrono>
#include <cstring>
#include <cstdio>
#include "sdk/public/steam/steam_api.h"
class StatsCallback {
public:
STEAM_CALLBACK(StatsCallback, onrecv, UserStatsReceived_t);
STEAM_CALLBACK(StatsCallback, onsend, UserStatsStored_t);
bool achlist;
bool store_complete;
bool unlockall;
char *achid;
};
void StatsCallback::onrecv(UserStatsReceived_t *stats) {
if (achlist) {
for (uint32 i = 0; i < SteamUserStats()->GetNumAchievements(); ++i) {
bool unlocked = false;
const char *id = SteamUserStats()->GetAchievementName(i);
const char *desc = SteamUserStats()->GetAchievementDisplayAttribute(id, "desc");
SteamUserStats()->GetAchievement(id, &unlocked);
if (!unlocked) {
printf("%s: %s\n", id, desc);
} else {
printf("[UNLOCKED] -> %s: %s\n", id, desc);
}
}
}
if (achid != nullptr) {
if (unlockall) {
for (uint32 i = 0; i < SteamUserStats()->GetNumAchievements(); ++i) {
const char *name = SteamUserStats()->GetAchievementName(i);
printf("Set achievement: %s\n", name);
SteamUserStats()->SetAchievement(name);
}
} else {
SteamUserStats()->SetAchievement(achid);
printf("Set achievement: %s\n", achid);
}
}
SteamUserStats()->StoreStats();
}
void StatsCallback::onsend(UserStatsStored_t *stats) {
store_complete = true;
}
int write_appid(char *appid) {
FILE *out = fopen("steam_appid.txt", "w");
fwrite(appid, sizeof(char), strlen(appid), out);
fclose(out);
return 0;
}
int usage() {
printf("\
./steamunlock -l [APPID] [ACHIEVEMENT_ID] \n\
\t-l \t list all achievements for provided APPID\n\
\t-h \t this help message\n\
\n\n");
return 0;
}
int main(int argc, char** argv) {
bool achlist = false;
char *appid = nullptr;
char *achid = nullptr;
if (argc <= 1) {
usage();
return 0;
}
for (int i = 1; i < argc; ++i) {
char *cur = argv[i];
if (cur[0] == '-') {
for (int x = 1; x < strlen(cur); ++x) {
switch(cur[x]) {
case 'h':
usage();
return 0;
case 'l':
achlist = true;
break;
}
}
} else {
if (!appid) {
appid = cur;
} else if (!achid) {
achid = cur;
}
}
}
printf("list all: %b\nappid: %s\nachid: %s\n\n", achlist, appid, achid);
write_appid(appid);
SteamAPI_Init();
StatsCallback stats;
stats.achlist = achlist;
stats.store_complete = false;
stats.achid = achid;
stats.unlockall = false;
if (achid != nullptr && strcmp(stats.achid, "all") == 0) {
stats.unlockall = true;
}
SteamUserStats()->RequestCurrentStats();
for (;;) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
SteamAPI_RunCallbacks();
if (stats.store_complete) {
break;
}
}
SteamAPI_Shutdown();
return 0;
}