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

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

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Level1.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","FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,70,0,1)
	
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+50")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKARFix_Enable","Aspect ratio fix","CKARFix_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("F3 0F ?? ?? ?? 0F ?? ?? 8B 83 ?? ?? ?? ?? 89 ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ASC-Win64-Shipping.exe+42E671F - 0F 29 45 F0            - movaps [rbp-10],xmm0
		--ASC-Win64-Shipping.exe+42E6723 - F3 0F 10 07            - movss xmm0,[rdi]
		--ASC-Win64-Shipping.exe+42E6727 - F2 0F 11 4D 00         - movsd [rbp+00],xmm1 <<==
		--ASC-Win64-Shipping.exe+42E672C - 0F 28 CF               - movaps xmm1,xmm7
		--ASC-Win64-Shipping.exe+42E672F - 48 89 74 24 30         - mov [rsp+30],rsi
	end

	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("C3 ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 48 89 ?? ?? ?? 57 48 83 ?? ?? 0F ?? ?? 48 8B F9 48 ?? ?? 0F ?? ?? F2 0F ?? ?? ?? F2 0F ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x5f,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Level1.exe+187FF5D - 89 41 40              - mov [rcx+40],eax
		--Level1.exe+187FF60 - 8B 42 44              - mov eax,[rdx+44]
		--Level1.exe+187FF63 - 89 41 44              - mov [rcx+44],eax
		--Level1.exe+187FF66 - 8B 41 48              - mov eax,[rcx+48]
		--Level1.exe+187FF69 - 33 42 48              - xor eax,[rdx+48]
	end

	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("8B ?? ?? 48 8B ?? E8 ?? ?? ?? ?? 48 ?? ?? 48 8D ?? ?? ?? ?? ?? 48 8D ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Level1.exe+3EDBD93 - 90                    - nop 
		--Level1.exe+3EDBD94 - CC                    - int 3 
		--Level1.exe+3EDBD95 - 8B 77 04              - mov esi,[rdi+04]
		--Level1.exe+3EDBD98 - 48 8B CB              - mov rcx,rbx
		--Level1.exe+3EDBD9B - E8 50 D4 80 03        - call Level1.exe+76E91F0
	end

	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			$$0 [(allocation)Variables->FOVIn],$$2						$ctx=1
			fld dword ptr [(allocation)Variables->FOVIn]
			fmul dword ptr [(allocation)Variables->factor]
			fptan
			fstp st0
			fld dword ptr [(allocation)Variables->ScreenRatio]			
			fdiv dword ptr [(allocation)Variables->AspectRatio]
			fmulp st1,st0
			fld1
			fpatan
			fdiv dword ptr [(allocation)Variables->factor]
			fst dword ptr [(allocation)Variables->CompensatedFOV]
			fadd dword ptr [(allocation)Variables->AdditionalFOV]
			fstp dword ptr [(allocation)Variables->FOVOut]
			$$0 $$2,[(allocation)Variables->FOVOut]						$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$2,[(allocation)Variables->ScreenRatio]				$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)DOF,DOF_cc:
			%originalcode%
			$$0 $$1,0													$ctx=1
			jmp %returnaddress%
			%end%
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFixEnabled)
		Toggle_CodeCave("ASPECT_cc",bAspect)
		Toggle_CodeCave("DOF_cc",bDOF)
	end	
			
	Write_FOV()
	Write_Aspect()
	
end

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

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.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 Write_Aspect()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AspectRatio"] then
		if bAspect == true then
			Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
		else
			Variables["AspectRatio"]:WriteFloat(fAspectRatio)
		end
	end

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKARFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)		
	Write_Aspect()
	Toggle_CodeCave("ASPECT_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
