#pragma once
#pragma once
#include "Utils/MemoryUtils.h"
#include "Client.h"

namespace Hooks {
	class RakPeers {
	private:
		typedef void(__thiscall* RakPeer_RunUpdateCycle_)(void*, void*);
		inline static RakPeer_RunUpdateCycle_ rakpeer_runupdatecycle;

		static void callback_runupdatecycle(void* _this, void* a2);
	public:
		inline static int state = -1;
		static void Init() {
			std::thread([&] {
				while (!Client::clientinstance || 
					!Client::clientinstance->getLoopbackPacketSender() ||
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem() ||
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem()->getRemoteConnectorComposite() || 
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem()->getRemoteConnectorComposite()->getRakNetConnector() ||
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem()->getRemoteConnectorComposite()->getRakNetConnector()->getRakPeer()
					) 
				{
					if (!Client::running) break;
					Sleep(0);
				}

				if (!Client::clientinstance || 
					!Client::clientinstance->getLoopbackPacketSender() ||
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem() ||
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem()->getRemoteConnectorComposite() || 
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem()->getRemoteConnectorComposite()->getRakNetConnector() ||
					!Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem()->getRemoteConnectorComposite()->getRakNetConnector()->getRakPeer()
					) return;

				auto ptr = *(uintptr_t**)(Client::clientinstance->getLoopbackPacketSender()->getNetworkSystem()->getRemoteConnectorComposite()->getRakNetConnector()->getRakPeer());

				state = Utils::Memory::CreateHook(reinterpret_cast<void*>(ptr[93]), (void*)&callback_runupdatecycle, &rakpeer_runupdatecycle);
				
				if (state == 0)
					MessageBoxA(0, _("The program was terminated due to an error.\nError code: 1-16"), _("Valkyrie"), MB_ICONERROR);
				else
					std::cout << _("Test 1-16... OK\n");
				}).detach();
		}
	};
}


