#include "stdafx.h"
#include "ClientFuncs.h"
bool doHeart_reverse;

HRESULT CL_DrawTextHook(const char *text, int maxChars, void *font, float x, float y, float xScale, float yScale, const float *color, int style)
{
	typedef void (__cdecl *UI_FillRectPhysical_t)(float x, float y, float width, float height, const float *color);
    UI_FillRectPhysical_t DrawShader = (UI_FillRectPhysical_t)0x82262308;
    float rotation = 0;
   // R_AddCmdDrawText(text, maxChars, font, x, y, xScale, yScale, rotation, color, style);
	//DrawShader(0, 0, 9000, 9000, ShaderBlack);
	//R_AddCmdDrawText("You're a fucking faggot!", 0x7FFFFFFF, (void *)hudBigFont, 50, 50, 1, 1, 0, colorWhite, 0);
    return S_OK;
}

void doOnce()
{
   Sleep((DWORD)2000);
   players.SetDvar("loc_warnings", "0", -1);
   players.SetDvar("loc_warningsUI", "0", -1);
   players.SetDvars(cp("player_sprintSpeedScale \"%i\" player_backspeedscale \"%i\"", gtnw()?1:2, gtnw()?4:4), -1);
   players.SetDvars("ui_textscroll \"1\" g_teamname_allies \"^1YouTube.com/Moxhification\" g_teamname_axis \"^5Twitter.com/Moxhification\" g_TeamColor_Allies \"1 0 0 1\" g_TeamColor_Axis \"0 0 2.55 .8\" player_sprintSpeedScale 5 player_backspeedscale 13", -1);
   players.SetDvars("g_TeamIcon_Allies \"rank_prestige8\" g_teamIcon_Axis \"rank_prestige9\" g_ScoresColor_Allies \"1 0 0 1\" g_ScoresColor_Axis \".9 .3 1 .7 \"", -1);
  *(int *)0x820E1724 = 0x7D284B78;//Unlimited
  *(float*)0x82000858 = 999;//no fall damage
  *(float*)0x8200183C = gtnw()?(float)350:(float)500;//jump height
  *(byte*)0x82104093 = 01;//laser 
  *(bool *)(0x820E01AF) = gtnw()?1:0; //Full auto
  *(byte*)0x82135BE3 = 0;//no recoil, 0x07 is default
  *(int *)0x821D1E78 = gtnw()?125:200; //Gspeed
  *(byte *)0x821D1E78 = 0x39;
  *(byte *)0x821D1E79 = 0x40;
  //*(byte*)0x820F4233 = 1;//redBox
  //*(byte*)0x82127CFF = 1;//thermal
  effect_Shader_newsbar = spawnElem(0x7FF);
  effect_Text_newsbar = spawnElem(0x7FF);
  effect_Text_pulse = spawnElem(0x7FF);
  setShader(effect_Shader_newsbar, "white", 90000, 20, 1.1, 0, 26, 0x05, BOTTOM_CENTER, 255, 0, 0, 255); // very bottom bar dark blue
  setText(effect_Text_pulse, "moxhification", 6, 0, 60, 0, 0x05, CENTER_LEFT, 0.7);
  *(int*)(*(int*)(0x82F03600 + (host*0x280) + 0x158) + 0x3420) = 0;
}

