#pragma once
#include "DNFTOOL.h"
#include "RSAC.h"


//-------------------------------------------------------------------------------------------Squirrel
// printFunc  输出调用栈和栈内容
typedef int(SqPrintFunc)(uint32_t v);
static SqPrintFunc* SQPrintFunc = (SqPrintFunc*)0x1354060;
// PushRoot
typedef int(SqPushRootFunc)(uint32_t v);
static SqPushRootFunc* SQPushRootTable = (SqPushRootFunc*)0x1358C50;
// Push 函数绑定闭包
typedef int(realSqNewClosure)(uint32_t v, void* funcAddr, int a);
static realSqNewClosure* RealSqNewClosure = (realSqNewClosure*)0x135B850;
#define INIT_NUT_ADDRESS 0x135B850
static realSqNewClosure* MLnewclosure = nullptr;
// New槽
typedef int(SqNewSlot)(uint32_t v, int a, BOOL b);
static SqNewSlot* SQNewSlot = (SqNewSlot*)0x135AA80;
// 平栈
typedef int(SqPopTop)(uint32_t v);
static SqPopTop* SQPopTop = (SqPopTop*)0x1358FF0;
// 平栈
typedef int(SqPop)(uint32_t v, int n);
static SqPop* SQPop = (SqPop*)0x1358FD0;
//SQ_Get
typedef int(Sq_Get)(uint32_t v, int n);
static Sq_Get* SQ_Get = (Sq_Get*)0x135AE30;

#define _RT_NULL			0x00000001
#define _RT_INTEGER			0x00000002
#define _RT_FLOAT			0x00000004
#define _RT_BOOL			0x00000008
#define _RT_STRING			0x00000010
#define _RT_TABLE			0x00000020
#define _RT_ARRAY			0x00000040
#define _RT_USERDATA		0x00000080
#define _RT_CLOSURE			0x00000100
#define _RT_NATIVECLOSURE	0x00000200
#define _RT_GENERATOR		0x00000400
#define _RT_USERPOINTER		0x00000800
#define _RT_THREAD			0x00001000
#define _RT_FUNCPROTO		0x00002000
#define _RT_CLASS			0x00004000
#define _RT_INSTANCE		0x00008000
#define _RT_WEAKREF			0x00010000

#define SQOBJECT_REF_COUNTED	0x08000000
#define SQOBJECT_NUMERIC		0x04000000
#define SQOBJECT_DELEGABLE		0x02000000
#define SQOBJECT_CANBEFALSE		0x01000000

//typedef enum tagSQObjectType {
//    OT_NULL = (_RT_NULL | SQOBJECT_CANBEFALSE),
//    OT_INTEGER = (_RT_INTEGER | SQOBJECT_NUMERIC | SQOBJECT_CANBEFALSE),
//    OT_FLOAT = (_RT_FLOAT | SQOBJECT_NUMERIC | SQOBJECT_CANBEFALSE),
//    OT_BOOL = (_RT_BOOL | SQOBJECT_CANBEFALSE),
//    OT_STRING = (_RT_STRING | SQOBJECT_REF_COUNTED),
//    OT_TABLE = (_RT_TABLE | SQOBJECT_REF_COUNTED | SQOBJECT_DELEGABLE),
//    OT_ARRAY = (_RT_ARRAY | SQOBJECT_REF_COUNTED),
//    OT_USERDATA = (_RT_USERDATA | SQOBJECT_REF_COUNTED | SQOBJECT_DELEGABLE),
//    OT_CLOSURE = (_RT_CLOSURE | SQOBJECT_REF_COUNTED),
//    OT_NATIVECLOSURE = (_RT_NATIVECLOSURE | SQOBJECT_REF_COUNTED),
//    OT_GENERATOR = (_RT_GENERATOR | SQOBJECT_REF_COUNTED),
//    OT_USERPOINTER = _RT_USERPOINTER,
//    OT_THREAD = (_RT_THREAD | SQOBJECT_REF_COUNTED),
//    OT_FUNCPROTO = (_RT_FUNCPROTO | SQOBJECT_REF_COUNTED), //internal usage only
//    OT_CLASS = (_RT_CLASS | SQOBJECT_REF_COUNTED),
//    OT_INSTANCE = (_RT_INSTANCE | SQOBJECT_REF_COUNTED | SQOBJECT_DELEGABLE),
//    OT_WEAKREF = (_RT_WEAKREF | SQOBJECT_REF_COUNTED)
//}SQObjectType;
//SQ_GetType
typedef int(Sq_GetType)(uint32_t v, int n);
static Sq_GetType* SQ_GetType = (Sq_GetType*)0x1358CD0;



