misc/xfire/xfiregameclient.cpp
author koda
Sat, 08 Oct 2011 13:39:40 +0200
changeset 6100 e6426c6b2882
parent 3697 d5b30d6373fc
permissions -rw-r--r--
sed commands should be tested beforehand
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2920
5cf48744a700 Engine:
smxx
parents: 2830
diff changeset
     1
/* This file is NOT open source. See "license.txt" to read the full license provided with the Xfire SDK. */
2830
571a9deffeac Frontend:
smxx
parents:
diff changeset
     2
571a9deffeac Frontend:
smxx
parents:
diff changeset
     3
#define WIN32_LEAN_AND_MEAN
571a9deffeac Frontend:
smxx
parents:
diff changeset
     4
#include <windows.h>
571a9deffeac Frontend:
smxx
parents:
diff changeset
     5
#include <tlhelp32.h>
571a9deffeac Frontend:
smxx
parents:
diff changeset
     6
571a9deffeac Frontend:
smxx
parents:
diff changeset
     7
#include "xfiregameclient.h"
571a9deffeac Frontend:
smxx
parents:
diff changeset
     8
571a9deffeac Frontend:
smxx
parents:
diff changeset
     9
static HMODULE g_toucan_dll = NULL;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    10
static void HelperInit();
571a9deffeac Frontend:
smxx
parents:
diff changeset
    11
static HMODULE HelperGetToucanDLL();
571a9deffeac Frontend:
smxx
parents:
diff changeset
    12
571a9deffeac Frontend:
smxx
parents:
diff changeset
    13
typedef int (*XfireSetCustomGameDataAFunction)(int , const char **, const char **);
571a9deffeac Frontend:
smxx
parents:
diff changeset
    14
typedef int (*XfireSetCustomGameDataWFunction)(int , const wchar_t **, const wchar_t **);
571a9deffeac Frontend:
smxx
parents:
diff changeset
    15
typedef int (*XfireSetCustomGameDataUTF8Function)(int , const char **, const char **);
571a9deffeac Frontend:
smxx
parents:
diff changeset
    16
571a9deffeac Frontend:
smxx
parents:
diff changeset
    17
static XfireSetCustomGameDataAFunction ptr_XfireSetCustomGameDataA = NULL;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    18
static XfireSetCustomGameDataWFunction ptr_XfireSetCustomGameDataW = NULL;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    19
static XfireSetCustomGameDataUTF8Function ptr_XfireSetCustomGameDataUTF8 = NULL;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    20
571a9deffeac Frontend:
smxx
parents:
diff changeset
    21
/* make sure we are going to call the ANSI version */
571a9deffeac Frontend:
smxx
parents:
diff changeset
    22
#ifdef MODULEENTRY32
571a9deffeac Frontend:
smxx
parents:
diff changeset
    23
#undef MODULEENTRY32
571a9deffeac Frontend:
smxx
parents:
diff changeset
    24
#endif
571a9deffeac Frontend:
smxx
parents:
diff changeset
    25
571a9deffeac Frontend:
smxx
parents:
diff changeset
    26
#ifdef Module32First
571a9deffeac Frontend:
smxx
parents:
diff changeset
    27
#undef Module32First
571a9deffeac Frontend:
smxx
parents:
diff changeset
    28
#endif
571a9deffeac Frontend:
smxx
parents:
diff changeset
    29
571a9deffeac Frontend:
smxx
parents:
diff changeset
    30
#ifdef Module32Next
571a9deffeac Frontend:
smxx
parents:
diff changeset
    31
#undef Module32Next
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2920
diff changeset
    32
#endif
2830
571a9deffeac Frontend:
smxx
parents:
diff changeset
    33
571a9deffeac Frontend:
smxx
parents:
diff changeset
    34
571a9deffeac Frontend:
smxx
parents:
diff changeset
    35
