Subversion Repositories WoWGM

Rev

Rev 34 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/******************************************************************************
*
*   Client.cpp
*
*
*   By Tristan Cormier - 11/20/2024
* 
***/


#include "pch.h"
#pragma hdrstop

#include "ClientCommands.h"
#include "ClientDebugCommands.h"
#include "ClientGMCommands.h"
#include <Console/ConsoleClient.h>
#include <WowSvcs/ClientServices.h>
#include <Glue/CGlueMgr.h>
#include <Ui/GameUI.h>
#include <FrameScript/FrameScript.h>


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

void (*ClientRegisterConsoleCommands)() = *(void(*)())0x401B60;

void (*CommonMainInitialize)() = *(void(*)())0x4047E0;
void (*CommonMainDestroy)() = *(void(*)())0x4066D0;

void (*InstallGameConsoleCommands)() = *(void(*)())0x407870;
void (*UninstallGameConsoleCommands)() = *(void(*)())0x406EF0;

void (*WowClientInit)() = *(void(*)())0x404130;
void (*WowClientDestroy)() = *(void(*)())0x402910;

int (*Script_GetBuildInfo)(lua_State *) = *(int(*)(lua_State *))0x4DBE60;


//=============================================================================
void ClientRegisterConsoleCommandsProc () {
    ClientRegisterConsoleCommands();
    
    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 CommonMainInitializeProc () {
    ConsoleAccessSetEnabled(TRUE);

    CommonMainInitialize();
}

//=============================================================================
void CommonMainDestroyProc () {
    CommonMainDestroy();
}

//=============================================================================
void InstallGameConsoleCommandsProc () {
    InstallGameConsoleCommands();

    ConsoleCommandRegister("godmode", CCommand_Godmode, GAME, NOHELP);
    ConsoleCommandRegister("beastmaster", CCommand_Beastmaster, GAME, NOHELP);

    DebugClientCommands::Install();
    GMClientCommands::Install();
}

//=============================================================================
void UninstallGameConsoleCommandsProc () {
    UninstallGameConsoleCommands();

    ConsoleCommandUnregister("godmode");
    ConsoleCommandUnregister("beastmaster");

    DebugClientCommands::Uninstall();
    GMClientCommands::Uninstall();
}

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

    // HACK:
    // The login password should be set via GlueXML but we do it in code
    WowGM::CGlueMgr::SetLoginPassword();

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

//=============================================================================
void WowClientDestroyProc () {
    ConsoleCommandUnregister("Bug");
    ConsoleCommandUnregister("Suggestion");
    
    WowClientDestroy();
}

//=============================================================================
static int Script_GetBuildInfoProc (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

    _lua_pushstring(L, "3.3.5");
    _lua_pushstring(L, "12340");
    _lua_pushstring(L, __DATE__);
    return 5;
}


//=============================================================================
void RegisterClientCallbacks () {
    DETOUR_INIT;

    DETOUR_ATTACH(
        ClientRegisterConsoleCommands,
        ClientRegisterConsoleCommandsProc
    );

    DETOUR_ATTACH(
        CommonMainInitialize,
        CommonMainInitializeProc
    );
    DETOUR_ATTACH(
        CommonMainDestroy,
        CommonMainDestroyProc
    );
    
    DETOUR_ATTACH(
        InstallGameConsoleCommands,
        InstallGameConsoleCommandsProc
    );
    DETOUR_ATTACH(
        UninstallGameConsoleCommands,
        UninstallGameConsoleCommandsProc
    );

    DETOUR_ATTACH(
        WowClientInit,
        WowClientInitProc
    );
    DETOUR_ATTACH(
        WowClientDestroy,
        WowClientDestroyProc
    );

    DETOUR_ATTACH(
        Script_GetBuildInfo,
        Script_GetBuildInfoProc
    );

    DETOUR_COMMIT;
}

//=============================================================================
void UnregisterClientCallbacks () {
    DETOUR_INIT;

    DETOUR_DETACH(
        ClientRegisterConsoleCommands,
        ClientRegisterConsoleCommandsProc
    );

    DETOUR_DETACH(
        CommonMainInitialize,
        CommonMainInitializeProc
    );
    DETOUR_DETACH(
        CommonMainDestroy,
        CommonMainDestroyProc
    );

    DETOUR_DETACH(
        InstallGameConsoleCommands,
        InstallGameConsoleCommandsProc
    );
    DETOUR_DETACH(
        UninstallGameConsoleCommands,
        UninstallGameConsoleCommandsProc
    );

    DETOUR_DETACH(
        WowClientInit,
        WowClientInitProc
    );
    DETOUR_DETACH(
        WowClientDestroy,
        WowClientDestroyProc
    );

    DETOUR_DETACH(
        Script_GetBuildInfo,
        Script_GetBuildInfoProc
    );

    DETOUR_COMMIT;
}


/******************************************************************************
*
*   Program entry point
*
***/

//=============================================================================
extern "C" BOOL WINAPI DllMain (
    HINSTANCE const instance,
    DWORD const     reason,
    LPVOID const    reserved
) {
    switch (reason) {
            case DLL_PROCESS_ATTACH: {
          // Allow callbacks outside of the .text section
          * ((int * )0xD415B8) = 0x00000001;
          * ((int * )0xD415BC) = 0x7FFFFFFF;

          // Initialize the Detour library
          DetourRestoreAfterWith();

          RegisterClientCallbacks();

          ConsoleCommandInitialize();
          CGlueMgr::Initialize();
          CGGameUI::Initialize();
          // TODO:
          //ClientServices_Initialize();
          break;
        }
                case DLL_THREAD_ATTACH:
                case DLL_THREAD_DETACH:
                        break;
                case DLL_PROCESS_DETACH: {
            UnregisterClientCallbacks();
            // TODO
            //ClientServices_Destroy();
            WowGM::CGlueMgr::Shutdown();
                        CGGameUI::Shutdown();
            break;
        }
    }

    return TRUE;
}


//===================================
// MIT License
//
// Copyright (c) 2024 by Tristan Cormier
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//===================================

Generated by GNU Enscript 1.6.5.90.