Subversion Repositories WoWGM

Rev

Rev 30 | Rev 32 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/****************************************************************************
*
*   Client.cpp
*
*   Program entry point (DllMain)
* 
***/

#include "pch.h"
#pragma hdrstop

#include "Client.h"
#include "ClientCommands.h"
#include "ClientDebugCommands.h"
#include <WowSvcs/ClientServices.h>
#include <Glue/CGlueMgr.h>
#include <Ui/GameUI.h>
#include <FrameScript/FrameScript.h>
#include <Object/ObjectClient/Player_C.h>


/****************************************************************************
*
*   Client memory addresses
*
***/

#define  CLIENTREGISTERCONSOLECOMMANDS    0x00401B60;
#define  CLIENTUNREGISTERCONSOLECOMMANDS  0x00402910;
#define  DESTROYHANDLERPLAYER             0x004066D0;
#define  INITIALIZEHANDLERPLAYER          0x004047E0;
#define  SCRIPT_GETBUILDINFO_PTR          0x004DBE60;
#define  WOWCLIENTINIT_PTR                0x00404130;


/****************************************************************************
*
*   Client function pointers
*
***/

void (*ClientRegisterConsoleCommandsPtr)() = *(void(*)())CLIENTREGISTERCONSOLECOMMANDS;

void (*ClientUnregisterConsoleCommandsPtr)()= *(void(*)())CLIENTUNREGISTERCONSOLECOMMANDS;

void (*DestroyHandlerPlayerPtr)() = *(void(*)())DESTROYHANDLERPLAYER;

void (*InitializeHandlerPlayerPtr)() = *(void(*)())INITIALIZEHANDLERPLAYER;

int (*Script_GetBuildInfoPtr)(lua_State*) = *(int(*)(lua_State *))SCRIPT_GETBUILDINFO_PTR;

void (*WowClientInitPtr)() = *(void(*)())WOWCLIENTINIT_PTR;


/****************************************************************************
*
*   Program entry point (DllMain)
*
***/

//===========================================================================
extern "C" BOOL WINAPI DllMain (HINSTANCE const instance,
                                DWORD const     reason,
                                LPVOID const    reserved) {

        switch (reason) {
                case DLL_PROCESS_ATTACH:
    {
                        WowGM::Initialize();
                        ConsoleCommandInitialize();
                        WowGM::InitializeHandlerPlayer();
                        WowGM::PlayerClientInitialize();
                        WowGM::ClientRegisterConsoleCommands();
                        WowGM::CGlueMgr::Initialize();
                        WowGM::CGGameUI::Initialize();
                        //ClientServices_Initialize();
                        WowGM::InstallGameConsoleCommands();
                        ConsoleAccessSetEnabled(TRUE);
                        break;
                }
                case DLL_THREAD_ATTACH:
                case DLL_THREAD_DETACH:
                        break;
                case DLL_PROCESS_DETACH:
    {
                        WowGM::DestroyHandlerPlayer();
                        WowGM::ClientUnregisterConsoleCommands();
      WowGM::CGlueMgr::Shutdown();
                        WowGM::CGGameUI::Shutdown();
                        //ClientServices_Destroy();
                        WowGM::UninstallGameConsoleCommands();
                        break;
                }
        }

        return TRUE;
}


/****************************************************************************
*
*   Private
*
***/

//===========================================================================
void WowGM::Initialize ()  {
        DetourRestoreAfterWith();
        FixInvalidPtrCheck();
}

//===========================================================================
void WowGM::FixInvalidPtrCheck () {
        // Allow callbacks outside of the .text section
        *((int*)0x00D415B8) = 0x00000001;
        *((int*)0x00D415BC) = 0x7FFFFFFF;
}

//===========================================================================
void InitializeHandlerPlayer () {
        InitializeHandlerPlayerPtr();

        ConsoleCommandExecute("run autoexec.wtf",FALSE);
}

//===========================================================================
void ClientRegisterConsoleCommands () {
        ClientRegisterConsoleCommandsPtr();

        g_password = CVar::Register("password",
                              "stored password for blizzcon, dev, qa, etc.",
                              NULL,
                              "",
                              NULL,
                              GAME,
                              false,
                              NULL,
                              false);

  ConsoleCommandRegister("Bug", CCommand_Bug, DEBUG, NOHELP);
  ConsoleCommandRegister("Suggestion", CCommand_Bug, DEBUG, NOHELP);
}

//===========================================================================
void DestroyHandlerPlayer () {
        DestroyHandlerPlayerPtr();
}

//===========================================================================
void ClientUnregisterConsoleCommands () {
        ClientUnregisterConsoleCommandsPtr();
  
  ConsoleCommandUnregister("Bug");
  ConsoleCommandUnregister("Suggestion");
}

//===========================================================================
int Script_GetBuildInfo (lua_State* L) {
        char* version = FrameScript_GetText("VERSION", -1, GENDER_NONE);
        _lua_pushstring(L, version);
#ifdef _DEBUG
        _lua_pushstring(L,"Debug");
#else

        _lua_pushstring(L, "GM");
#endif // #ifdef _DEBUG
        _lua_pushstring(L, "3.3.5");
        _lua_pushstring(L, "12340");
        _lua_pushstring(L, __DATE__);
        return 5;       // TODO: automatically return the number of arguments returned by Lua
}

//===========================================================================
void WowClientInit() {
        WowClientInitPtr();

        WowGM::CGlueMgr::SetLoginPassword();
        ConsoleCommandExecute("run autoexec.wtf",FALSE);
}


/****************************************************************************
*
*   Detours setup
*
***/

//===========================================================================
void WowGM::InitializeHandlerPlayer () {
        DETOUR_INIT;
        DETOUR_ATTACH(WowClientInitPtr,WowClientInit);
        DETOUR_ATTACH(InitializeHandlerPlayerPtr,::InitializeHandlerPlayer);
        DETOUR_ATTACH(Script_GetBuildInfoPtr, Script_GetBuildInfo);
        DETOUR_ATTACH(DestroyHandlerPlayerPtr,::DestroyHandlerPlayer);
        DETOUR_COMMIT;
}

//===========================================================================
void WowGM::ClientRegisterConsoleCommands () {
        DETOUR_INIT;
        DETOUR_ATTACH(ClientRegisterConsoleCommandsPtr,::ClientRegisterConsoleCommands);
        DETOUR_ATTACH(ClientUnregisterConsoleCommandsPtr,::ClientUnregisterConsoleCommands);
        DETOUR_COMMIT;
}

//===========================================================================
void WowGM::DestroyHandlerPlayer () {
        DETOUR_INIT;
        DETOUR_DETACH(WowClientInitPtr,WowClientInit);
        DETOUR_DETACH(InitializeHandlerPlayerPtr,::InitializeHandlerPlayer);
        DETOUR_DETACH(DestroyHandlerPlayerPtr,::DestroyHandlerPlayer);
        DETOUR_DETACH(Script_GetBuildInfoPtr, Script_GetBuildInfo);
        DETOUR_COMMIT;
}

//===========================================================================
void WowGM::ClientUnregisterConsoleCommands () {
        DETOUR_INIT;
        DETOUR_DETACH(ClientRegisterConsoleCommandsPtr,::ClientRegisterConsoleCommands);
        DETOUR_DETACH(ClientUnregisterConsoleCommandsPtr,::ClientUnregisterConsoleCommands);
        DETOUR_COMMIT;
}