Guest User

Untitled

a guest
Jul 5th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #define STEAMWORKS_CLIENT_INTERFACES
  2.  
  3. #include "Steamworks.h"
  4.  
  5. #pragma comment( lib, "../Resources/Libs/Win32/steamclient.lib" )
  6.  
  7. CSteamAPILoader loader;
  8.  
  9. int main()
  10. {
  11. CreateInterfaceFn factory = loader.GetSteam3Factory();
  12.  
  13. if ( !factory )
  14. {
  15. fprintf(stderr, "Unable to load steamclient factory.\n");
  16. return 1;
  17. }
  18.  
  19. IClientEngine *pClientEngine = (IClientEngine *)factory( CLIENTENGINE_INTERFACE_VERSION, NULL );
  20. if ( !pClientEngine )
  21. {
  22. fprintf(stderr, "Unable to get the client engine.\n");
  23. return 1;
  24. }
  25.  
  26. HSteamPipe hPipe;
  27. HSteamUser hUser = pClientEngine->CreateGlobalUser( &hPipe );
  28. if ( !hUser || !hPipe )
  29. {
  30. fprintf(stderr, "Unable to create the global user.\n");
  31. return 1;
  32. }
  33.  
  34. IClientUser *pClientUser = (IClientUser *)pClientEngine->GetIClientUser( hUser, hPipe, CLIENTUSER_INTERFACE_VERSION );
  35. if ( !pClientUser )
  36. {
  37. fprintf(stderr, "Unable to get the client user interface.\n");
  38. pClientEngine->ReleaseUser(hPipe, hUser);
  39. pClientEngine->BReleaseSteamPipe(hPipe);
  40. return 1;
  41. }
  42.  
  43. IClientFriends *pClientFriends = (IClientFriends *)pClientEngine->GetIClientFriends( hUser, hPipe, CLIENTFRIENDS_INTERFACE_VERSION );
  44. if ( !pClientFriends )
  45. {
  46. fprintf(stderr, "Unable to get the client friends interface.\n");
  47. pClientEngine->ReleaseUser(hPipe, hUser);
  48. pClientEngine->BReleaseSteamPipe(hPipe);
  49. return 1;
  50. }
  51.  
  52. IClientUtils *pClientUtils = (IClientUtils *)pClientEngine->GetIClientUtils( hPipe, CLIENTUTILS_INTERFACE_VERSION );
  53. if ( !pClientUtils )
  54. {
  55. fprintf(stderr, "Unable to get the client utils interface.\n");
  56. pClientEngine->ReleaseUser(hPipe, hUser);
  57. pClientEngine->BReleaseSteamPipe(hPipe);
  58. return 1;
  59. }
  60.  
  61. char szUsername[128] = "";
  62. char szPassword[128] = "";
  63.  
  64. printf("Enter your username: ");
  65. if(fgets(szUsername, sizeof(szUsername), stdin))
  66. {
  67. szUsername[strlen(szUsername) - 1] = 0;
  68. }
  69.  
  70. printf("Enter your password: ");
  71. if(fgets(szPassword, sizeof(szPassword), stdin))
  72. {
  73. szPassword[strlen(szPassword) - 1] = 0;
  74. }
  75.  
  76. pClientUser->LogOnWithPassword(false, szUsername, szPassword);
  77.  
  78. CallbackMsg_t callBack;
  79. CSteamID adminID;
  80.  
  81. while ( true )
  82. {
  83. if ( Steam_BGetCallback( hPipe, &callBack ) )
  84. {
  85. switch (callBack.m_iCallback)
  86. {
  87. case SteamServersConnected_t::k_iCallback:
  88. {
  89. printf("Successfully logged on!\n");
  90.  
  91. pClientUser->SetSelfAsPrimaryChatDestination();
  92. pClientFriends->SetPersonaState( k_EPersonaStateOnline );
  93.  
  94. CSteamID friendID;
  95. for (int i = 0; i < pClientFriends->GetFriendCount(k_EFriendFlagImmediate); i++)
  96. {
  97. friendID = pClientFriends->GetFriendByIndex(i, k_EFriendFlagImmediate);
  98.  
  99. if(strcmp(pClientFriends->GetFriendPersonaName(friendID), "Didrole") == 0) // Put the persona name of the friend to use as the 'front end' here.
  100. {
  101. const char* cszMessage = "I'm logged on!";
  102. pClientFriends->SendMsgToFriend(friendID, k_EChatEntryTypeChatMsg, cszMessage, strlen(cszMessage));
  103. adminID = friendID;
  104.  
  105. break;
  106. }
  107. }
  108. break;
  109. }
  110. case AccountInformationUpdated_t::k_iCallback:
  111. {
  112. printf("AccountInformationUpdated_t\n");
  113.  
  114. SteamAPICall_t hCall = pClientUser->RequestWebAuthToken();
  115.  
  116. bool bFailed = false;
  117. while(!pClientUtils->IsAPICallCompleted(hCall, &bFailed) && !bFailed)
  118. {
  119. Sleep(100);
  120. }
  121.  
  122. WebAuthRequestCallback_t webAuthRequest;
  123. if(pClientUtils->GetAPICallResult(hCall, &webAuthRequest, sizeof(webAuthRequest), webAuthRequest.k_iCallback, &bFailed) && !bFailed && webAuthRequest.m_bSuccessful)
  124. {
  125. printf("Token: %s\n", webAuthRequest.m_rgchToken);
  126. }
  127. else
  128. {
  129. fprintf(stderr, "RequestWebAuthToken failed\n");
  130. }
  131.  
  132. break;
  133. }
  134. case SteamServerConnectFailure_t::k_iCallback:
  135. {
  136. SteamServerConnectFailure_t *pConnectFailureInfo = (SteamServerConnectFailure_t *)callBack.m_pubParam;
  137. fprintf(stderr, "Logon failed with eResult %u !\n", pConnectFailureInfo->m_eResult);
  138.  
  139. pClientEngine->ReleaseUser(hPipe, hUser);
  140. pClientEngine->BReleaseSteamPipe(hPipe);
  141.  
  142. return 1;
  143. }
  144.  
  145. case FriendChatMsg_t::k_iCallback:
  146. {
  147. FriendChatMsg_t *pFriendMessageInfo = (FriendChatMsg_t *)callBack.m_pubParam;
  148.  
  149. EChatEntryType eMsgType;
  150. CSteamID chatter;
  151.  
  152. char szData[k_cchFriendChatMsgMax];
  153. memset(szData, 0, k_cchFriendChatMsgMax);
  154.  
  155. int iLength = pClientFriends->GetChatMessage(pFriendMessageInfo->m_ulSenderID, pFriendMessageInfo->m_iChatID, szData, sizeof(szData), &eMsgType, &chatter);
  156.  
  157. if (eMsgType == k_EChatEntryTypeChatMsg || eMsgType == k_EChatEntryTypeEmote)
  158. {
  159. printf("Message from %s: %s\n", pFriendMessageInfo->m_ulSenderID.Render(), szData);
  160.  
  161. if (strcmp(szData, "quit") == 0 && pFriendMessageInfo->m_ulSenderID == adminID)
  162. {
  163. pClientFriends->SetPersonaState( k_EPersonaStateOffline );
  164. pClientUser->LogOff();
  165. pClientEngine->ReleaseUser(hPipe, hUser);
  166. pClientEngine->BReleaseSteamPipe(hPipe);
  167.  
  168. return 0;
  169. }
  170.  
  171. pClientFriends->SendMsgToFriend(pFriendMessageInfo->m_ulSenderID, eMsgType, szData, iLength);
  172. }
  173. break;
  174. }
  175. }
  176.  
  177. Steam_FreeLastCallback( hPipe );
  178. }
  179.  
  180. #ifdef _WIN32
  181. Sleep(10);
  182. #else
  183. usleep(10000);
  184. #endif
  185. }
  186.  
  187. return 0;
  188. }
Add Comment
Please, Sign In to add comment