Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
16 KiB

  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. // Most Steam API functions allocate some 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. // If your application contains modules or libraries which could be built against different SDK
  84. // versions, then you should define VERSION_SAFE_STEAM_API_INTERFACES to enforce that you cannot
  85. // use the un-versioned global accessors. Instead, always create and use CSteamAPIContext objects
  86. // to retrieve interface pointers which match the Steamworks SDK headers which match your build.
  87. #if !defined( VERSION_SAFE_STEAM_API_INTERFACES ) && !defined( STEAM_API_EXPORTS )
  88. inline ISteamClient *SteamClient();
  89. inline ISteamUser *SteamUser();
  90. inline ISteamFriends *SteamFriends();
  91. inline ISteamUtils *SteamUtils();
  92. inline ISteamMatchmaking *SteamMatchmaking();
  93. inline ISteamUserStats *SteamUserStats();
  94. inline ISteamApps *SteamApps();
  95. inline ISteamNetworking *SteamNetworking();
  96. inline ISteamMatchmakingServers *SteamMatchmakingServers();
  97. inline ISteamRemoteStorage *SteamRemoteStorage();
  98. inline ISteamScreenshots *SteamScreenshots();
  99. inline ISteamHTTP *SteamHTTP();
  100. inline ISteamUnifiedMessages *SteamUnifiedMessages();
  101. inline ISteamController *SteamController();
  102. inline ISteamUGC *SteamUGC();
  103. inline ISteamAppList *SteamAppList();
  104. inline ISteamMusic *SteamMusic();
  105. inline ISteamMusicRemote *SteamMusicRemote();
  106. inline ISteamHTMLSurface *SteamHTMLSurface();
  107. inline ISteamInventory *SteamInventory();
  108. inline ISteamVideo *SteamVideo();
  109. #endif // VERSION_SAFE_STEAM_API_INTERFACES
  110. // Every compiled module will have its own inlined definitions of CSteamAPIContext::Init.
  111. // Do NOT share CSteamAPIContext pointers across modules unless you are sure that they will
  112. // all be compiled against the same SDK!
  113. class CSteamAPIContext
  114. {
  115. public:
  116. CSteamAPIContext() { Clear(); }
  117. void Clear();
  118. bool Init();
  119. ISteamClient* SteamClient() const { return m_pSteamClient; }
  120. ISteamUser* SteamUser() const { return m_pSteamUser; }
  121. ISteamFriends* SteamFriends() const { return m_pSteamFriends; }
  122. ISteamUtils* SteamUtils() const { return m_pSteamUtils; }
  123. ISteamMatchmaking* SteamMatchmaking() const { return m_pSteamMatchmaking; }
  124. ISteamUserStats* SteamUserStats() const { return m_pSteamUserStats; }
  125. ISteamApps* SteamApps() const { return m_pSteamApps; }
  126. ISteamMatchmakingServers* SteamMatchmakingServers() const { return m_pSteamMatchmakingServers; }
  127. ISteamNetworking* SteamNetworking() const { return m_pSteamNetworking; }
  128. ISteamRemoteStorage* SteamRemoteStorage() const { return m_pSteamRemoteStorage; }
  129. ISteamScreenshots* SteamScreenshots() const { return m_pSteamScreenshots; }
  130. ISteamHTTP* SteamHTTP() const { return m_pSteamHTTP; }
  131. ISteamUnifiedMessages* SteamUnifiedMessages() const { return m_pSteamUnifiedMessages; }
  132. ISteamController* SteamController() const { return m_pController; }
  133. ISteamUGC* SteamUGC() const { return m_pSteamUGC; }
  134. ISteamAppList* SteamAppList() const { return m_pSteamAppList; }
  135. ISteamMusic* SteamMusic() const { return m_pSteamMusic; }
  136. ISteamMusicRemote* SteamMusicRemote() const { return m_pSteamMusicRemote; }
  137. ISteamHTMLSurface* SteamHTMLSurface() const { return m_pSteamHTMLSurface; }
  138. ISteamInventory* SteamInventory() const { return m_pSteamInventory; }
  139. ISteamVideo* SteamVideo() const { return m_pSteamVideo; }
  140. private:
  141. ISteamClient *m_pSteamClient;
  142. ISteamUser *m_pSteamUser;
  143. ISteamFriends *m_pSteamFriends;
  144. ISteamUtils *m_pSteamUtils;
  145. ISteamMatchmaking *m_pSteamMatchmaking;
  146. ISteamUserStats *m_pSteamUserStats;
  147. ISteamApps *m_pSteamApps;
  148. ISteamMatchmakingServers *m_pSteamMatchmakingServers;
  149. ISteamNetworking *m_pSteamNetworking;
  150. ISteamRemoteStorage *m_pSteamRemoteStorage;
  151. ISteamScreenshots *m_pSteamScreenshots;
  152. ISteamHTTP *m_pSteamHTTP;
  153. ISteamUnifiedMessages *m_pSteamUnifiedMessages;
  154. ISteamController *m_pController;
  155. ISteamUGC *m_pSteamUGC;
  156. ISteamAppList *m_pSteamAppList;
  157. ISteamMusic *m_pSteamMusic;
  158. ISteamMusicRemote *m_pSteamMusicRemote;
  159. ISteamHTMLSurface *m_pSteamHTMLSurface;
  160. ISteamInventory *m_pSteamInventory;
  161. ISteamVideo *m_pSteamVideo;
  162. };
  163. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  164. // steam callback and call-result helpers
  165. //
  166. // The following macros and classes are used to register your application for
  167. // callbacks and call-results, which are delivered in a predictable manner.
  168. //
  169. // STEAM_CALLBACK macros are meant for use inside of a C++ class definition.
  170. // They map a Steam notification callback directly to a class member function
  171. // which is automatically prototyped as "void func( callback_type *pParam )".
  172. //
  173. // CCallResult is used with specific Steam APIs that return "result handles".
  174. // The handle can be passed to a CCallResult object's Set function, along with
  175. // an object pointer and member-function pointer. The member function will
  176. // be executed once the results of the Steam API call are available.
  177. //
  178. // CCallback and CCallbackManual classes can be used instead of STEAM_CALLBACK
  179. // macros if you require finer control over registration and unregistration.
  180. //
  181. // Callbacks and call-results are queued automatically and are only
  182. // delivered/executed when your application calls SteamAPI_RunCallbacks().
  183. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  184. // SteamAPI_RunCallbacks is safe to call from multiple threads simultaneously,
  185. // but if you choose to do this, callback code could be executed on any thread.
  186. // One alternative is to call SteamAPI_RunCallbacks from the main thread only,
  187. // and call SteamAPI_ReleaseCurrentThreadMemory regularly on other threads.
  188. S_API void S_CALLTYPE SteamAPI_RunCallbacks();
  189. // Declares a callback member function plus a helper member variable which
  190. // registers the callback on object creation and unregisters on destruction.
  191. // The optional fourth 'var' param exists only for backwards-compatibility
  192. // and can be ignored.
  193. #define STEAM_CALLBACK( thisclass, func, .../*callback_type, [deprecated] var*/ ) \
  194. _STEAM_CALLBACK_SELECT( ( __VA_ARGS__, 4, 3 ), ( /**/, thisclass, func, __VA_ARGS__ ) )
  195. // Declares a callback function and a named CCallbackManual variable which
  196. // has Register and Unregister functions instead of automatic registration.
  197. #define STEAM_CALLBACK_MANUAL( thisclass, func, callback_type, var ) \
  198. CCallbackManual< thisclass, callback_type > var; void func( callback_type *pParam )
  199. // Internal functions used by the utility CCallback objects to receive callbacks
  200. S_API void S_CALLTYPE SteamAPI_RegisterCallback( class CCallbackBase *pCallback, int iCallback );
  201. S_API void S_CALLTYPE SteamAPI_UnregisterCallback( class CCallbackBase *pCallback );
  202. // Internal functions used by the utility CCallResult objects to receive async call results
  203. S_API void S_CALLTYPE SteamAPI_RegisterCallResult( class CCallbackBase *pCallback, SteamAPICall_t hAPICall );
  204. S_API void S_CALLTYPE SteamAPI_UnregisterCallResult( class CCallbackBase *pCallback, SteamAPICall_t hAPICall );
  205. //-----------------------------------------------------------------------------
  206. // Purpose: base for callbacks and call results - internal implementation detail
  207. //-----------------------------------------------------------------------------
  208. class CCallbackBase
  209. {
  210. public:
  211. CCallbackBase() { m_nCallbackFlags = 0; m_iCallback = 0; }
  212. // don't add a virtual destructor because we export this binary interface across dll's
  213. virtual void Run( void *pvParam ) = 0;
  214. virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall ) = 0;
  215. int GetICallback() { return m_iCallback; }
  216. virtual int GetCallbackSizeBytes() = 0;
  217. protected:
  218. enum { k_ECallbackFlagsRegistered = 0x01, k_ECallbackFlagsGameServer = 0x02 };
  219. uint8 m_nCallbackFlags;
  220. int m_iCallback;
  221. friend class CCallbackMgr;
  222. private:
  223. CCallbackBase( const CCallbackBase& );
  224. CCallbackBase& operator=( const CCallbackBase& );
  225. };
  226. //-----------------------------------------------------------------------------
  227. // Purpose: templated base for callbacks - internal implementation detail
  228. //-----------------------------------------------------------------------------
  229. template< int sizeof_P >
  230. class CCallbackImpl : protected CCallbackBase
  231. {
  232. public:
  233. ~CCallbackImpl() { if ( m_nCallbackFlags & k_ECallbackFlagsRegistered ) SteamAPI_UnregisterCallback( this ); }
  234. void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; }
  235. protected:
  236. virtual void Run( void *pvParam ) = 0;
  237. virtual void Run( void *pvParam, bool /*bIOFailure*/, SteamAPICall_t /*hSteamAPICall*/ ) { Run( pvParam ); }
  238. virtual int GetCallbackSizeBytes() { return sizeof_P; }
  239. };
  240. //-----------------------------------------------------------------------------
  241. // Purpose: maps a steam async call result to a class member function
  242. // template params: T = local class, P = parameter struct
  243. //-----------------------------------------------------------------------------
  244. template< class T, class P >
  245. class CCallResult : private CCallbackBase
  246. {
  247. public:
  248. typedef void (T::*func_t)( P*, bool );
  249. CCallResult();
  250. ~CCallResult();
  251. void Set( SteamAPICall_t hAPICall, T *p, func_t func );
  252. bool IsActive() const;
  253. void Cancel();
  254. void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; }
  255. private:
  256. virtual void Run( void *pvParam );
  257. virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall );
  258. virtual int GetCallbackSizeBytes() { return sizeof( P ); }
  259. SteamAPICall_t m_hAPICall;
  260. T *m_pObj;
  261. func_t m_Func;
  262. };
  263. //-----------------------------------------------------------------------------
  264. // Purpose: maps a steam callback to a class member function
  265. // template params: T = local class, P = parameter struct,
  266. // bGameserver = listen for gameserver callbacks instead of client callbacks
  267. //-----------------------------------------------------------------------------
  268. template< class T, class P, bool bGameserver = false >
  269. class CCallback : public CCallbackImpl< sizeof( P ) >
  270. {
  271. public:
  272. typedef void (T::*func_t)(P*);
  273. // NOTE: If you can't provide the correct parameters at construction time, you should
  274. // use the CCallbackManual callback object (STEAM_CALLBACK_MANUAL macro) instead.
  275. CCallback( T *pObj, func_t func );
  276. void Register( T *pObj, func_t func );
  277. void Unregister();
  278. protected:
  279. virtual void Run( void *pvParam );
  280. T *m_pObj;
  281. func_t m_Func;
  282. };
  283. //-----------------------------------------------------------------------------
  284. // Purpose: subclass of CCallback which allows default-construction in
  285. // an unregistered state; you must call Register manually
  286. //-----------------------------------------------------------------------------
  287. template< class T, class P, bool bGameServer = false >
  288. class CCallbackManual : public CCallback< T, P, bGameServer >
  289. {
  290. public:
  291. CCallbackManual() : CCallback< T, P, bGameServer >( NULL, NULL ) {}
  292. // Inherits public Register and Unregister functions from base class
  293. };
  294. #ifdef _WIN32
  295. // disable this warning; this pattern need for steam callback registration
  296. #pragma warning( disable: 4355 ) // 'this' : used in base member initializer list
  297. #endif
  298. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  299. // steamclient.dll private wrapper functions
  300. //
  301. // The following functions are part of abstracting API access to the steamclient.dll, but should only be used in very specific cases
  302. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  303. // SteamAPI_IsSteamRunning() returns true if Steam is currently running
  304. S_API bool S_CALLTYPE SteamAPI_IsSteamRunning();
  305. // Pumps out all the steam messages, calling registered callbacks.
  306. // NOT THREADSAFE - do not call from multiple threads simultaneously.
  307. S_API void Steam_RunCallbacks( HSteamPipe hSteamPipe, bool bGameServerCallbacks );
  308. // register the callback funcs to use to interact with the steam dll
  309. S_API void Steam_RegisterInterfaceFuncs( void *hModule );
  310. // returns the HSteamUser of the last user to dispatch a callback
  311. S_API HSteamUser Steam_GetHSteamUserCurrent();
  312. // returns the filename path of the current running Steam process, used if you need to load an explicit steam dll by name.
  313. // 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
  314. S_API const char *SteamAPI_GetSteamInstallPath();
  315. // returns the pipe we are communicating to Steam with
  316. S_API HSteamPipe SteamAPI_GetHSteamPipe();
  317. // sets whether or not Steam_RunCallbacks() should do a try {} catch (...) {} around calls to issuing callbacks
  318. S_API void SteamAPI_SetTryCatchCallbacks( bool bTryCatchCallbacks );
  319. // backwards compat export, passes through to SteamAPI_ variants
  320. S_API HSteamPipe GetHSteamPipe();
  321. S_API HSteamUser GetHSteamUser();
  322. #if defined( VERSION_SAFE_STEAM_API_INTERFACES )
  323. // backwards compat with older SDKs
  324. S_API bool S_CALLTYPE SteamAPI_InitSafe();
  325. #endif
  326. #include "steam_api_internal.h"
  327. #endif // STEAM_API_H