Advertisement
c0001

steamtest.d

Jun 8th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.52 KB | None | 0 0
  1. import std.stdio;
  2. import core.thread;
  3.  
  4.  
  5. // Steamworks SDK: https://partner.steamgames.com/doc/sdk
  6. // Download link: https://partner.steamgames.com/downloads/steamworks_sdk.zip
  7. // A file named "steam_appid.txt" must be present in the working directory, containing the number 480 (ID for Steamworks public developer test application)
  8. // See SDK steam_api_flat.h, steam_api.h, steam_api_internal.h
  9.  
  10.  
  11. pragma(lib, "steam_api_omf");
  12.  
  13. // stdout autoflush fix for IDE compatibility, optional
  14. void write(T...)(lazy T t) { stdout.write(t); stdout.flush(); }
  15. void writef(T...)(lazy T t) { stdout.writef(t); stdout.flush(); }
  16. void writeln(T...)(lazy T t) { stdout.writeln(t); stdout.flush(); }
  17. void writefln(T...)(lazy T t) { stdout.writefln(t); stdout.flush(); }
  18.  
  19.  
  20. alias int HSteamPipe;
  21. alias int HSteamUser;
  22. alias ulong SteamAPICall_t;
  23. alias byte int8;
  24. alias ubyte uint8;
  25. alias short int16;
  26. alias ushort uint16;
  27. alias int int32;
  28. alias uint uint32;
  29. alias long int64;
  30. alias ulong uint64;
  31. enum {
  32.     STEAMCLIENT_INTERFACE_VERSION = "SteamClient017",
  33.     STEAMUTILS_INTERFACE_VERSION = "SteamUtils009",
  34.     STEAMUSER_INTERFACE_VERSION = "SteamUser019",
  35.     STEAMFRIENDS_INTERFACE_VERSION = "SteamFriends015",
  36.     STEAMUSERSTATS_INTERFACE_VERSION = "STEAMUSERSTATS_INTERFACE_VERSION011",
  37.     STEAMREMOTESTORAGE_INTERFACE_VERSION = "STEAMREMOTESTORAGE_INTERFACE_VERSION014",
  38. }
  39. enum {
  40.     k_iSteamUserStatsCallbacks = 1100,
  41. }
  42. enum ESteamAPICallFailure {
  43.     None = -1,          // no failure
  44.     SteamGone = 0,      // the local Steam process has gone away
  45.     NetworkFailure = 1, // the network connection to Steam has been broken, or was already broken
  46.     // SteamServersDisconnected_t callback will be sent around the same time
  47.     // SteamServersConnected_t will be sent when the client is able to talk to the Steam servers again
  48.     InvalidHandle = 2,  // the SteamAPICall_t handle passed in no longer exists
  49.     MismatchedCallback = 3,// GetAPICallResult() was called with the wrong callback type for this API call
  50. }
  51.  
  52. extern(C) {
  53.  
  54.     const SteamAPICall_t k_uAPICallInvalid = 0x0;
  55.  
  56.     class ISteamClient {}
  57.     class ISteamUtils {}
  58.     class ISteamUser {}
  59.     class ISteamUserStats {}
  60.     class ISteamFriends {}
  61.     class ISteamRemoteStorage {}
  62.  
  63.     bool SteamAPI_Init();
  64.     void SteamAPI_Shutdown();
  65.     // Interfaces
  66.     ISteamClient SteamClient();
  67.     ISteamUtils SteamAPI_ISteamClient_GetISteamUtils(ISteamClient instancePtr, HSteamPipe hSteamPipe, const char* pchVersion);
  68.     ISteamUser SteamAPI_ISteamClient_GetISteamUser(ISteamClient instancePtr, HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char* pchVersion);
  69.     ISteamUserStats SteamAPI_ISteamClient_GetISteamUserStats(ISteamClient instancePtr, HSteamUser hSteamUser, HSteamPipe hSteamPipe, const char* pchVersion);
  70.     // Handles
  71.     HSteamPipe SteamAPI_ISteamClient_CreateSteamPipe(ISteamClient instancePtr);
  72.     HSteamUser SteamAPI_ISteamClient_ConnectToGlobalUser(ISteamClient instancePtr, HSteamPipe hSteamPipe);
  73.     bool SteamAPI_ISteamClient_BReleaseSteamPipe(ISteamClient instancePtr, HSteamPipe hSteamPipe);
  74.     void SteamAPI_ISteamClient_ReleaseUser(ISteamClient instancePtr, HSteamPipe hSteamPipe, HSteamUser hUser);
  75.     // Misc
  76.     ulong SteamAPI_ISteamUser_GetSteamID(ISteamUser instancePtr);
  77.     bool SteamAPI_ISteamUser_BLoggedOn(ISteamUser instancePtr);
  78.  
  79.     // Callbacks/Call Results
  80.     void SteamAPI_RegisterCallResult(CCallbackBase pCallback, SteamAPICall_t hAPICall);
  81.     void SteamAPI_UnregisterCallResult(CCallbackBase pCallback, SteamAPICall_t hAPICall);
  82.     void SteamAPI_RunCallbacks();
  83.     bool SteamAPI_ISteamUtils_IsAPICallCompleted(ISteamUtils instancePtr, SteamAPICall_t hSteamAPICall, bool* pbFailed);
  84.     ESteamAPICallFailure SteamAPI_ISteamUtils_GetAPICallFailureReason(ISteamUtils instancePtr, SteamAPICall_t hSteamAPICall);
  85.     bool SteamAPI_ISteamUtils_GetAPICallResult(ISteamUtils instancePtr, SteamAPICall_t hSteamAPICall, void* pCallback, int cubCallback, int iCallbackExpected, bool* pbFailed);
  86.  
  87.     // Async Requests
  88.     SteamAPICall_t SteamAPI_ISteamUserStats_GetNumberOfCurrentPlayers(ISteamUserStats instancePtr);
  89.     struct NumberOfCurrentPlayers_t {
  90.         enum k_iCallback = k_iSteamUserStatsCallbacks + 7;
  91.         uint8 m_bSuccess;           // 1 if the call was successful
  92.         int32 m_cPlayers;           // Number of players currently playing
  93.     }
  94. }
  95.  
  96. extern(C++) {
  97.  
  98.     class CallHandler {}
  99.  
  100.     abstract class CCallbackBase {
  101.         this() { m_nCallbackFlags = 0; m_iCallback = 0; }
  102.         void Run( void *pvParam );
  103.         void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall );
  104.         int GetICallback() { return m_iCallback; }
  105.         int GetCallbackSizeBytes();
  106.         enum { k_ECallbackFlagsRegistered = 0x01, k_ECallbackFlagsGameServer = 0x02 }
  107.         uint8 m_nCallbackFlags;
  108.         int m_iCallback;
  109.         //friend class CCallbackMgr;
  110.         //CCallbackBase( const CCallbackBase& );
  111.         //CCallbackBase& operator=( const CCallbackBase& );
  112.     }
  113.     class CallResult(T, P) : CCallbackBase {
  114.         // T: A C++ class whose member function is called m_Func is to be called
  115.         // P: The struct type to cast the result pointer to
  116.  
  117.         //typedef void (T::*func_t)( P*, bool );
  118.         alias func_t = void function(P*, bool);
  119.  
  120.         this() {
  121.             m_hAPICall = k_uAPICallInvalid;
  122.             m_pObj = null;
  123.             m_Func = null;
  124.             m_iCallback = P.k_iCallback;
  125.         }
  126.         ~this() {}
  127.  
  128.         void Set(SteamAPICall_t hAPICall, T p, func_t func) {
  129.             /+if (m_hAPICall)
  130.                 SteamAPI_UnregisterCallResult(this, m_hAPICall);+/
  131.             m_hAPICall = hAPICall;
  132.             m_pObj = p;
  133.             m_Func = func;
  134.             /+if (hAPICall)
  135.                 SteamAPI_RegisterCallResult(this, hAPICall);+/
  136.         }
  137.         bool IsActive() {
  138.             return (m_hAPICall != k_uAPICallInvalid);
  139.         };
  140.         void Cancel() {
  141.             if (m_hAPICall != k_uAPICallInvalid) {
  142.                 /+SteamAPI_UnregisterCallResult(this, m_hAPICall);+/
  143.                 m_hAPICall = k_uAPICallInvalid;
  144.             }
  145.         }
  146.         void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; }
  147.  
  148.         final override void Run( void *pvParam ) {
  149.             if (pvParam !is null)  {
  150.                 P result = *(cast(P*) pvParam);
  151.                 writeln("Call Result returned data: %s", result);
  152.             } else {
  153.                 writeln("Call Result returned no data.");
  154.             }
  155.         }
  156.         final override void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall ) {
  157.             if (pvParam !is null)  {
  158.                 P result = *(cast(P*) pvParam);
  159.                 writeln("Call Result(%s) returned data: %s", hSteamAPICall, result);
  160.             } else {
  161.                 writeln("Call Result(%s) returned no data.", hSteamAPICall);
  162.             }
  163.         }
  164.         final override int GetCallbackSizeBytes() { return P.sizeof; }
  165.  
  166.         SteamAPICall_t m_hAPICall;
  167.         T m_pObj; // Object whose member function should be called to finalize callback (not used here)
  168.         func_t m_Func; // Member function of the object above to run
  169.     }
  170. }
  171.  
  172.  
  173. void main() {
  174.     writefln("Initializing.");
  175.     bool initialized = SteamAPI_Init();
  176.     assert(initialized, "Unable to initialize SteamAPI, ensure Steam client is running");
  177.     auto client = SteamClient();
  178.     assert(client !is null, "Unable to retrieve SteamClient interface");
  179.  
  180.     HSteamPipe pipe = SteamAPI_ISteamClient_CreateSteamPipe(client);
  181.     HSteamUser userPipe = SteamAPI_ISteamClient_ConnectToGlobalUser(client, pipe);
  182.  
  183.     scope(exit) {
  184.         if (initialized) {
  185.             if (userPipe)
  186.                 SteamAPI_ISteamClient_ReleaseUser(client, pipe, userPipe);
  187.             if (pipe)
  188.                 SteamAPI_ISteamClient_BReleaseSteamPipe(client, pipe);
  189.             SteamAPI_Shutdown();
  190.         }
  191.         writefln("Terminating.");
  192.     }
  193.  
  194.     auto utils = SteamAPI_ISteamClient_GetISteamUtils(client, pipe, STEAMUTILS_INTERFACE_VERSION);
  195.     auto user = SteamAPI_ISteamClient_GetISteamUser(client, userPipe, pipe, STEAMUSER_INTERFACE_VERSION);
  196.     auto userStats = SteamAPI_ISteamClient_GetISteamUserStats(client, userPipe, pipe, STEAMUSERSTATS_INTERFACE_VERSION);
  197.  
  198.     ulong userid = SteamAPI_ISteamUser_GetSteamID(user);
  199.     bool loggedOn = SteamAPI_ISteamUser_BLoggedOn(user);
  200.     writefln("User logged on: %s", loggedOn);
  201.  
  202.     writefln("Starting request.");
  203.     alias NumberOfCurrentPlayers_t RESULT;
  204.     auto cbk = new CallResult!(CallHandler, RESULT)();
  205.  
  206.     auto hid = SteamAPI_ISteamUserStats_GetNumberOfCurrentPlayers(userStats);
  207.     writefln("hid: %s", hid);
  208.     cbk.m_hAPICall = hid;
  209.     cbk.m_iCallback = NumberOfCurrentPlayers_t.k_iCallback;
  210.  
  211.     // Register CallResult here, should cause some method to fire when SteamAPI_RunCallbacks is executed?
  212.     SteamAPI_RegisterCallResult(cbk, hid);
  213.  
  214.     int attempts = 10;
  215.     bool failed;
  216.     bool finished;
  217.     while (attempts-- > 0) {
  218.         if (failed)
  219.             write("?");
  220.         else if (finished)
  221.             write("!");
  222.         else
  223.             write(".");
  224.  
  225.         SteamAPI_RunCallbacks();
  226.  
  227.         // Check request status manually
  228.         finished = SteamAPI_ISteamUtils_IsAPICallCompleted(utils, hid, &failed);
  229.  
  230.         Thread.sleep(msecs(500));
  231.     }
  232.     writeln();
  233.  
  234.     // Display results of manually checking call completion status
  235.     if (failed) {
  236.         auto failReason = SteamAPI_ISteamUtils_GetAPICallFailureReason(utils, hid);
  237.         writefln("Request failed: %s", failReason);
  238.     } else if (finished) {
  239.         RESULT result;
  240.         auto b = SteamAPI_ISteamUtils_GetAPICallResult(utils, hid, &result, result.sizeof, RESULT.k_iCallback, &failed);
  241.         writefln("Request completed: %s", result);
  242.     } else {
  243.         writeln("No response from request.");
  244.     }
  245.  
  246. }
  247.  
  248.  
  249.  
  250. /+ +++++++++++++++++++++ from steam_api.h:
  251.  
  252.  
  253. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  254. //  steam callback and call-result helpers
  255. //
  256. //  The following macros and classes are used to register your application for
  257. //  callbacks and call-results, which are delivered in a predictable manner.
  258. //
  259. //  STEAM_CALLBACK macros are meant for use inside of a C++ class definition.
  260. //  They map a Steam notification callback directly to a class member function
  261. //  which is automatically prototyped as "void func( callback_type *pParam )".
  262. //
  263. //  CCallResult is used with specific Steam APIs that return "result handles".
  264. //  The handle can be passed to a CCallResult object's Set function, along with
  265. //  an object pointer and member-function pointer. The member function will
  266. //  be executed once the results of the Steam API call are available.
  267. //
  268. //  CCallback and CCallbackManual classes can be used instead of STEAM_CALLBACK
  269. //  macros if you require finer control over registration and unregistration.
  270. //
  271. //  Callbacks and call-results are queued automatically and are only
  272. //  delivered/executed when your application calls SteamAPI_RunCallbacks().
  273. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  274.  
  275. // SteamAPI_RunCallbacks is safe to call from multiple threads simultaneously,
  276. // but if you choose to do this, callback code could be executed on any thread.
  277. // One alternative is to call SteamAPI_RunCallbacks from the main thread only,
  278. // and call SteamAPI_ReleaseCurrentThreadMemory regularly on other threads.
  279. S_API void S_CALLTYPE SteamAPI_RunCallbacks();
  280.  
  281.  
  282. // Declares a callback member function plus a helper member variable which
  283. // registers the callback on object creation and unregisters on destruction.
  284. // The optional fourth 'var' param exists only for backwards-compatibility
  285. // and can be ignored.
  286. #define STEAM_CALLBACK( thisclass, func, .../*callback_type, [deprecated] var*/ ) \
  287.     _STEAM_CALLBACK_SELECT( ( __VA_ARGS__, 4, 3 ), ( /**/, thisclass, func, __VA_ARGS__ ) )
  288.  
  289. // Declares a callback function and a named CCallbackManual variable which
  290. // has Register and Unregister functions instead of automatic registration.
  291. #define STEAM_CALLBACK_MANUAL( thisclass, func, callback_type, var )    \
  292.     CCallbackManual< thisclass, callback_type > var; void func( callback_type *pParam )
  293.  
  294.  
  295. // Internal functions used by the utility CCallback objects to receive callbacks
  296. S_API void S_CALLTYPE SteamAPI_RegisterCallback( class CCallbackBase *pCallback, int iCallback );
  297. S_API void S_CALLTYPE SteamAPI_UnregisterCallback( class CCallbackBase *pCallback );
  298. // Internal functions used by the utility CCallResult objects to receive async call results
  299. S_API void S_CALLTYPE SteamAPI_RegisterCallResult( class CCallbackBase *pCallback, SteamAPICall_t hAPICall );
  300. S_API void S_CALLTYPE SteamAPI_UnregisterCallResult( class CCallbackBase *pCallback, SteamAPICall_t hAPICall );
  301.  
  302.  
  303. //-----------------------------------------------------------------------------
  304. // Purpose: base for callbacks and call results - internal implementation detail
  305. //-----------------------------------------------------------------------------
  306. class CCallbackBase
  307. {
  308. public:
  309.     CCallbackBase() { m_nCallbackFlags = 0; m_iCallback = 0; }
  310.     // don't add a virtual destructor because we export this binary interface across dll's
  311.     virtual void Run( void *pvParam ) = 0;
  312.     virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall ) = 0;
  313.     int GetICallback() { return m_iCallback; }
  314.     virtual int GetCallbackSizeBytes() = 0;
  315.  
  316. protected:
  317.     enum { k_ECallbackFlagsRegistered = 0x01, k_ECallbackFlagsGameServer = 0x02 };
  318.     uint8 m_nCallbackFlags;
  319.     int m_iCallback;
  320.     friend class CCallbackMgr;
  321.  
  322. private:
  323.     CCallbackBase( const CCallbackBase& );
  324.     CCallbackBase& operator=( const CCallbackBase& );
  325. };
  326.  
  327. //-----------------------------------------------------------------------------
  328. // Purpose: templated base for callbacks - internal implementation detail
  329. //-----------------------------------------------------------------------------
  330. template< int sizeof_P >
  331. class CCallbackImpl : protected CCallbackBase
  332. {
  333. public:
  334.     ~CCallbackImpl() { if ( m_nCallbackFlags & k_ECallbackFlagsRegistered ) SteamAPI_UnregisterCallback( this ); }
  335.     void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; }
  336.  
  337. protected:
  338.     virtual void Run( void *pvParam ) = 0;
  339.     virtual void Run( void *pvParam, bool /*bIOFailure*/, SteamAPICall_t /*hSteamAPICall*/ ) { Run( pvParam ); }
  340.     virtual int GetCallbackSizeBytes() { return sizeof_P; }
  341. };
  342.  
  343.  
  344. //-----------------------------------------------------------------------------
  345. // Purpose: maps a steam async call result to a class member function
  346. //          template params: T = local class, P = parameter struct
  347. //-----------------------------------------------------------------------------
  348. template< class T, class P >
  349. class CCallResult : private CCallbackBase
  350. {
  351. public:
  352.     typedef void (T::*func_t)( P*, bool );
  353.  
  354.     CCallResult();
  355.     ~CCallResult();
  356.    
  357.     void Set( SteamAPICall_t hAPICall, T *p, func_t func );
  358.     bool IsActive() const;
  359.     void Cancel();
  360.  
  361.     void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; }
  362. private:
  363.     virtual void Run( void *pvParam );
  364.     virtual void Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall );
  365.     virtual int GetCallbackSizeBytes() { return sizeof( P ); }
  366.  
  367.     SteamAPICall_t m_hAPICall;
  368.     T *m_pObj;
  369.     func_t m_Func;
  370. };
  371.  
  372.  
  373.  
  374. //-----------------------------------------------------------------------------
  375. // Purpose: maps a steam callback to a class member function
  376. //          template params: T = local class, P = parameter struct,
  377. //          bGameserver = listen for gameserver callbacks instead of client callbacks
  378. //-----------------------------------------------------------------------------
  379. template< class T, class P, bool bGameserver = false >
  380. class CCallback : public CCallbackImpl< sizeof( P ) >
  381. {
  382. public:
  383.     typedef void (T::*func_t)(P*);
  384.  
  385.     // NOTE: If you can't provide the correct parameters at construction time, you should
  386.     // use the CCallbackManual callback object (STEAM_CALLBACK_MANUAL macro) instead.
  387.     CCallback( T *pObj, func_t func );
  388.  
  389.     void Register( T *pObj, func_t func );
  390.     void Unregister();
  391.  
  392. protected:
  393.     virtual void Run( void *pvParam );
  394.    
  395.     T *m_pObj;
  396.     func_t m_Func;
  397. };
  398.  
  399.  
  400. //-----------------------------------------------------------------------------
  401. // Purpose: subclass of CCallback which allows default-construction in
  402. //          an unregistered state; you must call Register manually
  403. //-----------------------------------------------------------------------------
  404. template< class T, class P, bool bGameServer = false >
  405. class CCallbackManual : public CCallback< T, P, bGameServer >
  406. {
  407. public:
  408.     CCallbackManual() : CCallback< T, P, bGameServer >( NULL, NULL ) {}
  409.  
  410.     // Inherits public Register and Unregister functions from base class
  411. };
  412.  
  413.  
  414.  
  415. #ifdef _WIN32
  416. // disable this warning; this pattern need for steam callback registration
  417. #pragma warning( disable: 4355 )    // 'this' : used in base member initializer list
  418. #endif
  419.  
  420.  
  421. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  422. //  steamclient.dll private wrapper functions
  423. //
  424. //  The following functions are part of abstracting API access to the steamclient.dll, but should only be used in very specific cases
  425. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  426.  
  427. // SteamAPI_IsSteamRunning() returns true if Steam is currently running
  428. S_API bool S_CALLTYPE SteamAPI_IsSteamRunning();
  429.  
  430. // Pumps out all the steam messages, calling registered callbacks.
  431. // NOT THREADSAFE - do not call from multiple threads simultaneously.
  432. S_API void Steam_RunCallbacks( HSteamPipe hSteamPipe, bool bGameServerCallbacks );
  433.  
  434. // register the callback funcs to use to interact with the steam dll
  435. S_API void Steam_RegisterInterfaceFuncs( void *hModule );
  436.  
  437. // returns the HSteamUser of the last user to dispatch a callback
  438. S_API HSteamUser Steam_GetHSteamUserCurrent();
  439.  
  440. // returns the filename path of the current running Steam process, used if you need to load an explicit steam dll by name.
  441. // 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
  442. S_API const char *SteamAPI_GetSteamInstallPath();
  443.  
  444. // returns the pipe we are communicating to Steam with
  445. S_API HSteamPipe SteamAPI_GetHSteamPipe();
  446.  
  447. // sets whether or not Steam_RunCallbacks() should do a try {} catch (...) {} around calls to issuing callbacks
  448. S_API void SteamAPI_SetTryCatchCallbacks( bool bTryCatchCallbacks );
  449.  
  450. // backwards compat export, passes through to SteamAPI_ variants
  451. S_API HSteamPipe GetHSteamPipe();
  452. S_API HSteamUser GetHSteamUser();
  453.  
  454. +/
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465. /+ +++++++++++++++++++++ from steam_api_internal.h:
  466.  
  467.  
  468. //-----------------------------------------------------------------------------
  469. // The following macros are implementation details, not intended for public use
  470. //-----------------------------------------------------------------------------
  471. #define _STEAM_CALLBACK_AUTO_HOOK( thisclass, func, param )
  472. #define _STEAM_CALLBACK_HELPER( _1, _2, SELECTED, ... )     _STEAM_CALLBACK_##SELECTED
  473. #define _STEAM_CALLBACK_SELECT( X, Y )                      _STEAM_CALLBACK_HELPER X Y
  474. #define _STEAM_CALLBACK_3( extra_code, thisclass, func, param ) \
  475.     struct CCallbackInternal_ ## func : private CCallbackImpl< sizeof( param ) > { \
  476.         CCallbackInternal_ ## func () { extra_code SteamAPI_RegisterCallback( this, param::k_iCallback ); } \
  477.         CCallbackInternal_ ## func ( const CCallbackInternal_ ## func & ) { extra_code SteamAPI_RegisterCallback( this, param::k_iCallback ); } \
  478.         CCallbackInternal_ ## func & operator=( const CCallbackInternal_ ## func & ) { return *this; } \
  479.         private: virtual void Run( void *pvParam ) { _STEAM_CALLBACK_AUTO_HOOK( thisclass, func, param ) \
  480.             thisclass *pOuter = reinterpret_cast<thisclass*>( reinterpret_cast<char*>(this) - offsetof( thisclass, m_steamcallback_ ## func ) ); \
  481.             pOuter->func( reinterpret_cast<param*>( pvParam ) ); \
  482.         } \
  483.     } m_steamcallback_ ## func ; void func( param *pParam )
  484. #define _STEAM_CALLBACK_4( _, thisclass, func, param, var ) \
  485.     CCallback< thisclass, param > var; void func( param *pParam )
  486.  
  487.  
  488. //-----------------------------------------------------------------------------
  489. // Purpose: maps a steam async call result to a class member function
  490. //          template params: T = local class, P = parameter struct
  491. //-----------------------------------------------------------------------------
  492. template< class T, class P >
  493. inline CCallResult<T, P>::CCallResult()
  494. {
  495.     m_hAPICall = k_uAPICallInvalid;
  496.     m_pObj = NULL;
  497.     m_Func = NULL;
  498.     m_iCallback = P::k_iCallback;
  499. }
  500.  
  501. template< class T, class P >
  502. inline void CCallResult<T, P>::Set( SteamAPICall_t hAPICall, T *p, func_t func )
  503. {
  504.     if ( m_hAPICall )
  505.         SteamAPI_UnregisterCallResult( this, m_hAPICall );
  506.  
  507.     m_hAPICall = hAPICall;
  508.     m_pObj = p;
  509.     m_Func = func;
  510.  
  511.     if ( hAPICall )
  512.         SteamAPI_RegisterCallResult( this, hAPICall );
  513. }
  514.  
  515. template< class T, class P >
  516. inline bool CCallResult<T, P>::IsActive() const
  517. {
  518.     return (m_hAPICall != k_uAPICallInvalid);
  519. }
  520.  
  521. template< class T, class P >
  522. inline void CCallResult<T, P>::Cancel()
  523. {
  524.     if ( m_hAPICall != k_uAPICallInvalid )
  525.     {
  526.         SteamAPI_UnregisterCallResult( this, m_hAPICall );
  527.         m_hAPICall = k_uAPICallInvalid;
  528.     }
  529.  
  530. }
  531.  
  532. template< class T, class P >
  533. inline CCallResult<T, P>::~CCallResult()
  534. {
  535.     Cancel();
  536. }
  537.  
  538. template< class T, class P >
  539. inline void CCallResult<T, P>::Run( void *pvParam )
  540. {
  541.     m_hAPICall = k_uAPICallInvalid; // caller unregisters for us
  542.     (m_pObj->*m_Func)((P *)pvParam, false);
  543. }
  544.  
  545. template< class T, class P >
  546. inline void CCallResult<T, P>::Run( void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall )
  547. {
  548.     if ( hSteamAPICall == m_hAPICall )
  549.     {
  550.         m_hAPICall = k_uAPICallInvalid; // caller unregisters for us
  551.         (m_pObj->*m_Func)((P *)pvParam, bIOFailure);
  552.     }
  553. }
  554.  
  555.  
  556. //-----------------------------------------------------------------------------
  557. // Purpose: maps a steam callback to a class member function
  558. //          template params: T = local class, P = parameter struct,
  559. //          bGameserver = listen for gameserver callbacks instead of client callbacks
  560. //-----------------------------------------------------------------------------
  561. template< class T, class P, bool bGameserver >
  562. inline CCallback< T, P, bGameserver >::CCallback( T *pObj, func_t func )
  563.     : m_pObj( NULL ), m_Func( NULL )
  564. {
  565.     if ( bGameserver )
  566.     {
  567.         this->SetGameserverFlag();
  568.     }
  569.     Register( pObj, func );
  570. }
  571.  
  572. template< class T, class P, bool bGameserver >
  573. inline void CCallback< T, P, bGameserver >::Register( T *pObj, func_t func )
  574. {
  575.     if ( !pObj || !func )
  576.         return;
  577.  
  578.     if ( this->m_nCallbackFlags & CCallbackBase::k_ECallbackFlagsRegistered )
  579.         Unregister();
  580.  
  581.     m_pObj = pObj;
  582.     m_Func = func;
  583.     // SteamAPI_RegisterCallback sets k_ECallbackFlagsRegistered
  584.     SteamAPI_RegisterCallback( this, P::k_iCallback );
  585. }
  586.  
  587. template< class T, class P, bool bGameserver >
  588. inline void CCallback< T, P, bGameserver >::Unregister()
  589. {
  590.     // SteamAPI_UnregisterCallback removes k_ECallbackFlagsRegistered
  591.     SteamAPI_UnregisterCallback( this );
  592. }
  593.  
  594. template< class T, class P, bool bGameserver >
  595. inline void CCallback< T, P, bGameserver >::Run( void *pvParam )
  596. {
  597.     (m_pObj->*m_Func)((P *)pvParam);
  598. }
  599.  
  600.  
  601. +/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement