#include "pch.h"
#include "lsquirrel.h"




int squirrel::SQloadfile(uint32_t v, const wchar_t* filename, BOOL printerror)
{
            //wchar_t* 转 char*
    char* fname = DNFTOOL::wchar_tTochar((wchar_t*)filename);

    FILE* file;
    file = fopen(fname, "rb");
    if (file)
    {
        //求得文件的大小
        fseek(file, 0, SEEK_END);
        int size = ftell(file);
        rewind(file);

        //申请一块能装下整个文件的空间 
        char* ar = (char*)malloc(sizeof(char) * size + 256);
        //读文件，每次读一个，共读size次 
        fread(ar, 1, size, file);
        ar[size] = '\0';
        int skey[] = Skey;//定义解密数组

        Cutecode(ar, skey);//解密
        fclose(file);//关闭文件

        wchar_t* te = DNFTOOL::charTowchar_t(ar);
        return SQ_Compilebuffer(v, te, wcslen(te), (wchar_t*)filename, true);
    }
    return -1;
}

int squirrel::SQdofile(uint32_t v, const wchar_t* filename, BOOL retval, BOOL printerror)
{
    if (SQloadfile(v, filename, printerror) >= 0)
    {
        SQPush(v, -2);
        if ((int)SQ_Call(v, 1, retval, 1) >= 0)
        {
            SQ_Remove(v, -(retval != 0) - 1);
            return 1;
        }
        SQPop(v, 1);
    }
    return -1;
}


int squirrel::SQdofileBuffer(uint32_t v, const wchar_t* filename, const wchar_t* str)
{
    //int oldtop = SQGetTop(v);
    if (SQ_Compilebuffer(v, str, wcslen(str), L"interactive console", false) >= 0)
    {
        SQPushRootTable(v);
        SQ_Call(v, 1, 1, 1);
        SQPop(v, 1);
    }
    //SQPop(v, oldtop);
    return -1;
}

int squirrel::LenheartGetIntData(uint32_t v)
{

    return 1;
}

//获取骑乘对象地址
int GetRidingObjectAddress(uint32_t v)
{
    int objAddress = GetSqrObject(v, 2);
    SQPushInt(v, *(int*)(objAddress + 0x54d0));
    return 1;
}
//获取对象地址
int squirrel::GetObjectAddress(uint32_t v)
{
    int objAddress = GetSqrObject(v, 2);
    SQPushInt(v, objAddress);
    return 1;
}
//获取对象名称
int squirrel::GetObjectName(uint32_t v)
{
    int objAddress = GetSqrObject(v, 2);
    int objNameAddress = *(int*)(objAddress + 0x258);
    char* str = DNFTOOL::UnicodeToUtf8((wchar_t*)objNameAddress);
    wchar_t* name = DNFTOOL::charTowchar_t(str);
    free (str);
    SQPushString(v, name, -1);
    delete[]name;
    return 1;
}

//获取对象属性
int squirrel::GetObjectInfo(uint32_t v)
{
    int objAddress;
    SQGetInt(v, 2, &objAddress);
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 4)
    {
        int InfoAddress;
        SQGetInt(v, 3, &InfoAddress);
        int Value = (objAddress + InfoAddress);

        BOOL Type;
        SQGetBool(v, 4, &Type);
        if (Type == TRUE)
        {
            SQPushInt(v, *(int*)Value);
        }
        else if (Type == FALSE)
        {
            SQPushFloat(v, *(FLOAT*)Value);
        }
    }
    else if (ParameterNum == 5)
    {
        int EquAddress;
        SQGetInt(v, 3, &EquAddress);
        int InfoAddress;
        SQGetInt(v, 4, &InfoAddress);
        int Value = (objAddress + EquAddress);
        Value = *(int*)Value;

        Value = Value + InfoAddress;
        BOOL Type;
        SQGetBool(v, 5, &Type);
        if (Type == TRUE)
        {
            SQPushInt(v, *(int*)Value);
        }
        else if (Type == FALSE)
        {
            SQPushFloat(v, *(FLOAT*)Value);
        }
    }
    else
    {
        SQ_Throwerror(v, L"Incorrect function argument");
        return 0;
    }

    return 1;
}

//解密获取对象属性
int squirrel::GetObjectDeInfo(uint32_t v)
{
    int objAddress;
    SQGetInt(v, 2, &objAddress);
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 4)
    {
        int InfoAddress;
        SQGetInt(v, 3, &InfoAddress);
        int Value = (objAddress + InfoAddress);

        BOOL Type;
        SQGetBool(v, 4, &Type);
        if (Type == TRUE)
        {
            SQPushInt(v, DNFTOOL::DNFDeCode(Value));
        }
        else if (Type == FALSE)
        {
            SQPushFloat(v, (FLOAT)DNFTOOL::DNFDeCode(Value));
        }
    }
    else if (ParameterNum == 5)
    {
        int EquAddress;
        SQGetInt(v, 3, &EquAddress);
        int InfoAddress;
        SQGetInt(v, 4, &InfoAddress);
        int Value = (objAddress + EquAddress);
        Value = *(int*)Value;

        Value = Value + InfoAddress;
        BOOL Type;
        SQGetBool(v, 5, &Type);
        if (Type == TRUE)
        {
            SQPushInt(v, DNFTOOL::DNFDeCode(Value));
        }
        else if (Type == FALSE)
        {
            SQPushFloat(v, (FLOAT)DNFTOOL::DNFDeCode(Value));
        }
    }
    else
    {
        SQ_Throwerror(v, L"Incorrect function argument");
        return 0;
    }

    return 1;
}

//设置对象属性
int squirrel::SetObjectInfo(uint32_t v)
{
    int objAddress;
    SQGetInt(v, 2, &objAddress);
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 5)
    {
        int InfoAddress;
        SQGetInt(v, 3, &InfoAddress);
        int Value = (objAddress + InfoAddress);

        BOOL Type;
        SQGetBool(v, 4, &Type);
        if (Type == TRUE)
        {
            int W_Value;
            SQGetInt(v, 5, &W_Value);
            *(int*)Value = W_Value;
        }
        else if (Type == FALSE)
        {
            FLOAT W_Value;
            SQGetFloat(v, 5, &W_Value);
            *(FLOAT*)Value = W_Value;
        }
    }
    else if (ParameterNum == 6)
    {
        int EquAddress;
        SQGetInt(v, 3, &EquAddress);
        int InfoAddress;
        SQGetInt(v, 4, &InfoAddress);
        int Value = (objAddress + EquAddress);
        Value = *(int*)Value;

        Value = Value + InfoAddress;
        BOOL Type;
        SQGetBool(v, 5, &Type);
        if (Type == TRUE)
        {
            int W_Value;
            SQGetInt(v, 6, &W_Value);
            *(int*)Value = W_Value;
        }
        else if (Type == FALSE)
        {
            FLOAT W_Value;
            SQGetFloat(v, 6, &W_Value);
            *(FLOAT*)Value = W_Value;
        }
    }
    else
    {
        SQ_Throwerror(v, L"Incorrect function argument");
        return 0;
    }

    return 1;
}

//加密设置对象属性
int squirrel::SetObjectDeInfo(uint32_t v)
{
    int objAddress;
    SQGetInt(v, 2, &objAddress);
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 5)
    {
        int InfoAddress;
        SQGetInt(v, 3, &InfoAddress);
        int Value = (objAddress + InfoAddress);

        BOOL Type;
        SQGetBool(v, 4, &Type);
        if (Type == TRUE)
        {
            int W_Value;
            SQGetInt(v, 5, &W_Value);
            DNFTOOL::DNFEnCode(Value, W_Value);
        }
        else if (Type == FALSE)
        {
            FLOAT W_Value;
            SQGetFloat(v, 5, &W_Value);
            DNFTOOL::DNFEnCode(Value, (int)W_Value);
        }
    }
    else if (ParameterNum == 6)
    {
        int EquAddress;
        SQGetInt(v, 3, &EquAddress);
        int InfoAddress;
        SQGetInt(v, 4, &InfoAddress);
        int Value = (objAddress + EquAddress);
        Value = *(int*)Value;

        Value = Value + InfoAddress;
        BOOL Type;
        SQGetBool(v, 5, &Type);
        if (Type == TRUE)
        {
            int W_Value;
            SQGetInt(v, 6, &W_Value);
            DNFTOOL::DNFEnCode(Value, W_Value);
        }
        else if (Type == FALSE)
        {
            FLOAT W_Value;
            SQGetFloat(v, 6, &W_Value);
            DNFTOOL::DNFEnCode(Value, (int)W_Value);
        }
    }
    else
    {
        SQ_Throwerror(v, L"Incorrect function argument");
        return 0;
    }

    return 1;
}

//获取对象等级
int squirrel::GetObjectLevel(uint32_t v)
{
    int objAddress = GetSqrObject(v, 2);
    int Level = DNFTOOL::DNFDeCode(objAddress + 0x1A4C);
    SQPushInt(v, Level);
    return 1;
}


//Test


//窗口CALL
typedef void(* NNoticeTCall)(DWORD thisc, DWORD Seat, DWORD a1, wchar_t* a2, DWORD a3, DWORD a4, DWORD a5);
static NNoticeTCall _ANoticeTcall = (NNoticeTCall)0xE6E070;

//打开特殊窗口
typedef void(_fastcall _Open_ExWindow)(int thisc, void*, int a2, char a3, char a4);
static _Open_ExWindow* Open_ExWindow = (_Open_ExWindow*)0xE718A0;
int sq_Open_ExWindow(uint32_t v) {
    int a1, a2, a3, a4;

    SQGetInt(v, 2, &a1);
    SQGetInt(v, 3, &a2);
    SQGetInt(v, 4, &a3);
    SQGetInt(v, 5, &a4);

    Open_ExWindow(a1,0,a2,a3,a4);
    return 0;
}



typedef int(_cdecl _sub7AAB60)(int a1);
static _sub7AAB60* getitemimg = (_sub7AAB60*)0x7aab60;

int squirrel::sq_GetImg(uint32_t v)
{
    int a1;
    SQGetInt(v, 2, &a1);

    int img = getitemimg(a1);
    SQPushInt(v, img);
    return 1;
}

typedef int(_cdecl _sub7AA800)(int a1, int a2, int a3, int a4, int a5, int a6, char a7);
static _sub7AA800* drawimg = (_sub7AA800*)0x7aa800;

//绘制Item
int squirrel::sq_DrawItem(uint32_t v)
{
    int a1, a2, a3, a4, a5, a6, a7;
    SQGetInt(v, 2, &a1);
    SQGetInt(v, 3, &a2);
    SQGetInt(v, 4, &a3);
    SQGetInt(v, 5, &a4);
    SQGetInt(v, 6, &a5);
    SQGetInt(v, 7, &a6);
    SQGetInt(v, 8, &a7);

    int img = getitemimg(a3);
    drawimg(a1, a2, img, a4, a5, a6, a7);

    return 0;
}

typedef int(_fastcall _Load_Npk)(int thisc, void*, int a2, wchar_t* a3);
typedef int(_fastcall _Get_Img)(int thisc, void*, int a2 );
typedef int(_fastcall _Draw_Img)(int thisc, void*, int X,int Y,int Img);
typedef int(_fastcall _Ex_Draw_Img)(DWORD thisc, DWORD Seat, int X, int Y, DWORD Img, int a5, int a6, int a7, int a8, float a9, float a10);
typedef int(_fastcall _Ex_Draw_Num)(DWORD thisc, DWORD Seat, int X, int Y, DWORD Value, DWORD RGBA, int a6);
typedef int(_fastcall _Ex_Draw_Init)(DWORD thisc, DWORD Seat,  DWORD Value, DWORD RGBA, DWORD asadsd,  DWORD asads3d);
typedef int(_cdecl _Get_Draw_This)(DWORD thisc);

static _Load_Npk* Load_Npk = (_Load_Npk*)0x11C0410;
static _Get_Img* Get_Img = (_Get_Img*)0x11AA190;
static _Draw_Img* Draw_Img = (_Draw_Img*)0x11A8F60;
static _Ex_Draw_Img* Ex_Draw_Img = (_Ex_Draw_Img*)0x11A97E0;
static _Ex_Draw_Num* Ex_Draw_Num = (_Ex_Draw_Num*)0x11b2390;
static _Ex_Draw_Init* Ex_Draw_Init = (_Ex_Draw_Init*)0x11B22C0;
static _Get_Draw_This* Get_Draw_This = (_Get_Draw_This*)0x11A5990;


//nut职业调用技能
typedef char(_fastcall _Test)(int thisc, void*, int a2);
static _Test* Test = (_Test*)0x67e8e0;

//typedef char(_fastcall _TestB)(int thisc, void*, int a2,int a3);
//static _TestB* TestB = (_TestB*)0xC9DB40;

typedef char(_fastcall _TestB)(int thisc, void*, int SkillId);
static _TestB* TestB = (_TestB*)0x497900;



typedef  void(_fastcall _120DDB0B)(BYTE* thisc);
static _120DDB0B* Old120DDB0B = (_120DDB0B*)0x120DDB0;

//使用技能
int sq_UseSkill(uint32_t v)
{
    int Key_Value;
    SQGetInt(v, 2, &Key_Value);

    BYTE* thisc = (BYTE*)(*(DWORD*)(0x1B470E0));
    thisc[269 + Key_Value] = 0x80;


    return 0;
}


//选择区域
int sq_Select_MiniMap_Index(uint32_t v) {
    int ReIndex;
    SQGetInt(v, 2, &ReIndex);


    static int dnf_103E030 = 0x103E030;
    static int dnf_287FA88 = 0x287FA88;
    static int dnf_103DCD0 = 0x103DCD0;
    static int dnf_103B240 = 0x103B240;
    static int dnf_2878D95 = 0x2878D95;
    static int dnf_1194FC0 = 0x1194FC0;
    static int dnf_410230 = 0x410230;
    static int dnf_102F4D0 = 0x102F4D0;

    _asm
    {

        mov ecx, dword ptr ds : [0x1A5FB20]
        mov esi, dword ptr ds : [ecx + 0x42DC]
        push ReIndex
        call dnf_103E030
        mov ecx, eax
        call dnf_287FA88
        push ReIndex
        call dnf_103E030
        mov ecx, eax
        call dnf_103DCD0
        mov ecx, eax
        mov dword ptr ds : [esi + 0x24] , eax
        call dnf_103B240
        mov ecx, dword ptr ds : [esi + 0x24]
        call dnf_2878D95
        push 0x1556138
        call dnf_1194FC0
        mov ecx, eax
        add esp, 4
        lea edi, dword ptr ds : [ecx + 2]
        dnf_1031651 :
        mov dx, word ptr ds : [ecx]
        add ecx, 2
        test dx, dx
        jne dnf_1031651
        sub ecx, edi
        sar ecx, 1
        push ecx
        push eax
        lea ecx, dword ptr ds : [esi + 0x144]
        call dnf_410230
        //mov ecx, esi
        //mov dword ptr ds : [esi + 0x28] , 14
        //call dnf_102F4D0
    }
    return 0;
}

int squirrel::sq_Test(uint32_t v)
{


    //int ReIndex;
    //SQGetInt(v, 2, &ReIndex);


    //static int dnf_103E030 = 0x103E030;
    //static int dnf_287FA88 = 0x287FA88;
    //static int dnf_103DCD0 = 0x103DCD0;
    //static int dnf_103B240 = 0x103B240;
    //static int dnf_2878D95 = 0x2878D95;
    //static int dnf_1194FC0 = 0x1194FC0;
    //static int dnf_410230  = 0x410230;
    //static int dnf_102F4D0 = 0x102F4D0;
   
    //_asm 
    //{
    //    
    //    mov ecx, dword ptr ds :[0x1A5FB20]
    //    mov esi, dword ptr ds :[ecx+0x42DC]
    //    push ReIndex
    //    call dnf_103E030                                
    //    mov ecx,eax                                     
    //    call dnf_287FA88                                
    //    push ReIndex
    //    call dnf_103E030                                
    //    mov ecx,eax                                     
    //    call dnf_103DCD0                                
    //    mov ecx,eax                                     
    //    mov dword ptr ds:[esi+0x24],eax                   
    //    call dnf_103B240                                
    //    mov ecx,dword ptr ds:[esi+0x24]                   
    //    call dnf_2878D95                                
    //    push 0x1556138                                
    //    call dnf_1194FC0                                
    //    mov ecx,eax                                     
    //    add esp,4                                       
    //    lea edi,dword ptr ds:[ecx+2] 
    //    dnf_1031651:
    //    mov dx,word ptr ds:[ecx]                        
    //    add ecx,2                                       
    //    test dx,dx                                      
    //    jne dnf_1031651                                 
    //    sub ecx,edi                                     
    //    sar ecx,1                                       
    //    push ecx                                        
    //    push eax                                        
    //    lea ecx,dword ptr ds:[esi+0x144]                  
    //    call dnf_410230                                 
    //    mov ecx,esi                                     
    //    mov dword ptr ds:[esi+0x28],14                    
    //    call dnf_102F4D0                                
    //}
    //int ObjectAddress;
    //int SkillId;
    //SQGetInt(v, 2, &ObjectAddress);
    //SQGetInt(v, 3, &SkillId);
    //Test(ObjectAddress, 0, SkillId);

    //TestB(ObjectAddress, 0, SkillId);

    //std::cout << ret << std::endl;

    int objaddress;

    int Key_Value;
    SQGetInt(v, 2, &objaddress);
    SQGetInt(v, 3, &Key_Value);

    Test(objaddress,0, Key_Value);



    return 0;
}

//设置渲染模式
typedef int(_cdecl _SetDrawImgModel)(int a1 , int a2);
static _SetDrawImgModel* SetDrawImgModel = (_SetDrawImgModel*)0x11A8A50;
//还原渲染模式
typedef int(_cdecl _ReleaseDrawImgModel)();
static _ReleaseDrawImgModel* ReleaseDrawImgModel = (_ReleaseDrawImgModel*)0x11A8B60;

int sq_SetDrawImgModel(uint32_t v)
{
    int M1;
    SQGetInt(v, 2, &M1);
    int M2;
    SQGetInt(v, 3, &M2);

    SetDrawImgModel(M1, M2);

    return 0;
}


int sq_ReleaseDrawImgModel(uint32_t v)
{
    ReleaseDrawImgModel();

    return 0;
}



//绘制Img
int squirrel::sq_DrawImg(uint32_t v)
{
    int Top = SQGetTop(v);
    if (Top == 4) {
        int imgbuf;
        SQGetInt(v, 2, &imgbuf);
        int X;
        SQGetInt(v, 3, &X);
        int Y;
        SQGetInt(v, 4, &Y);
        Draw_Img(*(int*)0x1B45B94, 0, X, Y, imgbuf);
        return 0;
    }
    if (Top == 5)
    {
        wchar_t* File;
        SQGetString(v, 2, &File);
        int Idx;
        SQGetInt(v, 3, &Idx);
        int X;
        SQGetInt(v, 4, &X);
        int Y;
        SQGetInt(v, 5, &Y);


        int npkbuf = Load_Npk(*(int*)0x1B4684C, 0, 0, File);
        int imgbuf = Get_Img(npkbuf, 0, Idx);
        Draw_Img(*(int*)0x1B45B94, 0, X, Y, imgbuf);
        return 0;
    }
    else if (Top == 6) {
        wchar_t* File;
        SQGetString(v, 2, &File);
        int Value;
        SQGetInt(v, 3, &Value);
        int X;
        SQGetInt(v, 4, &X);
        int Y;
        SQGetInt(v, 5, &Y);
        int rgba;
        SQGetInt(v, 6, &rgba);

        int thisc = Get_Draw_This(56);
        int npkbuf = Load_Npk(*(int*)0x1B4684C, 0, 0, File);
        int intibuf = Ex_Draw_Init(thisc, 0, *(int*)0x1B45B94, npkbuf, X, Y);//偏移多少的数值 -1 == 0  所需偏移 -  1
        *(int*)0x1A70190 = intibuf;
        Ex_Draw_Num(intibuf, 0, 100, 100, Value, rgba, 0);


    }
    else if (Top == 9 || Top == 10) {
        wchar_t* File;
        SQGetString(v, 2, &File);
        int Idx;
        SQGetInt(v, 3, &Idx);
        int X;
        SQGetInt(v, 4, &X);
        int Y;
        SQGetInt(v, 5, &Y);
        int Model;
        SQGetInt(v, 6, &Model);
        int rgba;
        SQGetInt(v, 7, &rgba);

        int Xf;
        SQGetFloat(v, 8,  (FLOAT*) & Xf);
        int Yf;
        SQGetFloat(v, 9, (FLOAT*) & Yf);

        int npkbuf = Load_Npk(*(int*)0x1B4684C, 0, 0, File);
        int imgbuf = Get_Img(npkbuf, 0, Idx);
        Ex_Draw_Img(*(int*)0x1B45B94, 0, X, Y, imgbuf, Xf, Yf, Model, rgba, 0, 0);
   

        return 0;
    }



    return 0;
}
typedef struct DamageFontInfo
{
    int Type[8][10];

} DamageFontInfo;
static DamageFontInfo MyFont = { 0 };

int squirrel::sq_IntiNumberDraw(uint32_t v)
{
    wchar_t* File;
    SQGetString(v, 2, &File);
    int Idx;
    SQGetInt(v, 3, &Idx);
    int Type;
    SQGetInt(v, 4, &Type);

    int npkbuf = Load_Npk(*(int*)0x1B4684C, 0, 0, File);
    
    for (size_t i = 0; i < 10; i++)
    {
        int imgbuf = Get_Img(npkbuf, 0, Idx + i);
        MyFont.Type[Type][i] = imgbuf;
    }

    return 0;
}
int squirrel::sq_DrawNumber(uint32_t v)
{
    int X;
    SQGetInt(v, 2, &X);
    int Y;
    SQGetInt(v, 3, &Y);
    int rgba;
    SQGetInt(v, 4, &rgba);

    int Xf;
    SQGetFloat(v, 5, (FLOAT*)&Xf);
    int Yf;
    SQGetFloat(v, 6, (FLOAT*)&Yf);
    int Type;
    SQGetInt(v, 7, &Type);
    int Interval;
    SQGetInt(v, 8, &Interval);
    int OneOffset;
    SQGetInt(v, 9, &OneOffset);
    int Number;
    SQGetInt(v, 10, &Number);

    std::string Value = std::to_string(Number);
    
    for (size_t i = 0; i < Value.size(); i++)
    {
        int Idx = Value[i] - 48;
        if (Idx == 1)Ex_Draw_Img(*(int*)0x1B45B94, 0, X + (i * Interval) + OneOffset, Y, MyFont.Type[Type][Idx], Xf, Yf, 0, rgba, 0, 0);
        else Ex_Draw_Img(*(int*)0x1B45B94, 0, X + (i * Interval), Y, MyFont.Type[Type][Idx], Xf, Yf, 0, rgba, 0, 0);
    }
    //Ex_Draw_Img(*(int*)0x1B45B94, 0, X, Y, imgbuf, Xf, Yf, Model, rgba, 0, 0);

    return 0;
}

static std::map<std::string, int>LenheartCodeWidth;

int sq_GetStringDrawLength(uint32_t v) {

    wchar_t* Str;
    SQGetString(v, 2, &Str);

    char* OutPutText = DNFTOOL::SquirrelU2W(Str);
    std::string RealStr = OutPutText;
    delete[]OutPutText;

    if (!LenheartCodeWidth.count("0")) {
        LenheartCodeWidth["0"] = 7;
        LenheartCodeWidth["1"] = 5;
        LenheartCodeWidth["2"] = 7;
        LenheartCodeWidth["3"] = 7;
        LenheartCodeWidth["4"] = 7;
        LenheartCodeWidth["5"] = 7;
        LenheartCodeWidth["6"] = 7;
        LenheartCodeWidth["7"] = 6;
        LenheartCodeWidth["8"] = 7;
        LenheartCodeWidth["9"] = 7;
        LenheartCodeWidth["z"] = 6;
        LenheartCodeWidth["y"] = 8;
        LenheartCodeWidth["x"] = 7;
        LenheartCodeWidth["w"] = 7;
        LenheartCodeWidth["v"] = 7;
        LenheartCodeWidth["u"] = 8;
        LenheartCodeWidth["t"] = 6;
        LenheartCodeWidth["s"] = 6;
        LenheartCodeWidth["r"] = 7;
        LenheartCodeWidth["q"] = 7;
        LenheartCodeWidth["p"] = 7;
        LenheartCodeWidth["o"] = 6;
        LenheartCodeWidth["n"] = 8;
        LenheartCodeWidth["m"] = 7;
        LenheartCodeWidth["l"] = 7;
        LenheartCodeWidth["k"] = 7;
        LenheartCodeWidth["j"] = 6;
        LenheartCodeWidth["i"] = 5;
        LenheartCodeWidth["h"] = 8;
        LenheartCodeWidth["g"] = 7;
        LenheartCodeWidth["f"] = 7;
        LenheartCodeWidth["e"] = 6;
        LenheartCodeWidth["d"] = 7;
        LenheartCodeWidth["c"] = 6;
        LenheartCodeWidth["b"] = 7;
        LenheartCodeWidth["a"] = 7;
        LenheartCodeWidth["A"] = 8;
        LenheartCodeWidth["B"] = 7;
        LenheartCodeWidth["C"] = 7;
        LenheartCodeWidth["D"] = 7;
        LenheartCodeWidth["E"] = 7;
        LenheartCodeWidth["F"] = 7;
        LenheartCodeWidth["G"] = 8;
        LenheartCodeWidth["H"] = 8;
        LenheartCodeWidth["I"] = 5;
        LenheartCodeWidth["J"] = 8;
        LenheartCodeWidth["K"] = 8;
        LenheartCodeWidth["L"] = 8;
        LenheartCodeWidth["M"] = 8;
        LenheartCodeWidth["N"] = 8;
        LenheartCodeWidth["O"] = 7;
        LenheartCodeWidth["P"] = 7;
        LenheartCodeWidth["Q"] = 7;
        LenheartCodeWidth["R"] = 8;
        LenheartCodeWidth["S"] = 7;
        LenheartCodeWidth["T"] = 7;
        LenheartCodeWidth["U"] = 8;
        LenheartCodeWidth["V"] = 8;
        LenheartCodeWidth["W"] = 7;
        LenheartCodeWidth["X"] = 7;
        LenheartCodeWidth["Y"] = 7;
        LenheartCodeWidth["Z"] = 7;
        LenheartCodeWidth[","] = 4;
        LenheartCodeWidth["，"] = 4;
        LenheartCodeWidth["."] = 3;
        LenheartCodeWidth["。"] = 6;
        LenheartCodeWidth["!"] = 3;
        LenheartCodeWidth["！"] = 4;
        LenheartCodeWidth["?"] = 7;
        LenheartCodeWidth["？"] = 8;
        LenheartCodeWidth[";"] = 3;
        LenheartCodeWidth["；"] = 4;
        LenheartCodeWidth["["] = 5;
        LenheartCodeWidth["]"] = 5;
        LenheartCodeWidth["{"] = 5;
        LenheartCodeWidth["}"] = 5;
        LenheartCodeWidth["("] = 5;
        LenheartCodeWidth[")"] = 5;
        LenheartCodeWidth["\""] = 6;
        LenheartCodeWidth["“"] = 7;
        LenheartCodeWidth["”"] = 7;
        LenheartCodeWidth["\""] = 4;
        LenheartCodeWidth["’"] = 4;
        LenheartCodeWidth["+"] = 7;
        LenheartCodeWidth["-"] = 8;
        LenheartCodeWidth["*"] = 7;
        LenheartCodeWidth["/"] = 8;
        LenheartCodeWidth["="] = 8;
    }

    int L = 0;

    int StrTextLength = 0;
    for (size_t i = 0; i < RealStr.length();)
    {
        int cplen = 1;
        if (RealStr[i] < 0)cplen = 2;
        std::string c = RealStr.substr(i, cplen);
        if (LenheartCodeWidth.count(c)) {
            L += LenheartCodeWidth[c];
        }
        else {
            L += 13;
        }
        i += cplen;
        StrTextLength++;
    }
    L -= StrTextLength;

    SQPushInt(v, L);
    return 1;
}


