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

--ControlVars
bFixEnabled = true
bFOV = true

--GAME VARS
fAdditionalAimFOV = 0
fAdditionalStoryFOV = 0
fAdditionalFlashlightBoostFOV = 0
fAdditionalFlareIgnitedFOV = 0

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "AlanWake2.exe"

--INJECTION BEHAVIOUR
InjectDelay = 100
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.AddHeader("Header_FOV","FOVs fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("StoryFOVSlider","StoryFOVSlider_Changed",35,100,160,35,0,70,0,1)
	StoryFOVSlider:SetTickFrequency(5)
	StoryFOVSlider:SetLabel1Text("-20")
	StoryFOVSlider:SetLabel2Text("+70")
	
	DefaultControls.AddFixedFOVSlider("AimFOVSlider","AimFOVSlider_Changed",35,160,160,35,0,70,0,1)
	AimFOVSlider:SetTickFrequency(5)
	AimFOVSlider:SetLabel1Text("-20")
	AimFOVSlider:SetLabel2Text("+70")

	DefaultControls.AddFixedFOVSlider("FlashlightBoostFOVSlider","FlashlightBoostFOVSlider_Changed",35,220,160,35,0,70,0,1)
	FlashlightBoostFOVSlider:SetTickFrequency(5)
	FlashlightBoostFOVSlider:SetLabel1Text("-20")
	FlashlightBoostFOVSlider:SetLabel2Text("+70")
	
	DefaultControls.AddFixedFOVSlider("FlareIgnitedFOVSlider","FlareIgnitedFOVSlider_Changed",35,280,160,35,0,70,0,1)
	FlareIgnitedFOVSlider:SetTickFrequency(5)
	FlareIgnitedFOVSlider:SetLabel1Text("-20")
	FlareIgnitedFOVSlider:SetLabel2Text("+70")
	
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("C5 FA ?? ?? ?? C5 ?? ?? ?? C5 E2 ?? ?? ?? ?? ?? ?? C5 E2",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--AlanWake2.exe+1AA3ECB - C4 C1 78 28 D2           - vmovaps xmm2,xmm10
		--AlanWake2.exe+1AA3ED0 - 48 8B CE                 - mov rcx,rsi
		--AlanWake2.exe+1AA3ED3 - C5 FA 10 48 28           - vmovss xmm1,[rax+28]
		--AlanWake2.exe+1AA3ED8 - C5 F2 5F DA              - vmaxss xmm3,xmm1,xmm2
		--AlanWake2.exe+1AA3EDC - C5 E2 5D 1D 60 C2 90 01  - vminss xmm3,xmm3,[AlanWake2.exe+33B0144]
	end	
	
	return true
	
end



function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("AdditionalAimFOV")
	Variables:PushFloat("AdditionalStoryFOV")
	Variables:PushFloat("AdditionalFlashlightFOV")
	Variables:PushFloat("AdditionalFlareIgnitedFOV")
	Variables:Allocate()

	ResolutionChanged()
	
		asm = [[	

			(codecave:jmp)FOV,FOV_cc:
				movss $$1,[$$2]													$ctx=1
				comiss $$1,[Aim]												$ctx=1
				je AimFOV
				comiss $$1,[Story]												$ctx=1
				je StoryFOV
				comiss $$1,[Flashlight_boost]									$ctx=1
				je FlashLight_boostFOV
				comiss $$1,[Flare_ignited]										$ctx=1
				je Flare_ignitedFOV
				jmp %returnaddress%
			
			AimFOV:
				addss $$1,[(allocation)Variables->AdditionalAimFOV]				$ctx=1
				jmp %returnaddress%
				
			StoryFOV:
				addss $$1,[(allocation)Variables->AdditionalStoryFOV]			$ctx=1
				jmp %returnaddress%
			
			FlashLight_boostFOV:
				addss $$1,[(allocation)Variables->AdditionalFlashlightFOV]		$ctx=1
				jmp %returnaddress%
				
			Flare_ignitedFOV:
				addss $$1,[(allocation)Variables->AdditionalFlareIgnitedFOV]	$ctx=1
				jmp %returnaddress%
				%end%

			Aim: (float)52.0f
			Story: (float)70.0f
			Flashlight_boost: (float)65.0f
			Flare_ignited: (float)75.0f
			
		]]
	
	RemoveAVXInstructions()

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		RestoreAVXInstructions()
		return ErrorOccurred("Assembly compilation failed...")
	else
		RestoreAVXInstructions()
		Toggle_CodeCave("FOV_cc",bFOV)
	end

	Write_Aim_FOV()
	Write_Story_FOV()
	Write_Flashlight_Boost_FOV()
	Write_Flare_Ignited_FOV()

end

function Periodic()

	local fStoryFOVIn = 70
	local fStoryFOVOut = fStoryFOVIn + fAdditionalStoryFOV
	local fAimFOVIn = 52
	local fAimFOVOut = fAimFOVIn + fAdditionalAimFOV
	local fFlashlightBoostFOVIn= 65
	local fFlashlightBoostFOVOut = fFlashlightBoostFOVIn + fAdditionalFlashlightBoostFOV
	local fFlareIgnitedFOVIn = 75
	local fFlareIgnitedFOVOut = fFlareIgnitedFOVIn + fAdditionalFlareIgnitedFOV

	local Variables = HackTool:GetAllocation("Variables")
	PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nWorld FOV In: %0.f -- FOV Out : %.0f , Aim FOV In: %0.f -- FOV Out : %.0f\r\nFlashlight boost FOV In: %0.f -- FOV Out : %.0f , Flare ignited FOV In: %0.f -- FOV Out : %.0f", fStoryFOVIn, fStoryFOVOut, fAimFOVIn, fAimFOVOut, fFlashlightBoostFOVIn, fFlashlightBoostFOVOut, fFlareIgnitedFOVIn, fFlareIgnitedFOVOut))
	
end

function AimFOVSlider_Changed(Sender)

	fAdditionalAimFOV = Sender:GetPosition() - 20
	lblAimFOVSlider.Caption:SetCaption( string.format("Aim: %.2f",fAdditionalAimFOV) )

	if bFOV then
		Write_Aim_FOV()
	end
	
	ForceUpdate()
	
end

function StoryFOVSlider_Changed(Sender)

	fAdditionalStoryFOV = Sender:GetPosition() - 20
	lblStoryFOVSlider.Caption:SetCaption( string.format("World: %.2f",fAdditionalStoryFOV) )

	if bFOV then
		Write_Story_FOV()
	end
	
	ForceUpdate()
	
end

function FlashlightBoostFOVSlider_Changed(Sender)

	fAdditionalFlashlightBoostFOV = Sender:GetPosition() - 20
	lblFlashlightBoostFOVSlider.Caption:SetCaption( string.format("Flash light boost: %.2f",fAdditionalFlashlightBoostFOV) )

	if bFOV then
		Write_Flashlight_Boost_FOV()
	end
	
	ForceUpdate()

end

function FlareIgnitedFOVSlider_Changed(Sender)

	fAdditionalFlareIgnitedFOV = Sender:GetPosition() - 20
	lblFlareIgnitedFOVSlider.Caption:SetCaption( string.format("Flare ignition: %.2f",fAdditionalFlareIgnitedFOV) )

	if bFOV then
		Write_Flare_Ignited_FOV()
	end
	
	ForceUpdate()
	
end

function Write_Aim_FOV()

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

end

function Write_Story_FOV()

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

function Write_Flashlight_Boost_FOV()

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

function Write_Flare_Ignited_FOV()

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

end

function RemoveAVXInstructions()

	local tSuspendedThreadID = HackTool:SuspendAttachedThread() 						

	local tAddress = HackTool:GetAddress("FOV")
	tAddress:WriteByte(0xF3,0)
	tAddress:WriteByte(0x0F,1)

	HackTool:ResumeThread(tSuspendedThreadID)

end

function RestoreAVXInstructions()

	local tSuspendedThreadID = HackTool:SuspendAttachedThread()
	
	local tAddress = HackTool:GetAddress("FOV")
	tAddress:WriteByte(0xC5,0)
	tAddress:WriteByte(0xFA,1)
	
	HackTool:ResumeThread(tSuspendedThreadID)

end


function ResolutionChanged() 
	
end

function Disable_Inject()

	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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


function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end