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

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

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Banishers-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)
	FOVSlider:SetLabel1Text("-20")
	FOVSlider:SetLabel2Text("+50")

	DefaultControls.AddHeader("Header_OtherFOV","Other FOV fine adjustment",15,160,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVOtherSlider","FOVOtherSlider_Changed",55,190,125,35,0,70,0,1)
	
	FOVOtherSlider:SetTickFrequency(10)
	FOVOtherSlider:SetLabel1Text("-20")
	FOVOtherSlider:SetLabel2Text("+50")

	DefaultControls.AddHeader("Header_MINFOV","Minimum FOV fine adjustment",15,250,210,17)	
	DefaultControls.AddFixedFOVSlider("MINFOVSlider","MINFOVSlider_Changed",55,280,125,35,10,80,0,1)
	
	MINFOVSlider:SetTickFrequency(10)
	MINFOVSlider:SetLabel1Text("10")
	MINFOVSlider:SetLabel2Text("80")
	
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKMINFOVFix_Enable","Minimum FOV fix","CKMINFOVFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKARFix_Enable","Aspect ratio fix","CKARFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field disable","CKDOFFix_Changed",255,161,180,14)
	DefaultControls.AddFixToggle("CKFRINGEFix_Enable","Chromatic aberration disable","CKFRINGEFix_Changed",255,181,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("FF 90 ?? ?? ?? ?? F3 0F ?? ?? ?? C6 84 ?? ?? ?? ?? ?? ?? 48 8B ?? ?? ?? ?? ?? ?? 48 ?? ?? 74 ??",tAddress,PAGE_EXECUTE_READ,0x06,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Banishers-Win64-Shipping.exe+49C438A - 48 8D 54 24 30        	  - lea rdx,[rsp+30]
		--Banishers-Win64-Shipping.exe+49C438F - FF 90 E8 06 00 00        - call qword ptr [rax+000006E8]
		--Banishers-Win64-Shipping.exe+49C4395 - F3 0F 10 70 30           - movss xmm6,[rax+30]
		--Banishers-Win64-Shipping.exe+49C439A - C6 84 24 F0 07 00 00 00  - mov byte ptr [rsp+000007F0],00
		--Banishers-Win64-Shipping.exe+49C43A2 - 48 8B 8C 24 70 07 00 00  - mov rcx,[rsp+00000770]
	end
	
	local tResultCount = HackTool:SignatureScanMulti("48 89 ?? ?? ?? 57 48 83 ?? ?? 0F ?? ?? 48 8B ?? 48 8B FA 0F ?? ?? F2 0F ?? ?? ?? F2 0F ?? ?? ??","ASPECTS",PAGE_EXECUTE_READ,0x53,Process_EXEName)
	
	if tResultCount ~= 2 then	
		return ErrorOccurred("Could not find ASPECTS injection point, " .. Process_FriendlyName .. 
							 " may have updated to a version that is no longer supported.\r\n\r\n" .. 
							 "Try selecting a different version and re-enable the fix."	)
	else
	
		local tAddress = HackTool:AddAddress("ASPECT2", HackTool:GetAddress( string.format("ASPECTS%d",1) ))
		print( tAddress:GetInfo(TYPE_ADDRESS) )	
		
	end	

	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("F3 0F ?? ?? 08 ?? ?? ?? 0F ?? ?? ?? A8 ?? 74",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Banishers-Win64-Shipping.exe+4A81A5D - F3 41 0F 59 C8          - mulss xmm1,xmm8
		--Banishers-Win64-Shipping.exe+4A81A62 - F3 0F 58 C8             - addss xmm1,xmm0
		--Banishers-Win64-Shipping.exe+4A81A66 - F3 0F 11 8F 08 06 00 00 - movss [rdi+00000608],xmm1
		--Banishers-Win64-Shipping.exe+4A81A6E - 0F B6 42 16             - movzx eax,byte ptr [rdx+16]
		--Banishers-Win64-Shipping.exe+4A81A72 - A8 20                   - test al,20
	end
	
	local tAddress = HackTool:AddAddress("FRINGE")
	
	if HackTool:SignatureScan("7F ?? 89 B3 ?? ?? ?? ?? 41 8B ?? ?? 39 ?? ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Banishers-Win64-Shipping.exe+4A82B41 - 48 8B 05 E0 BC 8D 03  - mov rax,[Banishers-Win64-Shipping.exe+835E828]
		--Banishers-Win64-Shipping.exe+4A82B48 - 39 30                 - cmp [rax],esi
		--Banishers-Win64-Shipping.exe+4A82B4A - 7F 06                 - jg Banishers-Win64-Shipping.exe+4A82B52
		--Banishers-Win64-Shipping.exe+4A82B4C - 89 B3 4C 1F 00 00     - mov [rbx+00001F4C],esi
		--Banishers-Win64-Shipping.exe+4A82B52 - 41 8B 04 3E           - mov eax,[r14+rdi]
	end
	
	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("MinFOV")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AdditionalOtherFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	Variables:PushInt("Aspect")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["MinFOV"]:WriteFloat(10)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AdditionalOtherFOV"]:WriteFloat(fAdditionalOtherFOV)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	if bAspect then
		Variables["Aspect"]:WriteInt(1)
	else
		Variables["Aspect"]:WriteInt(0)	
	end
	
	
	ResolutionChanged()	
				
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			maxss $$1,[(allocation)Variables->MinFOV]				$ctx=1
			movss [(allocation)Variables->FOVIn],$$1				$ctx=1
			fld dword ptr [(allocation)Variables->FOVIn]
			pushfq
			push rbx
			mov ebx,[(allocation)Variables->Aspect]
			cmp ebx,1
			jne next
			
			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]
			
			next:
			mov ebx,[(allocation)Variables->AspectRatio]
			cmp ebx,0x40155555
			jne otherFOV
			
			fadd dword ptr [(allocation)Variables->AdditionalFOV]
			jmp exit
			
			otherFOV:
			fadd dword ptr [(allocation)Variables->AdditionalOtherFOV]
	
			exit:
			pop rbx
			popfq
			fstp dword ptr [(allocation)Variables->FOVOut]
			$$0 $$1,[(allocation)Variables->FOVOut]					$ctx=1
			jmp %returnaddress%
			%end%

		(codecave:jmp)ASPECT2,ASPECT2_cc:
			$$0 [(allocation)Variables->AspectRatio],$$2			$ctx=1
			push rbx
			mov ebx,[(allocation)Variables->Aspect]
			cmp ebx,1
			pop rbx
			jne exitAspect
			$$0 $$2,[(allocation)Variables->ScreenRatio]			$ctx=1
			
			exitAspect:
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave)DOF,DOF_cc:
			nop dword ptr [rax+rax]
			nop dword ptr [rax+rax]
			%end%
			
		(codecave)FRINGE,FRINGE_cc:
			nop
			nop
			%end%
	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFixEnabled)
		Toggle_CodeCave("ASPECT2_cc",bFixEnabled)
		Toggle_CodeCave("DOF_cc",bDOF)
		Toggle_CodeCave("FRINGE_cc",bFringe)
	end	
			
	Write_FOV()
	Write_MinFOV()
	Write_OtherFOV()
	Write_AspectRatio()
	
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 FOVOtherSlider_Changed(Sender)

	fAdditionalOtherFOV = Sender:GetPosition() - 20
	lblFOVOtherSlider.Caption:SetCaption( string.format("Value: %.0f",fAdditionalOtherFOV) )
		
	Write_OtherFOV()	
	ForceUpdate()

end

function MINFOVSlider_Changed(Sender)

	fMinFOV = Sender:GetPosition()
	lblMINFOVSlider.Caption:SetCaption( string.format("Value: %.0f",fMinFOV) )
		
	Write_MinFOV()	
	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_OtherFOV()

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

function Write_MinFOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["MinFOV"] then
		if bMinFOV == true then
			Variables["MinFOV"]:WriteFloat(fMinFOV)
		else
			Variables["MinFOV"]:WriteFloat(10)
		end
	end

end

function Write_AspectRatio()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AspectRatio"] then
		if bAspect == false then
			Variables["Aspect"]:WriteInt(0)
		else
			Variables["Aspect"]:WriteInt(1)
		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 CKMINFOVFix_Changed(Sender)

	bMinFOV = Toggle_CheckFix(Sender)
	Write_MinFOV()
	ForceUpdate()

end

function CKARFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Write_AspectRatio()
	ForceUpdate()
	
end

function CKDOFFix_Changed(Sender)

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

end

function CKFRINGEFix_Changed(Sender)

	bFringe = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FRINGE_cc",bFringe)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
