Subversion Repositories eduke32

Rev

Rev 8096 | Rev 8122 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8094 hendricks2 1
// Voidwrap - A Steam API wrapper.
2
 
3
#define VOIDWRAP_ISEXPORTING
4
#include "voidwrap_steam.h"
5
#include "steam_api.h"
6
#include "compat.h"
7
 
8
static CSteamID SteamID;
9
static uint64_t SteamID64;
10
static uint64_t AppID;
11
 
12
 
8096 hendricks2 13
static VW_VOID_CONSTCHARPTR Callback_PrintDebug;
8094 hendricks2 14
 
15
static void PrintDebug(const char * fmt, ...)
16
{
17
    if (Callback_PrintDebug == nullptr)
18
    {
19
        return;
20
    }
21
 
22
    static char tmpstr[8192];
23
    va_list va;
24
 
25
    va_start(va, fmt);
26
    Bvsnprintf(tmpstr, sizeof(tmpstr), fmt, va);
27
    va_end(va);
28
 
29
    Callback_PrintDebug(tmpstr);
30
}
31
 
32
static void SteamAPIWarningMessageHook(int nSeverity, const char * pchDebugText)
33
{
34
    if (Callback_PrintDebug == nullptr)
35
    {
36
        return;
37
    }
38
 
39
    Callback_PrintDebug(pchDebugText);
40
}
41
 
42
#ifdef VWDEBUG
8096 hendricks2 43
VOIDWRAP_API void Voidwrap_Steam_SetCallback_PrintDebug(VW_VOID_CONSTCHARPTR function)
8094 hendricks2 44
{
45
    Callback_PrintDebug = function;
46
}
47
#endif
48
 
49
 
8096 hendricks2 50
class SteamStatsAndAchievementsHandler
51
{
52
public:
53
    SteamStatsAndAchievementsHandler()
54
        : m_pSteamUserStats{SteamUserStats()}
55
    { }
56
 
57
    void SetAchievement(char const * id);
58
    void SetStat(char const * id, int32_t value);
8112 terminx 59
    void ResetStats();
8096 hendricks2 60
 
61
    STEAM_CALLBACK(SteamStatsAndAchievementsHandler, OnUserStatsReceived, UserStatsReceived_t);
62
    STEAM_CALLBACK(SteamStatsAndAchievementsHandler, OnUserStatsStored, UserStatsStored_t);
63
 
64
    void Process();
65
 
66
private:
67
    ISteamUserStats * m_pSteamUserStats;
68
 
69
    bool m_bRequestedStats{};
70
    bool m_bStatsValid{};
71
 
72
    bool m_bStoreStats{};
73
};
74
 
75
void SteamStatsAndAchievementsHandler::SetAchievement(char const * id)
76
{
77
    if (nullptr == m_pSteamUserStats)
78
        return;
79
 
80
    m_pSteamUserStats->SetAchievement(id);
81
 
82
    m_bStoreStats = true;
83
}
84
 
85
void SteamStatsAndAchievementsHandler::SetStat(char const * id, int32_t value)
86
{
87
    if (nullptr == m_pSteamUserStats)
88
        return;
89
 
90
    m_pSteamUserStats->SetStat(id, value);
91
 
92
    m_bStoreStats = true;
93
}
94
 
8112 terminx 95
void SteamStatsAndAchievementsHandler::ResetStats()
96
{
97
    if (nullptr == m_pSteamUserStats)
98
        return;
99
 
100
    m_pSteamUserStats->ResetAllStats(true);
101
 
102
    m_bStoreStats = true;
103
}
104
 
8096 hendricks2 105
void SteamStatsAndAchievementsHandler::OnUserStatsReceived(UserStatsReceived_t * pCallback)
106
{
107
    if (nullptr == m_pSteamUserStats)
108
        return;
109
 
110
    if (SteamID64 != pCallback->m_nGameID)
111
        return;
112
 
113
    if (k_EResultOK == pCallback->m_eResult)
114
    {
115
        m_bStatsValid = true;
116
    }
117
    else
118
    {
119
        PrintDebug("RequestStats - failed, %d\n", pCallback->m_eResult);
120
    }
121
}
122
 
123
void SteamStatsAndAchievementsHandler::OnUserStatsStored(UserStatsStored_t * pCallback)
124
{
125
    if (SteamID64 != pCallback->m_nGameID)
126
        return;
127
 
128
    if (k_EResultOK == pCallback->m_eResult)
129
    {
130
    }
131
    else if (k_EResultInvalidParam == pCallback->m_eResult)
132
    {
133
        PrintDebug("StoreStats - some failed to validate\n");
134
    }
135
    else
136
    {
137
        PrintDebug("StoreStats - failed, %d\n", pCallback->m_eResult);
138
    }
139
}
140
 
141
void SteamStatsAndAchievementsHandler::Process()
142
{
143
    if (!m_bRequestedStats)
144
    {
145
        if (nullptr == m_pSteamUserStats)
146
        {
147
            m_bRequestedStats = true;
148
            return;
149
        }
150
 
151
        m_bRequestedStats = m_pSteamUserStats->RequestCurrentStats();
152
    }
153
 
154
    if (!m_bStatsValid)
155
        return;
156
 
157
    if (!m_bStoreStats)
158
        return;
159
 
160
    m_bStoreStats = !m_pSteamUserStats->StoreStats();
161
}
162
 
163
static SteamStatsAndAchievementsHandler * StatsAndAchievementsHandler;
164
 
