2022-08-03 19:21:03 +00:00
# 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 } ,
2022-09-20 11:56:38 +00:00
{ " TestMode " , false } ,
2022-08-03 19:21:03 +00:00
{ " 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 ) {
2022-09-20 11:56:38 +00:00
logger : : info ( " {} " , " Steam api init was successfull " ) ;
2022-08-03 19:21:03 +00:00
} else {
2022-09-20 11:56:38 +00:00
logger : : error ( " {} " , " Error while initializing the steam api " ) ;
2022-08-03 19:21:03 +00:00
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 {
2022-09-20 11:56:38 +00:00
logger : : info ( " {} " , " Already initialized steam api, skipping it " ) ;
2022-08-03 19:21:03 +00:00
}
}
catch ( const std : : exception & ex ) {
std : : string msg = " Exception while initializing the Steam API, steam achievements will not be available: " + std : : string ( ex . what ( ) ) ;
2022-09-20 11:56:38 +00:00
logger : : error ( " {} " , msg . c_str ( ) ) ;
2022-08-03 19:21:03 +00:00
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 ) ;
2022-09-20 11:56:38 +00:00
logger : : info ( " {} " , msg . c_str ( ) ) ;
2022-08-03 19:21:03 +00:00
uint32 achievementCount = this - > stats - > GetNumAchievements ( ) ;
msg = " There are " + std : : to_string ( achievementCount ) + " achievements " ;
2022-09-20 11:56:38 +00:00
logger : : info ( " {} " , msg . c_str ( ) ) ;
2022-08-03 19:21:03 +00:00
}
catch ( const std : : exception & ex ) {
std : : string msg = " Exception during steam callback: onUserStatsReceived. Failed to print data: " + std : : string ( ex . what ( ) ) ;
2022-09-20 11:56:38 +00:00
logger : : info ( " {} " , msg . c_str ( ) ) ;
2022-08-03 19:21:03 +00:00
}
}
bool AchievementHolder : : setAchievementUnlocked ( const char * achievementName )
{
std : : string msg = " Unlocking achievement: " + std : : string ( achievementName ) ;
2022-09-20 11:56:38 +00:00
logger : : info ( " {} " , msg . c_str ( ) ) ;
2022-08-03 19:21:03 +00:00
bool success = this - > stats - > SetAchievement ( achievementName ) ;
if ( ! success ) {
2022-09-20 11:56:38 +00:00
logger : : error ( " {} " , " Error while unlocking achievement " ) ;
2022-08-03 19:21:03 +00:00
return false ;
}
success = this - > stats - > StoreStats ( ) ;
if ( ! success ) {
2022-09-20 11:56:38 +00:00
logger : : error ( " {} " , " Error while storing unlocked achievement " ) ;
2022-08-03 19:21:03 +00:00
}
return success ;
}
void AchievementHolder : : start ( )
{
this - > stats - > RequestCurrentStats ( ) ;
}
}