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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0

--ControlVars
bFixEnabled = true
bFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "Warhammer 40000 Space Marine 2 - Retail.exe"

--INJECTION BEHAVIOUR
InjectDelay = 500
WriteInterval = 100
SearchInterval = 100
SuspendThread = false

--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("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddHeader("Header_FOV","In game 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("-20")
	FOVSlider:SetLabel2Text("+70")


end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("57 48 83 ?? ?? 0F 29 ?? ?? ?? 48 8B ?? F3 0F ?? ?? ?? 48 ?? ?? ?? E8 ?? ?? ?? ?? 48",tAddress,PAGE_EXECUTE_READ,0x0d,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Warhammer 40000 Space Marine 2 - Retail.exe+13041AA: 0F 29 74 24 20        - movaps [rsp+20],xmm6
		--Warhammer 40000 Space Marine 2 - Retail.exe+13041AF: 48 8B D9              - mov rbx,rcx
		--Warhammer 40000 Space Marine 2 - Retail.exe+13041B2: F3 0F 10 71 6C        - movss xmm6,[rcx+6C]
		--Warhammer 40000 Space Marine 2 - Retail.exe+13041B7: 48 83 C1 50           - add rcx,50
		--Warhammer 40000 Space Marine 2 - Retail.exe+13041BB: E8 70 A3 93 00        - call "Warhammer 40000 Space Marine 2 - Retail.exe"+1C3E530
	end
		
	return true
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	
	ResolutionChanged()	

	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn],$$1						$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]			$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$1						$ctx=1
			jmp %returnaddress%
			%end%
						
	]]

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

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, FOV Out : %.02f", fFOVIn, fFOVOut))
	end
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalFOV) )
		
	Write_FOV()	
	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 Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: %.0f",fAdditionalFOV) )
		
	Write_FOV()	
	ForceUpdate()

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
