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

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

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

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Tchia  "
Process_ClassName = "*"
Process_EXEName = "ProjetCaillou-Win64-Shipping.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","Remove cinematics black borders","CKAspect_Changed",255,120,210,14)
	
	DefaultControls.AddHeader("Header_FOV","FOV Fine adjustment",15,70,210,17)
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,50,0,1)
	
	FOVSlider:SetTickFrequency(5)
	FOVSlider:SetLabel1Text("0")
	FOVSlider:SetLabel2Text("50")

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("F3 0F ?? ?? ?? 8B 83 ?? ?? ?? ?? 89 47 ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--fortsolis.exe+3B204F9 - 48 8B 01              	- mov rax,[rcx]
		--fortsolis.exe+3B204FC - FF 90 A0 07 00 00        	- call qword ptr [rax+000007A0]
		--fortsolis.exe+3B20502 - F3 0F 11 47 30         	- movss [rdi+30],xmm0
		--fortsolis.exe+3B20507 - 48 8B 4D 30           	- mov rcx,[rbp+30]
		--fortsolis.exe+3B2050B - 48 8B 01              	- mov rax,[rcx]
	end
	
	local tAddress = HackTool:AddAddress("Aspect")
	if HackTool:SignatureScan("8B 42 ?? 89 41 ?? 8B 41 ?? 33 42 ?? 83 E0 ?? 31 41 ?? 8B 49 ?? 33 4A ?? 83 E1 02 31 4B ?? 48 8D 4B ??",tAddress,PAGE_EXECUTE_READ,0x0,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

	return true

end


function Enable_Inject() 

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

	local asm = [[
		(codecave:jmp)FOV,FOV_cc:
			$$0 [(allocation)Variables->FOV_In],$$2						$ctx=1
			fld 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 $$2,[(allocation)Variables->CompensatedFOV]				$ctx=1
			addss $$2,[(allocation)Variables->AdditionalFOV]			$ctx=1
			$$0 [(allocation)Variables->FOV_Out],$$2					$ctx=1			
			%originalcode%
			jmp %returnaddress%
			%end%			
			
		(codecave:jmp)Aspect,Aspect_cc:
			$$0 $$1,[(allocation)Variables->AspectRatio]				$ctx=1
			$$0 [$$2],$$1												$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%			
	]]

	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("Aspect_cc",bAspect)	
	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 bFOV == true then
			fFOVIn = Variables["FOV_In"]:ReadFloat()
			fFOVCompensated = Variables["CompensatedFOV"]:ReadFloat()
			fFOVOut = Variables["FOV_Out"]:ReadFloat()
			PluginViewport:AppendStatusMessage( string.format("===== Fix informations =====\r\nFOV In: %0.2f, FOV Compensated: %0.2f, FOV Out : %.02f", fFOVIn, fFOVCompensated, fFOVOut))
		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)

	if bFOV == true then
		WriteFOV()
	end

	Toggle_CodeCave("FOV_cc",bFOV)	
	ForceUpdate()
	
end

function CKAspect_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("Aspect_cc",bAspect)	
	ForceUpdate()
	
end

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

function WriteFOV() 

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

end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
