Skip to content

Commit

Permalink
Code refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
monkey0506 committed Oct 15, 2017
1 parent 5c5f9bf commit d140cc3
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 66 deletions.
127 changes: 79 additions & 48 deletions AGSteamPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,96 @@
//
// NOTICE: This file contains references to the Steamworks API. See the included
// LICENSE file for details and restrictions on using this file.
#include <string>
#include "ags2client/agsplugin.h"
#include "AGSteamPlugin.h"
#include "steam/steam_api.h"
using namespace AGSteam::Plugin;

static bool SteamInitialized = false;
static bool SteamOverlayActive = false;

struct GameOverlayActivatedListener
namespace AGSteam
{
public:
static GameOverlayActivatedListener& GetListener() noexcept
namespace Plugin
{
static GameOverlayActivatedListener listener;
return listener;
template<typename Listener, typename Event>
struct SteamEventListener
{
protected:
CCallback<Listener, Event> callback;

virtual void OnEventReceived(Event *event) = 0;

SteamEventListener() : callback(const_cast<Listener*>(static_cast<Listener const* const>(this)), &SteamEventListener::OnEventReceived)
{
}

public:
virtual ~SteamEventListener() = default;
};

struct UserStatsReceivedListener : public SteamEventListener<UserStatsReceivedListener, UserStatsReceived_t>
{
private:
void OnEventReceived(UserStatsReceived_t *event) override;
};

struct GameOverlayActivatedListener : public SteamEventListener<GameOverlayActivatedListener, GameOverlayActivated_t>
{
private:
void OnEventReceived(GameOverlayActivated_t *event) override;
};

struct AGSteamPlugin_Statics
{
public:
static std::string FIND_LEADERBOARD;
static GameOverlayActivatedListener GAME_OVERLAY_ACTIVATED_LISTENER;
static bool GAME_OVERLAY_ACTIVE;
static bool INITIALIZED;
static AGSteamPlugin PLUGIN;
static UserStatsReceivedListener USER_STATS_RECEIVED_LISTENER;
};
}
}

STEAM_CALLBACK(GameOverlayActivatedListener, OnGameOverlayActivated, GameOverlayActivated_t, CallbackGameOverlayActivated);
std::string AGSteamPlugin_Statics::FIND_LEADERBOARD;
GameOverlayActivatedListener AGSteamPlugin_Statics::GAME_OVERLAY_ACTIVATED_LISTENER;
bool AGSteamPlugin_Statics::GAME_OVERLAY_ACTIVE = false;
bool AGSteamPlugin_Statics::INITIALIZED = false;
AGSteamPlugin AGSteamPlugin_Statics::PLUGIN;
UserStatsReceivedListener AGSteamPlugin_Statics::USER_STATS_RECEIVED_LISTENER;

private:
GameOverlayActivatedListener() noexcept : CallbackGameOverlayActivated(this, &GameOverlayActivatedListener::OnGameOverlayActivated)
void UserStatsReceivedListener::OnEventReceived(UserStatsReceived_t *event)
{
if (event->m_nGameID != SteamUtils()->GetAppID())
{
return;
}
if (event->m_eResult == k_EResultOK)
{
AGSteamPlugin_Statics::INITIALIZED = true; // a callback has been received, stat+achievement info is now accessible
}
};

void GameOverlayActivatedListener::OnGameOverlayActivated(GameOverlayActivated_t *pCallback)
{
SteamOverlayActive = pCallback->m_bActive;
}

struct UserStatsReceivedListener
void GameOverlayActivatedListener::OnEventReceived(GameOverlayActivated_t *event)
{
public:
static UserStatsReceivedListener& GetListener() noexcept
{
static UserStatsReceivedListener listener;
return listener;
}

STEAM_CALLBACK(UserStatsReceivedListener, OnUserStatsReceived, UserStatsReceived_t, CallbackUserStatsReceived);

private:
UserStatsReceivedListener() noexcept : CallbackUserStatsReceived(this, &UserStatsReceivedListener::OnUserStatsReceived)
{
}
};

void UserStatsReceivedListener::OnUserStatsReceived(UserStatsReceived_t *pCallback)
{
if (pCallback->m_nGameID != SteamUtils()->GetAppID()) return;
if (pCallback->m_eResult == k_EResultOK)
{
SteamInitialized = true; // a callback has been received, stat+achievement info is now accessible
}
AGSteamPlugin_Statics::GAME_OVERLAY_ACTIVE = event->m_bActive;
}