//SQ_Rawget
typedef int(Sq_Rawget)(uint32_t v, int n);
static Sq_Rawget* SQ_Rawget = (Sq_Rawget*)0x135AEA0;
//SQ*Getlocal
typedef wchar_t* (SqGetlocal)(uint32_t v, int n, int idx);
static SqGetlocal* SQGetlocal = (SqGetlocal*)0x135AFD0;
//SQ_Wakeupvm
typedef int (Sq_Wakeupvm)(uint32_t v, int n, int retval, int raiseerror);
static Sq_Wakeupvm* SQ_Wakeupvm = (Sq_Wakeupvm*)0x135B0D0;
//SQ_Call
typedef int (Sq_CallFunc)(uint32_t v, int params, int retval, int raiseerror);
static Sq_CallFunc* SQ_Call = (Sq_CallFunc*)0x1359280;
//SQ_Remove
typedef int (SQ_RemoveFunc)(uint32_t v, int idx);
static SQ_RemoveFunc* SQ_Remove = (SQ_RemoveFunc*)0x1359000;
//GetInt
typedef int(SqGetIntFunc)(uint32_t v, uint32_t stackIndex, int* sint);
static SqGetIntFunc* SQGetInt = (SqGetIntFunc*)0x1358D70;
//GetFloat
typedef int(SqGetFloatFunc)(uint32_t v, uint32_t stackIndex, FLOAT* sfloat);
static SqGetFloatFunc* SQGetFloat = (SqGetFloatFunc*)0x1358DD0;
//GetBool
typedef int(SqGetBoolFunc)(uint32_t v, uint32_t stackIndex, BOOL* sbool);
static SqGetBoolFunc* SQGetBool = (SqGetBoolFunc*)0x1358E30;
//GetString
typedef int(SqGetStringFunc)(uint32_t v, uint32_t stackIndex, wchar_t** sstring);
static SqGetStringFunc* SQGetString = (SqGetStringFunc*)0x1358E70;
typedef int(SqGetStringCFunc)(uint32_t v, uint32_t stackIndex, char** sstring);
static SqGetStringCFunc* SQGetStringc = (SqGetStringCFunc*)0x1358E70;
//GetUserdata
typedef int(SqGetUserdataFunc)(uint32_t v, uint32_t stackIndex, DWORD* up, DWORD* up1);
static SqGetUserdataFunc* SQGetUserdata = (SqGetUserdataFunc*)0x1358EC0;
//GetObjtypetag
typedef int(SqGetObjtypetagFunc)(uint32_t v, DWORD* up);
static SqGetObjtypetagFunc* SQGetObjtypetag = (SqGetObjtypetagFunc*)0x1358F20;
//GetUserpointer
typedef int(SqGetUserpointerFunc)(uint32_t v, uint32_t idx, DWORD* up);
static SqGetUserpointerFunc* SQGetUserpointer = (SqGetUserpointerFunc*)0x1358F80;
//GetTop
typedef int(SqGetTopFunc)(uint32_t v);
static SqGetTopFunc* SQGetTop = (SqGetTopFunc*)0x1358FC0;
//获取SQR的对象转换为原始对象地址
typedef int(_cdecl __GetSqrObject)(uint32_t a1, int a2);
static __GetSqrObject* GetSqrObject = (__GetSqrObject*)0x5c1420;
static __GetSqrObject* GetExeObject = (__GetSqrObject*)0x5c13A0;
//Push
typedef int(SqPushFunc)(uint32_t v, int idx);
static SqPushFunc* SQPush = (SqPushFunc*)0x1358C90;
//PushString
typedef int(SqPushStringFunc)(uint32_t v, const wchar_t* s, int len);
typedef int(SqPushStringFuncs)(uint32_t v, const char* s, int len);
static SqPushStringFunc* SQPushString = (SqPushStringFunc*)0x1358A60;
static SqPushStringFuncs* SQPushStringc = (SqPushStringFuncs*)0x1358A60;
//PushInt
typedef int(SqPushIntFunc)(uint32_t v, int sint);
static SqPushIntFunc* SQPushInt = (SqPushIntFunc*)0x1358AD0;
//PushBool
typedef int(SqPushBoolFunc)(uint32_t v, BOOL sbool);
static SqPushBoolFunc* SQPushBool = (SqPushBoolFunc*)0x1358B10;
//PushFloat
typedef int(SqPushFloatFunc)(uint32_t v, FLOAT sfloat);
static SqPushFloatFunc* SQPushFloat = (SqPushFloatFunc*)0x1358B60;

//PushUserpointer
typedef int(SqPushUserpointerFunc)(uint32_t v, void* sfloat);
static SqPushUserpointerFunc* SqPushUserpointer = (SqPushUserpointerFunc*)0x1358BA0;

