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

--GAME VARS
fDefaultFOV = 85
fAdditionalPhotoFOV = 0

--ControlVars
bFixEnabled = true
bFOV = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "*"
Process_EXEName = "tomb123.exe"
Process_Tomb1DLL = "tomb1.dll"
Process_Tomb2DLL = "tomb2.dll"
Process_Tomb3DLL = "tomb3.dll"

--INJECTION BEHAVIOUR
InjectDelay = 500
WriteInterval = 100
SearchInterval = 100
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("-20")
	FOVSlider:SetLabel2Text("+50")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("PHOTOFOVTomb1")	
	if HackTool:SignatureScan("0F BF ?? ?? ?? ?? ?? 0F ?? ?? E8 ?? ?? ?? ?? 0F 10 ?? ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_Tomb1DLL) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--tomb2.dll+9B961 - 0F BF C2               - movsx eax,dx
		--tomb2.dll+9B964 - 66 89 0D 65 3D 22 00   - mov [tomb2.dll+2BF6D0],cx
		--tomb2.dll+9B96B - 0F BF 0D 16 70 08 00   - movsx ecx,word ptr [tomb2.dll+122988] <<==
		--tomb2.dll+9B972 - 0F AF C8               - imul ecx,eax
		--tomb2.dll+9B975 - E8 F6 0B FF FF         - call tomb2.dll+8C570
	end
	
	local tAddress = HackTool:AddAddress("PHOTOFOVTomb2")
	if HackTool:SignatureScan("0F BF ?? ?? ?? ?? ?? 0F ?? ?? E8 ?? ?? ?? ?? 0F 10 ?? ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_Tomb2DLL) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--tomb2.dll+9B961 - 0F BF C2               - movsx eax,dx
		--tomb2.dll+9B964 - 66 89 0D 65 3D 22 00   - mov [tomb2.dll+2BF6D0],cx
		--tomb2.dll+9B96B - 0F BF 0D 16 70 08 00   - movsx ecx,word ptr [tomb2.dll+122988] <<==
		--tomb2.dll+9B972 - 0F AF C8               - imul ecx,eax
		--tomb2.dll+9B975 - E8 F6 0B FF FF         - call tomb2.dll+8C570
	end
	
	local tAddress = HackTool:AddAddress("PHOTOFOVTomb3")
	if HackTool:SignatureScan("0F BF ?? ?? ?? ?? ?? 0F ?? ?? E8 ?? ?? ?? ?? 0F 10 ?? ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_Tomb3DLL) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--tomb2.dll+9B961 - 0F BF C2               - movsx eax,dx
		--tomb2.dll+9B964 - 66 89 0D 65 3D 22 00   - mov [tomb2.dll+2BF6D0],cx
		--tomb2.dll+9B96B - 0F BF 0D 16 70 08 00   - movsx ecx,word ptr [tomb2.dll+122988] <<==
		--tomb2.dll+9B972 - 0F AF C8               - imul ecx,eax
		--tomb2.dll+9B975 - E8 F6 0B FF FF         - call tomb2.dll+8C570
	end
	
	return true
end

function Enable_Inject()

	local TombRaiderDLL = HackTool:GetModuleAddress("tomb2.dll")


	local Variables = HackTool:AllocateMemory("Variables",256)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushInt("AdditionalPhotoFOV")
	Variables:Allocate(TombRaiderDLL)
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalPhotoFOV"]:WriteInt(fAdditionalPhotoFOV)
	
	ResolutionChanged()	
			
	local asm = [[	

		(codecave:jmp)PHOTOFOVTomb1,PHOTOFOVTomb1_cc:
			%originalcode%
			call setFOV
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)PHOTOFOVTomb2,PHOTOFOVTomb2_cc:
			%originalcode%
			call setFOV
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)PHOTOFOVTomb3,PHOTOFOVTomb3_cc:
			%originalcode%
			call setFOV
			jmp %returnaddress%
			%end%
			
		setFOV:
			cvtsi2ss xmm14,ecx												; Convert int 32 to floating point
			movss [(allocation)Variables->FOVIn],xmm14
			add ecx,[(allocation)Variables->AdditionalPhotoFOV]
			cvtsi2ss xmm14,ecx
			movss [(allocation)Variables->FOVOut],xmm14
			ret
			
	]]

	if HackTool:CompileAssembly(asm,"Fixes",TombRaiderDLL) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("PHOTOFOVTomb1_cc",bFOV)
		Toggle_CodeCave("PHOTOFOVTomb2_cc",bFOV)
		Toggle_CodeCave("PHOTOFOVTomb3_cc",bFOV)
	end	
			
	Write_FOV()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalPhotoFOV"] then

		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %0.2f, FOV Out : %.02f", fFOVIn, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fAdditionalPhotoFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("Value: +%.0f",fAdditionalPhotoFOV) )
		
	Write_FOV()	
	ForceUpdate()

end

function Write_FOV()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["AdditionalPhotoFOV"] then
		if bFOV == true then
			Variables["AdditionalPhotoFOV"]:WriteInt(fAdditionalPhotoFOV)
		else
			Variables["AdditionalPhotoFOV"]:WriteInt(0)
		end
	end

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

	bFOV = Toggle_CheckFix(Sender)
	Write_FOV()
	Toggle_CodeCave("PHOTOFOVTomb1_cc",bFOV)
	Toggle_CodeCave("PHOTOFOVTomb2_cc",bFOV)
	Toggle_CodeCave("PHOTOFOVTomb3_cc",bFOV)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
