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

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

--ControlVars
bFixEnabled = true
bFOV = true
bFringe = true
bFog = true

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

--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","In game FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFixedFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35,0,70,0,1)
	FOVSlider:SetTickFrequency(10)

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKFRINGEFOVFix_Enable","Chromatic aberrations fix","CKFRINGEFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKFOGGFix_Enable","Fog fix","CKFOGFix_Changed",255,141,180,14)


end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("77 ?? 48 8B ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ?? 48 ?? ?? ?? C3",tAddress,PAGE_EXECUTE_READ,0x10,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Stalker2-Win64-Shipping.exe+4B78B97 - FF 90 20 07 00 00     - call qword ptr [rax+00000720]
		--Stalker2-Win64-Shipping.exe+4B78B9D - F3 0F 10 40 30        - movss xmm0,[rax+30]
		--Stalker2-Win64-Shipping.exe+4B78BA2 - 48 83 C4 28           - add rsp,28
		--Stalker2-Win64-Shipping.exe+4B78BA6 - C3                    - ret 
		--Stalker2-Win64-Shipping.exe+4B78BA7 - CC                    - int 3 
	end

	local tAddress = HackTool:AddAddress("FRINGE")
	
	if HackTool:SignatureScan("7F ?? 89 B3 ?? ?? ?? ?? 8B ?? ?? 39 05 ?? ?? ?? ?? 0F",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Stalker2-Win64-Shipping.exe+4C331D5 - 48 8B 05 D4 49 03 04  - mov rax,[Stalker2-Win64-Shipping.exe+8C67BB0]
		--Stalker2-Win64-Shipping.exe+4C331DC - 39 30                 - cmp [rax],esi
		--Stalker2-Win64-Shipping.exe+4C331DE - 7F 06                 - jg Stalker2-Win64-Shipping.exe+4C331E6
		--Stalker2-Win64-Shipping.exe+4C331E0 - 89 B3 EC 1F 00 00     - mov [rbx+00001FEC],esi
		--Stalker2-Win64-Shipping.exe+4C331E6 - 8B 04 2F              - mov eax,[rdi+rbp]
	end

	local tAddress = HackTool:AddAddress("FOG")
	
	if HackTool:SignatureScan("75 ?? B3 ?? EB ?? 32 ?? 48 8B ?? ?? ?? 48 ?? ?? 74 ?? E8 ?? ?? ?? ?? 0F ?? ?? 48 8B",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Stalker2-Win64-Shipping.exe+31D330D - 48 8B 05 94 74 93 05  - mov rax,[Stalker2-Win64-Shipping.exe+8B0A7A8]
		--Stalker2-Win64-Shipping.exe+31D3314 - 83 78 04 01           - cmp dword ptr [rax+04],01
		--Stalker2-Win64-Shipping.exe+31D3318 - 75 04                 - jne Stalker2-Win64-Shipping.exe+31D331E
		--Stalker2-Win64-Shipping.exe+31D331A - B3 01                 - mov bl,01
		--Stalker2-Win64-Shipping.exe+31D331C - EB 02                 - jmp Stalker2-Win64-Shipping.exe+31D3320
	end
	
	return true
	
end

function Enable_Inject()
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("factor")
	
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["AspectRatio"]:WriteFloat(fAspectRatio169)
	Variables["factor"]:WriteFloat(fFactor)
	
	ResolutionChanged()	
		
	local asm = [[		

		(codecave:jmp)FOV,FOV_cc:
			movss [(allocation)Variables->FOVIn],xmm0
			addss xmm0,[(allocation)Variables->AdditionalFOV]
			movss [(allocation)Variables->FOVOut],xmm0
			%originalcode%
			%end%
						
		(codecave)FRINGE,FRINGE_cc:
			nop
			nop
			%end%
			
		(codecave)FOG,FOG_cc:
			jmp $$1															$ctx=1
			%end%
			
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("FRINGE_cc",bFringe)
		Toggle_CodeCave("FOG_cc",bFog)
	end	
			
	Write_FOV()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Out : %.2f", fFOVIn, fFOVOut))
	end
	
end

function FOVSlider_Changed(Sender)

	fAdditionalFOV = Sender:GetPosition() - 20
	lblFOVSlider.Caption:SetCaption( string.format("World FOV: %.0f",fAdditionalFOV) )
		
	Write_FOV()	
	ForceUpdate()

end

function Write_FOV()

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKFRINGEFix_Changed(Sender)

	bFringe = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FRINGE_cc",bFringe)
	ForceUpdate()

end

function CKFOGFix_Changed(Sender)

	bFog = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOG_cc",bFog)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
