#include "pch.h"
#include "sock.h"

extern  std::map<std::string, std::string>Json_STL;

void sock::Pack_Control(int idx, int code, void* p3, void* p4)
{
    DWORD Size;
    Net_Get_Dword(Size);
    char* Buffer = new char[Size + 1];
    Net_Get_Buffer(Buffer, Size);
    Buffer[Size] = '\0';

    /*
    std::fstream OutputFile;
    OutputFile.open("Yosin_Log.txt", std::ios::app);
    std::string line;
    OutputFile << Buffer << "\n";
    OutputFile.close();
    */

    rapidjson::Document Dom;
    Dom.Parse(DNFTOOL::UtfToGbk(Buffer).c_str());//加载 字符串
    int Op = Dom["op"].GetInt();//判断类型


#if defined GOLD_LIGHT
    wchar_t* ss = DNFTOOL::charTowchar_t(Buffer);
    uint32_t v = GetSqVm();
    SQPushRootTable(v);
    SQPushString(v, L"Sq_Pack_Control", -1);
    SQ_Get(v, -2);
    SQPushRootTable(v);
    SQPushString(v, ss, -1);
    SQ_Call(v, 2, 0, 1);
    SQPop(v, 2);
    delete[]ss;
    if (Op > 0 && Op <= 10)Json_STL["DragonBox"] = Buffer;
#else
    //排行榜
#if defined RANK_SWITCH
    if (Op >= 30 && Op <= 40)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif
    //龙盒
#if defined DRAGONBOX_SWITCH
    if (Op > 0 && Op <= 10)
    {
        Json_STL["DragonBox"] = Buffer;
    }
#endif
    //土罐
#if defined TUGUAN_SWITCH
    if (Op > 20008000 && Op <= 20008999)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif
    //世界Boss
#if defined WORLDBOSS_SWITCH
    if (Op > 10 && Op <= 20)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif
    //兑换CDK
#if defined CDKCHANGE_SWITCH
    if (Op > 20005000 && Op <= 20005999)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif
    //队伍DPS
#if defined TEAM_DPS_SWITCH
    if (Op > 610 && Op <= 620)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif
    //安图恩
#if defined ANTON_SWITCH
    if (Op > 1000 && Op <= 1100)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif
    //勋章
#if defined MEDAL_SWITCH
    if (Op > 20009000 && Op <= 20009999)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif

    //护石
#if defined HUSHI_SWITCH
    if (Op > 29991000 && Op <= 29991999)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif

    //buff换装
#ifdef BUFFSWITCHING_SWITCH
    if (Op > 20013000 && Op <= 20013999)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif 

    //强化系统
#ifdef NEWUPGRADE_SWITCH
    if (Op > 20018000 && Op <= 20018999)
    {
        uint32_t v = GetSqVm();
        SQPushRootTable(v);
        SQPushString(v, L"Sq_Pack_Control", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushString(v, DNFTOOL::charTowchar_t(Buffer), -1);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);
    }
#endif 
#endif
}

void sock::Pack_Control2(int idx, int code, void* p3, void* p4)
{
    DWORD Size;
    Net_Get_Dword(Size);
    char* Buffer = new char[Size + 1];
    Net_Get_Buffer(Buffer, Size);
    Buffer[Size] = '\0';

    std::cout << Size << std::endl;
    std::cout << Buffer << std::endl;

}

void sock::R_Register_Pack()
{
    auto Registerfunc = reinterpret_cast<register_pack_handler_t>(0x7186D0);

    Registerfunc(130, Pack_Control, 0);
    //Registerfunc(625, Pack_Control2, 0);
}

bool sock::Net_Get_Byte(BYTE& v)
{
    typedef bool(__cdecl* func_t)(BYTE*);
    return reinterpret_cast<func_t>(0x011AEA00)(&v);
}

bool sock::Net_Get_Short(short int& v)
{
    typedef bool(__cdecl* func_t)(short int*);
    return reinterpret_cast<func_t>(0x011AEA30)(&v);
}

bool sock::Net_Get_Dword(DWORD& v)
{
    typedef bool(__cdecl* func_t)(DWORD*);
    return reinterpret_cast<func_t>(0x011AEA60)(&v);
}

bool sock::Net_Get_Buffer(char* v, int sz)
{
    typedef bool(__cdecl* func_t)(char*, int);
    return reinterpret_cast<func_t>(0x011AEA90)(v, sz);
}
/*
bool sock::HookRegistPackHandler(int idx, pack_handler_t handler)
{
    return handleTable.emplace(idx, handler).second;
}
*/