首页 > 代码库 > 魔兽改建程序源码(一)

魔兽改建程序源码(一)

先贴出二进制下载地址:  http://download.csdn.net/detail/xyzzf/7322277

第一部分: 键盘映射DLL API实现:

#include <Windows.h>
#include <process.h>
#include <type_traits>

typedef WPARAM key_type;
typedef WPARAM value_type;
typedef struct _k2rt_tree_node
{
    key_type                key;   // virtual key code: mapping-key
    value_type              value; // virtual key code: mapping-value 
    struct _k2rt_tree_node* child;
    struct _k2rt_tree_node* next;
} k2rt_tree_node, *k2rt_tree_node_pointer, k2rt_tree, *k2rt_tree_pointer;

// shared data segment
#pragma data_seg(".shared")
__declspec(dllexport) HHOOK             udg_hhook = nullptr;
__declspec(dllexport) HHOOK             udg_hhook_launch = nullptr;
__declspec(dllexport) HMODULE           udg_target_hmod = nullptr;
__declspec(dllexport) HANDLE            udg_target_process = nullptr;
__declspec(dllexport) DWORD             udg_target_tid = 0;
__declspec(dllexport) HWND              udg_target_hwnd = nullptr;
__declspec(dllexport) k2rt_tree_pointer udg_mapper_list = nullptr;
__declspec(dllexport) bool              udg_start_war_require = false;
#pragma data_seg()
#pragma comment(linker,"/section:.shared,RWS")

/*
** @useful macro utils
*/
#define _WriteMemory(_Paddr,_Pval) ::WriteProcessMemory(udg_target_process, _Paddr, _Pval, sizeof(*_Pval), nullptr)
#define _ReadMemory(_Paddr,_Pval)  ::ReadProcessMemory(udg_target_process, _Paddr, _Pval, sizeof(*_Pval), nullptr)

#define _IsNotNullptr(ptr) ( (ptr) != nullptr )

#define LIST_PUSHBACK(_List,_Newkey,_Newval,_CreateFunc,_GetNextFunc,_SetNextFunc)  do {                 decltype(_List) next = nullptr;                                                                  decltype(_List) next_visitor = _List;                                                            while( _IsNotNullptr(next_visitor) && _IsNotNullptr(next = _GetNextFunc(next_visitor)) )         {                                                                                                    next_visitor = next;                                                                         }                                                                                                _SetNextFunc(next_visitor, _CreateFunc(_Newkey, _Newval) );                                  } while(false)

/*
** vtree(the same line nodes have same key,graph view:
**           ROOT -------- Only a placeholder node
**             |
**             |
**           child(1) ---> child(1).next(1) ... child(1).next(n)
**             |
**             |
**           child(2) ---> child(2).next(1) ... child(2).next(n)
**             .
**             .
**             .
**           child(n) ---> child(n).next(1) ... child(n).next(n)
**
*/
#define VTREE_PUSHBACK(_List,_Newkey,_Newval,_CreateFunc,_GetChildFunc,_SetChildFunc,_GetNextFunc,_SetNextFunc,_GetKeyFunc) do {         decltype(_List)  child = nullptr;                                                                         decltype(_List)  child_visitor = _List;                                                                   std::remove_const<decltype(_Newkey)>::type tempkey = -1;                                                  while( ( child_visitor != nullptr ) && ( _Newkey != ( tempkey = _GetKeyFunc(child_visitor) ) ) &&                ( ( child = _GetChildFunc(child_visitor) ) != nullptr ) )                                          {                                                                                                             child_visitor = child;                                                                                }                                                                                                         if(_Newkey == tempkey) {                                                                                      LIST_PUSHBACK(child_visitor, _Newkey, _Newval, _CreateFunc, _GetNextFunc, _SetNextFunc);              } else {                                                                                                      _SetChildFunc(child_visitor, _CreateFunc(_Newkey, _Newval) );                                         }                                                                                                     } while(false)