char* GBKTOUTF8(std::string& strGBK)//转码 GBK编码转成UTF8编码
{
    int len = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
    wchar_t* wszUtf8 = new wchar_t[len];
    memset(wszUtf8, 0, len);
    MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, wszUtf8, len);
    len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
    char* szUtf8 = new char[len + 1];
    memset(szUtf8, 0, len + 1);
    WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL, NULL);
    strGBK = szUtf8;
    delete[] szUtf8;
    delete[] wszUtf8;
    return (char*)strGBK.c_str();
}

#include "include/squirrel.h"
int sq_GetStringDrawArray(uint32_t v) {

    wchar_t* Str;
    SQGetString(v, 2, &Str);

    int Len;
    SQGetInt(v, 3, &Len);

    char* OutPutText = DNFTOOL::SquirrelU2W(Str);
    std::string RealStr = OutPutText;
    delete[]OutPutText;

    if (!LenheartCodeWidth.count("0")) {
        LenheartCodeWidth["0"] = 7;
        LenheartCodeWidth["1"] = 5;
        LenheartCodeWidth["2"] = 7;
        LenheartCodeWidth["3"] = 7;
        LenheartCodeWidth["4"] = 7;
        LenheartCodeWidth["5"] = 7;
        LenheartCodeWidth["6"] = 7;
        LenheartCodeWidth["7"] = 6;
        LenheartCodeWidth["8"] = 7;
        LenheartCodeWidth["9"] = 7;
        LenheartCodeWidth["z"] = 6;
        LenheartCodeWidth["y"] = 8;
        LenheartCodeWidth["x"] = 7;
        LenheartCodeWidth["w"] = 7;
        LenheartCodeWidth["v"] = 7;
        LenheartCodeWidth["u"] = 8;
        LenheartCodeWidth["t"] = 6;
        LenheartCodeWidth["s"] = 6;
        LenheartCodeWidth["r"] = 7;
        LenheartCodeWidth["q"] = 7;
        LenheartCodeWidth["p"] = 7;
        LenheartCodeWidth["o"] = 6;
        LenheartCodeWidth["n"] = 8;
        LenheartCodeWidth["m"] = 7;
        LenheartCodeWidth["l"] = 7;
        LenheartCodeWidth["k"] = 7;
        LenheartCodeWidth["j"] = 6;
        LenheartCodeWidth["i"] = 5;
        LenheartCodeWidth["h"] = 8;
        LenheartCodeWidth["g"] = 7;
        LenheartCodeWidth["f"] = 7;
        LenheartCodeWidth["e"] = 6;
        LenheartCodeWidth["d"] = 7;
        LenheartCodeWidth["c"] = 6;
        LenheartCodeWidth["b"] = 7;
        LenheartCodeWidth["a"] = 7;
        LenheartCodeWidth["A"] = 8;
        LenheartCodeWidth["B"] = 7;
        LenheartCodeWidth["C"] = 7;
        LenheartCodeWidth["D"] = 7;
        LenheartCodeWidth["E"] = 7;
        LenheartCodeWidth["F"] = 7;
        LenheartCodeWidth["G"] = 8;
        LenheartCodeWidth["H"] = 8;
        LenheartCodeWidth["I"] = 5;
        LenheartCodeWidth["J"] = 8;
        LenheartCodeWidth["K"] = 8;
        LenheartCodeWidth["L"] = 8;
        LenheartCodeWidth["M"] = 8;
        LenheartCodeWidth["N"] = 8;
        LenheartCodeWidth["O"] = 7;
        LenheartCodeWidth["P"] = 7;
        LenheartCodeWidth["Q"] = 7;
        LenheartCodeWidth["R"] = 8;
        LenheartCodeWidth["S"] = 7;
        LenheartCodeWidth["T"] = 7;
        LenheartCodeWidth["U"] = 8;
        LenheartCodeWidth["V"] = 8;
        LenheartCodeWidth["W"] = 7;
        LenheartCodeWidth["X"] = 7;
        LenheartCodeWidth["Y"] = 7;
        LenheartCodeWidth["Z"] = 7;
        LenheartCodeWidth[","] = 4;
        LenheartCodeWidth["，"] = 4;
        LenheartCodeWidth["."] = 3;
        LenheartCodeWidth["。"] = 6;
        LenheartCodeWidth["!"] = 3;
        LenheartCodeWidth["！"] = 4;
        LenheartCodeWidth["?"] = 7;
        LenheartCodeWidth["？"] = 8;
        LenheartCodeWidth[";"] = 3;
        LenheartCodeWidth["；"] = 4;
        LenheartCodeWidth["["] = 5;
        LenheartCodeWidth["]"] = 5;
        LenheartCodeWidth["{"] = 5;
        LenheartCodeWidth["}"] = 5;
        LenheartCodeWidth["("] = 5;
        LenheartCodeWidth[")"] = 5;
        LenheartCodeWidth["\""] = 6;
        LenheartCodeWidth["“"] = 7;
        LenheartCodeWidth["”"] = 7;
        LenheartCodeWidth["\""] = 4;
        LenheartCodeWidth["’"] = 4;
        LenheartCodeWidth["+"] = 7;
        LenheartCodeWidth["-"] = 8;
        LenheartCodeWidth["*"] = 7;
        LenheartCodeWidth["/"] = 8;
        LenheartCodeWidth["="] = 8;
    }

    std::vector<std::string>StrBuf;

    int StrTextLength = 0;
    for (size_t i = 0; i < RealStr.length();)
    {
        int cplen = 1;
        if (RealStr[i] < 0)cplen = 2;
        std::string c = RealStr.substr(i, cplen);

        StrBuf.push_back(c);
        i += cplen;
    }

    std::vector<std::string>PushStrBuf;
    std::string Ostr = "";
    for (unsigned int i = 0; i < StrBuf.size(); ++i)
    {
        int AddLen = 0;
        if (LenheartCodeWidth.count(StrBuf[i])) {
            AddLen = LenheartCodeWidth[StrBuf[i]];
        }
        else {
            AddLen = 13;
        }

        //超过了
        if (AddLen + StrTextLength > Len) {
            PushStrBuf.push_back(Ostr);
            StrTextLength = AddLen;
            Ostr = StrBuf[i];
        }
        else {
            Ostr += StrBuf[i];
            StrTextLength += AddLen;
            //最后一段
            if (i == StrBuf.size() - 1) {
                PushStrBuf.push_back(Ostr);
            }
        }

    }

    sq_newarray((HSQUIRRELVM)v, 0);

    for (unsigned int i = 0; i < PushStrBuf.size(); ++i)
    {
        std::string Buf = PushStrBuf[i];
        char* ss = GBKTOUTF8(Buf);
        //wchar_t* name = DNFTOOL::charTowchar_t((char*)Buf.c_str());

        wchar_t* aa = DNFTOOL::charTowchar_t(ss);
        //wchar_t* aa = DNFTOOL::SquirrelW2W(ss);
        SQPushString(v, aa, -1);
        
        sq_arrayappend((HSQUIRRELVM)v, -2);
        //delete[]ss;
        //delete[]name;

    }


    //sq_newarray(v, 3);
    return 1;
}


int sq_DrawWindow(uint32_t v)
{
    int Top = SQGetTop(v);
    int X;
    SQGetInt(v, 2, &X);
    int Y;
    SQGetInt(v, 3, &Y);
    int WindowWidth;
    SQGetInt(v, 4, &WindowWidth);
    int WindowHeight;
    SQGetInt(v, 5, &WindowHeight);

    //路径
    wchar_t* File = L"interface/windowcommon.img";
    int StartIdx = 0;

    int leftWidth = 11;//最左边的拼接的宽度
    int centerWidth = 12;//中间部分拼接的宽度

    int topHeight = 11;//最上面拼接的高度
    int centerHeight = 13;//中间部分拼接的高度



    if (Top == 11) {
        wchar_t* bFile;
        SQGetString(v, 6, &bFile);
        int Idx;
        SQGetInt(v, 7, &Idx);
        File = bFile;
        StartIdx = Idx;

        SQGetInt(v, 8, &leftWidth);
        SQGetInt(v, 9, &centerWidth);
        SQGetInt(v, 10, &topHeight);
        SQGetInt(v, 11, &centerHeight);
    }

    int widthCount = round(WindowWidth / centerWidth);
    int heightCount = round(WindowHeight / centerWidth);

    int npkbuf = Load_Npk(*(int*)0x1B4684C, 0, 0, File);
    int leftTopFrame = Get_Img(npkbuf, 0, 0 + StartIdx);
    int topCenterFrame = Get_Img(npkbuf, 0, 1 + StartIdx);
    int rightTopFrame = Get_Img(npkbuf, 0, 2 + StartIdx);
    int leftCenterFrame = Get_Img(npkbuf, 0, 3 + StartIdx);
    int centerFrame = Get_Img(npkbuf, 0, 4 + StartIdx);
    int rightCenterFrame = Get_Img(npkbuf, 0, 5 + StartIdx);
    int leftBottomFrame = Get_Img(npkbuf, 0, 6 + StartIdx);
    int centerBottomFrame = Get_Img(npkbuf, 0, 7 + StartIdx);
    int rightBottomFrame = Get_Img(npkbuf, 0, 8 + StartIdx);



    Draw_Img(*(int*)0x1B45B94, 0, X, Y, leftTopFrame);

    for (int i = 0; i < widthCount; i++) {
        Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * i, Y, topCenterFrame);
    }

    Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * widthCount, Y, rightTopFrame);


    //拼接中间和下面部分的
    //j为往下数的第几行
    for (int j = 0; j < heightCount; j++) {
        Draw_Img(*(int*)0x1B45B94, 0, X, Y + topHeight + centerHeight * j, leftCenterFrame);
        //绘制最左边的部分

        if (j == 0)
            Draw_Img(*(int*)0x1B45B94, 0, X, Y + topHeight + centerHeight * heightCount, leftBottomFrame);
        //这是绘制最下左边的拐角

    //绘制中间的部分
        for (int i = 0; i < widthCount; i++) {
            Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * i, Y + topHeight + centerHeight * j, centerFrame);
            if (j == 0)
                Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * i, Y + topHeight + centerHeight * heightCount, centerBottomFrame);
            //绘制中间最下面的部分
        }

        Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * widthCount, Y + topHeight + centerHeight * j, rightCenterFrame);
        //绘制最右边的部分

        if (j == 0)
            Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * widthCount, Y + topHeight + centerHeight * heightCount, rightBottomFrame);
        //绘制最右边最下面拐角处

    }


    if (heightCount == 0) {
        Draw_Img(*(int*)0x1B45B94, 0, X, Y + topHeight + centerHeight * heightCount, leftBottomFrame);
        for (int i = 0; i < widthCount; i++) {
            Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * i, Y + topHeight + centerHeight * heightCount, centerBottomFrame);
        }
        //绘制中间最下面的部分
        Draw_Img(*(int*)0x1B45B94, 0, X + leftWidth + centerWidth * widthCount, Y + topHeight + centerHeight * heightCount, rightBottomFrame);
        //绘制最右边最下面拐角处
    }


    return 0;
}


int sq_DrawButton(uint32_t v)
{
    int Top = SQGetTop(v);
    int X;
    SQGetInt(v, 2, &X);
    int Y;
    SQGetInt(v, 3, &Y);
    int WindowWidth;
    SQGetInt(v, 4, &WindowWidth);
    wchar_t* File;
    SQGetString(v, 5, &File);
    int StartIdx;
    SQGetInt(v, 6, &StartIdx);

    int widthCount = round(WindowWidth / 2);

    int npkbuf = Load_Npk(*(int*)0x1B4684C, 0, 0, File);
    int leftFrame = Get_Img(npkbuf, 0, 0 + StartIdx);
    int CenterFrame = Get_Img(npkbuf, 0, 1 + StartIdx);
    int rightFrame = Get_Img(npkbuf, 0, 2 + StartIdx);




    Draw_Img(*(int*)0x1B45B94, 0, X, Y, leftFrame);

    for (int i = 0; i < widthCount; i++) {
        Draw_Img(*(int*)0x1B45B94, 0, X + 28 + 2 * i, Y, CenterFrame);
    }

    Draw_Img(*(int*)0x1B45B94, 0, X + 28 + 2 * widthCount, Y, rightFrame);

    return 0;
}