165
VOIDWRAP_API void Voidwrap_Steam_UnlockAchievement(char const * id)
166
{
167
    if (nullptr == StatsAndAchievementsHandler)
168
        return;
169
 
170
    StatsAndAchievementsHandler->SetAchievement(id);
171
}
172
 
173
VOIDWRAP_API void Voidwrap_Steam_SetStat(char const * id, int32_t value)
174
{
175
    if (nullptr == StatsAndAchievementsHandler)
176
        return;
177
 
178
    StatsAndAchievementsHandler->SetStat(id, value);
179
}
180
 
8112 terminx 181
VOIDWRAP_API void Voidwrap_Steam_ResetStats()
182
{
183
    if (nullptr == StatsAndAchievementsHandler)
184
        return;
8096 hendricks2 185
 
8112 terminx 186
    StatsAndAchievementsHandler->ResetStats();
187
}
188
 
8094 hendricks2 189
#ifdef VWSCREENSHOT
190
class SteamScreenshotHandler
191
{
192
private:
193
    STEAM_CALLBACK(SteamScreenshotHandler, screenshotRequested, ScreenshotRequested_t);
194
    STEAM_CALLBACK(SteamScreenshotHandler, screenshotReady, ScreenshotReady_t);
195
};
196
 
197
static SteamScreenshotHandler * ScreenHandler;
8096 hendricks2 198
static VW_VOID Callback_ScreenshotRequested;
199
// static VW_VOID_INT32 Callback_ScreenshotReady;
8094 hendricks2 200
 
8096 hendricks2 201
VOIDWRAP_API void Voidwrap_Steam_SetCallback_ScreenshotRequested(VW_VOID function)
8094 hendricks2 202
{
203
    Callback_ScreenshotRequested = function;
204
}
205
 
206
#if 0
8096 hendricks2 207
VOIDWRAP_API void Voidwrap_Steam_SetCallback_ScreenshotReady(VW_VOID_INT32 function)
8094 hendricks2 208
{
209
    Callback_ScreenshotReady = function;
210
}
211
#endif
212
 
213
VOIDWRAP_API bool Voidwrap_Steam_SendScreenshot(char * filepath, int32_t width, int32_t height)
214
{
215
    if (INVALID_SCREENSHOT_HANDLE == SteamScreenshots()->AddScreenshotToLibrary(filepath, nullptr, width, height))
216
    {
217
        return false;
218
    }
219
 
220
    return true;
221
}
222
 
223
void SteamScreenshotHandler::screenshotRequested(ScreenshotRequested_t * pCallback)
224
{
225
    if (Callback_ScreenshotRequested != nullptr)
226
    {
227
        PrintDebug("SteamScreenshotHandler::screenshotRequested executed.");
228
        Callback_ScreenshotRequested();
229
    }
230
}
231
 
232
void SteamScreenshotHandler::screenshotReady(ScreenshotReady_t * pCallback)
233
{
234
    PrintDebug("SteamScreenshotHandler::screenshotReady executed. Result: %d", pCallback->m_eResult);
235
 
236
#if 0
237
    if (Callback_ScreenshotReady != nullptr)
238
    {
239
        Callback_ScreenshotReady(pCallback->m_eResult);
240
    }
241
#endif
242
}
243
#endif
244
 
245
 
246
#ifdef VWCONTROLLER
247
static int32_t NumControllerHandles;
248
static ControllerHandle_t * ControllerHandles;
249
 
250
VOIDWRAP_API int32_t Voidwrap_Steam_GetConnectedControllers()
251
{
252
    SteamController()->RunFrame(); // poll for any queued controller events
253
    NumControllerHandles = SteamController()->GetConnectedControllers(ControllerHandles);
254
    return NumControllerHandles;
255
}
256
#endif
257
 
258
 
259
VOIDWRAP_API bool Voidwrap_Steam_Init()
260
{
261
    if (!SteamAPI_Init())
262
    {
263
        SteamAPI_Shutdown();
264
        return false;
265
    }
266
 
267
    SteamUtils()->SetWarningMessageHook(&SteamAPIWarningMessageHook);
268
 
269
    SteamID = SteamUser()->GetSteamID();
270
    SteamID64 = SteamID.ConvertToUint64();
271
    AppID = SteamUtils()->GetAppID();
272
 
273
#if 0
274
    PrintDebug("AppID is %llu", AppID);
275
    if (SteamUtils()->IsOverlayEnabled()) { PrintDebug("Overlay is enabled."); }
276
#endif
277
 
8096 hendricks2 278
    StatsAndAchievementsHandler = new SteamStatsAndAchievementsHandler{};
279
 
8094 hendricks2 280
#ifdef VWSCREENSHOT
281
    SteamScreenshots()->HookScreenshots(true);
282
    if (SteamScreenshots()->IsScreenshotsHooked()) { PrintDebug("Screenshots hooked."); }
283
    ScreenHandler = new SteamScreenshotHandler();
284
#endif
285
 
286
#ifdef VWCONTROLLER
287
    if (SteamController()->Init()) { PrintDebug("Controller API init succeeded."); }
288
    ControllerHandles = new ControllerHandle_t[STEAM_CONTROLLER_MAX_COUNT];
289
#endif
290
 
291
    SteamAPI_RunCallbacks();
292
 
293
    return true;
294
}
295
 
296
VOIDWRAP_API void Voidwrap_Steam_Shutdown()
297
{
298
    SteamAPI_Shutdown();
299
}
300
 
301
VOIDWRAP_API void Voidwrap_Steam_RunCallbacks()
302
{
8096 hendricks2 303
    StatsAndAchievementsHandler->Process();
304
 
8094 hendricks2 305
    SteamAPI_RunCallbacks();
306
}