//************************************
// FileName:  D:\VisualStudioSource\dnf_project\src\controller.cpp
// FullName:  D:\VisualStudioSource\dnf_project\src
// Date:      2022/09/01
// By:        Vance
// Copyright (c) 2022.  Vance All rights reserved
//************************************

#include "controller.h"
#include <string>
extern HSQUIRRELVM v;
extern std::recursive_mutex SqMtx;

Controller::Controller() : assembler(Arch::kX86),
						   Asm(NULL)
{
	Asm = assembler.GetAssembler();
}

Controller::~Controller()
{
}

void Controller::init()
{
	// HOOK_SETUP(PacketDispatcher_doDispatchRindro);
}

int Controller::hook_PacketDispatcher_doDispatchRindro(PacketDispatcher *a1, CUser *user, int packet_class, int packet_id, char *packet_src, int packet_len, int a7, int a8)
{

	// 区域移动
	if (packet_id == 38)
	{
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		SQBool R;
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_move_area"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_pushinteger(v, int(*(unsigned char *)(packet_src + 0x0D)));
			sq_pushinteger(v, int(*(unsigned char *)(packet_src + 0x0E)));
			sq_call(v, 4, SQTrue, SQTrue); // calls the function
			sq_getbool(v, -1, &R);
		}
		sq_settop(v, top); // restores the original stack size
		if (!R)
			return 0;
	}
	// 普通输入
	// else if (packet_id == 17)
	// {
	// 	std::string str = Utils::PackToHexString((const unsigned char *)packet_src, 20);
	// 	str += ("RindroType");
	// 	str += std::to_string((int)(*(unsigned char *)(packet_src + 0x0d)));
	// 	std::lock_guard<std::recursive_mutex> lock(SqMtx);
	// 	SQBool R;
	// 	// 执行虚拟机Main函数
	// 	SQInteger top = sq_gettop(v); // saves the stack size before the call
	// 	sq_pushroottable(v);		  // pushes the global table
	// 	sq_pushstring(v, _SC("Cb_base_input"), -1);
	// 	if (SQ_SUCCEEDED(sq_get(v, -2)))
	// 	{						 // gets the fie123oo' from the global table
	// 		sq_pushroottable(v); // push the 'this' (in this case is the global table)
	// 		sq_pushuserpointer(v, user);
	// 		sq_pushstring(v, _SC((str).c_str()), -1);
	// 		sq_call(v, 3, SQTrue, SQTrue); // calls the function
	// 		sq_getbool(v, -1, &R);
	// 	}
	// 	sq_settop(v, top); // restores the original stack size
	// 	if (!R)
	// 		return 0;
	// }
	// GM输入
	else if (packet_id == 179)
	{
		// std::cout << "传入的: " << std::hex << (int)user << std::endl;
		std::string str = Utils::PackToHexString((const unsigned char *)packet_src, 13);
		str = str.substr(2);
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_gm_input"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_pushstring(v, _SC(str.c_str()), -1);
			sq_call(v, 3, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
	}
	// 特殊道具使用
	else if (packet_id == 255)
	{
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		SQBool R = true;
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_use_item_sp"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_pushinteger(v, int(*(int *)(packet_src + 0x0D)));
			sq_call(v, 3, SQTrue, SQTrue); // calls the function
			sq_getbool(v, -1, &R);
		}
		sq_settop(v, top); // restores the original stack size
		if (!R)
			return 0;
	}
	// 返回选择角色
	else if (packet_id == 7)
	{
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_return_select_character"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
	}
	// 更换装备
	else if (packet_id == 19)
	{
		int Ret = Get()->old_PacketDispatcher_doDispatchRindro(a1, user, packet_class, packet_id, packet_src, packet_len, a7, a8);
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_player_chanage_equ"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size

		return Ret;
	}
	// 组队  创建和加入
	else if (packet_id == 10)
	{
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		SQBool R;
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_userpartycreate"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQTrue, SQTrue); // calls the function
			sq_getbool(v, -1, &R);
		}
		sq_settop(v, top); // restores the original stack size
		if (!R)
			return 0;
	}
	// 组队  同意组队和同意加入
	else if (packet_id == 11)
	{
		int Ret = Get()->old_PacketDispatcher_doDispatchRindro(a1, user, packet_class, packet_id, packet_src, packet_len, a7, a8);
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_userpartyagree"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
		return Ret;
	}
	// 组队  退出队伍
	else if (packet_id == 13)
	{
		int Ret = Get()->old_PacketDispatcher_doDispatchRindro(a1, user, packet_class, packet_id, packet_src, packet_len, a7, a8);
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_userpartyexit"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
		return Ret;
	}
	// 组队  踢出队伍
	else if (packet_id == 14)
	{
		int Ret = Get()->old_PacketDispatcher_doDispatchRindro(a1, user, packet_class, packet_id, packet_src, packet_len, a7, a8);
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_userpartykick"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
		return Ret;
	}
	// 组队  委任队长
	else if (packet_id == 124)
	{
		int Ret = Get()->old_PacketDispatcher_doDispatchRindro(a1, user, packet_class, packet_id, packet_src, packet_len, a7, a8);
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_userpartygivemaster"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
		return Ret;
	}

	// 下线
	else if (packet_id == 3)
	{
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_player_exit"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_call(v, 2, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
	}
	// 查看信息
	else if (packet_id == 8)
	{
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("Cb_see_information"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_pushuserpointer(v, packet_src);
			sq_call(v, 3, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
	}
	// 自定义包
	else if (packet_id == 130)
	{
		std::string str = Utils::PackToHexString((const unsigned char *)packet_src, 13);
		std::lock_guard<std::recursive_mutex> lock(SqMtx);
		// 执行虚拟机Main函数
		SQInteger top = sq_gettop(v); // saves the stack size before the call
		sq_pushroottable(v);		  // pushes the global table
		sq_pushstring(v, _SC("OnClientSocketMsg"), -1);
		if (SQ_SUCCEEDED(sq_get(v, -2)))
		{						 // gets the fie123oo' from the global table
			sq_pushroottable(v); // push the 'this' (in this case is the global table)
			sq_pushuserpointer(v, user);
			sq_pushstring(v, _SC(str.c_str()), -1);
			sq_call(v, 3, SQFalse, SQTrue); // calls the function
		}
		sq_settop(v, top); // restores the original stack size
	}
	// return Get()->old_PacketDispatcher_doDispatchRindro(a1, user, packet_class, packet_id, packet_src, packet_len, a7, a8);
}
