Rise
The Vieneo Province
Loading...
Searching...
No Matches
steam_api_common.h
Go to the documentation of this file.
1//====== Copyright Valve Corporation, All rights reserved. ====================
2//
3// Steamworks SDK minimal include
4//
5// Defines the minimal set of things we need to use any single interface
6// or register for any callback.
7//
8//=============================================================================
9
10#ifndef STEAM_API_COMMON_H
11#define STEAM_API_COMMON_H
12
13#include "steamtypes.h"
14#include "steamclientpublic.h"
15
16// S_API defines the linkage and calling conventions for steam_api.dll exports
17#if defined( _WIN32 ) && !defined( _X360 )
18 #if defined( STEAM_API_EXPORTS )
19 #define S_API extern "C" __declspec( dllexport )
20 #elif defined( STEAM_API_NODLL )
21 #define S_API extern "C"
22 #else
23 #define S_API extern "C" __declspec( dllimport )
24 #endif // STEAM_API_EXPORTS
25#elif defined( __GNUC__ )
26 #if defined( STEAM_API_EXPORTS )
27 #define S_API extern "C" __attribute__ ((visibility("default")))
28 #else
29 #define S_API extern "C"
30 #endif // STEAM_API_EXPORTS
31#else // !WIN32
32 #if defined( STEAM_API_EXPORTS )
33 #define S_API extern "C"
34 #else
35 #define S_API extern "C"
36 #endif // STEAM_API_EXPORTS
37#endif
38
39#if defined( __cplusplus ) && ( __cplusplus >= 201103L )
40 #define S_OVERRIDE override
41#else
42 #define S_OVERRIDE
43#endif
44
45#if ( defined(STEAM_API_EXPORTS) || defined(STEAM_API_NODLL) ) && !defined(API_GEN)
46#define STEAM_PRIVATE_API( ... ) __VA_ARGS__
47#elif defined(STEAM_API_EXPORTS) && defined(API_GEN)
48#define STEAM_PRIVATE_API( ... )
49#else
50#define STEAM_PRIVATE_API( ... ) protected: __VA_ARGS__ public:
51#endif
52
53// handle to a communication pipe to the Steam client
55// handle to single instance of a steam user
57
58// A fixed size buffer to receive an error message that is returned by some API
59// calls.
60const int k_cchMaxSteamErrMsg = 1024;
62
63// #define away __cdecl on posix.
64// This is really, really bad. We're sorry. But it's been this way for
65// a long time now and it's scary to change it, as there may be others that
66// depend on it.
67#ifndef _WIN32
68 #define __cdecl
69#endif
70
71// function prototype
72extern "C" typedef void ( S_CALLTYPE *SteamAPIWarningMessageHook_t )( int, const char * );
73extern "C" typedef uint32 ( S_CALLTYPE *SteamAPI_CheckCallbackRegistered_t )( int iCallbackNum );
74#if defined( __SNC__ )
75 #pragma diag_suppress=1700 // warning 1700: class "%s" has virtual functions but non-virtual destructor
76#endif
77
78//----------------------------------------------------------------------------------------------------------------------------------------------------------//
79// steam callback and call-result helpers
80//
81// The following macros and classes are used to register your application for
82// callbacks and call-results, which are delivered in a predictable manner.
83//
84// STEAM_CALLBACK macros are meant for use inside of a C++ class definition.
85// They map a Steam notification callback directly to a class member function
86// which is automatically prototyped as "void func( callback_type *pParam )".
87//
88// CCallResult is used with specific Steam APIs that return "result handles".
89// The handle can be passed to a CCallResult object's Set function, along with
90// an object pointer and member-function pointer. The member function will
91// be executed once the results of the Steam API call are available.
92//
93// CCallback and CCallbackManual classes can be used instead of STEAM_CALLBACK
94// macros if you require finer control over registration and unregistration.
95//
96// Callbacks and call-results are queued automatically and are only
97// delivered/executed when your application calls SteamAPI_RunCallbacks().
98//
99// Note that there is an alternative, lower level callback dispatch mechanism.
100// See SteamAPI_ManualDispatch_Init
101//----------------------------------------------------------------------------------------------------------------------------------------------------------//
102
103// Dispatch all queued Steamworks callbacks.
104//
105// This is safe to call from multiple threads simultaneously,
106// but if you choose to do this, callback code could be executed on any thread.
107// One alternative is to call SteamAPI_RunCallbacks from the main thread only,
108// and call SteamAPI_ReleaseCurrentThreadMemory regularly on other threads.
110
111// Declares a callback member function plus a helper member variable which
112// registers the callback on object creation and unregisters on destruction.
113// The optional fourth 'var' param exists only for backwards-compatibility
114// and can be ignored.
115#define STEAM_CALLBACK( thisclass, func, .../*callback_type, [deprecated] var*/ ) \
116 _STEAM_CALLBACK_SELECT( ( __VA_ARGS__, 4, 3 ), ( /**/, thisclass, func, __VA_ARGS__ ) )
117
118// Declares a callback function and a named CCallbackManual variable which
119// has Register and Unregister functions instead of automatic registration.
120#define STEAM_CALLBACK_MANUAL( thisclass, func, callback_type, var ) \
121 CCallbackManual< thisclass, callback_type > var; void func( callback_type *pParam )
122
123// Dispatch callbacks relevant to the gameserver client and interfaces.
124// To register for these, you need to use STEAM_GAMESERVER_CALLBACK.
125// (Or call SetGameserverFlag on your CCallbackBase object.)
127
128// Same as STEAM_CALLBACK, but for callbacks on the gameserver interface.
129// These will be dispatched during SteamGameServer_RunCallbacks
130#define STEAM_GAMESERVER_CALLBACK( thisclass, func, /*callback_type, [deprecated] var*/... ) \
131 _STEAM_CALLBACK_SELECT( ( __VA_ARGS__, GS, 3 ), ( this->SetGameserverFlag();, thisclass, func, __VA_ARGS__ ) )
132#define STEAM_GAMESERVER_CALLBACK_MANUAL( thisclass, func, callback_type, var ) \
133 CCallbackManual< thisclass, callback_type, true > var; void func( callback_type *pParam )
134
135//-----------------------------------------------------------------------------
136// Purpose: base for callbacks and call results - internal implementation detail
137//-----------------------------------------------------------------------------
139{
140public:
142 // don't add a virtual destructor because we export this binary interface across dll's
143 virtual void Run( void *pvParam ) = 0;
144 virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall ) = 0;
145 int GetICallback() { return m_iCallback; }
146 virtual int GetCallbackSizeBytes() = 0;
147
148protected:
152 friend class CCallbackMgr;
153
154private:
157};
158
159//-----------------------------------------------------------------------------
160// Purpose: templated base for callbacks - internal implementation detail
161//-----------------------------------------------------------------------------
162template< int sizeof_P >
164{
165public:
168
169protected:
170 friend class CCallbackMgr;
171 virtual void Run( void *pvParam ) S_OVERRIDE = 0;
172 virtual void Run( void *pvParam, bool /*bIOFailure*/, SteamAPICall_t /*hSteamAPICall*/ ) S_OVERRIDE { Run( pvParam ); }
173 virtual int GetCallbackSizeBytes() S_OVERRIDE { return sizeof_P; }
174};
175
176
177//-----------------------------------------------------------------------------
178// Purpose: maps a steam async call result to a class member function
179// template params: T = local class, P = parameter struct
180//-----------------------------------------------------------------------------
181template< class T, class P >
183{
184public:
185 typedef void (T::*func_t)( P*, bool );
186
187 CCallResult();
188 ~CCallResult();
189
190 void Set( SteamAPICall_t hAPICall, T *p, func_t func );
191 bool IsActive() const;
192 void Cancel();
193
195private:
196 virtual void Run( void *pvParam ) S_OVERRIDE;
197 virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall ) S_OVERRIDE;
198 virtual int GetCallbackSizeBytes() S_OVERRIDE { return sizeof( P ); }
199
203};
204
205
206
207//-----------------------------------------------------------------------------
208// Purpose: maps a steam callback to a class member function
209// template params: T = local class, P = parameter struct,
210// bGameserver = listen for gameserver callbacks instead of client callbacks
211//-----------------------------------------------------------------------------
212template< class T, class P, bool bGameserver = false >
213class CCallback : public CCallbackImpl< sizeof( P ) >
214{
215public:
216 typedef void (T::*func_t)(P*);
217
218 // NOTE: If you can't provide the correct parameters at construction time, you should
219 // use the CCallbackManual callback object (STEAM_CALLBACK_MANUAL macro) instead.
220 CCallback( T *pObj, func_t func );
221
222 void Register( T *pObj, func_t func );
223 void Unregister();
224
225protected:
226 virtual void Run( void *pvParam ) S_OVERRIDE;
227
230};
231
232
233//-----------------------------------------------------------------------------
234// Purpose: subclass of CCallback which allows default-construction in
235// an unregistered state; you must call Register manually
236//-----------------------------------------------------------------------------
237template< class T, class P, bool bGameServer = false >
238class CCallbackManual : public CCallback< T, P, bGameServer >
239{
240public:
241 CCallbackManual() : CCallback< T, P, bGameServer >( nullptr, nullptr ) {}
242
243 // Inherits public Register and Unregister functions from base class
244};
245
246// Internal implementation details for all of the above
247#include "steam_api_internal.h"
248
249#endif // STEAM_API_COMMON_H
SCRIPT_CONTROL SCRIPT_STATE const void int
void(T::*) func_t(P *, bool)
virtual int GetCallbackSizeBytes() S_OVERRIDE
void SetGameserverFlag()
virtual void Run(void *pvParam) S_OVERRIDE
SteamAPICall_t m_hAPICall
void Set(SteamAPICall_t hAPICall, T *p, func_t func)
bool IsActive() const
friend class CCallbackMgr
CCallbackBase & operator=(const CCallbackBase &)
virtual void Run(void *pvParam)=0
virtual void Run(void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall)=0
CCallbackBase(const CCallbackBase &)
virtual int GetCallbackSizeBytes()=0
CCallback(T *pObj, func_t func)
void Register(T *pObj, func_t func)
void(T::*) func_t(P *)
virtual void Run(void *pvParam) S_OVERRIDE
friend class CCallbackMgr
virtual int GetCallbackSizeBytes() S_OVERRIDE
virtual ~CCallbackImpl()
virtual void Run(void *pvParam, bool, SteamAPICall_t) S_OVERRIDE
virtual void Run(void *pvParam) S_OVERRIDE=0
#define S_API
#define S_OVERRIDE
int32 HSteamPipe
S_API void S_CALLTYPE SteamAPI_RunCallbacks()
const int k_cchMaxSteamErrMsg
char SteamErrMsg[k_cchMaxSteamErrMsg]
const char *typedef uint32(S_CALLTYPE *SteamAPI_CheckCallbackRegistered_t)(int iCallbackNum)
S_API void S_CALLTYPE SteamGameServer_RunCallbacks()
typedef void(S_CALLTYPE *SteamAPIWarningMessageHook_t)(int
int32 HSteamUser
S_API void S_CALLTYPE SteamAPI_UnregisterCallback(class CCallbackBase *pCallback)
int int32
Definition steamtypes.h:57
uint64 SteamAPICall_t
Definition steamtypes.h:93
#define S_CALLTYPE
Definition steamtypes.h:6
unsigned char uint8
Definition steamtypes.h:11