QTfrontend/util/platform/xfiregameclient.cpp
author Periklis Ntanasis <pntanasis@gmail.com>
Thu, 19 Sep 2013 03:49:29 +0300
branchspacecampaign
changeset 9642 8a691e0f117a
parent 8440 ea4d6a7a2937
permissions -rw-r--r--
use consts for TStatInfo enum
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
{
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    37
    HelperInit();
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    38
    if (ptr_XfireSetCustomGameDataA &&
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    39
        ptr_XfireSetCustomGameDataW &&
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    40
        ptr_XfireSetCustomGameDataUTF8)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    41
        return 1;
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    42
    return 0;
2830
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
{
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    47
    HelperInit();
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    48
    if (ptr_XfireSetCustomGameDataA)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    49
        return ptr_XfireSetCustomGameDataA(num_keys, keys, values);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    50
    return 1;
2830
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
{
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    55
    HelperInit();
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    56
    if (ptr_XfireSetCustomGameDataW)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    57
        return ptr_XfireSetCustomGameDataW(num_keys, keys, values);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    58
    return 1;
2830
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
{
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    63
    HelperInit();
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    64
    if (ptr_XfireSetCustomGameDataUTF8)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    65
        return ptr_XfireSetCustomGameDataUTF8(num_keys, keys, values);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    66
    return 1;
2830
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
{
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    72
    if (!ptr_XfireSetCustomGameDataA ||
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    73
        !ptr_XfireSetCustomGameDataW ||
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    74
        !ptr_XfireSetCustomGameDataUTF8)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    75
    {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    76
        HMODULE toucan_dll = HelperGetToucanDLL();
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    77
        if (toucan_dll)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    78
        {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    79
            ptr_XfireSetCustomGameDataA = (XfireSetCustomGameDataAFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataA_V1");
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    80
            ptr_XfireSetCustomGameDataW = (XfireSetCustomGameDataWFunction)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataW_V1");
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    81
            ptr_XfireSetCustomGameDataUTF8 = (XfireSetCustomGameDataUTF8Function)::GetProcAddress(toucan_dll, "ToucanSendGameClientDataUTF8_V1");
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    82
        }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    83
    }
2830
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
{
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    89
    if (g_toucan_dll)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    90
        return g_toucan_dll;
2830
571a9deffeac Frontend:
smxx
parents:
diff changeset
    91
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    92
    /*
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    93
    ** We need to enumerate the DLLs loaded to find toucan dll.
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    94
    ** This is done because the toucan dll changes with each update.
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    95
    ** The toucan dll has the following format. "xfire_toucan_{BUILD_NUMBER}.dll"
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    96
    ** We simply try to find a dll w/ the prefix "xfire_toucan"
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    97
    */
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    98
    HANDLE snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
    99
    if (snapshot_handle != INVALID_HANDLE_VALUE)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   100
    {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   101
        MODULEENTRY32 module_entry;
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   102
        module_entry.dwSize = sizeof(MODULEENTRY32);
2830
571a9deffeac Frontend:
smxx
parents:
diff changeset
   103
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   104
        BOOL result = Module32First(snapshot_handle, &module_entry);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   105
        char module_name[] = "xfire_toucan";
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   106
        DWORD module_name_len = sizeof(module_name)-1;
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   107
        while (result)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   108
        {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   109
            if (CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, module_entry.szModule, module_name_len, module_name, module_name_len) == CSTR_EQUAL)
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   110
            {
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   111
                g_toucan_dll = module_entry.hModule;
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   112
                break;
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   113
            }
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   114
            result = Module32Next(snapshot_handle, &module_entry);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   115
        }
2830
571a9deffeac Frontend:
smxx
parents:
diff changeset
   116
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   117
        CloseHandle(snapshot_handle);
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   118
    }
2830
571a9deffeac Frontend:
smxx
parents:
diff changeset
   119
8440
ea4d6a7a2937 complete tabs and trailing whitespace formatting on frontend
koda
parents: 8381
diff changeset
   120
    return g_toucan_dll;
2920
5cf48744a700 Engine:
smxx
parents: 2830
diff changeset
   121
}