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

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

--GAME VARS
fDefaultFOV = 90
fOriginalAspect = 2.35
fAdditionalFOV = 0
fFOV = 90


--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "The Devil in Me  "
Process_ClassName = "UnrealWindow"
Process_EXEName = "*"

--INJECTION BEHAVIOUR
InjectDelay = 1500 
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.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKARatioFix_Enable","Aspect Ratio fix","CKARatioFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("AspectFix")
		
	if HackTool:SignatureScan("8B 83 ?? ?? ?? ?? 89 47 ?? 0F B6 83 ?? ?? ?? ?? 33",tAddress,PAGE_EXECUTE_READ,0x06,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("F3 0F 10 83 ?? ?? ?? ?? F3 0F 11 47 ?? 8B",tAddress,PAGE_EXECUTE_READ,0x08,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
		
	return true
end



function Enable_Inject() 

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

	ResolutionChanged()
	
		asm = [[	

			(codecave:jmp)FOV,FOV_cc:
				movss [(allocation)Variables->FOVIn],$$2				$ctx=1
				addss $$2,[(allocation)Variables->AdditionalFOV]		$ctx=1
				movss [(allocation)Variables->FOVOut],$$2				$ctx=1
				%originalcode%
				jmp %returnaddress%			
				%end%
				
			(codecave:jmp)AspectFix,AspectFix_cc:
				fld dword ptr [(allocation)Variables->ScreenAspect]
				fstp dword ptr [$$1]									$ctx=1
				jmp %returnaddress%
				%end%

								
		]]

	if HackTool:CompileAssembly(asm,"AspectFix") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("AspectFix_cc",bAspectRatio)
		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
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.f, FOV Out : %.0f", fFOVIn, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fFOV = (Sender:GetScaledFloat(1) / 1.6) + 37.5
	lblFOVSlider.Caption:SetCaption( string.format("Additional FOV : %.2f",fFOV) )
	Write_FOV()
	ForceUpdate()
	
end

function Write_FOV()

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

end

function ResolutionChanged() 

	local ScreenAspect = DisplayInfo:GetAspectRatio()
	
	local Variables = HackTool:GetAllocation("Variables")
	if Variables then
		Variables["ScreenAspect"]:WriteFloat(ScreenAspect)
	end		
	
end

function Disable_Inject()
	CleanUp()
end

function CKFOVFix_Changed(Sender)

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

function CKARatioFix_Changed(Sender)

	bAspectRatio = Toggle_CheckFix(Sender)
	Toggle_CodeCave("AspectFix_cc",bAspectRatio)
	ForceUpdate()

end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end