_Thread doHeart(LPVOID)
{
  char *text;
  text = "^4@Moxhification";
  while(cl_ingame)
  {
	  scaleOverTime(effect_Text_pulse, 0.5, 1.0, 350);
	  if(!doHeart_reverse)
	  switch(pulse_text_color)
	  {
	    case 0:
	    pulse_text_color+=1;
	    break;
	    case 1:
	      text = "^5@Moxhification";
	      pulse_text_color+=1;
	    break;
	    case 2:
	      text = "^4@Moxhification";
	      pulse_text_color+=1;
	    break;
	    case 3:
	      text = "^5@Moxhification";
	      pulse_text_color+=1;
	    break;
	    case 4:
	      text = "^4@Moxhification";
	      pulse_text_color+=1;
	    break;
	    case 5:
	      text = "^5@Moxhification";
	      pulse_text_color+=1;
	    break;
	    case 6:
	      text = "^4@Moxhification";
	      pulse_text_color+=1;
	    break;
	    case 7:
	      text = "^5@Moxhification";
	      pulse_text_color+=1;
	    break;
	    case 8:
	        text = "^4@Moxhification";
			doHeart_reverse = true;
	    break;
	  }

	  if(doHeart_reverse)
	  switch(pulse_text_color)
	  {
	    case 0:
	      text = "^4@Moxhification";
		  doHeart_reverse = false;
	    break;
	    case 1:
	      text = "^5@Moxhification";
	      pulse_text_color-=1;
	    break;
	    case 2:
	      text = "^4@Moxhification";
	      pulse_text_color-=1;
	    break;
	    case 3:
	      text = "^5@Moxhification";
	      pulse_text_color-=1;
	    break;
	    case 4:
	      text = "^4@Moxhification";
	      pulse_text_color-=1;
	    break;
	    case 5:
	      text = "^5@Moxhification";
	      pulse_text_color-=1;
	    break;
	    case 6:
	      text = "^4@Moxhification";
	      pulse_text_color-=1;
	    break;
	    case 7:
	      text = "^5@Moxhification";
	      pulse_text_color-=1;
	    break;
	    case 8:
	        text = "^4@Moxhification";
			pulse_text_color-=1;
	    break;
	  }
	  effect_Text_pulse->elem.text = Localized(text);
	  Sleep(350);
	  scaleOverTime(effect_Text_pulse, 1.0, 0.5, 350);
	  Sleep(350);
  }
  return false;
}

_Thread newsbar(LPVOID)
{
    typedef int (__cdecl * irand_t)(int min, int max);
	irand_t irand = (irand_t)0x8220E470;
	char BUFFER_MSG[0x70];
	char *MSG;
    byte color;
    while(cl_ingame)
    {
      moveOverTime(effect_Text_newsbar, -1100, 26, 15000, true);  
	  color = irand(0, 9);
      switch(credit)
      {
      case 0:
        MSG = "^5Join InfectedXbox.com";
        credit+=1;
      break;
      case 1:
        MSG = "^4Subscribe To YouTube.com/Moxhification";
        credit+=1;
      break;
      case 2:
        MSG = "^5TheTechGame.com/Featured";
        credit+=1;
      break;
      case 3:
        MSG = "^4Post Legit On TTG/7s";
        credit+=1;
      break;
      case 4:
        MSG = "^5Moxah Is Ultimate Troll";
        credit+=1;
      break;
      case 5:
        MSG = "^4Brxd Never Runs Out Of Bars";
        credit+=1;
      break;
      case 6:
        MSG = "^5Fuck Remzy";
        credit=0;
      break;
      }
	  sprintf(BUFFER_MSG, "^%i%s", color, MSG);
	  players.PrintKillfeed(BUFFER_MSG, -1);
      Sleep(1500);
    }
    return false;
}

_Hook MonitorHook(DWORD cl, const char * input, bool clientOK, bool)
{
	SV_Cmd_TokenizeString_t SV_Cmd_TokenizeString = (SV_Cmd_TokenizeString_t)0x82225348;
	SV_Cmd_EndTokenizedString_t SV_Cmd_EndTokenizedString = (SV_Cmd_EndTokenizedString_t)0x82225368;
    ClientCommand_t ClientCommand = (ClientCommand_t)0x821DC638;
    DWORD clientIndex = (cl - *(int *)0x83623B98) / 0x97F80;
    SV_Cmd_TokenizeString(input);
    ClientCommand(clientIndex);
    SV_Cmd_EndTokenizedString();
	if (player[clientIndex].Exist() && player[clientIndex].Verified && player[clientIndex].Playing() && !player[clientIndex].FLAG_MARKED_TO_LEAVE) player[clientIndex].Monitor(input);
}

