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

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

--GAME VARS
fFOV = 1.0
fFOVScale = 1.0
fDefaultAspectRatio = 1.777777791
fAspectRatio = DisplayInfo:GetfOffsetWidth() / DisplayInfo:GetfOffsetHeight()
Default_Aspect = 0

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "ACMirage.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,250,0,1)
	FOVSlider:SetTickFrequency(15)
	FOVSlider:SetLabel1Text("0.5%")
	FOVSlider:SetLabel2Text("2.5%")

	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)
	DefaultControls.AddFixToggle("CKChromaticFix_Enable","Chromatic aberration disable","CKChromaticFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? 48 8B ?? ?? ?? 0F ?? ?? 0F ?? ?? ?? ?? 48 8B",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ACMirage.exe+3837B9C - F3 0F 59 F0            	- mulss xmm6,xmm0
		--ACMirage.exe+3837BA0 - EB 08                 		- jmp ACMirage.exe+3837BAA
		--ACMirage.exe+3837BA2 - F3 0F 59 B3 F4 02 00 00   	- mulss xmm6,[rbx+000002F4]
		--ACMirage.exe+3837BAA - 48 8B 5C 24 40        		- mov rbx,[rsp+40]
		--ACMirage.exe+3837BAF - 0F 28 C6               	- movaps xmm0,xmm6
	end
	
	local tAddress = HackTool:AddAddress("CHROMATIC")
	if HackTool:SignatureScan("80 ?? ?? ?? 75 ?? 49 8B ?? E8 ?? ?? ?? ?? 84 ?? 74 ?? 8B",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ACMirage.exe+254778D - 49 8B 46 30           - mov rax,[r14+30]
		--ACMirage.exe+2547791 - 48 8B 48 60           - mov rcx,[rax+60]
		--ACMirage.exe+2547795 - 80 79 32 01           - cmp byte ptr [rcx+32],01
		--ACMirage.exe+2547799 - 75 4E                 - jne ACMirage.exe+25477E9
		--ACMirage.exe+254779B - 49 8B CE              - mov rcx,r14
	end
	
	local tAddress = HackTool:AddAddress("ASPECT")
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? ?? ?? ?? ?? 72 ?? 0F 2F ?? ?? ?? ?? ?? 73 ?? F3 0F",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--ACMirage.exe+380C74C - F3 0F 5E C1               - divss xmm0,xmm1
		--ACMirage.exe+380C750 - 0F 28 D0                  - movaps xmm2,xmm0
		--ACMirage.exe+380C753 - F3 0F 5C 05 B9 05 17 01   - subss xmm0,[ACMirage.exe+497CD14]
		--ACMirage.exe+380C75B - F3 0F 59 15 E5 07 E3 00   - mulss xmm2,[ACMirage.exe+463CF48]
		--ACMirage.exe+380C763 - 0F 54 05 66 CA DD 00      - andps xmm0,[ACMirage.exe+45E91D0]
	end
	
	--local AspectRatios = HackTool:SignatureScanMulti("0F C6 ?? ?? E8 ?? ?? ?? ?? 84 ?? 74 ?? F3 0F ?? ?? ?? ?? ?? ?? EB ?? F3 0F ?? ?? ?? ?? ?? ?? 41 ?? ?? ?? 0F","AspectRatio",PAGE_EXECUTE_READ,0xd,Process_EXEName)
	--if AspectRatios ~= 2 then	
	--	return ErrorOccurred("Could not find aspect ratio injection point, " .. Process_FriendlyName .. 
	--						 " may have updated to a version that is no longer supported.\r\n\r\n" .. 
	--						 "Try selecting a different version and re-enable the fix."	)
	--else
	--
	--	local tAddress = HackTool:AddAddress("FirstAspectRatio", HackTool:GetAddress( string.format("AspectRatio%d",1) ))
	--	print( tAddress:GetInfo(TYPE_ADDRESS) )	
	--
	--	tAddress:AcquireAddress(0x4)		
	--	print( tAddress:GetInfo(TYPE_FLOAT) )		
	--	Default_Aspect = tAddress:ReadFloat()
	--	tAddress:SetAutoUnprotect(true)	
	--end

	return true
	
end



function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVScale")
	Variables:PushFloat("DefaultAspect")
	Variables:PushFloat("Aspect")
	Variables:Allocate()
	Variables["Aspect"]:WriteFloat(fAspectRatio)
	Variables["DefaultAspect"]:WriteFloat(fDefaultAspectRatio)

	ResolutionChanged()
	
		asm = [[	

			(codecave)FOV,FOV_cc:
				$$0 $$1,[(allocation)Variables->FOVScale]		$ctx=1
				%end%
				
			(codecave)CHROMATIC,CHROMATIC_cc:
				$$0 byte ptr [$$1],1							$ctx=1
				%end%

			(codecave)ASPECT,ASPECT_cc:
				$$0 $$1,[(allocation)Variables->Aspect]			$ctx=1
				%end%
		]]

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

	--if bAspectFix == true then
	--	WriteAspectRatio()
	--end

	Write_FOV()

end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
end

function FOVSlider_Changed(Sender)

	fFOVScale = Sender:GetPosition()	
	fFOVScale = (fFOVScale / 100)
	lblFOVSlider.Caption:SetCaption( string.format("Scaling: %.2f%%",fFOVScale) )

	if bFOV then
		Write_FOV()
	end
	
	ForceUpdate()
	
end

function Write_FOV()

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

end

function ResolutionChanged() 

	fAspectRatio = DisplayInfo:GetfOffsetWidth() / DisplayInfo:GetfOffsetHeight()
	
end

function Disable_Inject()

	RestoreAspectRatio()
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKChromaticFix_Changed(Sender)

	bChromatic = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CHROMATIC_cc",bChromatic)
	ForceUpdate()
	
end

function CKARatioFix_Changed(Sender)

	bAspectFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ASPECT_cc",bAspectFix)
	--if bAspectFix == false then 
	--	RestoreAspectRatio()
	--else
	--	WriteAspectRatio()
	--end
	
	ForceUpdate()		
	
end

function WriteAspectRatio()
	
	local AspectRatio = HackTool:GetAddress("FirstAspectRatio")
	
	if AspectRatio then
		AspectRatio:WriteFloat( fAspectRatio )
	end
	
end

function RestoreAspectRatio()

	local AspectRatio = HackTool:GetAddress("FirstAspectRatio")
	
	if AspectRatio then
		AspectRatio:WriteFloat( Default_Aspect )
	end
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end