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

--GAME VARS
fAdditionalFOV = 0
fDefaultAspectRatio = 1.777777791
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fFactor = 0.00872665
fFOV_Compensated = 0

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true
AdditionalFOV = 1.0

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "fortsolis.exe"

--INJECTION BEHAVIOUR
InjectDelay = 2000
WriteInterval = 500
SearchInterval = 500
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.AddFixToggle("CKFOVAdjustment_Enable","FOV Adjustment","CKFOV_Changed",255,100,180,14)
	DefaultControls.AddFixToggle("CKDisableLetterBox_Enable","Override aspect limitation","CKAspect_Changed",255,120,180,14)
	
	DefaultControls.AddHeader("Header_FOV","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("0")
	FOVSlider:SetLabel2Text("90")

end

function Configure_SignatureScan() 
		
	local tAddress = HackTool:AddAddress("Aspect")
	if HackTool:SignatureScan("F2 0F ?? ?? ?? 8B 48 ?? 89 4F ?? 8B ?? ?? 89 47 ?? 8B 43 ?? 89 47 ?? 8B 43 ?? 89 47 ?? 8B 43 ?? 89 47 ?? 8B 43 ?? 89 47 ?? 8B 43 ??",tAddress,PAGE_EXECUTE_READ,0x29,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--fortsolis.exe+3B20451 - 8B 43 44              - mov eax,[rbx+44]
		--fortsolis.exe+3B20454 - 89 47 44              - mov [rdi+44],eax
		--fortsolis.exe+3B20457 - 8B 43 48              - mov eax,[rbx+48]
		--fortsolis.exe+3B2045A - 89 47 48              - mov [rdi+48],eax
		--fortsolis.exe+3B2045D - 8B 47 4C              - mov eax,[rdi+4C]
	end
	
	local tAddress = HackTool:AddAddress("Aspect2")
	if HackTool:SignatureScan("77 ?? 48 ?? ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0b,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--fortsolis.exe+3CFA754 - 48 8B 01              - mov rax,[rcx]
		--fortsolis.exe+3CFA757 - FF 90 28 07 00 00     - call qword ptr [rax+00000728]
		--fortsolis.exe+3CFA75D - F3 0F 10 40 30        - movss xmm0,[rax+30] <<==
		--fortsolis.exe+3CFA762 - 48 83 C4 28           - add rsp,28
		--fortsolis.exe+3CFA766 - C3                    - ret 
	end
	 

	return true

end


function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOV_In")
	Variables:PushFloat("FOV_Out")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("DefaultAspectRatio")
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("factor")
	Variables:Allocate()
	Variables["AspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["DefaultAspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["factor"]:WriteFloat(fFactor)
	Variables["ScreenRatio"]:WriteFloat(DisplayInfo:GetWidth() / DisplayInfo:GetHeight())
	
	ResolutionChanged()

	local asm = [[
			
		(codecave:jmp)Aspect,Aspect_cc:
			$$0 $$1,[(allocation)Variables->ScreenRatio]				$ctx=1
			$$0 [$$2],$$1												$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)Aspect2,Aspect2_cc:
			fld dword ptr [$$2]											$ctx=1
			fst dword ptr [(allocation)Variables->FOV_In]
			fmul dword ptr [(allocation)Variables->factor]
			fptan
			fstp st0
			fld dword ptr [(allocation)Variables->AspectRatio]
			fdiv dword ptr [(allocation)Variables->DefaultAspectRatio]
			fmulp st1,st0
		    fld1
		    fpatan
			fdiv dword ptr [(allocation)Variables->factor]						; calculate compensated FOV
			fstp dword ptr [(allocation)Variables->CompensatedFOV]				; and retrieve it
			$$0 $$1,[(allocation)Variables->CompensatedFOV]				$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]			$ctx=1
			$$0 [(allocation)Variables->FOV_Out],$$1					$ctx=1
			jmp %returnaddress%
			%end%
	]]

	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("Aspect_cc",bAspect)
		Toggle_CodeCave("Aspect2_cc",bFixEnabled)
	end
	
	if bAspect == true then
		WriteAspect()
	end
	
	if bFOV == true then
		WriteFOV()
	end
	
end

function Periodic()

	PluginViewport:AppendStatusMessage( "\r\n" )

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalFOV"] then
		if bFixEnabled == true then
			fFOV_In = Variables["FOV_In"]:ReadFloat()
			fFOV_Out = Variables["FOV_Out"]:ReadFloat()
			fFOV_Compensated = Variables["CompensatedFOV"]:ReadFloat()
			if fFOV_Compensated == 0 then
				fFOV_Compensated = fFOV_Out
			end
			PluginViewport:AppendStatusMessage( string.format("FOV In=%.2f, FOV Compensated=%.2f, FOV Out=%.2f", fFOV_In, fFOV_Compensated, fFOV_Out) )
		end
	end

end

function Disable_Inject()

	CleanUp()
	
end

function FOVSlider_Changed(Sender)

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

end

function CKFOV_Changed(Sender)

	bFOV = Toggle_CheckFix(Sender)
	WriteFOV()
	ForceUpdate()
	
end

function CKAspect_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	
	if bAspect == false then
		fFOV_Compensated = 0
	end

	WriteAspect()
	Toggle_CodeCave("Aspect_cc",bAspect)
	ForceUpdate()
	
end

function ResolutionChanged() 
	
	fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
	
end

function WriteFOV() 

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

end

function WriteAspect()
	local fAspect = fDefaultAspectRatio
	if bAspect == true then
		fAspect = fAspectRatio
	end
	
	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AspectRatio"] then
		Variables["AspectRatio"]:WriteFloat(fAspect)
	end
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
