// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include "HookUi.hpp"
#include "Hook.hpp"



void LenheartThread()
{
    while (true)
    {
        Sleep(10);
        static int 可开始执行判断;
        if (DNFTOOL::GetHook(0x1A5FB4C, "0x14+0x28+",0) == 6)
        {
            可开始执行判断 = 1;
        }
        if (可开始执行判断 == 1)
        {
            //获取验证服务器ip
            std::string Rqip = DNFTOOL::GetIP();
            //获取用户IP
            std::string ippack = DNFTOOL::GetUserIp();

            std::vector<std::string> IpArr;
            DNFTOOL::Split(Rqip, IpArr, "->");

            for (auto it = IpArr.begin(); it != IpArr.end(); ++it) {
                //验证并请求加载脚本
                std::string Iname = *it;
                std::vector<std::string> YmArr;
                DNFTOOL::Split(Iname, YmArr, "$$");
                bool In = DNFTOOL::ReqIpLicense(ippack, YmArr[0], YmArr[1]);
                //std::cout << Iname << "连接状态:" << In << std::endl;
                if (In)return;
            }


            return;
        }

    }
}


//初始化入口
void Init() {

    //是否加载百级UI
    FILE* file = fopen("ImagePacks2/!HUD_Yosin百级UI.NPK", "rb");
    if (file)
    {
        HookHudUi();
        fclose(file);
    }

    //是否开启控制台
    FILE* file2 = fopen("ip.txt", "rb");
    if (file2)
    {
        AllocConsole();
        SetConsoleTitle(L"Rindro_Console");
        SetConsoleOutputCP(65001);
        freopen(("CONOUT$"), ("w"), stdout);
        freopen(("CONOUT$"), ("w"), stderr);
        freopen(("CONIN$"), ("r"), stdin);

        *(BYTE*)0x006FCC7D = 0xEB;//双开jmp
        *(BYTE*)0x006FCC7E = 0x09;
        *(BYTE*)0x006FCC7F = 0x90;//双开nop
        *(BYTE*)0x01122DA1 = 0xEB;//双开jmp
        *(BYTE*)0x01162ACC = 0xEB;//双开jmp
        fclose(file2);
    }

    //消息框黏贴
    *(WORD*)0x011C53B0 = 0x01B0;
    //修复233
    memcpy((void*)0x00727017, "\xEB\x11\x90\x90\x90\x90\x90", 7);

    //注册HOOK
    RegisterHook();

#ifdef SELL
    //多线程验证
    DWORD threadID;
    HANDLE Thand = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LenheartThread, NULL, 0, &threadID);
#endif



}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        Init();
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

