steam_api.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //====== Copyright 1996-2008, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef STEAM_API_H
  7. #define STEAM_API_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "isteamclient.h"
  12. #include "isteamuser.h"
  13. #include "isteamfriends.h"
  14. #include "isteamutils.h"
  15. #include "isteammatchmaking.h"
  16. #include "isteamuserstats.h"
  17. #include "isteamapps.h"
  18. #include "isteamnetworking.h"
  19. #include "isteamremotestorage.h"
  20. #include "isteamscreenshots.h"
  21. #include "isteammusic.h"
  22. #include "isteammusicremote.h"
  23. #include "isteamhttp.h"
  24. #include "isteamunifiedmessages.h"
  25. #include "isteamcontroller.h"
  26. #include "isteamugc.h"
  27. #include "isteamapplist.h"
  28. #include "isteamhtmlsurface.h"
  29. #include "isteaminventory.h"
  30. #include "isteamvideo.h"
  31. // Steam API export macro
  32. #if defined( _WIN32 ) && !defined( _X360 )
  33. #if defined( STEAM_API_EXPORTS )
  34. #define S_API extern "C" __declspec( dllexport )
  35. #elif defined( STEAM_API_NODLL )
  36. #define S_API extern "C"
  37. #else
  38. #define S_API extern "C" __declspec( dllimport )
  39. #endif // STEAM_API_EXPORTS
  40. #elif defined( GNUC )
  41. #if defined( STEAM_API_EXPORTS )
  42. #define S_API extern "C" __attribute__ ((visibility("default")))
  43. #else
  44. #define S_API extern "C"
  45. #endif // STEAM_API_EXPORTS
  46. #else // !WIN32
  47. #if defined( STEAM_API_EXPORTS )
  48. #define S_API extern "C"
  49. #else
  50. #define S_API extern "C"
  51. #endif // STEAM_API_EXPORTS
  52. #endif
  53. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  54. // Steam API setup & shutdown
  55. //
  56. // These functions manage loading, initializing and shutdown of the steamclient.dll
  57. //
  58. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  59. // SteamAPI_Init must be called before using any other API functions. If it fails, an
  60. // error message will be output to the debugger (or stderr) with further information.
  61. S_API bool S_CALLTYPE SteamAPI_Init();
  62. // SteamAPI_Shutdown should be called during process shutdown if possible.
  63. S_API void S_CALLTYPE SteamAPI_Shutdown();
  64. // SteamAPI_RestartAppIfNecessary ensures that your executable was launched through Steam.
  65. //
  66. // Returns true if the current process should terminate. Steam is now re-launching your application.
  67. //
  68. // Returns false if no action needs to be taken. This means that your executable was started through
  69. // the Steam client, or a steam_appid.txt file is present in your game's directory (for development).
  70. // Your current process should continue if false is returned.
  71. //
  72. // NOTE: If you use the Steam DRM wrapper on your primary executable file, this check is unnecessary
  73. // since the DRM wrapper will ensure that your application was launched properly through Steam.
  74. S_API bool S_CALLTYPE SteamAPI_RestartAppIfNecessary( uint32 unOwnAppID );
  75. // Many Steam API functions allocate a small amount of thread-local memory for parameter storage.
  76. // SteamAPI_ReleaseCurrentThreadMemory() will free API memory associated with the calling thread.
  77. // This function is also called automatically by SteamAPI_RunCallbacks(), so a single-threaded
  78. // program never needs to explicitly call this function.
  79. S_API void S_CALLTYPE SteamAPI_ReleaseCurrentThreadMemory();
  80. // crash dump recording functions
  81. S_API void S_CALLTYPE SteamAPI_WriteMiniDump( uint32 uStructuredExceptionCode, void* pvExceptionInfo, uint32 uBuildID );
  82. S_API void S_CALLTYPE SteamAPI_SetMiniDumpComment( const char *pchMsg );
  83. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  84. // Global accessors for Steamworks C++ APIs. See individual isteam*.h files for details.
  85. // You should not cache the results of these accessors or pass the result pointers across
  86. // modules! Different modules may be compiled against different SDK header versions, and
  87. // the interface pointers could therefore be different across modules. Every line of code
  88. // which calls into a Steamworks API should retrieve the interface from a global accessor.
  89. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  90. #if !defined( STEAM_API_EXPORTS )
  91. inline ISteamClient *SteamClient();
  92. inline ISteamUser *SteamUser();
  93. inline ISteamFriends *SteamFriends();
  94. inline ISteamUtils *SteamUtils();
  95. inline ISteamMatchmaking *SteamMatchmaking();
  96. inline ISteamUserStats *SteamUserStats();
  97. inline ISteamApps *SteamApps();
  98. inline ISteamNetworking *SteamNetworking();
  99. inline ISteamMatchmakingServers *SteamMatchmakingServers();
  100. inline ISteamRemoteStorage *SteamRemoteStorage();
  101. inline ISteamScreenshots *SteamScreenshots();
  102. inline ISteamHTTP *SteamHTTP();
  103. inline ISteamUnifiedMessages *SteamUnifiedMessages();
  104. inline ISteamController *SteamController();
  105. inline ISteamUGC *SteamUGC();
  106. inline ISteamAppList *SteamAppList();
  107. inline ISteamMusic *SteamMusic();
  108. inline ISteamMusicRemote *SteamMusicRemote();
  109. inline ISteamHTMLSurface *SteamHTMLSurface();
  110. inline ISteamInventory *SteamInventory();
  111. inline ISteamVideo *SteamVideo();
  112. #endif // VERSION_SAFE_STEAM_API_INTERFACES
  113. // CSteamAPIContext encapsulates the Steamworks API global accessors into
  114. // a single object. This is DEPRECATED and only remains for compatibility.
  115. class CSteamAPIContext
  116. {
  117. public:
  118. // DEPRECATED - there is no benefit to using this over the global accessors
  119. CSteamAPIContext() { Clear(); }
  120. void Clear();
  121. bool Init();
  122. ISteamClient* SteamClient() const { return m_pSteamClient; }
  123. ISteamUser* SteamUser() const { return m_pSteamUser; }
  124. ISteamFriends* SteamFriends() const { return m_pSteamFriends; }
  125. ISteamUtils* SteamUtils() const { return m_pSteamUtils; }
  126. ISteamMatchmaking* SteamMatchmaking() const { return m_pSteamMatchmaking; }
  127. ISteamUserStats* SteamUserStats() const { return m_pSteamUserStats; }
  128. ISteamApps* SteamApps() const { return m_pSteamApps; }
  129. ISteamMatchmakingServers* SteamMatchmakingServers() const { return m_pSteamMatchmakingServers; }
  130. ISteamNetworking* SteamNetworking() const { return m_pSteamNetworking; }
  131. ISteamRemoteStorage* SteamRemoteStorage() const { return m_pSteamRemoteStorage; }
  132. ISteamScreenshots* SteamScreenshots() const { return m_pSteamScreenshots; }
  133. ISteamHTTP* SteamHTTP() const { return m_pSteamHTTP; }
  134. ISteamUnifiedMessages* SteamUnifiedMessages() const { return m_pSteamUnifiedMessages; }
  135. ISteamController* SteamController() const { return m_pController; }
  136. ISteamUGC* SteamUGC() const { return m_pSteamUGC; }
  137. ISteamAppList* SteamAppList() const { return m_pSteamAppList; }
  138. ISteamMusic* SteamMusic() const { return m_pSteamMusic; }
  139. ISteamMusicRemote* SteamMusicRemote() const { return m_pSteamMusicRemote; }
  140. ISteamHTMLSurface* SteamHTMLSurface() const { return m_pSteamHTMLSurface; }
  141. ISteamInventory* SteamInventory() const { return m_pSteamInventory; }
  142. ISteamVideo* SteamVideo() const { return m_pSteamVideo; }
  143. // DEPRECATED - there is no benefit to using this over the global accessors
  144. private:
  145. ISteamClient *m_pSteamClient;
  146. ISteamUser *m_pSteamUser;
  147. ISteamFriends *m_pSteamFriends;
  148. ISteamUtils *m_pSteamUtils;
  149. ISteamMatchmaking *m_pSteamMatchmaking;
  150. ISteamUserStats *m_pSteamUserStats;
  151. ISteamApps *m_pSteamApps;
  152. ISteamMatchmakingServers *m_pSteamMatchmakingServers;
  153. ISteamNetworking *m_pSteamNetworking;
  154. ISteamRemoteStorage *m_pSteamRemoteStorage;
  155. ISteamScreenshots *m_pSteamScreenshots;
  156. ISteamHTTP *m_pSteamHTTP;
  157. ISteamUnifiedMessages *m_pSteamUnifiedMessages;
  158. ISteamController *m_pController;
  159. ISteamUGC *m_pSteamUGC;
  160. ISteamAppList *m_pSteamAppList;
  161. ISteamMusic *m_pSteamMusic;
  162. ISteamMusicRemote *m_pSteamMusicRemote;
  163. ISteamHTMLSurface *m_pSteamHTMLSurface;
  164. ISteamInventory *m_pSteamInventory;
  165. ISteamVideo *m_pSteamVideo;
  166. };
  167. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  168. // steam callback and call-result helpers
  169. //
  170. // The following macros and classes are used to register your application for
  171. // callbacks and call-results, which are delivered in a predictable manner.
  172. //
  173. // STEAM_CALLBACK macros are meant for use inside of a C++ class definition.
  174. // They map a Steam notification callback directly to a class member function
  175. // which is automatically prototyped as "void func( callback_type *pParam )".
  176. //
  177. // CCallResult is used with specific Steam APIs that return "result handles".
  178. // The handle can be passed to a CCallResult object's Set function, along with
  179. // an object pointer and member-function pointer. The member function will
  180. // be executed once the results of the Steam API call are available.
  181. //
  182. // CCallback and CCallbackManual classes can be used instead of STEAM_CALLBACK
  183. // macros if you require finer control over registration and unregistration.
  184. //
  185. // Callbacks and call-results are queued automatically and are only
  186. // delivered/executed when your application calls SteamAPI_RunCallbacks().
  187. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  188. // SteamAPI_RunCallbacks is safe to call from multiple threads simultaneously,
  189. // but if you choose to do this, callback code could be executed on any thread.
  190. // One alternative is to call SteamAPI_RunCallbacks from the main thread only,
  191. // and call SteamAPI_ReleaseCurrentThreadMemory regularly on other threads.
  192. S_API void S_CALLTYPE SteamAPI_RunCallbacks();
  193. // Declares a callback member function plus a helper member variable which
  194. // registers the callback on object creation and unregisters on destruction.
  195. // The optional fourth 'var' param exists only for backwards-compatibility
  196. // and can be ignored.
  197. #define STEAM_CALLBACK( thisclass, func, .../*callback_type, [deprecated] var*/ ) \
  198. _STEAM_CALLBACK_SELECT( ( __VA_ARGS__, 4, 3 ), ( /**/, thisclass, func, __VA_ARGS__ ) )
  199. // Declares a callback function and a named CCallbackManual variable which
  200. // has Register and Unregister functions instead of automatic registration.
  201. #define STEAM_CALLBACK_MANUAL( thisclass, func, callback_type, var ) \
  202. CCallbackManual< thisclass, callback_type > var; void func( callback_type *pParam )
  203. // Internal functions used by the utility CCallback objects to receive callbacks
  204. S_API void S_CALLTYPE SteamAPI_RegisterCallback( class CCallbackBase *pCallback, int iCallback );
  205. S_API void S_CALLTYPE SteamAPI_UnregisterCallback( class CCallbackBase *pCallback );
  206. // Internal functions used by the utility CCallResult objects to receive async call results
  207. S_API void S_CALLTYPE SteamAPI_RegisterCallResult( class CCallbackBase *pCallback, SteamAPICall_t hAPICall );
  208. S_API void S_CALLTYPE SteamAPI_UnregisterCallResult( class CCallbackBase *pCallback, SteamAPICall_t hAPICall );
  209. //-----------------------------------------------------------------------------
  210. // Purpose: base for callbacks and call results - internal implementation detail
  211. //-----------------------------------------------------------------------------
  212. class CCallbackBase
  213. {
  214. public:
  215. CCallbackBase() { m_nCallbackFlags = 0; m_iCallback = 0; }
  216. // don't add a virtual destructor because we export this binary interface across dll's
  217. virtual void Run( void *pvParam ) = 0;
  218. virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall ) = 0;
  219. int GetICallback() { return m_iCallback; }
  220. virtual int GetCallbackSizeBytes() = 0;
  221. protected:
  222. enum { k_ECallbackFlagsRegistered = 0x01, k_ECallbackFlagsGameServer = 0x02 };
  223. uint8 m_nCallbackFlags;
  224. int m_iCallback;
  225. friend class CCallbackMgr;
  226. private:
  227. CCallbackBase( const CCallbackBase& );
  228. CCallbackBase& operator=( const CCallbackBase& );
  229. };
  230. //-----------------------------------------------------------------------------
  231. // Purpose: templated base for callbacks - internal implementation detail
  232. //-----------------------------------------------------------------------------
  233. template< int sizeof_P >
  234. class CCallbackImpl : protected CCallbackBase
  235. {
  236. public:
  237. ~CCallbackImpl() { if ( m_nCallbackFlags & k_ECallbackFlagsRegistered ) SteamAPI_UnregisterCallback( this ); }
  238. void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; }
  239. protected:
  240. virtual void Run( void *pvParam ) = 0;
  241. virtual void Run( void *pvParam, bool /*bIOFailure*/, SteamAPICall_t /*hSteamAPICall*/ ) { Run( pvParam ); }
  242. virtual int GetCallbackSizeBytes() { return sizeof_P; }
  243. };
  244. //-----------------------------------------------------------------------------
  245. // Purpose: maps a steam async call result to a class member function
  246. // template params: T = local class, P = parameter struct
  247. //-----------------------------------------------------------------------------
  248. template< class T, class P >
  249. class CCallResult : private CCallbackBase
  250. {
  251. public:
  252. typedef void (T::*func_t)( P*, bool );
  253. CCallResult();
  254. ~CCallResult();
  255. void Set( SteamAPICall_t hAPICall, T *p, func_t func );
  256. bool IsActive() const;
  257. void Cancel();
  258. void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; }
  259. private:
  260. virtual void Run( void *pvParam );
  261. virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall );
  262. virtual int GetCallbackSizeBytes() { return sizeof( P ); }
  263. SteamAPICall_t m_hAPICall;
  264. T *m_pObj;
  265. func_t m_Func;
  266. };
  267. //-----------------------------------------------------------------------------
  268. // Purpose: maps a steam callback to a class member function
  269. // template params: T = local class, P = parameter struct,
  270. // bGameserver = listen for gameserver callbacks instead of client callbacks
  271. //-----------------------------------------------------------------------------
  272. template< class T, class P, bool bGameserver = false >
  273. class CCallback : public CCallbackImpl< sizeof( P ) >
  274. {
  275. public:
  276. typedef void (T::*func_t)(P*);
  277. // NOTE: If you can't provide the correct parameters at construction time, you should
  278. // use the CCallbackManual callback object (STEAM_CALLBACK_MANUAL macro) instead.
  279. CCallback( T *pObj, func_t func );
  280. void Register( T *pObj, func_t func );
  281. void Unregister();
  282. protected:
  283. virtual void Run( void *pvParam );
  284. T *m_pObj;
  285. func_t m_Func;
  286. };
  287. //-----------------------------------------------------------------------------
  288. // Purpose: subclass of CCallback which allows default-construction in
  289. // an unregistered state; you must call Register manually
  290. //-----------------------------------------------------------------------------
  291. template< class T, class P, bool bGameServer = false >
  292. class CCallbackManual : public CCallback< T, P, bGameServer >
  293. {
  294. public:
  295. CCallbackManual() : CCallback< T, P, bGameServer >( NULL, NULL ) {}
  296. // Inherits public Register and Unregister functions from base class
  297. };
  298. #ifdef _WIN32
  299. // disable this warning; this pattern need for steam callback registration
  300. #pragma warning( disable: 4355 ) // 'this' : used in base member initializer list
  301. #endif
  302. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  303. // steamclient.dll private wrapper functions
  304. //
  305. // The following functions are part of abstracting API access to the steamclient.dll, but should only be used in very specific cases
  306. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  307. // SteamAPI_IsSteamRunning() returns true if Steam is currently running
  308. S_API bool S_CALLTYPE SteamAPI_IsSteamRunning();
  309. // Pumps out all the steam messages, calling registered callbacks.
  310. // NOT THREADSAFE - do not call from multiple threads simultaneously.
  311. S_API void Steam_RunCallbacks( HSteamPipe hSteamPipe, bool bGameServerCallbacks );
  312. // register the callback funcs to use to interact with the steam dll
  313. S_API void Steam_RegisterInterfaceFuncs( void *hModule );
  314. // returns the HSteamUser of the last user to dispatch a callback
  315. S_API HSteamUser Steam_GetHSteamUserCurrent();
  316. // returns the filename path of the current running Steam process, used if you need to load an explicit steam dll by name.
  317. // DEPRECATED - implementation is Windows only, and the path returned is a UTF-8 string which must be converted to UTF-16 for use with Win32 APIs
  318. S_API const char *SteamAPI_GetSteamInstallPath();
  319. // returns the pipe we are communicating to Steam with
  320. S_API HSteamPipe SteamAPI_GetHSteamPipe();
  321. // sets whether or not Steam_RunCallbacks() should do a try {} catch (...) {} around calls to issuing callbacks
  322. S_API void SteamAPI_SetTryCatchCallbacks( bool bTryCatchCallbacks );
  323. // backwards compat export, passes through to SteamAPI_ variants
  324. S_API HSteamPipe GetHSteamPipe();
  325. S_API HSteamUser GetHSteamUser();
  326. #if defined( VERSION_SAFE_STEAM_API_INTERFACES )
  327. // exists only for backwards compat with code written against older SDKs
  328. S_API bool S_CALLTYPE SteamAPI_InitSafe();
  329. #endif
  330. #include "steam_api_internal.h"
  331. #endif // STEAM_API_H