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

--ControlVars
bFixEnabled = true
bAspect = true
bFOV = true

--GAME VARS
fDefaultFOV = 0
fAspectRatio = 0
fAdditionalFOV = 0

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Little Hope  "
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.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,70,0,1)
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("+0")
	FOVSlider:SetLabel2Text("+70")	
	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("FOV")
	
	if HackTool:SignatureScan("E8 ?? ?? ?? ?? F3 0F 59 ?? ?? ?? ?? ?? EB ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 0F",tAddress,PAGE_EXECUTE_READ,0x05,Process_EXEName) == 0 then	
	--if HackTool:SignatureScan("EB ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? 8B ?? ?? ?? ?? ?? 89",tAddress,PAGE_EXECUTE_READ,0x02,Process_EXEName) == 0 then		
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--LittleHope-Win64-Shipping.exe+375FE3F - F3 0F5E C1            - divss xmm0,xmm1
		--LittleHope-Win64-Shipping.exe+375FE43 - E8 ED0E6B01           - call LittleHope-Win64-Shipping.exe+4E10D35
		--LittleHope-Win64-Shipping.exe+375FE48 - F3 0F59 05 90EE7802   - mulss xmm0,[LittleHope-Win64-Shipping.exe+5EEECE0]
		--LittleHope-Win64-Shipping.exe+375FE50 - EB 03                 - jmp LittleHope-Win64-Shipping.exe+375FE55
		--LittleHope-Win64-Shipping.exe+375FE52 - 0F57 C0               - xorps xmm0,xmm0
	end

	local tAddress = HackTool:AddAddress("AspectFix1")
	
	if HackTool:SignatureScan("76 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? F3 0F 11 ?? ?? ?? ?? ?? F3 0F 11 ?? ?? ?? ?? ?? 0F",tAddress,PAGE_EXECUTE_READ,0x0E,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--LittleHope-Win64-Shipping.exe+375FE6A - F3 0F10 83 3C080000   - movss xmm0,[rbx+0000083C]
		--LittleHope-Win64-Shipping.exe+375FE72 - F3 0F5E C1            - divss xmm0,xmm1
		--LittleHope-Win64-Shipping.exe+375FE76 - F3 0F11 83 44080000   - movss [rbx+00000844],xmm0
		--LittleHope-Win64-Shipping.exe+375FE7E - F3 0F11 83 18020000   - movss [rbx+00000218],xmm0
		--LittleHope-Win64-Shipping.exe+375FE86 - 0F28 74 24 20         - movaps xmm6,[rsp+20]
	end
	
	local tAddress = HackTool:AddAddress("AspectFix2")
	
	if HackTool:SignatureScan("76 ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? F3 0F ?? ?? E8 ?? ?? ?? ?? F3 0F",tAddress,PAGE_EXECUTE_READ,0x02,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--LittleHope-Win64-Shipping.exe+375FE29 - F3 0F11 83 64080000   - movss [rbx+00000864],xmm0
		--LittleHope-Win64-Shipping.exe+375FE31 - 76 1F                 - jna LittleHope-Win64-Shipping.exe+375FE52
		--LittleHope-Win64-Shipping.exe+375FE33 - F3 0F10 83 3C080000   - movss xmm0,[rbx+0000083C]
		--LittleHope-Win64-Shipping.exe+375FE3B - F3 0F58 C9            - addss xmm1,xmm1
		--LittleHope-Win64-Shipping.exe+375FE3F - F3 0F5E C1            - divss xmm0,xmm1
	end
	
	return true
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] and bFOV 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 Enable_Inject() 

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

	ResolutionChanged()
	
		asm = [[
		
			(codecave:jmp)FOV,FOV_cc:
				%originalcode%
				push rax
				movd eax,xmm1
				cmp eax,0x27BF1AA7										; will not affect main menu
				je exit				
				
				movss [(allocation)Variables->FOVIn],$$1				$ctx=1
				addss $$1,[(allocation)Variables->AdditionalFOV]		$ctx=1
				movss [(allocation)Variables->FOVOut],$$1				$ctx=1				
				
				exit:
				pop rax
				jmp %returnaddress%
				%end%

			(codecave:jmp)AspectFix1,AspectFix1_cc:
				movss $$2,[(allocation)Variables->AspectRatio]			$ctx=1
				%originalcode%
				jmp %returnaddress%
				%end%
			
			; following code will compensate widers aspect ratios for FOV
			(codecave:jmp)AspectFix2,AspectFix2_cc:
				movss xmm15,[$$2+4]										$ctx=1
				mulss xmm15,[(allocation)Variables->AspectRatio]
				movss $$1,xmm15											$ctx=1
				jmp %returnaddress%
				%end%
				
		]]

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

	Write_FOV()

end

function CKFOVFix_Changed(Sender)

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

function CKARatioFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("AspectFix1_cc",bAspect)
	Toggle_CodeCave("AspectFix2_cc",bAspect)
	ForceUpdate()

end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition()
	lblFOVSlider.Caption:SetCaption( string.format("%.0f",fAdditionalFOV) )
	
	if bFOV then
		Write_FOV()
	end
	
	ForceUpdate()
	
end

function Write_FOV()

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

end

function ResolutionChanged() 

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

function Disable_Inject()
	CleanUp()
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end