////sq_newarray
//typedef int(_sq_newarray)(uint32_t v, int count);
//static _sq_newarray* sq_newarray = (_sq_newarray*)0x135A490;



//SQLOADFILE
typedef void* (SQLOADFILEFunc)(uint32_t v, const wchar_t* filename, BOOL printerror);
static SQLOADFILEFunc* SQLOADFILEF = (SQLOADFILEFunc*)0x13560E0;
//SQDOFILE
typedef void* (SQDOFILEFunc)(uint32_t v, const wchar_t* filename, BOOL retval, BOOL printerror);
static SQDOFILEFunc* SQDOFILE = (SQDOFILEFunc*)0x1356240;



typedef void* (*SQFILE)();
#define SQ_SUCCEEDED(res) (res>=0)
//SQ_io_file_lexfeed_编码
//typedef int(LSQLEXREADFUNC)(int n);
typedef int(*LSQLEXREADFUNC)(int n);
static LSQLEXREADFUNC SQ_io_file_lexfeed_ASCII = (LSQLEXREADFUNC)0x1355F60;
static LSQLEXREADFUNC SQ_io_file_lexfeed_UCS2_BE = (LSQLEXREADFUNC)0x1356050;
static LSQLEXREADFUNC SQ_io_file_lexfeed_UCS2_LE = (LSQLEXREADFUNC)0x1356020;
static LSQLEXREADFUNC SQ_io_file_lexfeed_UTF8 = (LSQLEXREADFUNC)0x1355F90;
//SQfopen
typedef SQFILE* (SQfopenFunc)(const wchar_t* filename, const wchar_t* type);
static SQfopenFunc* SQfopen = (SQfopenFunc*)0x1355DC0;
//SQfread
typedef int(SQfreadFunc)(void* buffer, int a, int b, void* file);
static SQfreadFunc* SQfread = (SQfreadFunc*)0x1355DD0;
//SQwrite
typedef int(SQwriteFunc)(void* buffer, int a, int b, void* file);
static SQwriteFunc* SQwrite = (SQwriteFunc*)0x1355DE0;
//SQfseek
typedef int(SQfseekFunc)(void* buffer, int a, int b);
static SQfseekFunc* SQfseek = (SQfseekFunc*)0x1355DF0;
//SQftell
typedef int(SQftellFunc)(void* file);
static SQftellFunc* SQftell = (SQftellFunc*)0x1355E50;
//SQ_File_read
typedef int(SQ_File_readFunc)(void* file, void* afile, int a);
static SQ_File_readFunc* SQ_File_read = (SQ_File_readFunc*)0x1356090;
//SQ_Readclosure
typedef int(SQ_ReadclosureFunc)(uint32_t v, SQ_File_readFunc a, void* file);
static SQ_ReadclosureFunc* SQ_Readclosure = (SQ_ReadclosureFunc*)0x1359460;
//SQ_Fclose
typedef int(SQ__FcloseFunc)(void* file);
static SQ__FcloseFunc* SQ__Fclose = (SQ__FcloseFunc*)0x1355E70;
//SQ_Compile
typedef int(SQ_CompileFunc)(uint32_t v, LSQLEXREADFUNC FUNC, void* file, const wchar_t* filename, BOOL printerror);
static SQ_CompileFunc* SQ_Compile = (SQ_CompileFunc*)0x135A390;
//SQ_Compilebuffer
typedef int(SQ_CompilebufferFunc)(uint32_t v, const wchar_t* s,int size, wchar_t* filename, BOOL printerror);
static SQ_CompilebufferFunc* SQ_Compilebuffer = (SQ_CompilebufferFunc*)0x135B2C0;
//SQ_Throwerror
typedef int(SQ_ThrowerrorFunc)(uint32_t v, const wchar_t* error);
static SQ_ThrowerrorFunc* SQ_Throwerror = (SQ_ThrowerrorFunc*)0x13591A0;

//获取Squirrel v 基址
inline uint32_t GetSqVm();





class squirrel
{
private:


public://原生
    static int OutError(wchar_t* Error, uint32_t v);


public://加载
	static int SQloadfile(uint32_t v, const wchar_t* filename, BOOL printerror);
	static int SQdofile(uint32_t v, const wchar_t* filename, BOOL retval, BOOL printerror);

    static int SQloadfileBuffer(uint32_t v, const wchar_t* filename, const wchar_t* str,BOOL printerror);
    static int SQdofileBuffer(uint32_t v, const wchar_t* filename, const wchar_t* str);

