101 lines
3.1 KiB
C++
101 lines
3.1 KiB
C++
#include "Achievements.h"
|
|
#include "Util.h"
|
|
#include "steam/steam_api.h"
|
|
#include <string>
|
|
#include <processenv.h>
|
|
|
|
namespace Achievements {
|
|
|
|
void startSteam()
|
|
{
|
|
std::map<std::string, bool> settings{
|
|
{ "SendAchievementsToLE", false },
|
|
{ "TestMode", true },
|
|
{ "ShowWarningOnInitFail", true }
|
|
};
|
|
LoadINI(&settings, "Data/SKSE/Plugins/EnderalSteam.ini");
|
|
|
|
AchievementsEnabled(!settings.at("TestMode"));
|
|
|
|
if (settings.at("TestMode")) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (SteamInstance() == nullptr) {
|
|
SteamAPI_Shutdown();
|
|
|
|
if (settings.at("SendAchievementsToLE")) {
|
|
SetEnvironmentVariable(L"SteamAppID", L"933480");
|
|
SetEnvironmentVariable(L"SteamGameId", L"933480");
|
|
} else {
|
|
SetEnvironmentVariable(L"SteamAppID", L"976620");
|
|
SetEnvironmentVariable(L"SteamGameId", L"976620");
|
|
}
|
|
|
|
bool success = SteamAPI_Init();
|
|
|
|
if (success) {
|
|
logger::info("Steam api init was successfull");
|
|
} else {
|
|
logger::error("Error while initializing the steam api");
|
|
if (settings.at("ShowWarningOnInitFail")) {
|
|
RE::DebugMessageBox("Unable to initialize Steam achievements. Try to restart the game and the Steam client. This warning can be disabled in SKSE\\Plugins\\EnderalSteam.ini.");
|
|
}
|
|
}
|
|
|
|
SteamInstance(new AchievementHolder());
|
|
}
|
|
else {
|
|
logger::info("Already initialized steam api, skipping it");
|
|
}
|
|
}
|
|
catch (const std::exception& ex) {
|
|
std::string msg = "Exception while initializing the Steam API, steam achievements will not be available: " + std::string(ex.what());
|
|
logger::error(msg.c_str());
|
|
if (settings.at("ShowWarningOnInitFail")) {
|
|
RE::DebugMessageBox("Unable to initialize Steam achievements. Try to restart the game and the Steam client. This warning can be disabled in SKSE\\Plugins\\EnderalSteam.ini.");
|
|
}
|
|
}
|
|
}
|
|
|
|
AchievementHolder::AchievementHolder() : stats(SteamUserStats()), callback(this, &AchievementHolder::onUserStatsReceived)
|
|
{
|
|
}
|
|
|
|
void AchievementHolder::onUserStatsReceived(UserStatsReceived_t * event) {
|
|
try {
|
|
std::string msg = "User id: " + std::to_string(event->m_steamIDUser.ConvertToUint64()) + ", game id: " + std::to_string(event->m_nGameID) + ", success state: " + std::to_string(event->m_eResult);
|
|
logger::info(msg.c_str());
|
|
|
|
uint32 achievementCount = this->stats->GetNumAchievements();
|
|
msg = "There are " + std::to_string(achievementCount) + " achievements";
|
|
logger::info(msg.c_str());
|
|
}
|
|
catch (const std::exception& ex) {
|
|
std::string msg = "Exception during steam callback: onUserStatsReceived. Failed to print data: " + std::string(ex.what());
|
|
logger::info(msg.c_str());
|
|
}
|
|
}
|
|
|
|
bool AchievementHolder::setAchievementUnlocked(const char * achievementName)
|
|
{
|
|
std::string msg = "Unlocking achievement: " + std::string(achievementName);
|
|
logger::info(msg.c_str());
|
|
bool success = this->stats->SetAchievement(achievementName);
|
|
if (!success) {
|
|
logger::error("Error while unlocking achievement");
|
|
return false;
|
|
}
|
|
success = this->stats->StoreStats();
|
|
if (!success) {
|
|
logger::error("Error while storing unlocked achievement");
|
|
}
|
|
return success;
|
|
}
|
|
|
|
void AchievementHolder::start()
|
|
{
|
|
this->stats->RequestCurrentStats();
|
|
}
|
|
} |