/** @declaration
** user interfaces ( used by external )
*/
extern "C" __declspec(dllexport) void         k2ud_mapper_initialize(HANDLE hProcess, DWORD tid, HWND hwnd);
extern "C" __declspec(dllexport) void         k2ud_mapper_finalize(void);
extern "C" __declspec(dllexport) bool         k2ud_mapper_enable_mapping(void);
extern "C" __declspec(dllexport) bool         k2ud_mapper_disable_mapping(void);
/*
*  params: key:    new short-key
*               value: original short-key
*/
extern "C" __declspec(dllexport) unsigned int k2ud_mapper_insert_mapping(const key_type key, const value_type value);
extern "C" __declspec(dllexport) void         k2ud_mapper_erase_mapping(const key_type key);
extern "C" __declspec(dllexport) void         k2ud_mapper_clear_mapping(void);
extern "C" __declspec(dllexport) bool         k2ud_launch_war3_quickly(void);


/** @declaration
** runtime interface( used at internal )
*/
static k2rt_tree_node_pointer k2rt_tree_node_create(const key_type key, const value_type value);
static void                   k2rt_tree_node_free(k2rt_tree_node_pointer);
static void                   k2rt_tree_list_free(k2rt_tree_node_pointer);

static key_type               k2rt_tree_node_get_key(k2rt_tree_node_pointer);
static value_type             k2rt_tree_node_get_value(k2rt_tree_node_pointer);
static k2rt_tree_node_pointer k2rt_tree_node_get_child(k2rt_tree_node_pointer);
static k2rt_tree_node_pointer k2rt_tree_node_get_next(k2rt_tree_node_pointer);

static void                   k2rt_tree_node_set_key(k2rt_tree_node_pointer, const key_type key);
static void                   k2rt_tree_node_set_value(k2rt_tree_node_pointer, const value_type value);
static void                   k2rt_tree_node_set_child(k2rt_tree_node_pointer, k2rt_tree_node_pointer child);
static void                   k2rt_tree_node_set_next(k2rt_tree_node_pointer, k2rt_tree_node_pointer next);

static LRESULT WINAPI         k2rt_GetMsgProc(int code, WPARAM wParam, LPARAM lParam);
static LRESULT WINAPI         k2rt_KeyboardProc(int code, WPARAM wParam, LPARAM lParam);

/** @implementation
** user interfaces ( used by external )
*/
extern "C" __declspec(dllexport) void k2ud_mapper_initialize(HANDLE process, DWORD tid, HWND hwnd)
{
    udg_target_process = process;
    udg_target_tid     = tid;
    udg_target_hwnd    = hwnd;
    udg_mapper_list    = k2rt_tree_node_create(-1, -1); // Just a reserved node
}

extern "C" __declspec(dllexport) void k2ud_mapper_finalize(void)
{ // destory the vtree at remoate process
    k2rt_tree_node_pointer ptr = udg_mapper_list;
    k2rt_tree_node_pointer freeing = nullptr;
    while(ptr != nullptr)
    {
        freeing = ptr;
        ptr = k2rt_tree_node_get_child(ptr);
        k2rt_tree_list_free(freeing);
    }

    udg_mapper_list = nullptr;
    udg_target_hwnd = nullptr;
    udg_target_tid = 0;
    udg_target_process = nullptr;
}