AGSteamPlugin& AGSteamPlugin::GetAGSteamPlugin() noexcept
{
static AGSteamPlugin plugin;
return plugin;
return AGSteamPlugin_Statics::PLUGIN;
}

void AGSteamPlugin_Initialize() noexcept
{
if (!SteamInitialized)
if (!AGSteamPlugin_Statics::INITIALIZED)
{
if (!SteamAPI_Init())
{
return;
}
GameOverlayActivatedListener::GetListener(); // ensure that game overlay listener is created
UserStatsReceivedListener::GetListener(); // ensure that user stats listener is created
(void)AGSteamPlugin_Statics::GAME_OVERLAY_ACTIVATED_LISTENER; // ensure that game overlay listener is created
(void)AGSteamPlugin_Statics::USER_STATS_RECEIVED_LISTENER; // ensure that user stats listener is created
SteamUserStats()->RequestCurrentStats();
}
}
Expand All @@ -84,7 +102,7 @@ bool AGSteamPlugin::IsInitialized() const noexcept
{
// helper method for the plugin to ensure that the call to Steam is placed before any other Steam functions
// this function also serves for the AGS property
return SteamInitialized;
return AGSteamPlugin_Statics::INITIALIZED;
}

void AGSteamPlugin::ResetStatsAndAchievements() const noexcept
Expand Down Expand Up @@ -120,7 +138,7 @@ void AGSteamPlugin::Update() const noexcept

float AGSteamPlugin::GetVersion() const noexcept
{
return 3.2f;
return 3.3f;
}

char const* AGSteamPlugin::GetAGSPluginName() const noexcept
Expand All @@ -133,7 +151,20 @@ char const* AGSteamPlugin::GetAGSPluginDesc() const noexcept
return "AGSteam: Steam API Plugin for AGS (C) 2011-2017 MonkeyMoto Productions, Inc.";
}

bool AGSteamPlugin::ClaimKeyPress(int data, int(*IsKeyPressed)(int)) const noexcept
bool AGSteamPlugin::ClaimKeyPress(int data, int (*IsKeyPressed)(int)) const noexcept
{
return SteamOverlayActive;
return AGSteamPlugin_Statics::GAME_OVERLAY_ACTIVE;
}

extern "C" void SteamLeaderboards_FindLeaderboard(char const *leaderboardName);

void AGSteamPlugin::RegisterScriptFunctions(IAGSEngine *engine) const noexcept
{
std::string &findLeaderboard = AGSteamPlugin_Statics::FIND_LEADERBOARD;
if (findLeaderboard.empty())
{
findLeaderboard = std::string(GetClientNameForScript()) + "::FindLeaderboard^1";
}
IAGS2Client::RegisterScriptFunctions(engine);
engine->RegisterScriptFunction(findLeaderboard.c_str(), reinterpret_cast<void*>(SteamLeaderboards_FindLeaderboard));
}
5 changes: 4 additions & 1 deletion AGSteamPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ namespace AGSteam
{
namespace Plugin
{
struct AGSteamPlugin_Statics;

class AGSteamPlugin : public AGS2Client::IAGS2Client
{
protected:
friend AGSteamPlugin_Statics;

DEFAULT_CTOR(AGSteamPlugin);

public:
Expand All @@ -32,8 +35,8 @@ namespace AGSteam
char const* GetAGSPluginDesc() const noexcept override;
float GetVersion() const noexcept override;
bool ClaimKeyPress(int data, int(*IsKeyPressed)(int)) const noexcept override;
void RegisterScriptFunctions(IAGSEngine *engine) const noexcept override;
};

} // namespace Plugin
} // namespace AGSteam

Expand Down
8 changes: 4 additions & 4 deletions Solutions/VS2017/AGSteam.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release-disjoint|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug-disjoint|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v141_xp</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
17 changes: 15 additions & 2 deletions SteamAchievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@
#include "steam/steam_api.h"
using namespace AGSteam::Plugin;

namespace AGSteam
{
namespace Plugin
{
struct SteamAchievements_Statics
{
public:
static SteamAchievements ACHIEVEMENTS;
};
}
}

SteamAchievements SteamAchievements_Statics::ACHIEVEMENTS;

SteamAchievements& SteamAchievements::GetSteamAchievements() noexcept
{
static SteamAchievements achievements;
return achievements;
return SteamAchievements_Statics::ACHIEVEMENTS;
}