int XfireIsLoaded()
571a9deffeac Frontend:
smxx
parents:
diff changeset
    36
{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    37
	HelperInit();
571a9deffeac Frontend:
smxx
parents:
diff changeset
    38
	if (ptr_XfireSetCustomGameDataA &&
571a9deffeac Frontend:
smxx
parents:
diff changeset
    39
		ptr_XfireSetCustomGameDataW &&
571a9deffeac Frontend:
smxx
parents:
diff changeset
    40
		ptr_XfireSetCustomGameDataUTF8)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    41
		return 1;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    42
	return 0;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    43
}
571a9deffeac Frontend:
smxx
parents:
diff changeset
    44
571a9deffeac Frontend:
smxx
parents:
diff changeset
    45
int XfireSetCustomGameDataA(int num_keys, const char **keys, const char **values)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    46
{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    47
	HelperInit();
571a9deffeac Frontend:
smxx
parents:
diff changeset
    48
	if (ptr_XfireSetCustomGameDataA)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    49
		return ptr_XfireSetCustomGameDataA(num_keys, keys, values);
571a9deffeac Frontend:
smxx
parents:
diff changeset
    50
	return 1;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    51
}
571a9deffeac Frontend:
smxx
parents:
diff changeset
    52
571a9deffeac Frontend:
smxx
parents:
diff changeset
    53
int XfireSetCustomGameDataW(int num_keys, const wchar_t **keys, const wchar_t **values)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    54
{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    55
	HelperInit();
571a9deffeac Frontend:
smxx
parents:
diff changeset
    56
	if (ptr_XfireSetCustomGameDataW)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    57
		return ptr_XfireSetCustomGameDataW(num_keys, keys, values);
571a9deffeac Frontend:
smxx
parents:
diff changeset
    58
	return 1;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    59
}
571a9deffeac Frontend:
smxx
parents:
diff changeset
    60
571a9deffeac Frontend:
smxx
parents:
diff changeset
    61
int XfireSetCustomGameDataUTF8(int num_keys, const char **keys, const char **values)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    62
{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    63
	HelperInit();
571a9deffeac Frontend:
smxx
parents:
diff changeset
    64
	if (ptr_XfireSetCustomGameDataUTF8)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    65
		return ptr_XfireSetCustomGameDataUTF8(num_keys, keys, values);
571a9deffeac Frontend:
smxx
parents:
diff changeset
    66
	return 1;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    67
}
571a9deffeac Frontend:
smxx
parents:
diff changeset
    68
571a9deffeac Frontend:
smxx
parents:
diff changeset
    69
/* ------------------------------------------------------------------------- */
571a9deffeac Frontend:
smxx
parents:
diff changeset
    70
static void HelperInit()
571a9deffeac Frontend:
smxx
parents:
diff changeset
    71
{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    72
	if (!ptr_XfireSetCustomGameDataA ||
571a9deffeac Frontend:
smxx
parents:
diff changeset
    73
		!ptr_XfireSetCustomGameDataW ||
571a9deffeac Frontend:
smxx
parents:
diff changeset
    74
		!ptr_XfireSetCustomGameDataUTF8)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    75
	{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    76
		HMODULE toucan_dll = HelperGetToucanDLL();
571a9deffeac Frontend:
smxx
parents:
diff changeset
    77
		if (toucan_dll)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    78
		{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    79
			ptr_XfireSetCustomGameDataA = (XfireSetCustomGameDataAFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataA_V1");
571a9deffeac Frontend:
smxx
parents:
diff changeset
    80
			ptr_XfireSetCustomGameDataW = (XfireSetCustomGameDataWFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataW_V1");
571a9deffeac Frontend:
smxx
parents:
diff changeset
    81
			ptr_XfireSetCustomGameDataUTF8 = (XfireSetCustomGameDataUTF8Function)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataUTF8_V1");
571a9deffeac Frontend:
smxx
parents:
diff changeset
    82
		}
571a9deffeac Frontend:
smxx
parents:
diff changeset
    83
	}
571a9deffeac Frontend:
smxx
parents:
diff changeset
    84
}
571a9deffeac Frontend:
smxx
parents:
diff changeset
    85
571a9deffeac Frontend:
smxx
parents:
diff changeset
    86
571a9deffeac Frontend:
smxx
parents:
diff changeset
    87
static HMODULE HelperGetToucanDLL()
571a9deffeac Frontend:
smxx
parents:
diff changeset
    88
{
571a9deffeac Frontend:
smxx
parents:
diff changeset
    89
	if (g_toucan_dll)
571a9deffeac Frontend:
smxx
parents:
diff changeset
    90
		return g_toucan_dll;
571a9deffeac Frontend:
smxx
parents:
diff changeset
    91
571a9deffeac Frontend:
smxx
parents:
diff changeset
    92
	/*
571a9deffeac Frontend:
smxx
parents:
diff changeset
    93
	** We need to enumerate the DLLs loaded to find toucan dll.
571a9deffeac Frontend:
smxx
parents:
diff changeset
    94
	** This is done because the toucan dll changes with each update.
571a9deffeac Frontend:
smxx
parents:
diff changeset
    95
	** The toucan dll has the following format. "xfire_toucan_{BUILD_NUMBER}.dll"
571a9deffeac Frontend:
smxx
parents:
diff changeset
    96
	** We simply try to find a dll w/ the prefix "xfire_toucan"
571a9deffeac Frontend:
smxx
parents:
diff changeset
    97
	*/
571a9deffeac Frontend:
smxx
parents:
diff changeset
    98
	HANDLE snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
571a9deffeac Frontend:
smxx
parents:
diff changeset
    99
	if (snapshot_handle != INVALID_HANDLE_VALUE)
571a9deffeac Frontend:
smxx
parents:
diff changeset
   100
	{
571a9deffeac Frontend:
smxx
parents:
diff changeset
   101
		MODULEENTRY32 module_entry;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2920
diff changeset
   102
		module_entry.dwSize = sizeof(MODULEENTRY32);
2830
571a9deffeac Frontend:
smxx
parents:
diff changeset
   103
571a9deffeac Frontend:
smxx
parents:
diff changeset
   104
		BOOL result = Module32First(snapshot_handle, &module_entry);
571a9deffeac Frontend:
smxx
parents:
diff changeset
   105
		char module_name[] = "xfire_toucan";
571a9deffeac Frontend:
smxx
parents:
diff changeset
   106
		DWORD module_name_len = sizeof(module_name)-1;
571a9deffeac Frontend:
smxx
parents:
diff changeset
   107
		while (result)
571a9deffeac Frontend:
smxx
parents:
diff changeset
   108
		{
571a9deffeac Frontend:
smxx
parents:
diff changeset
   109
			if (CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, module_entry.szModule, module_name_len, module_name, module_name_len) == CSTR_EQUAL)
571a9deffeac Frontend:
smxx
parents:
diff changeset
   110
			{
571a9deffeac Frontend:
smxx
parents:
diff changeset
   111
				g_toucan_dll = module_entry.hModule;
571a9deffeac Frontend:
smxx
parents:
diff changeset
   112
				break;
571a9deffeac Frontend:
smxx
parents:
diff changeset
   113
			}
571a9deffeac Frontend:
smxx
parents:
diff changeset
   114
			result = Module32Next(snapshot_handle, &module_entry);
571a9deffeac Frontend:
smxx
parents:
diff changeset
   115
		}
571a9deffeac Frontend:
smxx
parents:
diff changeset
   116
571a9deffeac Frontend:
smxx
parents:
diff changeset
   117
		CloseHandle(snapshot_handle);
571a9deffeac Frontend:
smxx
parents:
diff changeset
   118
	}
571a9deffeac Frontend:
smxx
parents:
diff changeset
   119
571a9deffeac Frontend:
smxx
parents:
diff changeset
   120
	return g_toucan_dll;
2920
5cf48744a700 Engine:
smxx
parents: 2830
diff changeset
   121
}