require(GlobalDependencys:GetDependency("StandardBase"):GetPackageName())

--GAME VARS
fAdditionalFOV = 0
fDefaultAspectRatio = 1.777777777
fFOVMax = 110

--ControlVars
bFixEnabled = true
bFOVFix = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Watch_Dogs"
Process_ClassName = "Watch_Dogs"
Process_EXEName = "watch_dogs.exe"

--INJECTION BEHAVIOUR
InjectDelay = 1000
WriteInterval = 100
SearchInterval = 250
SuspendThread = true

--Name                         Manual/Auto/Hybrid  		Steam/Origin/Any                IncludeFile:Configure;Enable;Periodic;Disable;
SupportedVersions = { 		
{"Automatically Detect",       "Hybrid",  			"Any",	                         "Configure_SignatureScan;Enable_Inject;Periodic;Disable_Inject;"},
}

function Init_Controls()
	DefaultControls.AddHeader("Header_FOV","FOV Fine adjustment",15,70,210,17)
	DefaultControls.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddHeader("Header_FixesEnableDisable","Individual Fixes",245,70,210,17)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV Fix","CKFOVFix_Changed",255,101,180,14)	
end


function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("FOVFix")
	if HackTool:SignatureScan("F30F58??????0000F30F10??????0000F30F11??24??F30F11??24??48????F30F",tAddress,PAGE_EXECUTE_READ,0x8,"Disrupt_b64.dll") == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )		
	end		

	return true

end


function Enable_Inject() 
WindowTool:TrickWindowIntoActivate()
	local DisruptDLLAddress = HackTool:GetModuleAddress("Disrupt_b64.dll")

	FOVCalculator = HackTool:InjectFOVCalculator("FOVCalculator", DisruptDLLAddress)
	
	ResolutionChanged()
	
	local asm = [[	
		(codecave:jmp)FOVFix,FOVFix_cc:							
			fld dword ptr [$$2]					  $context=1
			call (allocation)FOVCalculator
			fstp dword ptr [$$2]                  $context=1
			%originalcode%
			jmp %returnaddress%
			%end%
	]]

	if HackTool:CompileAssembly(asm,"FOVFix",DisruptDLLAddress) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOVFix_cc",bFOVFix)
	end	
		
end

function Periodic()

	PluginViewport:AppendStatusMessage(	string.format("\r\n%s",FOVCalculatorSummary("FOVCalculator","InGame",bFOVFix)) )	
	
end

function Disable_Inject()
	
	CleanUp()
	
end


function ResolutionChanged() 

	UpdateFOVCalculator("FOVCalculator",DisplayInfo:GetAspectRatio()/fDefaultAspectRatio,fAdditionalFOV)	

end

function HK_IncreaseFOV()	
	FOVSlider:OffsetPosition(1)
end

function HK_DecreaseFOV()	
	FOVSlider:OffsetPosition(-1)
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetScaledFloat(4)
	UpdateFOVCalculator_AdditionalFOV("FOVCalculator",fAdditionalFOV)
	lblFOVSlider.Caption:SetCaption( string.format("Value: %.2f",fAdditionalFOV) )	
	ForceUpdate()	
		
end

function CKFOVFix_Changed(Sender)

	bFOVFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOVFix_cc",bFOVFix)	
	ForceUpdate()		
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end