    static int LenheartGetIntData(uint32_t v);//获取静态数据
public://绘制类
    static int sq_GetImg(uint32_t v);
    static int sq_DrawItem(uint32_t v);
    static int sq_DrawImg(uint32_t v);//绘制Img
    static int sq_IntiNumberDraw(uint32_t v);//初始化数字绘制
    static int sq_DrawNumber(uint32_t v);//数字绘制

public://对象类
    static int GetObjectAddress(uint32_t v);//获取对象地址
    static int GetObjectName(uint32_t v);//获取对象名字
    static int GetObjectInfo(uint32_t v);//获取对象属性
    static int GetObjectDeInfo(uint32_t v);//获取对象属性
    static int SetObjectInfo(uint32_t v);//设置对象属性
    static int SetObjectDeInfo(uint32_t v);//设置对象属性
    static int GetObjectLevel(uint32_t v);//获取对象等级
    static int sq_GetPlayerEachName(uint32_t v);//获取玩家交互对象名字


public:
    
    //NUT API 接口
    static int sq_GetSkillAddress(uint32_t v);//获取技能地址
    static int sq_Test(uint32_t v);//测试接口
    static int sq_OpenConsole(uint32_t v);
    //打开控制台
    static int sq_Switching(uint32_t v);//换装参数
    static int sq_MoveMap(uint32_t v);//顺图
    static int GetCharacterAttribute(uint32_t v);//读人物或装备属性
    static int SetCharacterAttribute(uint32_t v);//写人物或装备属性
    static int GetTownIndex(uint32_t v);//获取城镇编号
    static int GetRegionIndex(uint32_t v);//获取城镇区域编号
    static int GetTownXpos(uint32_t v);//获取城镇X坐标
    static int GetTownYpos(uint32_t v);//获取城镇Y坐标
    static int GetFatigue(uint32_t v);//获取疲劳值
    static int GetExp(uint32_t v);//获取经验值
    static int GoDungeon(uint32_t v);//去副本
    static int GoTown(uint32_t v);//回城
    static int MoveTown(uint32_t v);//回城
    static int LDofile(uint32_t v);//Ldofile
    static int LReadAddress(uint32_t v);//读内存
    static int LWriteAddress(uint32_t v);//写内存
    static int Lcout(uint32_t v);//Lcout
    static int sq_DrawCode(uint32_t v);//绘制字符
    static int NewWindows(uint32_t v);//新建窗口
    static int sq_Cmd(uint32_t v);//Cmd命令
    static int SetSlot(uint32_t v);//设置UI槽坐标
    static int RegisterItemColor_STL(uint32_t v);//设置项目颜色
    static int RegisterCodeDraw_STL(uint32_t v);//设置项目颜色
    static int Sout(uint32_t v);//输出
    static int Clock(uint32_t v);//获取程序运行时间

public://发包 API
    static int SendPackType(uint32_t v);//发包类型
    static int SendPackByte(uint32_t v);//发包Byte
    static int SendPackWord(uint32_t v);//发包Word
    static int SendPackDWord(uint32_t v);//发包DWord
    static int SendPackWChar(uint32_t v);//发包char*
    static int SendPack(uint32_t v);//发包

    static int GivePlayerItem(uint32_t v);//发物品给玩家
    static int GivePlayerEqu(uint32_t v);//发装备给玩家

public://容器类接口
    static int Get_STL(uint32_t v);//查询 类型容器
    static int Set_STL(uint32_t v);//设置 类型容器
    static int New_STL(uint32_t v);//new 类型容器
    static int Timer_STL(uint32_t v);//时间容器
    static int Jsoner_STL(uint32_t v);//时间容器

    static int Coder_STL(uint32_t v);//时间容器

public://龙盒
    static int Sync_Dragon_Pack(uint32_t v);//同步 龙盒数据包
    static int Get_Dragon_Pack(uint32_t v);//获取 龙盒数据包
    static int Redom_Dragon(uint32_t v);//发送 开龙盒
    static int Get_DragonModel(uint32_t v);//获取 龙盒 模式
    static int Set_DragonModel(uint32_t v);//设置 龙盒 模式



public:

	//新增nut接口funcName绑定C语言函数funcAddr
	static void RegisterNutApi(const wchar_t* funcName, void* funcAddr, uint32_t v);
	//注册接口
	static void R_Register_Nut();

public:
    //首次初始化Nut
    static void InitGameScript();
    //循环调用Nut回调
    static void SqrCallBackFunc();
};



//新增Nut接口绑定C函数
// 示例如下
/*
static int GoDungeon(uint32_t v)
{
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4 = 0;

    int num;
    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  表示该函数有返回值传给nut脚本 0则无
    return 1;
}
*/
//新增C++调用 nut
// 示例如下
/*
    uint32_t v = GetSqVm();
    SQPushRootTable(v);
    SQPushString(v, L"dofile", -1);
    SQ_Get(v, -2);
    SQPushRootTable(v);
    SQPushString(v, L"1.cpp", -1);
    SQ_Call(v, 2, 1, 0);
    SQPop(v, 2);
*/