std::string& ReplaceAll(std::string& str, const std::string& src, const std::string& dst) {
    std::string::size_type pos(0);
    while (true) {
        if ((pos = str.find(src)) != std::string::npos) {
            str.replace(pos, src.length(), dst);
        }
        else {
            break;
        }
    }
    return str;
}
int DecondeJson(uint32_t v) {
    wchar_t* OutPutBuffer;
    SQGetString(v, 2, &OutPutBuffer);
    //char* OutPutText = DNFTOOL::SquirrelU2W(OutPutBuffer);
    char* OutPutText = DNFTOOL::wchar_tTochar(OutPutBuffer);
    std::string str = OutPutText;
    delete[]OutPutText;

    size_t pos = 0;
    bool Ars = false;
    while ((pos = str.find("\"", pos)) != std::string::npos) {
        // 判断双引号是否在Key的位置
        if (pos > 0) {
            if (str[pos - 1] == '[') {
                Ars = true;
            }
            if ((str[pos - 1] == '{' || str[pos + 1] == ':' || str[pos - 1] == ',') && !Ars) {
                // 删除双引号
                str.erase(pos, 1);
            }
            else {
                pos++;
            }
            if (str[pos] == ']') {
                Ars = false;
            }
        }
        else {
            pos++;
        }
    }

    str = ReplaceAll(str, ":", "=");

    wchar_t* ss = DNFTOOL::charTowchar_t((char*)str.c_str());
    SQPushString(v, ss, -1);
    delete[]ss;

    return 1;
}
std::string EncodeARRAY(HSQUIRRELVM v, std::string Jso);
std::string EncodeTABLE(HSQUIRRELVM v, std::string Jso);

std::string EncodeARRAY(HSQUIRRELVM v, std::string Jso) {
    Jso += "[";

    sq_pushnull((HSQUIRRELVM)v);  // null iterator
    while (SQ_SUCCEEDED(sq_next((HSQUIRRELVM)v, -2)))
    {
        //const SQChar* Key;
        //sq_getstring((HSQUIRRELVM)v, -2, &Key);


        //char* OutPutText = DNFTOOL::SquirrelU2W(Key);
        //std::string str = OutPutText;
        //delete[]OutPutText;

        //Jso += "\"";
        //Jso += str;
        //Jso += "\"";
        //Jso += ":";

        SQObjectType Type = sq_gettype((HSQUIRRELVM)v, -1);
        switch (Type)
        {
        case OT_INTEGER: {
            SQInteger value;
            sq_getinteger(v, -1, &value);
            Jso += std::to_string(value);
            Jso += ",";
        }
                       break;
        case OT_FLOAT:
        {
            SQFloat value;
            sq_getfloat(v, -1, &value);
            Jso += std::to_string(value);
            Jso += ",";
        }
        break;
        case OT_BOOL:
        {
            SQBool value;
            sq_getbool(v, -1, &value);
            Jso += std::to_string(value);
            Jso += ",";
        }
        break;
        case OT_STRING:
        {
            const SQChar* value;
            sq_getstring((HSQUIRRELVM)v, -1, &value);

            char* vOutPutText = DNFTOOL::SquirrelU2W(value);
            std::string vstr = vOutPutText;
            delete[]vOutPutText;
            Jso += "\"";
            Jso += vstr;
            Jso += "\"";
            Jso += ",";
        }
        break;
        case OT_TABLE:
        {
            SQInteger Top = sq_gettop(v);
            SQObject obj;
            sq_getstackobj(v, -1, &obj);
            sq_pushobject(v, obj);
            Jso = EncodeTABLE(v, Jso);
            sq_settop(v, Top);
            Jso += ",";
        }
        break;
        case OT_ARRAY:
        {
            SQInteger Top = sq_gettop(v);
            SQObject obj;
            sq_getstackobj(v, -1, &obj);
            sq_pushobject(v, obj);
            Jso = EncodeARRAY(v, Jso);
            sq_settop(v, Top);
            Jso += ",";
        }
            break;
        default:
            break;
        }

        //这里-1是值，-2是键
        sq_pop((HSQUIRRELVM)v, 2); //在下一次迭代之前弹出键和值
    }
    sq_pop((HSQUIRRELVM)v, 1); //在下一次迭代之前弹出键和值
    Jso = Jso.substr(0, Jso.length() - 1);
    Jso += "]";
    return Jso;
}

std::string EncodeTABLE(HSQUIRRELVM v,std::string Jso) {

    Jso += "{";

    sq_pushnull((HSQUIRRELVM)v);  // null iterator
    while (SQ_SUCCEEDED(sq_next((HSQUIRRELVM)v, -2)))
    {
        const SQChar* Key;
        sq_getstring((HSQUIRRELVM)v, -2, &Key);


        char* OutPutText = DNFTOOL::SquirrelU2W(Key);
        std::string str = OutPutText;
        delete[]OutPutText;

        Jso += "\"";
        Jso += str;
        Jso += "\"";
        Jso += ":";

        SQObjectType Type = sq_gettype((HSQUIRRELVM)v, -1);
        switch (Type)
        {
        case OT_INTEGER: {
            SQInteger value;
            sq_getinteger(v, -1, &value);
            Jso += std::to_string(value);
            Jso += ",";
        }
            break;
        case OT_FLOAT:
        {
            SQFloat value;
            sq_getfloat(v, -1, &value);
            Jso += std::to_string(value);
            Jso += ",";
        }
            break;
        case OT_BOOL:
        {
            SQBool value;
            sq_getbool(v, -1, &value);
            Jso += std::to_string(value);
            Jso += ",";
        }
            break;
        case OT_STRING:
        {
            const SQChar* value;
            sq_getstring((HSQUIRRELVM)v, -1, &value);

            char* vOutPutText = DNFTOOL::SquirrelU2W(value);
            std::string vstr = vOutPutText;
            delete[]vOutPutText;
            Jso += "\"";
            Jso += vstr;
            Jso += "\"";
            Jso += ",";
        }
            break;
        case OT_TABLE:
        {
            SQInteger Top = sq_gettop(v);
            SQObject obj;
            sq_getstackobj(v, -1, &obj);
            sq_pushobject(v, obj);
            Jso = EncodeTABLE(v, Jso);
            sq_settop(v, Top);
            Jso += ",";
        }
            break;
        case OT_ARRAY:
        {
            SQInteger Top = sq_gettop(v);
            SQObject obj;
            sq_getstackobj(v, -1, &obj);
            sq_pushobject(v, obj);
            Jso = EncodeARRAY(v, Jso);
            sq_settop(v, Top);
            Jso += ",";
        }
            break;
        default:
            break;
        }

        //这里-1是值，-2是键
        sq_pop((HSQUIRRELVM)v, 2); //在下一次迭代之前弹出键和值
    }
    sq_pop((HSQUIRRELVM)v, 1); //在下一次迭代之前弹出键和值
    Jso = Jso.substr(0, Jso.length() - 1);
    Jso += "}";
    return Jso;
}

int EncondeJson(uint32_t v) {

    std::string Jso = "";
    std::string RealJso = EncodeTABLE((HSQUIRRELVM) v, Jso);
    char* sbuf = GBKTOUTF8(RealJso);
    wchar_t* ss = DNFTOOL::charTowchar_t(sbuf);
    SQPushString(v, ss, -1);
    return 1;
}


//加载IMG帧
//preloadnpk(*(int*)0x1b4684c,img,0)
//getimg(preloadnpk, 帧)
//huizhi(*(int*)1B45B94,x,y, getimg)

//换装参数Call
typedef int _E49DB0();
static _E49DB0* FuncE49DB0 = (_E49DB0*)0xE49DB0;
int squirrel::sq_Switching(uint32_t v)
{
    DWORD V2 = FuncE49DB0();
    SQPushInt(v, V2);
    return 1;
}

typedef int __fastcall sub_TTTTT(int a1,int a2,int a3);
static sub_TTTTT* Funcsub_GetSkillAddress = (sub_TTTTT*)0x8406C0;
int squirrel::sq_GetSkillAddress(uint32_t v)
{
    int SkillId;
    SQGetInt(v, 2, &SkillId);
    DWORD ADDRESS = Funcsub_GetSkillAddress(*(int*)0x1AB7CDC,0, SkillId);
    SQPushInt(v, ADDRESS);
    return 1;
}

typedef  void(_fastcall _DrawSkill)(int a1, int seat, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11);
static _DrawSkill* DrawSkill = (_DrawSkill*)0x909d70;
int sq_DrawSkill(uint32_t v)
{
    int a1, x, y, a4, a5, a6, a7, a8, a9,a10,a11;


    //a1技能地址
    SQGetInt(v, 2, &a1);

    SQGetInt(v, 3, &x);
    SQGetInt(v, 4, &y);

    SQGetInt(v, 5, &a4);
    SQGetInt(v, 6, &a5);
    SQGetInt(v, 7, &a6);
    SQGetInt(v, 8, &a7);
    SQGetInt(v, 9, &a8);
    SQGetInt(v, 10, &a9);
    SQGetInt(v, 11, &a10);

    DrawSkill(a1,0,*(int*)0x1AB7CDC,x,y,a4,a5,a6,a7,a8,a9,a10);
    return 1;
}



int squirrel::sq_GetPlayerEachName(uint32_t v)
{
    int objNameAddress = *(int*)(0x1ade0e0);
    char* str = DNFTOOL::UnicodeToUtf8((wchar_t*)(objNameAddress + 0x20));
    wchar_t* name = DNFTOOL::charTowchar_t(str);
    free(str);
    SQPushString(v, name, -1);
    delete[]name;

    return 1;
}

int squirrel::sq_OpenConsole(uint32_t v)
{
    wchar_t* OutPutBuffer;
    SQGetString(v, 2, &OutPutBuffer);
    char* OutPutText = DNFTOOL::SquirrelU2W(OutPutBuffer);

    AllocConsole();
    SetConsoleTitleA(OutPutText);
    //SetConsoleOutputCP(65001);
    freopen(("CONOUT$"), ("w"), stdout);
    freopen(("CONOUT$"), ("w"), stderr);
    freopen(("CONIN$"), ("r"), stdin);

    return 0;
}




int squirrel::sq_MoveMap(uint32_t v)
{
    int dct;
    SQGetInt(v, 2, &dct);
    DWORD Address1 = 0x1A5FB18;
    DWORD Address2 = 0x7CE9E0;
    _asm
    {
        mov ecx, [Address1]
        mov ecx, [ecx]
        mov ecx, [ecx + 0x20a050]
        mov ecx, [ecx + 0x4c]
        push  0x1
        push  0x1
        push  0x0
        push  0x0
        push  0x0
        push  0x0
        push  0x0
        push  dct
        call Address2
    }
    return 0;
}

//输出
int squirrel::Sout(uint32_t v)
{
    int Top = SQGetTop(v);
    if (Top <= 0)
    {
        SQ_Throwerror(v, L"Incorrect function argument");
        return 0;
    }

    wchar_t* OutPutBuffer;
    SQGetString(v, 2, &OutPutBuffer);
    char* OutPutText = DNFTOOL::SquirrelU2W(OutPutBuffer);

    std::vector<std::string> Data;
    DNFTOOL::Split(OutPutText, Data, "%L");
    delete []OutPutText;

    if (Top != Data.size() + 1)
    {
        SQ_Throwerror(v, L"Incorrect function argument");
        return 0;
    }

    size_t Fnum = Data.size();
    if (Fnum > 1)Fnum -= 1;
    std::string Text = "";
    for (size_t i = 0; i < Fnum; i++)
    {
        std::string Parameter;
        //获取值的类型
        int ValueType = SQ_GetType(v, 3 + i);
        //判断值类型
        switch (ValueType)
        {
        case OT_INTEGER://int类型
        {
            int Value;
            SQGetInt(v, 3 + i, &Value);
            Parameter = std::to_string(Value);
            break;
        }
        case OT_FLOAT://float类型
        {
            FLOAT Value;
            SQGetFloat(v, 3 + i, &Value);
            std::ostringstream oss;
            oss << Value;
            std::string str(oss.str());
            Parameter = str;
            break;
        }
        case OT_BOOL://bool类型
        {
            BOOL Value;
            SQGetBool(v, 3 + i, &Value);
            switch (Value)
            {
            case true:
                Parameter = "true";
                break;
            case false:
                Parameter = "false";
                break;
            }
            //Parameter = std::to_string(Value);
            break;
        }
        case OT_STRING://string类型
        {
            wchar_t* Value;
            SQGetString(v, 3 + i, &Value);
            char* str = DNFTOOL::SquirrelU2W(Value);
            Parameter = str;
            delete []str;
            break;
        }
        default:
            Parameter = " ";
            break;
        }
        Text += Data[i];
        Text += Parameter;
    }
    std::cout << Text << std::endl;
    return 0;
}

int squirrel::Clock(uint32_t v)
{
    SQPushInt(v, clock());
    return 1;
}

