Advertisement
Guest User

Untitled

a guest
May 29th, 2017
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.75 KB | None | 0 0
  1.  
  2. #include "Steamworks.h"
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7. #pragma comment( lib, "../steamclient" )
  8. #pragma comment( lib, "../steam" )
  9.  
  10.  
  11. void ShowError( TSteamError *err )
  12. {
  13.     if ( !IS_STEAM_ERROR( (*err) ) )
  14.         return;
  15.  
  16.     std::cout << "Error: " << err->szDesc << "\n";
  17.  
  18.     getchar();
  19.  
  20.     exit( 0 );
  21. }
  22.  
  23. int main()
  24. {
  25.     //CSteamAPILoader loader;
  26.     //CreateInterfaceFn factory = loader.Load();
  27.  
  28.     /*
  29.     HMODULE hModuleSteamClient = GetModuleHandleA("steamclient.dll");
  30.     if ( !hModuleSteamClient )
  31.         hModuleSteamClient = LoadLibraryExA("steamclient.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  32.     CreateInterfaceFn factory = (CreateInterfaceFn)(GetProcAddress(hModuleSteamClient, CREATEINTERFACE_PROCNAME));
  33.     */
  34. #if defined _WIN32
  35.     CreateInterfaceFn factory = (CreateInterfaceFn)GetProcAddress(GetModuleHandleA("steamclient.dll"), "CreateInterface");
  36. #elif defined _LINUX
  37.     void* steamclient_library = dlopen("steamclient.so", RTLD_LAZY);
  38.     CreateInterfaceFn factory = (CreateInterfaceFn)dlsym(steamclient_library, "CreateInterface");
  39.     dlclose(steamclient_library);
  40. #endif
  41.  
  42.     if ( !factory )
  43.     {
  44.         std::cout << "Unable to load steamclient factory." << std::endl;
  45.         return 0;
  46.     }
  47.  
  48.     ISteamClient009 *steamClient = (ISteamClient009 *)factory( STEAMCLIENT_INTERFACE_VERSION_009, NULL );
  49.     IClientEngine *clientEngine = (IClientEngine *)factory( CLIENTENGINE_INTERFACE_VERSION, NULL );
  50.  
  51.     HSteamPipe hPipe;
  52.     HSteamUser hUser = clientEngine->CreateGlobalUser( &hPipe );
  53.     if ( !hUser || !hPipe )
  54.     {
  55.         std::cout << "Unable to connect to global user." << std::endl;
  56.         return 0;
  57.     }
  58.  
  59.     IClientUser *clientUser = (IClientUser *)clientEngine->GetIClientUser( hUser, hPipe, CLIENTUSER_INTERFACE_VERSION );
  60.     IClientFriends *clientFriends = (IClientFriends *)clientEngine->GetIClientFriends( hUser, hPipe, CLIENTFRIENDS_INTERFACE_VERSION );
  61.     ISteam2Bridge002 *steam2Bridge = (ISteam2Bridge002 *)steamClient->GetISteamGenericInterface( hUser, hPipe, STEAM2BRIDGE_INTERFACE_VERSION_002 );
  62.  
  63.     TSteamError error;
  64.  
  65.     if ( !SteamStartEngine( &error ) )
  66.         ShowError( &error );
  67.  
  68.     if ( !SteamStartup( STEAM_USING_ALL, &error ) )
  69.         ShowError( &error );
  70.    
  71.     /*
  72.     HMODULE hModule = GetModuleHandle( "steam.dll" );
  73.     if ( !hModule )
  74.     {
  75.         std::cout << "Unable to get steam.dll module.\n";
  76.         return 0;
  77.     }
  78.  
  79.     FactoryFn fn = (FactoryFn)GetProcAddress( hModule, "_f" );
  80.     if ( !fn )
  81.     {
  82.         std::cout << "Unable to get factory.\n";
  83.         return 0;
  84.     }
  85.     */
  86.  
  87. #if defined _WIN32
  88.     FactoryFn fn = (FactoryFn)GetProcAddress(GetModuleHandleA("steam.dll"), "_f");
  89. #elif defined _LINUX
  90.     void* steam_library = dlopen("libsteam.so", RTLD_LAZY);
  91.     FactoryFn fn = (FactoryFn)dlsym(steam_library, "_f");
  92.     dlclose(steam_library);
  93. #endif
  94.  
  95.     ISteam006 *steam = (ISteam006*)fn( "Steam006" );
  96.     if ( !steam )
  97.     {
  98.         std::cout << "Unable to get ISteam006.\n";
  99.         return 0;
  100.     }
  101.  
  102.     std::string userName = "username"; // Username
  103.     std::string passWord = "password"; // Password
  104.  
  105.     //std::cout << "Username: ";
  106.     //std::cin >> userName;
  107.  
  108.     //std::cout << "Password: ";
  109.     //std::cin >> passWord;
  110.  
  111.     clientUser->SetLoginInformation( userName.c_str(), passWord.c_str(), false );
  112.  
  113.     SteamCallHandle_t handle = steam->Login( userName.c_str(), passWord.c_str(), 1, &error );
  114.     if ( handle == STEAM_INVALID_CALL_HANDLE )
  115.         ShowError( &error );
  116.  
  117.     if ( !steam->BlockingCall( handle, 100, &error ) )
  118.         ShowError( &error );
  119.  
  120.     char szUser[ 255 ];
  121.     uint32 userChars;
  122.     TSteamGlobalUserID globalId;
  123.  
  124.     if ( !steam->GetUser( szUser, sizeof( szUser ), &userChars, &globalId, &error ) )
  125.         ShowError( &error );
  126.  
  127.     char szEmailAddress[ 255 ];
  128.     uint32 emailChars;
  129.  
  130.     if ( !steam->GetCurrentEmailAddress( szEmailAddress, sizeof( szEmailAddress ), &emailChars, &error ) )
  131.         ShowError( &error );
  132.  
  133.     CSteamID steamId;
  134.     steamId.SetFromSteam2( &globalId, steam2Bridge->GetConnectedUniverse() );
  135.  
  136.     clientUser->SetEmail( szEmailAddress );
  137.  
  138.     clientUser->LogOn( steamId );
  139.  
  140.     ELogonState state = clientUser->GetLogonState();
  141.  
  142.     while ( state == k_ELogonStateLoggingOn )
  143.     {
  144.         state = clientUser->GetLogonState();
  145.     }
  146.  
  147.     std::cout << "Logged on!\n";
  148.  
  149.     CallbackMsg_t callBack;
  150.     CSteamID adminID;
  151.  
  152.     while ( true )
  153.     {
  154.         if ( Steam_BGetCallback( hPipe, &callBack ) )
  155.         {
  156.             switch (callBack.m_iCallback)
  157.             {
  158.             case SteamServersConnected_t::k_iCallback:
  159.                 {
  160.                     std::cout << "Successfully logged on!" << std::endl;
  161.  
  162.                     clientUser->SetComputerInUse();
  163.                     clientUser->SetSelfAsPrimaryChatDestination();
  164.                     clientFriends->SetPersonaState( k_EPersonaStateOnline );
  165.  
  166.                     CSteamID friendID;
  167.                     for (int i = 0; i < clientFriends->GetFriendCount(k_EFriendFlagImmediate); i++)
  168.                     {
  169.                         friendID = clientFriends->GetFriendByIndex(i, k_EFriendFlagImmediate);
  170.  
  171.                         if (strcmp(clientFriends->GetFriendPersonaName(friendID), ":MIB: Asherkin") != 0) // Put the persona name of the friend to use as the 'front end' here.
  172.                             continue;
  173.  
  174.                         clientFriends->SendMsgToFriend(friendID, k_EChatEntryTypeChatMsg, "I'm logged on!", 15);
  175.                         adminID = friendID;
  176.                     }
  177.                     break;
  178.                 }
  179.  
  180.             case FriendChatMsg_t::k_iCallback:
  181.                 {
  182.                     FriendChatMsg_t *friendMessageInfo = (FriendChatMsg_t *)callBack.m_pubParam;
  183.  
  184.                     EChatEntryType msgType;
  185.                     CSteamID chatter;
  186.  
  187.                     // allocate data for message  
  188.                     char *pvData = new char[2049];  
  189.                     memset(pvData, 0, 2049);  
  190.  
  191.                     int length = clientFriends->GetChatMessage(friendMessageInfo->m_ulSender, friendMessageInfo->m_iChatID, pvData, 2049, &msgType, &chatter);  
  192.  
  193.                     if (msgType & k_EChatEntryTypeChatMsg || msgType & k_EChatEntryTypeEmote)  
  194.                     {  
  195.                         std::cout << "Message from " << friendMessageInfo->m_ulSender.Render() << ": " << pvData << std::endl;
  196.  
  197.                         if (friendMessageInfo->m_ulSender == adminID && strcmp(pvData, "quit") == 0)
  198.                         {
  199.                             clientFriends->SetPersonaState( k_EPersonaStateOffline );
  200.                             clientUser->LogOff();
  201.                             steam->Logout(&error);
  202.                                
  203.                             while ( state == k_ELogonStateLoggingOff || state == k_ELogonStateLoggedOn )
  204.                             {
  205.                                 state = clientUser->GetLogonState();
  206.                             }
  207.  
  208.                             exit( 0 );
  209.                         }
  210.  
  211.                         clientFriends->SendMsgToFriend(friendMessageInfo->m_ulSender, msgType, pvData, length);
  212.                     }  
  213.  
  214.                     // clean up  
  215.                     delete [] pvData;
  216.  
  217.                     break;
  218.                 }
  219.             }
  220.  
  221.             /*std::cout << std::dec;
  222.             std::cout << "Callback: " << callBack.m_iCallback << " Size: " << callBack.m_cubParam << "\n\t";
  223.  
  224.            
  225.             for ( int i = 0; i < callBack.m_cubParam; i++ )
  226.             {
  227.                 unsigned char value = callBack.m_pubParam[ i ];
  228.  
  229.                 std::cout << std::hex << std::setw( 2 ) << std::setfill( '0' ) << std::uppercase << (unsigned int)value;
  230.                 std::cout << " ";
  231.             }
  232.  
  233.             std::cout << "\n";
  234.             */
  235.  
  236.             Steam_FreeLastCallback( hPipe );
  237.         }
  238.  
  239. #if defined _WIN32
  240.         Sleep(10);
  241. #elif defined _LINUX
  242.         sleep(10);
  243. #endif
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement