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