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

--GAME VARS
fAdditionalFOV = 0
fDefaultAspectRatio = 1.777777791
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fAspectRatio169 = 1.777777791
fFactor = 0.00872665
fAspectFactor = (fAspectRatio) * 13.36499977

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
bResolution = true
AdditionalFOV = 1.0

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

--INJECTION BEHAVIOUR
InjectDelay = 1000
WriteInterval = 500
SearchInterval = 300
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.AddFixToggle("CKFOVAdjustment_Enable","FOV Adjustment","CKFOV_Changed",255,100,180,14)
	DefaultControls.AddFixToggle("CKResolution_Enable","Resolution fix","CKResolution_Changed",255,120,180,14)
	DefaultControls.AddFixToggle("CKDisableLetterBox_Enable","Aspect ratio fix","CKAspect_Changed",255,140,210,14)
	
	DefaultControls.AddHeader("Header_FOV","FOV Fine adjustment",15,70,210,17)
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,90,0,1)
	
	FOVSlider:SetTickFrequency(10)
	FOVSlider:SetLabel1Text("0")
	FOVSlider:SetLabel2Text("90")

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("F3 0F ?? ?? ?? 48 8B ?? 48 8B ?? 4C 8D ?? ?? 48 ?? ?? FF 90",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Brothers-Win64-Shipping.exe+30F92DE - 48 8B 01              - mov rax,[rcx]
		--Brothers-Win64-Shipping.exe+30F92E1 - FF 90 C0 07 00 00     - call qword ptr [rax+000007C0]
		--Brothers-Win64-Shipping.exe+30F92E7 - F3 0F 11 47 30        - movss [rdi+30],xmm0
		--Brothers-Win64-Shipping.exe+30F92EC - 48 8B 0E              - mov rcx,[rsi]
		--Brothers-Win64-Shipping.exe+30F92EF - 48 8B 01              - mov rax,[rcx]
	end
	
	local tAddress = HackTool:AddAddress("RESHEIGHT")
	if HackTool:SignatureScan("44 89 ?? ?? ?? ?? ?? 44 89 ?? ?? ?? ?? ?? 44 89 ?? ?? ?? ?? ?? 88 1D ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Brothers-Win64-Shipping.exe+359C7F0 - 8D 42 FF       	         - lea eax,[rdx-01]
		--Brothers-Win64-Shipping.exe+359C7F3 - 44 89 2D 0E 75 7F 04     - mov [Brothers-Win64-Shipping.exe+7D93D08],r13d
		--Brothers-Win64-Shipping.exe+359C7FA - 44 89 25 0B 75 7F 04     - mov [Brothers-Win64-Shipping.exe+7D93D0C],r12d
		--Brothers-Win64-Shipping.exe+359C801 - 44 89 35 08 75 7F 04     - mov [Brothers-Win64-Shipping.exe+7D93D10],r14d
		--Brothers-Win64-Shipping.exe+359C808 - 88 1D 06 75 7F 04        - mov [Brothers-Win64-Shipping.exe+7D93D14],bl
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	if HackTool:SignatureScan("76 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? EB ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F",tAddress,PAGE_EXECUTE_READ,0x13,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Brothers-Win64-Shipping.exe+29C6B93 - EB 03                 	- jmp Brothers-Win64-Shipping.exe+29C6B98
		--Brothers-Win64-Shipping.exe+29C6B95 - 0F 57 C0               	- xorps xmm0,xmm0
		--Brothers-Win64-Shipping.exe+29C6B98 - F3 0F 11 83 44 0A 00 00 - movss [rbx+00000A44],xmm0
		--Brothers-Win64-Shipping.exe+29C6BA0 - F3 0F 10 83 5C 0A 00 00 - movss xmm0,[rbx+00000A5C]
		--Brothers-Win64-Shipping.exe+29C6BA8 - F3 0F 59 83 44 0A 00 00 - mulss xmm0,[rbx+00000A44]
	end
	
	local tAddress = HackTool:AddAddress("ASPECT2")
	if HackTool:SignatureScan("0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 76 ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x0b,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Brothers-Win64-Shipping.exe+29C668C - 0F 2F C2               	- comiss xmm0,xmm2
		--Brothers-Win64-Shipping.exe+29C668F - F3 0F 10 89 5C 0A 00 00 - movss xmm1,[rcx+00000A5C]
		--Brothers-Win64-Shipping.exe+29C6697 - F3 0F 59 89 3C 0A 00 00 - mulss xmm1,[rcx+00000A3C]
		--Brothers-Win64-Shipping.exe+29C669F - 76 18                 	- jna Brothers-Win64-Shipping.exe+29C66B9
		--Brothers-Win64-Shipping.exe+29C66A1 - 0F 28 D1               	- movaps xmm2,xmm1
	end
	
	return true

end

function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOV_In")
	Variables:PushFloat("FOV_Out")
	Variables:PushFloat("CompensatedFOV")
	Variables:PushInt("Width")
	Variables:PushInt("Height")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("factor")
	Variables:PushFloat("AspectFactor")
	Variables:Allocate()
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	Variables["Width"]:WriteInt(DisplayInfo:GetWidth())
	Variables["Height"]:WriteInt(DisplayInfo:GetHeight())
	Variables["AspectFactor"]:WriteFloat(fAspectFactor)
	
	ResolutionChanged()

	local asm = [[
	
		(codecave:jmp)FOV,FOV_cc:
			fld dword ptr [$$1+0x4]									$ctx=1
			fstp dword ptr [(allocation)Variables->FOV_In]
			fld dword ptr [$$1]										$ctx=1
			fstp dword ptr [(allocation)Variables->CompensatedFOV]
			addss $$2,[(allocation)Variables->AdditionalFOV]		$ctx=1
			$$0 [(allocation)Variables->FOV_Out],$$2				$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)RESHEIGHT,RESHEIGHT_cc:
			mov r13d,[(allocation)Variables->Width]
			mov r12d,[(allocation)Variables->Height]
			%originalcode%
			jmp %returnaddress%
			%end%

		(codecave:jmp)ASPECT,ASPECT_cc:
			$$0 $$2,[(allocation)Variables->ScreenRatio]			$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)ASPECT2,ASPECT2_cc:
			$$0 $$1,[(allocation)Variables->AspectFactor]			$ctx=1
			jmp %returnaddress%
			%end%
			
	]]

	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("RESHEIGHT_cc",bResolution)
		Toggle_CodeCave("ASPECT_cc",bAspect)
		Toggle_CodeCave("ASPECT2_cc",bAspect)
	end
	
	if bFOV == true then
		WriteFOV()
	end
	
end

function Periodic()

	PluginViewport:AppendStatusMessage( "\r\n" )

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalFOV"] then

		if bFOV == true then
			PluginViewport:AppendStatusMessage( string.format("FOV In=%.2f, Compensated FOV=%.2f, Out=%.2f",	Variables["FOV_In"]:ReadFloat(), Variables["CompensatedFOV"]:ReadFloat(), Variables["FOV_Out"]:ReadFloat() ) )
		end

	end

end

function Disable_Inject()

	CleanUp()
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition()
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalFOV) )
		
	if bFOV == true then
		WriteFOV()
	end
	
	ForceUpdate()

end

function CKFOV_Changed(Sender)

	bFOV = Toggle_CheckFix(Sender)

	if bFOV == true then
		WriteFOV()
	end

	Toggle_CodeCave("FOV_cc",bFOV)	
	ForceUpdate()
	
end

function CKResolution_Changed(Sender)

	bResolution = Toggle_CheckFix(Sender)
	Toggle_CodeCave("RESHEIGHT_cc",bResolution)
	ForceUpdate()

end

function CKAspect_Changed(Sender)

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

function ResolutionChanged() 
	
	fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
	
end

function WriteFOV() 

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

end

function Init()

	Init_BaseControls()
	Init_Controls()
	
end

function DeInit()

	DisableFix()
	
end