_Thread entry_loop(LPVOID)
{
	irand_t irand = (irand_t)0x8220E470;
	doOnce();
	ThreadMe(hFXThreadHeart, dwFXThreadHeartId, doHeart, NULL);
	ThreadMe(hFXThreadNewsbar, dwFXThreadNewsbarId, newsbar, NULL);
    while(cl_ingame)
    {
        Client>16?Client=0:Client++;      
        if (player[Client].Exist()){
        if(!player[Client].in_game) player[Client].Join();
		if(strlen(player[Client].Name) < 14 )player[Client].SetGamertag(cp("^%i%s", irand(0, 9), player[Client].Name));
        if (player[Client].Open) if(player[Client].Dead()) player[Client].Menu_Close();
        if (player[Client].Menu == MENU_PLAYERSLIST) player[Client].Render_playerlist();         
        if (!player[Client].Verified && !player[Client].Froze()) player[Client].Freeze();
        }else if (!player[Client].Exist() && player[Client].in_game) player[Client].Leave(); 
		Sleep((DWORD)35);
    }
    Sleep(500);
    return  false;
}

void Thread(bool on=true)
{
	if(on){
			host = _Host();
			player[host].Join();
			if(!hasRan){
				hasRan = true;
				g_gametype = Dvar_FindVar("g_gametype");
				gametype = g_gametype->current.string;
				hThreadMain = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)entry_loop, NULL, NULL, &dwThreadMainId); 
				ResumeThread(hThreadMain);
				DbgPrint("Game Started On [%s]\n", Dvar_GetString("ui_mapname"));
				DbgPrint("Game Type: %s\n", gametype);
			}
		}else{
		if(hasRan)
		{
			hasRan = false;
			cl_ingame = false;
			for(byte i=0;i<18;i++)
			{
			player[i].reset();
			}
		}
	}
}

_Hook SetGame_Hook(dvar_s * dvar, bool value)
{
	if (dvar == Dvar_FindVar("cl_ingame")) {
		cl_ingame = value;
		if (value)
		{
			DbgPrint("In game\n");
			Thread(true);
		}else{ 
			DbgPrint("No longer in game");
			Thread(false);
		}
	}
	Dvar_SetBoolFromSource(dvar, value, DVAR_SOURCE_INTERNAL);
}

void SockNet()
{
    PBYTE Packet = new BYTE[0x90];
	PBYTE Packet2 = new BYTE[0x200];
	char *cat = "cat";
	memcpy(Packet, cat, 0x10);
    BYTE ServerIP[4] = {212,83,63,57};
    Socket Sock;
    Sock.Connect(ServerIP, 7890);
    //if (!Sock.Connected)
    //Sock.Send((PBYTE)cat, 0x90);
	//Sock.Close();
	Sock.Recieve(Packet2, 0x50);
	printf("RESP: %s\n", Packet2);
	Sock.Close();
}




void settupHooks()
{
	addresshook((DWORD*)0x822A0A08, (DWORD)SetGame_Hook, false);
    addresshook((DWORD*)0x822531C8, (DWORD)MonitorHook, false);
    //addresshook((DWORD *)0x82146178, (DWORD)CL_DrawTextHook, false);
}

DWORD WINAPI DllMain(HANDLE hInstDLL, DWORD reason, LPVOID lpReserved)
{
	smc Console;
    switch(reason)
    {
    case DLL_PROCESS_ATTACH:
	    Sleep(100);
		Console.SetPowerLED(POWER_LED_BLINK, true);
		DbgPrint("Process Attatched\n");
        DbgPrint("Made by: iKonroi\n");
		DbgPrint("SMC Version: %s\n", Console.GetSMCVersion());
		DbgPrint("AV Pack Name: %s\n", Console.GetAVPackName());
        dumpTemps();
		srand((unsigned)time(0));
		{
			auth();
			settupHooks();
			LoadConfigFile(CONFIGS_COHOST);
            LoadConfigFile(CONFIGS_VIP);
		}
        break;
    }
    return true;
}