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

--GAME VARS
fAdditionalFOV = 0
fDefaultAspectRatio = 1.777777791
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Under The Waves  "
Process_ClassName = "*"
Process_EXEName = "UnderTheWaves-Win64-Shipping.exe"

--INJECTION BEHAVIOUR
InjectDelay = 0
WriteInterval = 500
SearchInterval = 500
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("CKDisableLetterBox_Enable","Disable letterbox","CKAspect_Changed",255,120,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("74 ?? E8 ?? ?? ?? ?? 0F ?? ?? 48 8B ?? ?? ?? ?? ?? ?? 48 ?? ?? E8 ?? ?? ?? ?? 0F 28 ?? ?? ?? ?? ?? ?? 48 81 ?? ?? ?? ?? ?? C3",tAddress,PAGE_EXECUTE_READ,0x07,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--UnderTheWaves-Win64-Shipping.APlayerCameraManager::GetFOVAngle+5D - 74 05                 	- je UnderTheWaves-Win64-Shipping.APlayerCameraManager::GetFOVAngle+64
		--UnderTheWaves-Win64-Shipping.APlayerCameraManager::GetFOVAngle+5F - E8 FC 63 39 FE           	- call UnderTheWaves-Win64-Shipping.FMemory::Free
		--UnderTheWaves-Win64-Shipping.APlayerCameraManager::GetFOVAngle+64 - 0F 28 C6               	- movaps xmm0,xmm6
		--UnderTheWaves-Win64-Shipping.APlayerCameraManager::GetFOVAngle+67 - 48 8B 8C 24 20 06 00 00  	- mov rcx,[rsp+00000620]
		--UnderTheWaves-Win64-Shipping.APlayerCameraManager::GetFOVAngle+6F - 48 33 CC              	- xor rcx,rsp
	end
	
	local tAddress = HackTool:AddAddress("Aspect")
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83 ?? ?? ?? ?? 89 47 ?? 0F B6 83 ?? ?? ?? ?? 33 ?? ??",tAddress,PAGE_EXECUTE_READ,0x1d,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--UnderTheWaves-Win64-Shipping.UCameraComponent::GetCameraView+F05 - 89 47 2C              	- mov [rdi+2C],eax
		--UnderTheWaves-Win64-Shipping.UCameraComponent::GetCameraView+F08 - 0F B6 83 0C 02 00 00   - movzx eax,byte ptr [rbx+0000020C]
		--UnderTheWaves-Win64-Shipping.UCameraComponent::GetCameraView+F0F - 33 47 30              	- xor eax,[rdi+30]
		--UnderTheWaves-Win64-Shipping.UCameraComponent::GetCameraView+F12 - 83 E0 01              	- and eax,01
		--UnderTheWaves-Win64-Shipping.UCameraComponent::GetCameraView+F15 - 31 47 30              	- xor [rdi+30],eax
	end

	local tAddress = HackTool:AddAddress("Aspect2")
	if HackTool:SignatureScan("0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 76 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? F3 0F 59 ?? ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x29,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
		--UnderTheWaves-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+D9 - F3 0F 10 05 97 CD 41 01 - movss xmm0,[UnderTheWaves-Win64-Shipping.exe+3C28168]
		--UnderTheWaves-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+E1 - F3 0F 5E C1            	- divss xmm0,xmm1
		--UnderTheWaves-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+E5 - F3 0F 59 83 DC 07 00 00 - mulss xmm0,[rbx+000007DC]
		--UnderTheWaves-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+ED - E8 81 77 35 01          - call UnderTheWaves-Win64-Shipping.atanf
		--UnderTheWaves-Win64-Shipping.UCineCameraComponent::RecalcDerivedData+F2 - F3 0F 59 05 CA 1F 9F 01 - mulss xmm0,[UnderTheWaves-Win64-Shipping.exe+41FD3B4]
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
 

	return true

end


function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOV_In")
	Variables:PushFloat("FOV_Out")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:Allocate()
	Variables["AspectRatio"]:WriteFloat(fAspectRatio)
	
	ResolutionChanged()

	local asm = [[
	
		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			movss [(allocation)Variables->FOV_In],$$1				$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]		$ctx=1
			movss [(allocation)Variables->FOV_Out],$$1				$ctx=1			
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)Aspect2,Aspect2_cc:
			movss xmm15,[$$2+4]										$ctx=1
			mulss xmm15,[(allocation)Variables->AspectRatio]
			mulss $$1,xmm15											$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave)Aspect,Aspect_cc:
			$$0 $$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("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, Out=%.2f",	Variables["FOV_In"]: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 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
