Game Development Community

dev|Pro Game Development Curriculum

X-Fire Integration continued

by Skylar Kelty · 05/29/2006 (11:53 am) · 6 comments

X-Fire is now working in my game (at least I think it is, I have no way of testing it right now)

I can get it to compile and start missions, so I think its all good ;)

Code says it all so here goes (once I have tested it this will go up as a resource):

New File (game/net/xfire.cc)
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <tlhelp32.h>

#include "xfire.h"

static HMODULE g_toucan_dll = NULL;
static void HelperInit();
static HMODULE HelperGetToucanDLL();

typedef int (*XfireSetCustomGameDataWFunction)(int , const wchar_t **, const wchar_t **);

static XfireSetCustomGameDataWFunction ptr_XfireSetCustomGameDataW = NULL;

/* make sure we are going to call the ANSI version */
#ifdef MODULEENTRY32
#undef MODULEENTRY32
#endif

#ifdef Module32First
#undef Module32First
#endif

#ifdef Module32Next
#undef Module32Next
#endif 


int XfireIsLoaded()
{
	HelperInit();
	if (ptr_XfireSetCustomGameDataW)
		return 1;
	return 0;
}

int XfireSetCustomGameDataW(int num_keys, const wchar_t **keys, const wchar_t **values)
{
	HelperInit();
	if (ptr_XfireSetCustomGameDataW)
		return ptr_XfireSetCustomGameDataW(num_keys, keys, values);
	return 1;
}

/* ------------------------------------------------------------------------- */
static void HelperInit()
{
	if (!ptr_XfireSetCustomGameDataW)
	{
		HMODULE toucan_dll = HelperGetToucanDLL();
		if (toucan_dll)
		{
			ptr_XfireSetCustomGameDataW = (XfireSetCustomGameDataWFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataW_V1");
	   }
	}
}


static HMODULE HelperGetToucanDLL()
{
	if (g_toucan_dll)
		return g_toucan_dll;

	/*
	** We need to enumerate the DLLs loaded to find toucan dll.
	** This is done because the toucan dll changes with each update.
	** The toucan dll has the following format. "xfire_toucan_{BUILD_NUMBER}.dll"
	** We simply try to find a dll w/ the prefix "xfire_toucan"
	*/
	HANDLE snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
	if (snapshot_handle != INVALID_HANDLE_VALUE)
	{
		MODULEENTRY32 module_entry;
		module_entry.dwSize = sizeof(MODULEENTRY32); 

		BOOL result = Module32First(snapshot_handle, &module_entry);
		char module_name[] = "xfire_toucan";
		DWORD module_name_len = sizeof(module_name)-1;
		while (result)
		{
			if (CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, module_entry.szModule, module_name_len, module_name, module_name_len) == CSTR_EQUAL)
			{
				g_toucan_dll = module_entry.hModule;
				break;
			}
			result = Module32Next(snapshot_handle, &module_entry);
		}

		CloseHandle(snapshot_handle);
	}

	return g_toucan_dll;
}
That contains some unessesary stuff i think

New File (game/net/xfire.h)
#ifndef __XFIREGAMECLIENT_H__
#define __XFIREGAMECLIENT_H__

#ifdef __cplusplus
extern "C" {
#endif

/*
**  XfireIsLoaded()
**
**  returns 1 if application can talk to Xfire, 0 otherwise
*/
int XfireIsLoaded();

/*
**  XfireSetCustomGameDataA()
**
**  UNICODE version to tell xfire of custom game data
*/
int XfireSetCustomGameDataW(int num_keys, const wchar_t **keys, const wchar_t **values);

#define XfireSetCustomGameData XfireSetCustomGameDataW

#ifdef __cplusplus
}
#endif

#endif /* __XFIREGAMECLIENT_H__ */

That probably has some unnecessary stuff too

Now In player.cc
//-----------------------------------------------------
   // X-Fire
   //-----------------------------------------------------
     #include "game/net/xfire.h"
   //-----------------------------------------------------
   // X-Fire
   //-----------------------------------------------------

// Anchor point compression
const F32 sAnchorMaxDistance = 32;

   //-----------------------------------------------------
   // X-Fire
   //-----------------------------------------------------
     const char *key[100],*value[100];
   //-----------------------------------------------------
   // X-Fire
   //-----------------------------------------------------

bool Player::onAdd()
{
   //-----------------------------------------------------
   // X-Fire
   //-----------------------------------------------------
key[0] = "Status";
value[0] = "Playing";
   //-----------------------------------------------------
   // X-Fire
   //-----------------------------------------------------


Theres a lot more, but that is the basic idea, like I said when I have it all together and i've tested it I will submit it as a resource

#1
05/29/2006 (11:54 am)
Thinking about it I don't need the
ifdef unicode
stuff, it will be defined in 1.4

Updated blog to take out non-unicode stuff

Could you please tell me how bad my coding is and what (if anything) I've done wrong
#2
05/29/2006 (12:14 pm)
Have you got you're game showing up on the xfire list?
#3
05/29/2006 (12:16 pm)
Yes
#4
05/30/2006 (2:42 am)
Nice work...
I'll try this out sometime
DHL:I should have this ;)
#5
05/30/2006 (3:24 am)
Er this isnt the complete list of changes nor will your game show up in xfire, it is a starter to get you going
#6
02/28/2009 (4:49 pm)
¿ Xfire works in TGE or only TGEA (directx) ?