bool SteamAchievements::ResetAchievement(char const *ID) const noexcept
Expand Down
4 changes: 4 additions & 0 deletions SteamAchievements.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ namespace AGSteam
{
namespace Plugin
{
struct SteamAchievements_Statics;

class SteamAchievements : public AGS2Client::IClientAchievements
{
protected:
friend SteamAchievements_Statics;

DEFAULT_CTOR(SteamAchievements);

public:
Expand Down
38 changes: 31 additions & 7 deletions SteamLeaderboards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
#include "ags2client/IAGS2Client.h"
#include "SteamLeaderboards.h"
#include "steam/steam_api.h"
#include "ags2client/Cpp11Fix.h"
using namespace AGSteam::Plugin;

struct LeaderboardListener
{
private:
friend SteamLeaderboards_Statics;

#ifdef AGS2CLIENT_HAS_CPP11
LeaderboardListener() = default;
#else
LeaderboardListener() {};
#endif // AGS2CLIENT_HAS_CPP11

public:
static LeaderboardListener& GetLeaderboardListener() noexcept
{
static LeaderboardListener listener;
return listener;
}
static LeaderboardListener& GetLeaderboardListener() noexcept;

#define LEADERBOARD_LISTENER_CALLRESULT(thisclass, func, param, var) CCallResult<thisclass, param> var; void func(param *pParam, bool bIOFailure)
LEADERBOARD_LISTENER_CALLRESULT(LeaderboardListener, OnFindLeaderboard, LeaderboardFindResult_t, CallResultFindLeaderboard);
Expand All @@ -34,6 +33,27 @@ struct LeaderboardListener
#undef LEADERBOARD_LISTENER_CALLRESULT
};

namespace AGSteam
{
namespace Plugin
{
struct SteamLeaderboards_Statics
{
public:
static LeaderboardListener LISTENER;
static SteamLeaderboards LEADERBOARDS;
};
}
}

LeaderboardListener SteamLeaderboards_Statics::LISTENER;
SteamLeaderboards SteamLeaderboards_Statics::LEADERBOARDS;

LeaderboardListener& LeaderboardListener::GetLeaderboardListener() noexcept
{
return SteamLeaderboards_Statics::LISTENER;
}

static struct
{
bool HasLeaderboard;
Expand All @@ -45,8 +65,7 @@ static struct

SteamLeaderboards& SteamLeaderboards::GetSteamLeaderboards() noexcept
{
static SteamLeaderboards leaderboards;
return leaderboards;
return SteamLeaderboards_Statics::LEADERBOARDS;
}

void SteamLeaderboards::RequestLeaderboard(char const *leaderboardName, AGS2Client::LeaderboardScore::Type type, int limit) const noexcept
Expand All @@ -60,6 +79,11 @@ void SteamLeaderboards::RequestLeaderboard(char const *leaderboardName, AGS2Clie
listener.CallResultFindLeaderboard.Set(SteamUserStats()->FindLeaderboard(leaderboardName), &listener, &LeaderboardListener::OnFindLeaderboard);
}

extern "C" void SteamLeaderboards_FindLeaderboard(char const *leaderboardName)
{
SteamLeaderboards::GetSteamLeaderboards().RequestLeaderboard(nullptr, AGS2Client::LeaderboardScore::AroundUser, 10);
}

void LeaderboardListener::OnUploadScore(LeaderboardScoreUploaded_t *callback, bool IOFailure)
{
}
Expand Down
4 changes: 3 additions & 1 deletion SteamLeaderboards.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ namespace AGSteam
{
namespace Plugin
{
struct SteamLeaderboards_Statics;

class SteamLeaderboards : public AGS2Client::IClientLeaderboards
{
private:
friend SteamLeaderboards_Statics;

DEFAULT_CTOR(SteamLeaderboards);

public:
Expand All @@ -28,7 +31,6 @@ namespace AGSteam
int GetLeaderScore(int index) const noexcept override;
int GetLeaderCount() const noexcept override;
};

} // namespace Plugin
} // namespace AGSteam

Expand Down
17 changes: 15 additions & 2 deletions SteamStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@
#include "steam/steam_api.h"
using namespace AGSteam::Plugin;

namespace AGSteam
{
namespace Plugin
{
struct SteamStats_Statics
{
public:
static SteamStats STATS;
};
}
}

SteamStats SteamStats_Statics::STATS;

SteamStats& SteamStats::GetSteamStats() noexcept
{
static SteamStats stats;
return stats;
return SteamStats_Statics::STATS;
}

int SteamStats::GetIntStat(char const *name) const noexcept
Expand Down
Loading

0 comments on commit d140cc3

Please sign in to comment.