extern "C" __declspec(dllexport) bool k2ud_mapper_enable_mapping(void)
{
    if(0 == udg_target_tid)
    {
        ::MessageBox(nullptr, TEXT("The target thread id is unspecified,please call k2ud_mapper_initialize function!"), TEXT("error from key_mapper2.dll::k2ud_mapper_enable_mapping"), MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    if(nullptr == udg_hhook)
    {
        udg_hhook = ::SetWindowsHookEx(WH_KEYBOARD, k2rt_KeyboardProc, udg_target_hmod, udg_target_tid);
    }
    return udg_hhook != nullptr;
}

extern "C" __declspec(dllexport) bool k2ud_mapper_disable_mapping(void)
{
    if(udg_hhook != nullptr)
    {
        if(::UnhookWindowsHookEx(udg_hhook))
        {
            udg_hhook = nullptr;
            return true;
        }
        DWORD ec = GetLastError();
        if(ec == ERROR_INVALID_HANDLE)
        {
            udg_hhook = nullptr;
            return true;
        }
    }
    return false;
}

extern "C" __declspec(dllexport) unsigned int k2ud_mapper_insert_mapping(const key_type key, const value_type value)
{
    if(key == value) return 0;

    VTREE_PUSHBACK(udg_mapper_list,
        key,
        value,
        k2rt_tree_node_create,
        k2rt_tree_node_get_child,
        k2rt_tree_node_set_child,
        k2rt_tree_node_get_next,
        k2rt_tree_node_set_next,
        k2rt_tree_node_get_key);

    return 1;
}

extern "C" __declspec(dllexport) void k2ud_mapper_erase_mapping(const key_type key)
{
    key_type               tempkey = 0;
    k2rt_tree_pointer      child_visitor = udg_mapper_list;
    k2rt_tree_node_pointer parent_visitor = child_visitor;
    k2rt_tree_node_pointer child = nullptr;
    // search
    while( (tempkey = k2rt_tree_node_get_key(child_visitor) ) != key 
        && ( child = k2rt_tree_node_get_child(child_visitor) ) != nullptr ) 
    {
        parent_visitor = child_visitor;
        child_visitor = child;
    }
    // if found, remove it
    if(key == tempkey) {
        k2rt_tree_node_set_child(parent_visitor, k2rt_tree_node_get_child(child_visitor) );
        k2rt_tree_list_free(child_visitor);
    }
}

extern "C" __declspec(dllexport) void k2ud_mapper_clear_mapping(void)
{
    k2rt_tree_node_pointer ptr = k2rt_tree_node_get_child(udg_mapper_list);
    k2rt_tree_node_pointer freeing = nullptr;
    while(ptr != nullptr)
    {
        freeing = ptr;
        ptr = k2rt_tree_node_get_child(ptr);
        k2rt_tree_list_free(freeing);
    }
    k2rt_tree_node_set_child(udg_mapper_list, nullptr);
}


/** @implementation
** runtime interfaces ( used by external )
*/
static k2rt_tree_node_pointer k2rt_tree_node_create(const key_type key, const value_type value)
{
    if(nullptr == udg_target_process)
    {
        ::MessageBox(nullptr, TEXT("The target process is unspecified, please call k2ud_mapper_initialize function!"), TEXT("error from key_mapper2.dll::k2rt_tree_node_create"), MB_OK | MB_ICONEXCLAMATION);
        return nullptr;
    }
    static unsigned int zero = 0;
    k2rt_tree_node_pointer _Pnode = (k2rt_tree_node_pointer)::VirtualAllocEx(udg_target_process, 
        nullptr, 
        sizeof(k2rt_tree_node), 
        MEM_COMMIT, 
        PAGE_READWRITE);
    _WriteMemory(&_Pnode->key, &key);
    _WriteMemory(&_Pnode->value, &value);
    _WriteMemory(&_Pnode->child, &zero);
    _WriteMemory(&_Pnode->next, &zero);
    return _Pnode;
}

static void  k2rt_tree_node_free(k2rt_tree_node_pointer _Pnode)
{
    ::VirtualFreeEx(udg_target_process, _Pnode, 0, MEM_RELEASE);
}

static void k2rt_tree_list_free(k2rt_tree_node_pointer _Phead)
{
    k2rt_tree_node_pointer ptr = _Phead;
    k2rt_tree_node_pointer freeing = nullptr;
    while(ptr != nullptr)
    {
        freeing = ptr;
        ptr = k2rt_tree_node_get_next(ptr);
        k2rt_tree_node_free(freeing);
    }
}

static key_type k2rt_tree_node_get_key(k2rt_tree_node_pointer _Pnode)
{
    key_type key = 0;
    _ReadMemory(&_Pnode->key, &key);
    return key;
}

static key_type k2rt_tree_node_get_value(k2rt_tree_node_pointer _Pnode)
{
    value_type value = http://www.mamicode.com/0;>