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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio169 = 1.777777791

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
bDOF = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Avowed-Win64-Shipping.exe"

--INJECTION BEHAVIOUR
InjectDelay = 500
WriteInterval = 100
SearchInterval = 100
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_FixesEnableDisable","Individual Fixes",245,70,210,17)
	
	DefaultControls.AddHeader("Header_FOV","In game FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,70,0,1)
	FOVSlider:SetTickFrequency(10)

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKASPECTFix_Enable","Cutscenes pillarboxing fix","CKASPECTFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("77 ?? 48 ?? ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ?? 48 83",tAddress,PAGE_EXECUTE_READ,0x0b,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Avowed-Win64-Shipping.exe+3396264 - 48 8B 01              - mov rax,[rcx]
		--Avowed-Win64-Shipping.exe+3396267 - FF 90 50 07 00 00     - call qword ptr [rax+00000750]
		--Avowed-Win64-Shipping.exe+339626D - F3 0F 10 40 30        - movss xmm0,[rax+30]
		--Avowed-Win64-Shipping.exe+3396272 - 48 83 C4 28           - add rsp,28
		--Avowed-Win64-Shipping.exe+3396276 - C3                    - ret 
	end
	
	local tAddress = HackTool:AddAddress("ASPECT1")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? EB ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Avowed-Win64-Shipping.exe+2A21902 - 0F 2F CA               	- comiss xmm1,xmm2
		--Avowed-Win64-Shipping.exe+2A21905 - 76 0E                 	- jna "Avowed-Win64-Shipping.exe"+2A21915
		--Avowed-Win64-Shipping.exe+2A21907 - F3 0F 10 83 1C 0B 00 00   - movss xmm0,[rbx+00000B1C]
		--Avowed-Win64-Shipping.exe+2A2190F - F3 0F 5E C1            	- divss xmm0,xmm1
		--Avowed-Win64-Shipping.exe+2A21913 - EB 03                 	- jmp "Avowed-Win64-Shipping.exe"+2A21918
	end
	
	local tAddress = HackTool:AddAddress("ASPECT2")
	
	if HackTool:SignatureScan("0F ?? ?? 76 ?? F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 76",tAddress,PAGE_EXECUTE_READ,0x18,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Avowed-Win64-Shipping.exe+2A2140C - 0F 2F C2               	- comiss xmm0,xmm2
		--Avowed-Win64-Shipping.exe+2A2140F - F3 0F 10 89 3C 0B 00 00   - movss xmm1,[rcx+00000B3C]
		--Avowed-Win64-Shipping.exe+2A21417 - F3 0F 59 89 1C 0B 00 00   - mulss xmm1,[rcx+00000B1C]
		--Avowed-Win64-Shipping.exe+2A2141F - 76 18                 	- jna "Avowed-Win64-Shipping.exe"+2A21439
		--Avowed-Win64-Shipping.exe+2A21421 - 0F 28 D1               	- movaps xmm2,xmm1
	end
	
	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("8B ?? ?? 48 ?? ?? E8 ?? ?? ?? ?? 0F ?? ?? 48 8D ?? ?? ?? ?? ?? 48 C1",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Avowed-Win64-Shipping.exe+1F17EF3 - 75 05                 - jne "Avowed-Win64-Shipping.exe"+1F17EFA
		--Avowed-Win64-Shipping.exe+1F17EF5 - BF 04 00 00 00        - mov edi,00000004
		--Avowed-Win64-Shipping.exe+1F17EFA - 8B 3C 37              - mov edi,[rdi+rsi]
		--Avowed-Win64-Shipping.exe+1F17EFD - 48 8B CB              - mov rcx,rbx
		--Avowed-Win64-Shipping.exe+1F17F00 - E8 EB A0 53 01        - call "Avowed-Win64-Shipping.exe"+3451FF0
	end
	
	return true
	
end

function Enable_Inject()
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("NewFactor")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
		
	ResolutionChanged()	

	local asm = [[		

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn], $$1							$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]				$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$1							$ctx=1			
			jmp %returnaddress%
			%end%
								
		(codecave:jmp)ASPECT1,ASPECT1_cc:
			%originalcode%
		    fld dword ptr [$$2+4]											$ctx=1
		    fmul dword ptr [(allocation)Variables->ScreenRatio]
		    fstp dword ptr [(allocation)Variables->NewFactor]
		    $$0 $$1,[(allocation)Variables->NewFactor]						$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)ASPECT2,ASPECT2_cc:
		    fld dword ptr [$$2+4]											$ctx=1
		    fmul dword ptr [(allocation)Variables->ScreenRatio]
		    fstp dword ptr [(allocation)Variables->NewFactor]
		    $$0 $$1,[(allocation)Variables->NewFactor]						$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave)DOF,DOF_cc:
			xor $$1,$$1														$ctx=1
			nop
			%end%
			
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("ASPECT1_cc",bAspect)
		Toggle_CodeCave("ASPECT2_cc",bAspect)
		Toggle_CodeCave("DOF_cc",bDOF)
	end	
			
	Write_FOV()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Out : %.2f", fFOVIn, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("World FOV: %.0f",fAdditionalFOV) )
		
	Write_FOV()	
	ForceUpdate()

end

function Write_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalFOV"] then
		if bFOV == true then
			Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
		else
			Variables["AdditionalFOV"]:WriteFloat(0)
		end
	end

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

	bFOV = Toggle_CheckFix(Sender)
	Write_FOV()
	Toggle_CodeCave("FOV_cc",bFOV)
	ForceUpdate()
	
end

function CKASPECTFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ASPECT1_cc",bAspect)
	Toggle_CodeCave("ASPECT2_cc",bAspect)
	ForceUpdate()
	
end

function CKDOFFix_Changed(Sender)

	bDOF = Toggle_CheckFix(Sender)
	Toggle_CodeCave("DOF_cc",bDOF)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
		
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
