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

--ControlVars
bFixEnabled = true
bFOV = true
bAspectRatio = true
bAspectFix = true

--GAME VARS
fFOV = 1.0
FOVScale = 1.0
fFOVScalingIn = 1.0
fFOVScalingOut = 1.0
fDefaultAspectRatio = 1.777777791
fAspectRatio = 1.777777791

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Assassin's Creed Valhalla"
Process_ClassName = "*"
Process_EXEName = "ACValhalla.exe"

--INJECTION BEHAVIOUR
InjectDelay = 500 
WriteInterval = 500
SearchInterval = 1000
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,50,200,0,1)
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("50%")
	FOVSlider:SetLabel2Text("200%")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKARatioFix_Enable","Cinematics aspect fix","CKARatioFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("F3 0F 59 B3 ?? ?? ?? ?? 48 8B 5C 24 ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ACValhalla.exe+87B1BC: F3 0F 59 F0              - mulss xmm6,xmm0
		--ACValhalla.exe+87B1C0: EB 08                    - jmp ACValhalla.exe+87B1CA
		--ACValhalla.exe+87B1C2: F3 0F 59 B3 F4 02 00 00  - mulss xmm6,[rbx+000002F4]
		--ACValhalla.exe+87B1CA: 48 8B 5C 24 40           - mov rbx,[rsp+40]
		--ACValhalla.exe+87B1CF: 0F 28 C6                 - movaps xmm0,xmm6		
	end

	local tAddress = HackTool:AddAddress("Aspect")
	
	--if HackTool:SignatureScan("F3 0F 59 ?? ?? ?? ?? ?? BA ?? ?? ?? ?? F3 41",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
	--ACValhalla.exe+189A498: 0F 28 F8                    - movaps xmm7,xmm0
	--ACValhalla.exe+189A49B: 65 48 8B 04 25 58 00 00 00  - mov rax,gs:[00000058]
	--ACValhalla.exe+189A4A4: F3 0F 59 3D F0 D5 CB 02     - mulss xmm7,[ACValhalla.exe+4557A9C]
	--ACValhalla.exe+189A4AC: BA A4 0C 00 00              - mov edx,00000CA4
	--ACValhalla.exe+189A4B1: F3 41 0F 11 76 20           - movss [r14+20],xmm6

	if HackTool:SignatureScan("F3 0F 5C ?? ?? ?? ?? ?? F3 0F 59 ?? ?? ?? ?? ?? 0F 54 ?? ?? ?? ?? ?? 0F 2F ?? ?? ?? ?? ?? 72",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ACValhalla.exe+85426C: F3 0F 5E C1              - divss xmm0,xmm1
		--ACValhalla.exe+854270: 0F 28 D0                 - movaps xmm2,xmm0
		--ACValhalla.exe+854273: F3 0F 5C 05 21 38 D0 03  - subss xmm0,[ACValhalla.exe+4557A9C]
		--ACValhalla.exe+85427B: F3 0F 59 15 F5 37 D0 03  - mulss xmm2,[ACValhalla.exe+4557A78]
		--ACValhalla.exe+854283: 0F 54 05 06 4C CB 03     - andps xmm0,[ACValhalla.exe+4508E90]				
	end

		
	local tAddress = HackTool:AddAddress("BaseAspect")	
	if HackTool:SignatureScan("AB AA AA 3F CD CC CC 3F 39 8E E3 3F",tAddress,PAGE_READONLY,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:AddSubOffset("AspectRatio",0x8):GetInfo(TYPE_FLOAT) )
		tAddress["AspectRatio"]:SetAutoUnprotect(true)
	end

	return true
end



function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVScalingIn")
	Variables:PushFloat("FOVScalingOut")
	Variables:PushFloat("FOV")
	Variables:PushFloat("DefaultAspect")
	Variables:PushFloat("Aspect")
	Variables:Allocate()
	Variables["FOVScalingIn"]:WriteFloat(fFOVScalingIn)
	Variables["FOVScalingOut"]:WriteFloat(fFOVScalingOut)
	Variables["DefaultAspect"]:WriteFloat(fDefaultAspectRatio)

	ResolutionChanged()
	
		asm = [[	

			(codecave:jmp)FOV,FOV_cc:
				fld dword ptr [$$2]										$ctx=1
				fstp dword ptr [(allocation)Variables->FOVScalingIn]
				mulss $$1,[(allocation)Variables->FOVScalingOut]		$ctx=1
				jmp %returnaddress%			
				%end%
												
			(codecave:jmp)Aspect,Aspect_cc:
				;mulss $$1,[(allocation)Variables->DefaultAspect]		$ctx=1
				subss $$1,[(allocation)Variables->Aspect]				$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("Aspect_cc",bAspectFix)
	end

	Write_FOV()

end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVScalingIn"] and Variables["FOVScalingOut"] then
		fFOVScalingIn = Variables["FOVScalingIn"]:ReadFloat()
		fFOVScalingOut = Variables["FOVScalingOut"]:ReadFloat()		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV Scaling In: %.2f, Out : %.2f", fFOVScalingIn, fFOVScalingOut))
	end
	
	if bAspectFix and Variables["Aspect"] then
		local BaseAspect = HackTool:GetAddress("BaseAspect")
		if BaseAspect then
			fAspectRatio = DisplayInfo:GetfOffsetWidth() / DisplayInfo:GetfOffsetHeight()
			Variables["Aspect"]:WriteFloat(fAspectRatio)
			--BaseAspect["AspectRatio"]:WriteFloat( fAspectRatio )
		end
	end
	
end

function FOVSlider_Changed(Sender)

	fFOV = Sender:GetPosition()
	lblFOVSlider.Caption:SetCaption( string.format("Value: %.0f%%",fFOV) )
	
	FOVScale = (fFOV / 100)
	
	if bFOV then
		Write_FOV()
	end
	
	ForceUpdate()
	
end

function Write_FOV()

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

end

function ResolutionChanged() 

end

function Disable_Inject()

	RestoreAspectRatio()
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function RestoreAspectRatio()

	local BaseAspect = HackTool:GetAddress("BaseAspect")
	
	if BaseAspect then
		BaseAspect["AspectRatio"]:WriteFloat( fDefaultAspectRatio )
	end
	
end

function CKARatioFix_Changed(Sender)

	bAspectFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("Aspect_cc",bAspectFix)
	if bAspectFix == false then RestoreAspectRatio() end
	ForceUpdate()		
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end