#include "Achievements.h" #include "Util.h" #include "steam/steam_api.h" #include #include namespace Achievements { void startSteam() { std::map settings{ { "SendAchievementsToLE", false }, { "TestMode", false }, { "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(); } }