//读人物 或 装备属性
int squirrel::GetCharacterAttribute(uint32_t v)
{
    int n1, n2;
    int num = SQGetTop(v);

    int CharAddr = *(int*)(0x1AB7CDC);
    if (num == 3)
    {
        SQGetInt(v, 2, &n1);
        SQGetInt(v, 3, &n2);
        int TValue = *(int*)(CharAddr+DNFTOOL::GetEquAddr(n2));
        int SValue = (TValue + n1);
        if(SValue < 0x400000)
            SQPushInt(v, 0);
        else if (n1 != 0x8 && n1 != 0x1C && n1 != 0xF4 && n1 != 0x854)
            SQPushInt(v, (DNFTOOL::DNFDeCode(SValue)));
        else
            SQPushInt(v, (*(int*)(SValue)));
    }
    else if (num == 2)
    {
        SQGetInt(v, 2, &n1);
        int Value = (CharAddr + n1);

        SQPushInt(v, (DNFTOOL::DNFDeCode(Value)));
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}
//写人物 或 装备属性
int squirrel::SetCharacterAttribute(uint32_t v)
{
    int n1, n2, n3;

    int num = SQGetTop(v);

    int CharAddr = *(int*)(0x1AB7CDC);
    if (num == 4)
    {
        SQGetInt(v, 2, &n1);
        SQGetInt(v, 3, &n2);
        SQGetInt(v, 4, &n3);

        int TValue = *(int*)(CharAddr + DNFTOOL::GetEquAddr(n2));
        int SValue = (TValue + n1);
        if (SValue < 0x400000)
            SQPushBool(v, false);
        else if (n1 != 0x8 && n1 != 0x1C && n1 != 0xF4)
            DNFTOOL::DNFEnCode(SValue, n3);
        else
            *(int*)SValue = n3;
        SQPushBool(v, true);
    }
    else if (num == 3)
    {
        SQGetInt(v, 2, &n1);
        SQGetInt(v, 3, &n2);

        int Value = (CharAddr + n1);
        DNFTOOL::DNFEnCode(Value, n2);
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }

    return 1;
}
//获取城镇编号
int squirrel::GetTownIndex(uint32_t v)
{
    SQPushInt(v, DNFTOOL::GetHook(0x1A5E258, "0xAC+0xD4+"));
    return 1;
}
//获取城镇区域编号
int squirrel::GetRegionIndex(uint32_t v)
{
    SQPushInt(v, *(int*)(DNFTOOL::GetHook(0x1A5E258, "0xAC+0xD8+")));
    return 1;
}
//获取城镇X坐标
int squirrel::GetTownXpos(uint32_t v)
{
    SQPushInt(v, DNFTOOL::GetHook(0x1AB7CE0, "0x2BC+"));
    return 1;
}
//获取城镇Y坐标
int squirrel::GetTownYpos(uint32_t v)
{
    SQPushInt(v, DNFTOOL::GetHook(0x1AB7CE0, "0x2C0+"));
    return 1;
}
//获取疲劳值
int squirrel::GetFatigue(uint32_t v)
{
    int Type;

    int num = SQGetTop(v);
    if (num == 2)
    {
        SQGetInt(v, 2, &Type);

        switch (Type)
        {
        case 0:
            SQPushInt(v, DNFTOOL::DNFDeCode(0x1AB7E1C));
            break;
        case 1:
            SQPushInt(v, DNFTOOL::DNFDeCode(0x1AB7E10));
            break;
        default:
            SQPushBool(v,false);
            break;
        }
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}
//获取经验值
int squirrel::GetExp(uint32_t v)
{
    int Type;

    int num = SQGetTop(v);
    if (num == 2)
    {
        SQGetInt(v, 2, &Type);

        switch (Type)
        {
        case 0:
            SQPushInt(v, DNFTOOL::DNFDeCode(DNFTOOL::GetHook(0x1AB7CDC, "0x3C4C+",1)));
            break;
        case 1:
            SQPushInt(v, DNFTOOL::DNFDeCode(DNFTOOL::GetHook(0x1AB7CDC, "0x3C40+",1)));
            break;
        default:
            SQPushBool(v, false);
            break;
        }
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}

typedef  int(_fastcall _9166B0)(int a1, int seat);
static _9166B0* GetSp = (_9166B0*)0x9166B0;
//获取sp点
static int sq_GetSp(uint32_t v)
{
    int Type;

    int num = SQGetTop(v);
    if (num == 1)
    {
        SQPushInt(v, GetSp(NULL,0));
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}


typedef  int(_61A5F0)();
static _61A5F0* GetPass = (_61A5F0*)0x61A5F0;

//获取冒险团等级
static int sq_GetPassLevel(uint32_t v)
{
    int Passobj = GetPass();
    int l = *(int*)Passobj;
    SQPushInt(v, l);
    return 1;
}



typedef  int(_fastcall _4DE680)(int a1, int seat);
static _4DE680* RefreshEventIcon = (_4DE680*)0x4DE680;
//刷新活动图标
static int sq_RefreshEventIcon(uint32_t v)
{
    RefreshEventIcon(0x01A39B78,0);
    return 0;
}



//发包类型
int squirrel::SendPackType(uint32_t v)
{
    int n1;
    int num = SQGetTop(v);
    if (num == 2)
    {
        SQGetInt(v, 2, &n1);
        _SendpacksType(*_SendClass, 0, n1);

        SQPushBool(v, true);
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}
//发包Byte
int squirrel::SendPackByte(uint32_t v)
{
    int n1;
    int num = SQGetTop(v);
    if (num == 2)
    {
        SQGetInt(v, 2, &n1);
        _SendPacksByte(*_SendClass, 0, n1);
        SQPushBool(v, true);
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}
//发包Word
int squirrel::SendPackWord(uint32_t v)
{
    int n1;
    int num = SQGetTop(v);
    if (num == 2)
    {
        SQGetInt(v, 2, &n1);
        _SendPacksWord(*_SendClass, 0, n1);
        SQPushBool(v, true);
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}
//发包DWord
int squirrel::SendPackDWord(uint32_t v)
{
    int n1;
    int num = SQGetTop(v);
    if (num == 2)
    {
        SQGetInt(v, 2, &n1);
        _SendPacksDWord(*_SendClass, 0, n1);
        SQPushBool(v, true);
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}
//发包wchar_t*  (转了char*)
int squirrel::SendPackWChar(uint32_t v)
{
    wchar_t* n1;
    int num = SQGetTop(v);
    if (num == 2)
    {
        SQGetString(v, 2, &n1);
        //wchar_t* 转 char*
        char* fname = DNFTOOL::wchar_tTochar(n1);
        _SendPacksChar(*_SendClass, 0, fname, strlen(fname));
        delete []fname;
        SQPushBool(v, true);
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}
//发包
int squirrel::SendPack(uint32_t v)
{
    int num = SQGetTop(v);
    if (num == 1)
    {
        _SendPacks();
        SQPushBool(v, true);
    }
    else
    {
        SQPushString(v, L"parameter error", -1);
    }
    return 1;
}


//去副本
int squirrel::GoDungeon(uint32_t v)
{
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4 = 0;

    int num = SQGetTop(v);

    if (num == 2)
    {
        SQGetInt(v, 2, &n1);
    }
    else if (num == 5)
    {
        SQGetInt(v, 2, &n1);
        SQGetInt(v, 3, &n2);
        SQGetInt(v, 4, &n3);
        SQGetInt(v, 5, &n4);
    }
    else
    {
        SQPushBool(v, false);
        return 1;
    }

    _SendpacksType(*_SendClass, 0, 15);
    _SendPacks();

    _SendpacksType(*_SendClass, 0, 16);
    _SendPacksWord(*_SendClass, 0, n1);
    _SendPacksByte(*_SendClass, 0, n2);
    _SendPacksByte(*_SendClass, 0, n3);
    _SendPacksByte(*_SendClass, 0, n4);
    _SendPacks();

    SQPushBool(v, true);
    return 1;
}
//回城
int squirrel::GoTown(uint32_t v)
{
    int num = SQGetTop(v);
    if (num == 1)
    {
        _SendpacksType(*_SendClass, 0, 0x2D);
        _SendPacks();
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
//移动城镇
int squirrel::MoveTown(uint32_t v)
{
    int TownIndex;
    int ReIndex;
    int XPos;
    int YPos;
    int num = SQGetTop(v);
    if (num == 5)
    {
        SQGetInt(v, 2, &TownIndex);
        SQGetInt(v, 3, &ReIndex);
        SQGetInt(v, 4, &XPos);
        SQGetInt(v, 5, &YPos);

        _SendpacksType(*_SendClass, 0, 38);
        _SendPacksByte(*_SendClass, 0, TownIndex);
        _SendPacksByte(*_SendClass, 0, ReIndex);
        _SendPacksWord(*_SendClass, 0, XPos);
        _SendPacksWord(*_SendClass, 0, YPos);
        _SendPacksByte(*_SendClass, 0, 5);
        _SendPacksWord(*_SendClass, 0, 0);
        _SendPacksWord(*_SendClass, 0, 0);
        _SendPacks();

        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
//Ldofile
int squirrel::LDofile(uint32_t v)
{
    wchar_t* n1;

    int num = SQGetTop(v);

    if (num == 2)
    {
        SQGetString(v, 2, &n1);
        SQPopTop(v);
        /*
        size_t len = wcslen(n1) + 1;
        size_t converted = 0;
        char* CStr;
        CStr = (char*)malloc(len * sizeof(char));
        wcstombs_s(&converted, CStr, len, n1, _TRUNCATE);
        */

        squirrel::SQdofile(v, n1, false, false);
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }

    return 1;
}

//内存操作
#if defined ADDRESS_API_SWITCH
//读内存
int squirrel::LReadAddress(uint32_t v)
{
    //内存地址 int型
    int Address;

    //获取参数个数
    int ParameterNum = SQGetTop(v);
    //1个参数时
    if (ParameterNum == 2)
    {
        //获取值的类型
        int ParameterType = SQ_GetType(v, 2);

        switch (ParameterType)
        {
        case OT_INTEGER://int类型
        {
            //获取地址
            SQGetInt(v, 2, &Address);
            int Value = *(int*)Address;
            SQPushInt(v, Value);
            return 1;
        }
        break;
        case OT_STRING://String类型
        {

        }
        break;
        }


        SQPushBool(v, true);
    }
    if (ParameterNum == 3)
    {
        //地址
        int Address;
        //偏移
        wchar_t* offset;

        SQGetInt(v, 2, &Address);
        SQGetString(v,3,&offset);

        int Value = DNFTOOL::GetHook(Address, DNFTOOL::wchar_tTochar(offset));
        SQPushInt(v, Value);
        return 1;
    }
    else
    {
        SQPushBool(v, false);
        return 1;
    }
    return 0;
}

//写内存
int LWriteAddressB(uint32_t v)
{
    //内存地址 int型
    int Address;
    //获取参数个数
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 3)
    {
        //获取地址
        SQGetInt(v, 2, &Address);
        //获取修改的值
        int WriteValue;
        SQGetInt(v, 3, &WriteValue);

        *(BYTE*)Address = (BYTE)WriteValue;
    }
    return 0;
}
//写内存
int squirrel::LWriteAddress(uint32_t v)
{
    //内存地址 int型
    int Address;

    //获取参数个数
    int ParameterNum = SQGetTop(v);
    //1个参数时
    if (ParameterNum == 3)
    {
        //获取值的类型
        int ParameterType = SQ_GetType(v, 2);
        
        switch (ParameterType)
        {
        case OT_INTEGER://int类型
        {
            //获取地址
            SQGetInt(v, 2, &Address);
            //获取修改的值
            int WriteValue;
            SQGetInt(v, 3, &WriteValue);

            *(int*)Address = WriteValue;
            SQPushBool(v, true);
            return 1;
        }
        break;
        case OT_STRING://String类型
        {
        }
        break;
        }


        SQPushBool(v, true);
    }
    if (ParameterNum == 4)
    {
        //地址
        int Address;
        //偏移
        wchar_t* offset;
        //修改值
        int WriteValue;

        SQGetInt(v, 2, &Address);
        SQGetString(v, 3, &offset);
        SQGetInt(v, 4, &WriteValue);

        int SelectAddress = DNFTOOL::GetHook(Address, DNFTOOL::wchar_tTochar(offset),1);
        *(int*)SelectAddress = WriteValue;
    }
    else
    {
        SQPushBool(v, false);
        return 1;
    }
    return 0;
}
#endif

//Lcout
int squirrel::Lcout(uint32_t v)
{
    char* str = NULL;
    int type = NULL;
    int color = NULL;
    int num = SQGetTop(v);

    if (num == 2 || num == 3 || num == 4)
    {
        switch (num)
        {
        case 2:
            SQGetStringc(v, 2, &str);
            break;
        case 3:
            SQGetStringc(v, 2, &str);
            SQGetInt(v, 3, &type);
            break;
        case 4:
            SQGetStringc(v, 2, &str);
            SQGetInt(v, 3, &type);
            SQGetInt(v, 4, &color);
            break;
        }

        SQPopTop(v);

        DNFTOOL::GMNotice(str, type, color);

        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }

    return 1;
}
//新建窗口
int squirrel::NewWindows(uint32_t v)
{
    char* str = NULL;
    int type = NULL;
    int color = NULL;
    int num = SQGetTop(v);

    if (num == 2 || num == 3 || num == 4)
    {
        switch (num)
        {
        case 2:
            SQGetStringc(v, 2, &str);
            break;
        case 3:
            SQGetStringc(v, 2, &str);
            SQGetInt(v, 3, &type);
            break;
        case 4:
            SQGetStringc(v, 2, &str);
            SQGetInt(v, 3, &type);
            SQGetInt(v, 4, &color);
            break;
        }
        
        DNFTOOL::WindowsNotice(str, type, color);

        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }

    return 1;
}
int squirrel::sq_Cmd(uint32_t v)
{
    wchar_t* OutPutBuffer;
    SQGetString(v, 2, &OutPutBuffer);
    char* OutPutText = DNFTOOL::SquirrelU2W(OutPutBuffer);

    system(OutPutText);
    //WinExec(OutPutText, SW_NORMAL);
    delete[]OutPutText;
    return 1;
}
//设置UI槽坐标
int squirrel::SetSlot(uint32_t v)
{
    int Type = NULL;
    int Index = NULL;
    int Xpos = NULL;
    int Ypos = NULL;


    int OneAddr = NULL;
    int* xpos = NULL;
    int* ypos = NULL;

    int ParameterNum = SQGetTop(v);

    if (ParameterNum == 5)
    {
        SQGetInt(v, 2, &Type);
        SQGetInt(v, 3, &Index);
        SQGetInt(v, 4, &Xpos);
        SQGetInt(v, 5, &Ypos);

        SQPopTop(v);

        switch (Type)
        {
        case 0://拓展技能栏
            OneAddr = *(int*)0x1ADE0CC;
            OneAddr = *(int*)(OneAddr + (0x60 + (4 * Index)));
            xpos = (int*)(OneAddr + (0x14));
            ypos = (int*)(OneAddr + (0x18));
            *xpos = Xpos;
            *ypos = Ypos;
            break;
        case 1://基础技能栏
            OneAddr = *(int*)0x1ADE0CC;
            OneAddr = *(int*)(OneAddr + (0x30 + (4 * Index)));
            xpos = (int*)(OneAddr + (0x14));
            ypos = (int*)(OneAddr + (0x18));
            *xpos = Xpos;
            *ypos = Ypos;
            break;
        case 2://切换技能栏
            OneAddr = *(int*)0x1ADE0CC;
            OneAddr = *(int*)(OneAddr + (0x124 + (4 * Index)));
            xpos = (int*)(OneAddr + (0x14));
            ypos = (int*)(OneAddr + (0x18));
            *xpos = Xpos;
            *ypos = Ypos;
            break;
        case 3://快捷物品栏
            OneAddr = *(int*)0x1ADE0CC;
            OneAddr = *(int*)(OneAddr + (0x18 + (4 * Index)));
            xpos = (int*)(OneAddr + (0x14));
            ypos = (int*)(OneAddr + (0x18));
            *xpos = Xpos;
            *ypos = Ypos;
            break;
        case 4://特性技能展开栏
            OneAddr = *(int*)0x1ADE0CC;
            OneAddr = *(int*)(OneAddr + 0xC);
            OneAddr = *(int*)(OneAddr + 0x4);
            OneAddr = *(int*)(OneAddr + 0x0);
            OneAddr = *(int*)(OneAddr + 0x34);
            OneAddr = *(int*)(OneAddr + 0x4);
            OneAddr = *(int*)(OneAddr + 0x28);
            OneAddr = *(int*)(OneAddr + 0x4);

            xpos = (int*)(OneAddr + (0x394));
            ypos = (int*)(OneAddr + (0x398));
            *xpos = Xpos;
            *ypos = Ypos;
            break;
        case 5://特性技能技能栏
            OneAddr = *(int*)(0x16E95AC + 0x400000);
            OneAddr = *(int*)(OneAddr + 0x68);
            OneAddr = *(int*)(OneAddr + 0x0);
            OneAddr = *(int*)(OneAddr + 0x8);
            OneAddr = *(int*)(OneAddr + 0x64);
            OneAddr = *(int*)(OneAddr + 0x0);
            OneAddr = *(int*)(OneAddr + 0x1C);
            OneAddr = *(int*)(OneAddr + 0x0);

            xpos = (int*)(OneAddr + (0x1794));
            ypos = (int*)(OneAddr + (0x1798));
            *xpos = Xpos;
            *ypos = Ypos;
            break;
        case 6://菜单栏
            OneAddr = *(int*)0x1ADE0CC;
            OneAddr = *(int*)(OneAddr + (0x84 + (4 * Index)));
            xpos = (int*)(OneAddr + (0x14));
            ypos = (int*)(OneAddr + (0x18));
            *xpos = Xpos;
            *ypos = Ypos;
            break;
        }

        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }

    return 1;
}


//设置项目名称颜色
#if defined ITEMRARITY
std::map< int, int>ItemColorMap;
int squirrel::RegisterItemColor_STL(uint32_t v)
{
    int ItemID, Clolr;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 3)
    {
        SQGetInt(v, 2, &ItemID);
        SQGetInt(v, 3, &Clolr);
        ItemColorMap[ItemID] = Clolr;
    }
    return 0;
}
#endif

//设置文字HOOK
#ifdef CODEDRAW
struct CodeDrawObj
{
    int Color;
    std::string str;
};
std::map< std::string , CodeDrawObj>CodeDrawMap;
int squirrel::RegisterCodeDraw_STL(uint32_t v)
{
    int Clolr;
    wchar_t* Key;
    wchar_t* OutPutBuffer;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 4)
    {
        SQGetString(v, 2, &Key);
        SQGetInt(v, 3, &Clolr);
        SQGetString(v, 4, &OutPutBuffer);

        char* OutPutKey = DNFTOOL::SquirrelU2W(Key);
        std::string BufferKey = OutPutKey;
        delete[]OutPutKey;

        char* OutPutText = DNFTOOL::SquirrelU2W(OutPutBuffer);
        std::string str = OutPutText;
        delete[]OutPutText;

        CodeDrawObj buffobj;
        buffobj.Color = Clolr;
        buffobj.str = str;
        CodeDrawMap[BufferKey] = buffobj;
    }
    return 0;
}
#endif 


//普通 STL
#if defined NORMAL_STL
//查询 类型容器
int squirrel::Get_STL(uint32_t v)
{
    char* Name;
    int Type;
    int Idx;
    int ParameterNum = SQGetTop(v);

    if (ParameterNum == 4)
    {
        //获取容器名字
        SQGetStringc(v, 2, &Name);
        //获取容器类型
        SQGetInt(v, 3, &Type);
        //获取查询号位
        SQGetInt(v, 4, &Idx);
        if (STL::Check_STL(Name, Type) == 0)
        {
            SQPushBool(v, false);
            return 1;
        }
        switch (Type)
        {
        case 0:
            SQPushInt(v, Int_STL[Name]);
            break;
        case 1:
            SQPushFloat(v, Float_STL[Name]);
            break;
        case 2:
            SQPushStringc(v, String_STL[Name].c_str(), strlen(String_STL[Name].c_str()));
            break;
        case 3:
            SQPushBool(v, Bool_STL[Name]);
            break;
        case 4:
            SQPushInt(v, STL::GetIntArr_STL(Name, Idx));
            break;
        case 5:
            SQPushFloat(v, STL::GetFloatArr_STL(Name, Idx));
            break;
        case 6:
            SQPushStringc(v, STL::GetStringArr_STL(Name, Idx).c_str(), strlen(STL::GetStringArr_STL(Name, Idx).c_str()));
            break;
        case 7:
            SQPushBool(v, STL::GetBoolArr_STL(Name, Idx));
            break;
        default:
            return 0;
            break;
        }
        return 1;
    }
    else
    {
        return 0;
    }
}
//设置 类型容器
int squirrel::Set_STL(uint32_t v)
{
    char* Name;
    int Type;
    int Idx;
    int ParameterNum = SQGetTop(v);

    int IntValue;
    FLOAT FloatValue;
    char* StrValue;
    BOOL BoolValue;
    if (ParameterNum == 5)
    {
        //获取容器名字
        SQGetStringc(v, 2, &Name);
        //获取容器类型
        SQGetInt(v, 3, &Type);
        //获取修改号位
        SQGetInt(v, 4, &Idx);

        //获取修改值
        switch (Type)
        {
        case 0:
            SQGetInt(v, 5, &IntValue);
            Int_STL[Name] = IntValue;
            break;
        case 1:
            SQGetFloat(v, 5, &FloatValue);
            Float_STL[Name] = FloatValue;
            break;
        case 2:
            SQGetStringc(v, 5, &StrValue);
            String_STL[Name] = StrValue;
            break;
        case 3:
            SQGetBool(v, 5, &BoolValue);
            Bool_STL[Name] = BoolValue;
            break;
        case 4:
            SQGetInt(v, 5, &IntValue);
            STL::SetIntArr_STL(Name,Idx, IntValue);
            break;
        case 5:
            SQGetFloat(v, 5, &FloatValue);
            STL::SetFloatArr_STL(Name, Idx, FloatValue);
            break;
        case 6:
            SQGetStringc(v, 5, &StrValue);
            STL::SetStringArr_STL(Name, Idx, StrValue);
            break;
        case 7:
            SQGetBool(v, 5, &BoolValue);
            STL::SetBoolArr_STL(Name, Idx, BoolValue);
            break;
        default:
            SQPushBool(v, false);
            return 1;
            break;
        }
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
//new 类型容器
int squirrel::New_STL(uint32_t v)
{
    char* Name;
    int Type;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 3)
    {
        //获取容器名字
        SQGetStringc(v, 2, &Name);
        //获取容器类型
        SQGetInt(v, 3, &Type);
        if (STL::Check_STL(Name, Type) != 0)
        {
            SQPushBool(v, false);
            return 1;
        }
        switch (Type)
        {
        case 0:
                Int_STL[Name] = 0;//单Int容器
            break;
        case 1:
                Float_STL[Name] = 0.0;//单Float容器
            break;
        case 2:
                String_STL[Name] = "zero";//单String容器
            break;
        case 3:
                Bool_STL[Name] = false;//单Bool容器
            break;
        case 4:
                STL::BuildIntArr_STL(Name);//Int数组容器
            break;
        case 5:
                STL::BuildFloatArr_STL(Name);//Float数组容器
            break;
        case 6:
                STL::BuildStringArr_STL(Name);//String数组容器
            break;
        case 7:
                STL::BuildBoolArr_STL(Name);//Bool数组容器
            break;
        default:
            SQPushBool(v, false);
            return 1;
            break;
        }
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
#endif

//时间 STL
#if defined TIME_STL
extern  std::map<wchar_t*, TimeSTLStruct>Time_STL;
//时间容器
int squirrel::Timer_STL(uint32_t v)
{
    wchar_t* Name;
    int MaxTime;
    int Type;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 4)
    {
        //获取容器名字
        SQGetString(v, 2, &Name);
        //获取最大值
        SQGetInt(v, 3, &MaxTime);
        //获取容器类型
        SQGetInt(v, 4, &Type);

        switch (Type)
        {
        case 0: //设置时间容器
        {
            if (Time_STL.count(Name) == 1)//如果这个标识容器存在 就返回 否则创建
            {
                SQPushBool(v, false);
                return 1;
            }
            TimeSTLStruct pack;//定义结构体
            pack.Name = Name;//设置容器标识名称
            pack.Max_Time = MaxTime;//设置 标识时间容器最大时间
            pack.Now_Time = 0;//设置 标识时间容器当前时间
            pack.Static_Time = 0;//设置 基础时间（用于运算）
            Time_STL[Name] = pack;//设置包给标识时间容器
        }
        break;
        case 1: //启动时间容器
        {
            if (Time_STL[Name].Static_Time != 0)break;//只有基础时间为0的时候记录当前时间标准
            Time_STL[Name].Static_Time =  clock();
           
        }
        break;
        case 2://获取时间容器
        {
            int Time = clock() - Time_STL[Name].Static_Time + Time_STL[Name].Now_Time;
            if(Time_STL[Name].Static_Time + Time_STL[Name].Now_Time == 0)SQPushBool(v, false);//没启动返回0
            else if (Time <= Time_STL[Name].Max_Time)SQPushInt(v, Time);//返回当前容器时间
            else SQPushInt(v, (int)Time_STL[Name].Max_Time);
            return 1;
        }
        break;
        case 3://重置时间容器
        {
            Time_STL[Name].Static_Time = 0;
        }
        break;
        case 4://暂停时间容器
        {
            Time_STL[Name].Now_Time += clock() - Time_STL[Name].Static_Time;
            Time_STL[Name].Static_Time = 0;
        }
        break;
        case 5://继续时间容器
        {
            if (Time_STL[Name].Static_Time != 0)break;//只有基础时间为0的时候记录当前时间标准
            Time_STL[Name].Static_Time =  clock();
        }
        break;
        case 6://销毁时间容器
        {
            Time_STL.erase(Name);//销毁容器
        }
        break;
        default:
        {
            SQPushBool(v, false);
            return 1;
        }
        break;
        }
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
#endif

//龙盒
#if defined DRAGONBOX_SWITCH
//同步 龙盒数据包
int squirrel::Sync_Dragon_Pack(uint32_t v)
{
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 1)
    {
        STL::SyncDargonBox_STL();
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
//获取 龙盒数据包
int squirrel::Get_Dragon_Pack(uint32_t v)
{
    int Type;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 2)
    {
        SQGetInt(v, 2, &Type);

        int Pack_Value;
        //单抽模式
        Pack_Value = STL::SelectDargonBox_STL(Type);
        SQPushInt(v, Pack_Value);

        return 1;
    }
    if (ParameterNum == 3)
    {
        int Index;
        SQGetInt(v, 2, &Type);
        SQGetInt(v, 3, &Index);

        int Pack_Value;
        Pack_Value = STL::SelectDargonBox_STL(Type, Index);

        SQPushInt(v, Pack_Value);
        return 1;
    }
    else
    {
        SQPushBool(v, false);
    }

    return 1;
}
//发送 开龙盒
int squirrel::Redom_Dragon(uint32_t v)
{
    int Type;
    int Port;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 3)
    {
        SQGetInt(v, 2, &Type);
        SQGetInt(v, 3, &Port);
        rapidjson::StringBuffer buffer;
        rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);

        writer.StartObject();
        writer.Key("op");
        writer.Int(Type);
        writer.EndObject();

        char* str = (char*)buffer.GetString();

        _SendpacksType(*_SendClass, 0, Port);
        _SendPacksChar(*_SendClass, 0, str, strlen(str));
        _SendPacks();
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;

}
//获取 龙盒 模式
int squirrel::Get_DragonModel(uint32_t v)
{
    int Type;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 2)
    {
        SQGetInt(v, 2, &Type);
        int Model = STL::SelectDargonModel_STL(Type);//龙盒模式
        SQPushInt(v, Model);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
//设置 龙盒 模式
int squirrel::Set_DragonModel(uint32_t v)
{
    int Model;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 2)
    {
        SQGetInt(v, 2, &Model);
        STL::DrawDargonModel_STL(Model);//龙盒模式
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    return 1;
}
#endif


//Json STL
#if defined JSON_STL
//构造Json类型容器
std::map<std::string, std::string>Json_STL;
//Json容器
int squirrel::Jsoner_STL(uint32_t v)
{
    wchar_t* WName;
    int Type;
    int ParameterNum = SQGetTop(v);
    if (ParameterNum == 3)
    {
        //获取容器名字
        SQGetString(v, 2, &WName);
        char* CName = DNFTOOL::wchar_tTochar(WName);
        std::string Name = CName;
        delete []CName;
        //获取值的类型
        int ValueType = SQ_GetType(v, 3);

        switch (ValueType)
        {
        case OT_INTEGER://int类型
        {
            //获取模式
            SQGetInt(v, 3, &Type);

            switch (Type)
            {
            case 0://构造
            {
                if (Json_STL.count(Name) == 0)//如果这个hash不存在 就创建
                {
                    //构造对应 hash 的 Json容器
                    std::string str = "";
                    Json_STL[Name] = str;
                }
                else
                {
                    SQPushBool(v, false);
                    return 1;
                }
                break;
            }
            case 1://读取整体
            {
                wchar_t* buffer = DNFTOOL::charTowchar_t((char*)Json_STL[Name].c_str());
                SQPushString(v, buffer, wcslen(buffer));//传给nut
                delete []buffer;
                return 1;
                break;
            }
            case 2://销毁
                Json_STL.erase(Name);//销毁容器
                break;
            }
        }
        break;
        case OT_STRING://String类型
        {
            
            if (Json_STL.count(Name) == 0)//如果这个hash不存在 就返回
            {
                SQPushBool(v,false);
                return 1;
            }
            
            wchar_t* GKey;
            //获取访问需求
            SQGetString(v, 3, &GKey);
            char* key = DNFTOOL::SquirrelU2W(GKey);
            //char* key = DNFTOOL::wchar_tTochar(GKey);
            rapidjson::Document Dom;
            Dom.Parse(Json_STL[Name].c_str());//加载 字符串

            std::vector<std::string> Data;
            DNFTOOL::Split(key, Data);
            delete []key;
            rapidjson::Value Vbuffer;
            for (unsigned int i = 0; i < Data.size(); i++)
            {
                if (i == 0)
                {
                    Vbuffer = Dom[Data[i].c_str()];
                }
                else if (DNFTOOL::isNum(Data[i].c_str()) != 0)
                {
                    if(Vbuffer.Empty() == 0 && Vbuffer.Size() > atoi(Data[i].c_str()))Vbuffer = Vbuffer[atoi(Data[i].c_str())];
                    else
                    {
                        SQPushBool(v, false);
                        return 1;
                    }
                }
                else
                {
                    if (Vbuffer.Empty() == 0)Vbuffer = Vbuffer[Data[i].c_str()];
                }

            }

            if (Vbuffer.IsBool())
            {
                SQPushBool(v, Vbuffer.GetBool());
                return 1;
            }
            else if (Vbuffer.Empty() == false || (Vbuffer.IsInt() && Vbuffer.GetInt()==0))
            {
                if (Vbuffer.IsString())
                {
                    wchar_t* buffer = DNFTOOL::charTowchar_t((char*)Vbuffer.GetString());
                    SQPushString(v, buffer, wcslen(buffer));
                    delete []buffer;
                    return 1;
                }
                else if (Vbuffer.IsInt())
                {
                    SQPushInt(v, Vbuffer.GetInt());
                    return 1;
                }
                else if (Vbuffer.IsFloat())
                {
                    SQPushFloat(v, Vbuffer.GetFloat());
                    return 1;
                }
                else
                {
                    
                    rapidjson::StringBuffer jsonBuffer;
                    rapidjson::Writer<rapidjson::StringBuffer> writer(jsonBuffer);
                    Vbuffer.Accept(writer);
                    const char* json = jsonBuffer.GetString();
                    wchar_t* buffer = DNFTOOL::charTowchar_t((char*)json);
                    SQPushString(v, buffer, wcslen(buffer));
                    delete[]buffer;
                    return 1;
                }
            }
            else
            {
                SQPushBool(v, false);
                return 1;
            }
        }
            break;
        }
        SQPushBool(v, true);
    }
    else if (ParameterNum == 4)
    {
        //获取容器名字
        SQGetString(v, 2, &WName);
        char* CName = DNFTOOL::wchar_tTochar(WName);
        std::string Name = CName;
        delete []CName;
        //获取Key 的名字
        wchar_t* keybuffer;
        SQGetString(v, 3, &keybuffer);
        //转化为可用Key
        char* key = DNFTOOL::wchar_tTochar(keybuffer);
              
        //构造json dom
        rapidjson::Document Dom;
        Dom.Parse(Json_STL[Name].c_str());//加载 字符串
        if(Json_STL[Name].length() == 0 )Dom.SetObject();//如果未初始化就初始化一次

        rapidjson::Value BB;

        //获取值的类型
        int ValueType = SQ_GetType(v, 4);
        //判断值类型
        switch (ValueType)
        {
        case OT_INTEGER://int类型
        {
            int Value;
            SQGetInt(v, 4, &Value);
            BB.SetInt(Value);    
            break;
        }
        case OT_FLOAT://float类型
        {
            FLOAT Value;
            SQGetFloat(v, 4, &Value);
            BB.SetFloat(Value);
            break;
        }
        case OT_BOOL://bool类型
        {
            BOOL Value;
            SQGetBool(v, 4, &Value);
            BB.SetBool(Value);
            break;
        }
        case OT_STRING://string类型
        {
            wchar_t* Value;
            SQGetString(v, 4, &Value);
            char* str = DNFTOOL::wchar_tTochar(Value);
            BB.SetString(rapidjson::StringRef(str));
            delete []str;
            break;
        }
        default:
            break;
        }

        if (Dom[key].IsNull())//如果键值为空就新增
        {
            Dom.AddMember(rapidjson::StringRef(key), BB, Dom.GetAllocator());
        }
        else//否则就修改
        {
            Dom[key] = BB;
        }

        rapidjson::StringBuffer jsonBuffer;
        rapidjson::Writer<rapidjson::StringBuffer> writer(jsonBuffer);
        Dom.Accept(writer);
        Json_STL[Name] = jsonBuffer.GetString();
        SQPushBool(v, true);

        delete []key;
    }
    else if (ParameterNum == 5)
    {
        //获取容器名字
        SQGetString(v, 2, &WName);
        char* CName = DNFTOOL::wchar_tTochar(WName);
        std::string Name = CName;

        //获取Str 的内容
        wchar_t* str;
        SQGetString(v, 3, &str);
        //转化为可用内容
        char* Jso = DNFTOOL::wchar_tTochar(str);

        //获取类型
        int Type;
        SQGetInt(v, 4, &Type);
        switch (Type)
        {
        case 0:
            Json_STL[Name] = Jso;
            break;
        case 1:
            if(Json_STL[Name].empty())Json_STL[Name] = Jso;
            break;
        default:
            break;
        }
        delete []CName;
        delete []Jso;
    }
    else
    {
        SQPushBool(v, false);
    }
    
    return 1;
}
#endif



//字符 STL
#if defined CODE_STL
extern  std::vector< DrawCodestruct>DrawCodeT1_STL;
extern  std::vector< DrawCodestruct>DrawCodeT2_STL;
extern  std::vector< DrawCodestruct>DrawCodeT3_STL;
int squirrel::Coder_STL(uint32_t v)
{
    wchar_t* Str;
    int XPos;
    int YPos;
    int Color;
    int Type;
    int ParameterNum = SQGetTop(v);
    
    if (ParameterNum == 6)
    {
        //获取字符串内容
        SQGetString(v, 2, &Str);
        //获取X坐标
        SQGetInt(v, 3, &XPos);
        //获取X坐标
        SQGetInt(v, 4, &YPos);
        //获取颜色
        SQGetInt(v, 5, &Color);
        //获取类型
        SQGetInt(v, 6, &Type);
        
        //松鼠 Wchar_t  转换为  Unicode
        char* OutPutText = DNFTOOL::SquirrelU2W(Str);
        std::string str = OutPutText;
        //wchar_t * str = DNFTOOL::char2wchar(OutPutText);
        delete []OutPutText;
        //delete Str;

        DrawCodestruct Buffer;
        Buffer.str = str;
        Buffer.Xpos = XPos;
        Buffer.Ypos = YPos;
        Buffer.Color = Color;

        switch (Type)
        {
        case 0:
        {
            DrawCodeT1_STL.push_back(Buffer);
        }
        break;
        case 1:
        {
            DrawCodeT2_STL.push_back(Buffer);
        }
        break;
        case 2:
        {
            DrawCodeT3_STL.push_back(Buffer);
        }
        break;
        }
        
        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
    }
    
    return 1;
}
#endif


//绘制字符
#ifdef DRAW_CODE
typedef DWORD(_fastcall _FontInit)(DWORD thisc, DWORD Seat);
static _FontInit* sub_11BE9A0 = (_FontInit*)0x11BE9A0;
static _FontInit* sub_11BE980 = (_FontInit*)0x11BE980;
typedef DWORD(_fastcall _BFontInit)(DWORD thisc, DWORD Seat);
static _BFontInit* sub_1206570 = (_BFontInit*)0x1206570;
typedef DWORD(_fastcall _FontC)(DWORD thisc, DWORD Seat, DWORD Font);
static _FontC* sub_1206550 = (_FontC*)0x1206550;
static DrawCode NewDrawCodeF = (DrawCode)0x01206BD0;

int squirrel::sq_DrawCode(uint32_t v)
{
    wchar_t* Str;
    int XPos;
    int YPos;
    int Color;
    int Type;
    int Stroke;

    int ParameterNum = SQGetTop(v);

    if (ParameterNum == 4)
    {
        Color = (int)0xfffffffff;
        Type = 1;
        Stroke = 0;

        SQPushBool(v, true);
    }
    else if (ParameterNum == 5)
    {
        //获取颜色
        SQGetInt(v, 5, &Color);
        Type = 1;
        Stroke = 0;

        SQPushBool(v, true);
    }
    else if (ParameterNum == 6)
    {
        //获取颜色
        SQGetInt(v, 5, &Color);
        //获取类型
        SQGetInt(v, 6, &Type);
        Stroke = 0;

        SQPushBool(v, true);
    }
    else if (ParameterNum == 7)
    {
        //获取颜色
        SQGetInt(v, 5, &Color);
        //获取类型
        SQGetInt(v, 6, &Type);
        //获取描边
        SQGetInt(v, 7, &Stroke);

        SQPushBool(v, true);
    }
    else
    {
        SQPushBool(v, false);
        return 1;
    }

    //获取字符串内容
    SQGetString(v, 2, &Str);
    //获取X坐标
    SQGetInt(v, 3, &XPos);
    //获取X坐标
    SQGetInt(v, 4, &YPos);
    //松鼠 Wchar_t  转换为  Unicode
    char* OutPutText = DNFTOOL::SquirrelU2W(Str);
    //转为正确的Unicode字符
    wchar_t* str = DNFTOOL::char2wchar(OutPutText);

    //申请笔
    unsigned char* font = new unsigned char[1000];
    //初始化笔
    sub_11BE9A0((DWORD)font, 0);
    sub_11BE980((DWORD)font, 0);
    //是否描边
    font[1] = Stroke;

    int FontObj;

    switch (Type)
    {
    case 1:
        FontObj = *(int*)0x1B468CC;
        break;
    case 2:
        FontObj = *(int*)0x1B468DC;
        break;
    case 3:
        FontObj = *(int*)0x1B468D4;
        break;
    default:
        FontObj = *(int*)0x1B468CC;
        break;
    }

    //字体
    *(int*)((int)font + 8) = FontObj;

    //得到绘制句柄
    DWORD base = *(int*)0x1B45B94;
    //设置笔
    sub_1206550(base, 0, (DWORD)font);
    //绘制字体
    NewDrawCodeF(base, 0, XPos, YPos, Color, (int)str);
    //取消笔
    sub_1206570(base, 0);


    delete[]OutPutText;
    delete[]str;
    delete[]font;
    return 1;
}

#endif 





typedef int(_Sq_Err)(uint32_t v);
static _Sq_Err* Sq_Err = (_Sq_Err*)0x13542F0;

//获取Squirrel v 基址
inline uint32_t GetSqVm()
{
    return *(uint32_t*)0x1AF3544;
}
//注册 Nut API
void squirrel::RegisterNutApi(const wchar_t* funcName, void* funcAddr, uint32_t v = NULL)
{
    if (v == NULL)
        v = GetSqVm();
    SQPushRootTable(v);
    SQPushString(v, funcName, -1);
    RealSqNewClosure(v, funcAddr, 0);
    SQNewSlot(v, -3, false);
    SQPopTop(v);
}

void squirrel::R_Register_Nut()
{
    RegisterNutApi(L"L_sq_Test", squirrel::sq_Test);
    RegisterNutApi(L"L_sq_DrawImg", squirrel::sq_DrawImg);
    RegisterNutApi(L"L_sq_ReleaseDrawImgModel", sq_ReleaseDrawImgModel);
    RegisterNutApi(L"L_sq_SetDrawImgModel", sq_SetDrawImgModel);
    RegisterNutApi(L"L_sq_IntiNumberDraw", squirrel::sq_IntiNumberDraw);
    RegisterNutApi(L"L_sq_DrawNumber", squirrel::sq_DrawNumber);
    RegisterNutApi(L"L_sq_DrawWindow", sq_DrawWindow);
    RegisterNutApi(L"L_sq_DrawButton", sq_DrawButton);
    RegisterNutApi(L"L_sq_GetStringDrawLength", sq_GetStringDrawLength);
    RegisterNutApi(L"L_sq_GetStringDrawArray", sq_GetStringDrawArray);
    RegisterNutApi(L"L_sq_DecondeJson", DecondeJson);
    RegisterNutApi(L"L_sq_EncondeJson", EncondeJson);


    RegisterNutApi(L"L_sq_Open_ExWindow", sq_Open_ExWindow);//创建特殊窗口
    RegisterNutApi(L"L_sq_Select_MiniMap_Index", sq_Select_MiniMap_Index);//选择大地图区域
    RegisterNutApi(L"L_Sq_DrawSkill", sq_DrawSkill);//绘制技能
    RegisterNutApi(L"L_sq_UseSkill", sq_UseSkill);//绘制技能
    RegisterNutApi(L"L_Sq_GetPlayerEachName", sq_GetPlayerEachName);
    RegisterNutApi(L"L_Sq_GetSkillAddress", sq_GetSkillAddress);
    RegisterNutApi(L"L_sq_OpenConsole", squirrel::sq_OpenConsole);
    RegisterNutApi(L"L_sq_MoveMap", squirrel::sq_MoveMap);
    RegisterNutApi(L"L_Sq_Err", Sq_Err);

    RegisterNutApi(L"L_Sq_GetImg", sq_GetImg);
    RegisterNutApi(L"L_Sq_DrawItem", sq_DrawItem);

    RegisterNutApi(L"L_Sq_GetObjectAddress", GetObjectAddress);
    RegisterNutApi(L"L_Sq_GetRidingObjectAddress", GetRidingObjectAddress);
    RegisterNutApi(L"L_Sq_GetObjectName", GetObjectName);
    RegisterNutApi(L"L_Sq_GetObjectInfo", GetObjectInfo);
    RegisterNutApi(L"L_Sq_GetObjectDeInfo", GetObjectDeInfo);
    RegisterNutApi(L"L_Sq_SetObjectInfo", SetObjectInfo);
    RegisterNutApi(L"L_Sq_SetObjectDeInfo", SetObjectDeInfo);
    RegisterNutApi(L"L_Sq_GetObjectLevel", GetObjectLevel);


    RegisterNutApi(L"L_sq_Switching", squirrel::sq_Switching);//换装参数获取

    //人物或装备属性 查看 修改 开启
#if defined CHRATRBT_SWITCH
    RegisterNutApi(L"L_sq_GetCharacterAttribute", squirrel::GetCharacterAttribute);//获取人物或装备属性
    RegisterNutApi(L"L_sq_SetCharacterAttribute", squirrel::SetCharacterAttribute);//设置人物或装备属性
#endif

#if defined TOWN_SWITCH
    RegisterNutApi(L"L_sq_GetTownIndex", squirrel::GetTownIndex);//获取城镇编号
    RegisterNutApi(L"L_sq_GetRegionIndex", squirrel::GetRegionIndex);//获取区域编号
    RegisterNutApi(L"L_sq_GetTownXpos", squirrel::GetTownXpos);//获取城镇X坐标
    RegisterNutApi(L"L_sq_GetTownYpos", squirrel::GetTownYpos);//获取城镇Y坐标
#endif

#if defined CHRINFO_SWITCH
    RegisterNutApi(L"L_sq_GetFatigue", squirrel::GetFatigue);//获取疲劳值
    RegisterNutApi(L"L_sq_GetExp", squirrel::GetExp);//获取经验值
    RegisterNutApi(L"L_sq_GetSp", sq_GetSp);//获取SP点
    RegisterNutApi(L"L_sq_GetPassLevel", sq_GetPassLevel);//获取冒险团等级
    RegisterNutApi(L"L_sq_RefreshEventIcon", sq_RefreshEventIcon);//刷新活动图标
#endif

#if defined SEND_PACK_SWITCH
    RegisterNutApi(L"L_sq_SendPackType", squirrel::SendPackType);//发包类型
    RegisterNutApi(L"L_sq_SendPackByte", squirrel::SendPackByte);//包数据Byte
    RegisterNutApi(L"L_sq_SendPackWord", squirrel::SendPackWord);//包数据Word
    RegisterNutApi(L"L_sq_SendPackDWord", squirrel::SendPackDWord);//包数据DWord
    RegisterNutApi(L"L_sq_SendPackWChar", squirrel::SendPackWChar);//包数据DWord
    RegisterNutApi(L"L_sq_SendPack", squirrel::SendPack);//发包
#if defined SEND_API_SWITCH
    RegisterNutApi(L"L_sq_GoDungeon", squirrel::GoDungeon);//去副本
    RegisterNutApi(L"L_sq_GoTown", squirrel::GoTown);//去城镇
    RegisterNutApi(L"L_sq_MoveTown", squirrel::MoveTown);//去城镇
#endif
#endif


#ifdef DRAW_CODE
    RegisterNutApi(L"L_sq_DrawCode", squirrel::sq_DrawCode);//绘制字符
#endif

#if defined DOFILE_API_SWITCH
    RegisterNutApi(L"L_sq_Dofile", squirrel::LDofile);//加密读取
#endif

#if defined GETINTDATA_API_SWITCH
    //RegisterNutApi(L"L_sq_LenheartGetIntData", (void*)0x005E3DB0);//读取静态数据
#endif

#if defined ADDRESS_API_SWITCH
    RegisterNutApi(L"L_sq_RA", squirrel::LReadAddress);//读内存
    RegisterNutApi(L"L_sq_WA", squirrel::LWriteAddress);//写内存
    RegisterNutApi(L"L_sq_WAB", LWriteAddressB);//写内存
#endif

#if defined LCOUT_API_SWITCH
    RegisterNutApi(L"L_cout", squirrel::Lcout);//输出公告
    RegisterNutApi(L"Sout", squirrel::Sout);//输出
    RegisterNutApi(L"Clock", squirrel::Clock);//输出
#endif

#if defined NEW_WINDOW_API_SWITCH
    RegisterNutApi(L"L_NewWindows", squirrel::NewWindows);//创建窗口
    RegisterNutApi(L"L_Cmd", squirrel::sq_Cmd);//创建窗口
#endif

#if defined SET_SLOT_API_SWITCH
    RegisterNutApi(L"L_SetSlot", squirrel::SetSlot);//设置槽坐标
#endif

    //普通 STL
#if defined NORMAL_STL
    RegisterNutApi(L"L_New_STL", squirrel::New_STL);//新建容器
    RegisterNutApi(L"L_Set_STL", squirrel::Set_STL);//设置容器
    RegisterNutApi(L"L_Get_STL", squirrel::Get_STL);//获取容器
#endif

    //时间 STL
#if defined TIME_STL
    RegisterNutApi(L"L_Timer_STL", squirrel::Timer_STL);//时间容器
#endif

    //Json STL
#if defined JSON_STL
    RegisterNutApi(L"L_Json_STL", squirrel::Jsoner_STL);//Json容器
#endif

    //字符 STL
#if defined CODE_STL
    RegisterNutApi(L"L_Code_STL", squirrel::Coder_STL);//字符容器
#endif


#ifdef ITEMRARITY
    RegisterNutApi(L"L_RegisterItemColor_STL", squirrel::RegisterItemColor_STL);//注册Item颜色
#endif


#ifdef CODEDRAW
    RegisterNutApi(L"L_RegisterCodeDraw_STL", squirrel::RegisterCodeDraw_STL);//注册文字绘制HOOK
#endif

    //龙盒
#if defined DRAGONBOX_SWITCH
    //RegisterNutApi(L"L_Get_DragonModel", squirrel::Get_DragonModel);//获取龙盒模式
    //RegisterNutApi(L"L_Set_DragonModel", squirrel::Set_DragonModel);//设置龙盒模式
    //RegisterNutApi(L"L_Redom_Dragon", squirrel::Redom_Dragon);//抽奖
    //RegisterNutApi(L"L_Get_Dragon_Pack", squirrel::Get_Dragon_Pack);//查询龙盒包
    //RegisterNutApi(L"L_Sync_Dragon_Pack", squirrel::Sync_Dragon_Pack);//同步龙盒包
#endif
}


//typedef struct _DofileObject
//{
//    int State;
//    std::string DofileArr[];
//}_DofileObject;
//extern _DofileObject DofileObject;
#include "conio.h"
#pragma warning(disable:4996)
//线程循环调用Sqr回调函数
void squirrel::SqrCallBackFunc()
{
    //if (DofileObject.State == 0) {
    //    DofileObject.State = 1;
    //    for (size_t i = 0; i < DofileObject.DofileArr->size(); i++)
    //    {
    //        std::cout << DofileObject.DofileArr[i] << std::endl;
    //    }
    //    DofileObject.DofileArr->clear();
    //    DofileObject.State = 0;
    //}

    uint32_t v = GetSqVm();

    static int SqrCallBackFuncTimer = clock();
    int NowTime = clock();
    if ((NowTime - SqrCallBackFuncTimer) >= 500)
    {
        DWORD Time = clock();
        DWORD VmTop = SQGetTop(v);
        //std::cout << "stack: " << VmTop << std::endl;
        SQPushRootTable(v);
        SQPushString(v, L"Lenheart", -1);
        SQ_Get(v, -2);
        SQPushRootTable(v);
        SQPushInt(v, VmTop);
        SQ_Call(v, 2, 0, 1);
        SQPop(v, 2);

        SqrCallBackFuncTimer = NowTime;
    }

}







LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode >= 0)
    {
        // 将MSG结构体指针转换为PMSG类型
        PMSG pMsg = (PMSG)lParam;

        // 处理窗口消息
        switch (pMsg->message)
        {
        case WM_KEYDOWN: {
            uint32_t v = GetSqVm();
            int Top = SQGetTop(v);
            SQPushRootTable(v);
            SQPushString(v, L"KeyInputCallBack", -1);
            if (SQ_Get(v, -2) >= 0) {
                SQPushRootTable(v);
                SQPushInt(v, pMsg->wParam);
                SQ_Call(v, 2, 0, 1);
                SQPop(v, 2);
            }
            else {
                SQPop(v, 1);
            }
        }
                       break;
                       // 添加其他需要处理的消息
        default:
            break;
        }

    }

    // 调用下一个hook过程
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}


#ifdef SQUNICODE

#define scvprintf vfwprintf
#else

#define scvprintf vfprintf
#endif

void printfunc(HSQUIRRELVM v, const SQChar* s, ...)
{
    va_list vl;
    va_start(vl, s);
    scvprintf(stdout, s, vl);
    va_end(vl);
}

void squirrel::InitGameScript()
{
    uint32_t v = GetSqVm();
    //消息框黏贴
#ifdef COPY_MESSAGE
    * (WORD*)0x011C53B0 = 0x01B0;
#endif // COPY_MESSAGE



    FILE* file = fopen("DNF.exe", "rb");
    if (file)
    {
        int size = filelength(fileno(file));
        if (size < 30770624) {
            auto Registerfunc = reinterpret_cast<register_pack_handler_t>(0x7186D0);
            Registerfunc(130, sock::Pack_Control, 0);

            sq_setprintfunc((HSQUIRRELVM) v, (SQPRINTFUNCTION) printfunc);
        }
        fclose(file);
    }



    HHOOK hHook = SetWindowsHookEx(WH_GETMESSAGE, HookProc, NULL, GetCurrentThreadId());


#ifdef SELL
    SQPushRootTable(v);
    SQPushString(v, L"L_sq_Dofile", -1);
    SQ_Get(v, -2);
    SQPushRootTable(v);
    SQPushString(v, L"licbylist.dll", -1);
    SQ_Call(v, 2, 0, 1);
    SQPop(v, 2);
#else
    SQPushRootTable(v);
    SQPushString(v, L"dofile", -1);
    SQ_Get(v, -2);
    SQPushRootTable(v);
    SQPushString(v, L"sqr/DofileList.nut", -1);
    SQ_Call(v, 2, 0, 1);
    SQPop(v, 2);
#endif 

}
