QTfrontend/util/platform/xfiregameclient.cpp
changeset 8440 ea4d6a7a2937
parent 8381 588a8e6e2041
equal deleted inserted replaced
8439:3850c4bfe6b5 8440:ea4d6a7a2937
    32 #endif
    32 #endif
    33 
    33 
    34 
    34 
    35 int XfireIsLoaded()
    35 int XfireIsLoaded()
    36 {
    36 {
    37 	HelperInit();
    37     HelperInit();
    38 	if (ptr_XfireSetCustomGameDataA &&
    38     if (ptr_XfireSetCustomGameDataA &&
    39 		ptr_XfireSetCustomGameDataW &&
    39         ptr_XfireSetCustomGameDataW &&
    40 		ptr_XfireSetCustomGameDataUTF8)
    40         ptr_XfireSetCustomGameDataUTF8)
    41 		return 1;
    41         return 1;
    42 	return 0;
    42     return 0;
    43 }
    43 }
    44 
    44 
    45 int XfireSetCustomGameDataA(int num_keys, const char **keys, const char **values)
    45 int XfireSetCustomGameDataA(int num_keys, const char **keys, const char **values)
    46 {
    46 {
    47 	HelperInit();
    47     HelperInit();
    48 	if (ptr_XfireSetCustomGameDataA)
    48     if (ptr_XfireSetCustomGameDataA)
    49 		return ptr_XfireSetCustomGameDataA(num_keys, keys, values);
    49         return ptr_XfireSetCustomGameDataA(num_keys, keys, values);
    50 	return 1;
    50     return 1;
    51 }
    51 }
    52 
    52 
    53 int XfireSetCustomGameDataW(int num_keys, const wchar_t **keys, const wchar_t **values)
    53 int XfireSetCustomGameDataW(int num_keys, const wchar_t **keys, const wchar_t **values)
    54 {
    54 {
    55 	HelperInit();
    55     HelperInit();
    56 	if (ptr_XfireSetCustomGameDataW)
    56     if (ptr_XfireSetCustomGameDataW)
    57 		return ptr_XfireSetCustomGameDataW(num_keys, keys, values);
    57         return ptr_XfireSetCustomGameDataW(num_keys, keys, values);
    58 	return 1;
    58     return 1;
    59 }
    59 }
    60 
    60 
    61 int XfireSetCustomGameDataUTF8(int num_keys, const char **keys, const char **values)
    61 int XfireSetCustomGameDataUTF8(int num_keys, const char **keys, const char **values)
    62 {
    62 {
    63 	HelperInit();
    63     HelperInit();
    64 	if (ptr_XfireSetCustomGameDataUTF8)
    64     if (ptr_XfireSetCustomGameDataUTF8)
    65 		return ptr_XfireSetCustomGameDataUTF8(num_keys, keys, values);
    65         return ptr_XfireSetCustomGameDataUTF8(num_keys, keys, values);
    66 	return 1;
    66     return 1;
    67 }
    67 }
    68 
    68 
    69 /* ------------------------------------------------------------------------- */
    69 /* ------------------------------------------------------------------------- */
    70 static void HelperInit()
    70 static void HelperInit()
    71 {
    71 {
    72 	if (!ptr_XfireSetCustomGameDataA ||
    72     if (!ptr_XfireSetCustomGameDataA ||
    73 		!ptr_XfireSetCustomGameDataW ||
    73         !ptr_XfireSetCustomGameDataW ||
    74 		!ptr_XfireSetCustomGameDataUTF8)
    74         !ptr_XfireSetCustomGameDataUTF8)
    75 	{
    75     {
    76 		HMODULE toucan_dll = HelperGetToucanDLL();
    76         HMODULE toucan_dll = HelperGetToucanDLL();
    77 		if (toucan_dll)
    77         if (toucan_dll)
    78 		{
    78         {
    79 			ptr_XfireSetCustomGameDataA = (XfireSetCustomGameDataAFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataA_V1");
    79             ptr_XfireSetCustomGameDataA = (XfireSetCustomGameDataAFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataA_V1");
    80 			ptr_XfireSetCustomGameDataW = (XfireSetCustomGameDataWFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataW_V1");
    80             ptr_XfireSetCustomGameDataW = (XfireSetCustomGameDataWFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataW_V1");
    81 			ptr_XfireSetCustomGameDataUTF8 = (XfireSetCustomGameDataUTF8Function)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataUTF8_V1");
    81             ptr_XfireSetCustomGameDataUTF8 = (XfireSetCustomGameDataUTF8Function)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataUTF8_V1");
    82 		}
    82         }
    83 	}
    83     }
    84 }
    84 }
    85 
    85 
    86 
    86 
    87 static HMODULE HelperGetToucanDLL()
    87 static HMODULE HelperGetToucanDLL()
    88 {
    88 {
    89 	if (g_toucan_dll)
    89     if (g_toucan_dll)
    90 		return g_toucan_dll;
    90         return g_toucan_dll;
    91 
    91 
    92 	/*
    92     /*
    93 	** We need to enumerate the DLLs loaded to find toucan dll.
    93     ** We need to enumerate the DLLs loaded to find toucan dll.
    94 	** This is done because the toucan dll changes with each update.
    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"
    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"
    96     ** We simply try to find a dll w/ the prefix "xfire_toucan"
    97 	*/
    97     */
    98 	HANDLE snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
    98     HANDLE snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
    99 	if (snapshot_handle != INVALID_HANDLE_VALUE)
    99     if (snapshot_handle != INVALID_HANDLE_VALUE)
   100 	{
   100     {
   101 		MODULEENTRY32 module_entry;
   101         MODULEENTRY32 module_entry;
   102 		module_entry.dwSize = sizeof(MODULEENTRY32);
   102         module_entry.dwSize = sizeof(MODULEENTRY32);
   103 
   103 
   104 		BOOL result = Module32First(snapshot_handle, &module_entry);
   104         BOOL result = Module32First(snapshot_handle, &module_entry);
   105 		char module_name[] = "xfire_toucan";
   105         char module_name[] = "xfire_toucan";
   106 		DWORD module_name_len = sizeof(module_name)-1;
   106         DWORD module_name_len = sizeof(module_name)-1;
   107 		while (result)
   107         while (result)
   108 		{
   108         {
   109 			if (CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, module_entry.szModule, module_name_len, module_name, module_name_len) == CSTR_EQUAL)
   109             if (CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, module_entry.szModule, module_name_len, module_name, module_name_len) == CSTR_EQUAL)
   110 			{
   110             {
   111 				g_toucan_dll = module_entry.hModule;
   111                 g_toucan_dll = module_entry.hModule;
   112 				break;
   112                 break;
   113 			}
   113             }
   114 			result = Module32Next(snapshot_handle, &module_entry);
   114             result = Module32Next(snapshot_handle, &module_entry);
   115 		}
   115         }
   116 
   116 
   117 		CloseHandle(snapshot_handle);
   117         CloseHandle(snapshot_handle);
   118 	}
   118     }
   119 
   119 
   120 	return g_toucan_dll;
   120     return g_toucan_dll;
   121 }
   121 }