[ create a new paste ] login | about

Link: http://codepad.org/6gFXtNLi    [ raw code | fork ]

C++, pasted on Oct 5:
#include "SteamAPI.h"

#include <iomanip>
#include <ctime>
#include <sstream>


void Error(std::string msg)
{
	std::cout << "Error: " << msg << std::endl;

	getchar();
	exit(EXIT_FAILURE);
}
void Error(std::string msg, int errorCode)
{
	std::cout << "Error: " << msg << " Error code: " << errorCode << std::endl;

	getchar();
	exit(EXIT_FAILURE);
}


int main()
{
	SetDllDirectory("C:\\Program Files\\Steam\\");

	eInitError errorCode;
	if (!Steamworks_Init(eInitSteamClient, errorCode))
	{
		std::string errMsg = "Unable to init steamworks : ";
		errMsg += EnumString<eInitError>::From(errorCode);
		errMsg += "\n";

		Error(errMsg);
	}

	int error;
	ISteamClient008 *steamClient = (ISteamClient008 *)CreateInterface(STEAMCLIENT_INTERFACE_VERSION_008, &error);
	if (!steamClient)
		Error("Unable to get steam client.", error);

	HSteamPipe hSteamPipe = steamClient->CreateSteamPipe();
	HSteamUser hSteamUser = steamClient->ConnectToGlobalUser(hSteamPipe);

	ISteamFriends001 *steamFriends = (ISteamFriends001 *)steamClient->GetISteamFriends( hSteamUser, hSteamPipe, STEAMFRIENDS_INTERFACE_VERSION_001 );
	ISteamUser012 *steamUser = (ISteamUser012 *)steamClient->GetISteamUser(hSteamUser, hSteamPipe, STEAMUSER_INTERFACE_VERSION_012);
	CSteamID *cID = &( steamUser->GetSteamID() );
	uint64 ownID = (uint64)(*cID).ConvertToUint64();

	HSteamCall hSteamCall;
	CallbackMsg_t CallbackMsg;
	EPersonaState state = (EPersonaState)8;
	steamFriends->SetPersonaState(state);

	while (!GetAsyncKeyState(VK_ESCAPE)) // loop until we escape
	{
		if (Steam_BGetCallback(hSteamPipe, &CallbackMsg, &hSteamCall))
		{
			if (CallbackMsg.m_iCallback == FriendChatMsg_t::k_iCallback)
			{
				FriendChatMsg_t *chatMsg = (FriendChatMsg_t *)CallbackMsg.m_pubParam;
				EFriendMsgType msgType;

				// allocate data for message
				char *pvData = new char[256];
				memset(pvData, 0, 256);

				// i have no clue if steam dellocates this or if we should
				const char *friendName = steamFriends->GetFriendPersonaName(chatMsg->m_ulOtherID);
				steamFriends->GetChatMessage(chatMsg->m_ulSteamID, chatMsg->m_iChatID, pvData, 256, &msgType);

				if (msgType & k_EFriendMsgTypeTyping)
				{
					std::cout << "* " << friendName << " is typing a message..." << std::endl;
				}
				else
				{
					std::cout << "* Message from " << friendName << ": " << pvData << std::endl;
					
					if (chatMsg->m_ulOtherID != ownID)
					{
						EFriendMsgType msgType = (EFriendMsgType)k_EFriendMsgTypeChat;
						steamFriends->SendMsgToFriend(chatMsg->m_ulOtherID, msgType, "I'm eating, brb");
					}

				}

				// clean up
				delete [] pvData;

			}

			Steam_FreeLastCallback( hSteamPipe );
		}
		
		Sleep(1);
	}

	Steamworks_Shutdown();

	return 0;
}


Create a new paste based on this one


Comments: