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

--ControlVars
bFixEnabled = true
bCinematics = true
bFOV = true
bCamFOV = true

--GAME VARS
fDefaultFOV = 90
fDefaultAspectRatio = 1.7777778
fAdditionalFOV = 0
fFOV = 90
fAdditionalFOVCinematics = 50
fCinematicsFOV = 50


--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Evil West  "
Process_ClassName = "UnrealWindow"
Process_EXEName = "*"

--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.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddHeader("Header_FOVCinematics","Cinematics FOV fine adjustment",15,160,210,17)	
	DefaultControls.AddFOVSlider("FOVCinematicsSlider","FOVCinematicsSlider_Changed",55,190,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKCamFOVFix_Enable","Camera FOV fix","CKCamFOVFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKARatioFix_Enable","Cinematics Fix","CKARatioFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("AspectRatio")
	if HackTool:SignatureScan("8B 42 ?? 89 41 ?? 8B 41 ?? 33 42 ?? 83 E0 ?? 31 41 ?? 8B 4A ?? 33 4B",tAddress,PAGE_EXECUTE_READ,0x03,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
		-- HighMoon-Win64-Shipping.exe+2FD25EC: 8B 42 2C     - mov eax,[rdx+2C]
		-- HighMoon-Win64-Shipping.exe+2FD25EF: 89 41 2C     - mov [rcx+2C],eax
		-- HighMoon-Win64-Shipping.exe+2FD25F2: 8B 42 30     - mov eax,[rdx+30]
		-- HighMoon-Win64-Shipping.exe+2FD25F5: 89 41 30     - mov [rcx+30],eax
		-- HighMoon-Win64-Shipping.exe+2FD25F8: 8B 41 34     - mov eax,[rcx+34]
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end

	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B 83 ?? ?? ?? ?? 89 47",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
		-- HighMoon-Win64-Shipping.exe+2FE2464: F3 0F 58 83 08 02 00 00     - addss xmm0,[rbx+00000208]
		-- HighMoon-Win64-Shipping.exe+2FE246C: EB 08                       - jmp HighMoon-Win64-Shipping.exe+2FE2476
		-- HighMoon-Win64-Shipping.exe+2FE246E: F3 0F 10 83 08 02 00 00     - movss xmm0,[rbx+00000208]
		-- HighMoon-Win64-Shipping.exe+2FE2476: F3 0F 11 47 18              - movss [rdi+18],xmm0
		-- HighMoon-Win64-Shipping.exe+2FE247B: 8B 83 18 02 00 00           - mov eax,[rbx+00000218]		
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end

	local tAddress = HackTool:AddAddress("CamFOV")
	
	if HackTool:SignatureScan("F3 0F 10 87 ?? ?? ?? ?? F3 0F ?? ?? F3 0F ?? ?? 48 8B",tAddress,PAGE_EXECUTE_READ,0x0C,Process_EXEName) == 0 then	
		-- HighMoon-Win64-Shipping.exe+17516AF: F3 0F 10 87 20 04 00 00  - movss xmm0,[rdi+00000420]
		-- HighMoon-Win64-Shipping.exe+17516B7: F3 0F 58 03              - addss xmm0,[rbx]
		-- HighMoon-Win64-Shipping.exe+17516BB: F3 0F 11 03              - movss [rbx],xmm0
		-- HighMoon-Win64-Shipping.exe+17516BF: 48 8B BC 24 10 01 00 00  - mov rdi,[rsp+00000110]
		-- HighMoon-Win64-Shipping.exe+17516C7: 48 8B 9C 24 08 01 00 00  - mov rbx,[rsp+00000108]
		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("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("FOVCinematicsIn")
	Variables:PushFloat("FOVCinematicsOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AdditionalFOVCinematics")
	Variables:PushFloat("ScreenAspect")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AdditionalFOVCinematics"]:WriteFloat(fAdditionalFOVCinematics)

	ResolutionChanged()
	
		asm = [[	

			(codecave:jmp)CamFOV,CamFOV_cc:
				pushf
				cmp r12,0
				jne exit
				movss [(allocation)Variables->FOVCinematicsIn],$$2				$ctx=1
				addss $$2,[(allocation)Variables->AdditionalFOVCinematics]		$ctx=1
				movss [(allocation)Variables->FOVCinematicsOut],$$2				$ctx=1
				
				exit:
				popfq
				%originalcode%
				jmp %returnaddress%			
				%end%

			(codecave:jmp)FOV,FOV_cc:
				%originalcode%
				movss [(allocation)Variables->FOVIn],$$1						$ctx=1
				addss $$1,[(allocation)Variables->AdditionalFOV]				$ctx=1
				movss [(allocation)Variables->FOVOut],$$1						$ctx=1
				jmp %returnaddress%			
				%end%
				
			(codecave:jmp)AspectRatio,AspectRatio_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("FOV_cc",bFOV)
		Toggle_CodeCave("CamFOV_cc",bCamFOV)
		Toggle_CodeCave("AspectRatio_cc",bCinematics)
	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()		
		fFOVCinematicsIn = Variables["FOVCinematicsIn"]:ReadFloat()
		fFOVCinematicsOut = Variables["FOVCinematicsOut"]:ReadFloat()		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.f, FOV Out : %.0f\r\nCinematics FOV In: %0.f, Cinematics FOV Out : %.0f", fFOVIn, fFOVOut, fFOVCinematicsIn, fFOVCinematicsOut))
	end
	
end

function FOVSlider_Changed(Sender)

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

function FOVCinematicsSlider_Changed(Sender)

	fCinematicsFOV = (Sender:GetScaledFloat(2)) + 25
	lblFOVCinematicsSlider.Caption:SetCaption( string.format("Additional Cinematics FOV : %.2f",fCinematicsFOV) )
	Write_FOVCinematics()
	ForceUpdate()
	
end

function Write_FOV()

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

end

function Write_FOVCinematics()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["AdditionalFOVCinematics"] then
		Variables["AdditionalFOVCinematics"]:WriteFloat(fCinematicsFOV)
	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 CKCamFOVFix_Changed(Sender)

	bCamFOV = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CamFOV_cc",bCamFOV)
	ForceUpdate()
	
end

function CKARatioFix_Changed(Sender)

	bCinematics = Toggle_CheckFix(Sender)
	Toggle_CodeCave("AspectRatio_cc",bCinematics)
	ForceUpdate()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end