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

--GAME VARS
fDefaultFOV = 90
fDefaultAspectRatio = 1.7777778
fAdditionalFOV = 0
fFOV = 90

--ControlVars
bFixEnabled = true
bFOV = true
bFMV = true
bFrameTime = true
bAR = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "DEATH STRANDING DIRECTOR'S CUT"
--Process_WindowName = "Death Stranding"
Process_ClassName = "*"
Process_EXEName = "ds.exe"

--INJECTION BEHAVIOUR
InjectDelay = 0
WriteInterval = 0
SearchInterval = 0
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.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKMarkersFix_Enable","Markers fix","CKMarkersFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKFMVFix_Enable","FMVs fix","CKFMVFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKFMVFRTIMEFix_Enable","Cinematics Frametime fix","CKFMVFRTIMEFix_Changed",255,161,180,14)

end

function Configure_SignatureScan() 

	--local tAddress = HackTool:AddAddress("AR2")
	
	--if HackTool:SignatureScan("39 8E E3 3F 55 55 15 40",tAddress,PAGE_READONLY,0x0,Process_EXEName) == 0 then
		--return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	--else	
	    --print( tAddress:GetInfo(TYPE_ADDRESS) )
		--tAddress:AddSubOffset("16x9",0x0)
		--tAddress:AddSubOffset("21x9",0x4)
		--Default_16x9 = tAddress["16x9"]:ReadFloat()
		--Default_21x9 = tAddress["21x9"]:ReadFloat()
		--tAddress["16x9"]:SetAutoUnprotect(true)
		--tAddress["21x9"]:SetAutoUnprotect(true)
	--end
	
	local tAddress = HackTool:AddAddress("FOV")
	
	--if HackTool:SignatureScan("73 ?? C5 FA 10 83 ?? ?? ?? ?? 40 38 B3",tAddress,PAGE_EXECUTE_READ,0x0A,Process_EXEName) == 0 then	
	if HackTool:SignatureScan("E8 6C ?? ?? ?? C5 FA 10 4F ?? C5 F2",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then		
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end

	local tAddress = HackTool:AddAddress("FOV2")
	
	if HackTool:SignatureScan("C5 EA ?? ?? ?? ?? ?? ?? C5 ?? ?? ?? C5 ?? ?? ?? E8 ?? ?? ?? ?? C5 CA 5E",tAddress,PAGE_EXECUTE_READ,0x15,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end

	local tAddress = HackTool:AddAddress("AR")
	
	if HackTool:SignatureScan("C5 D2 5E C2 C5 FA 5C 0D ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x04,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
	    print( tAddress:GetInfo(TYPE_ADDRESS) )
	end

	local tAddress = HackTool:AddAddress("FMV1")
	
	if HackTool:SignatureScan("8B 84 24 ?? ?? ?? ?? 48 8D 94 24 ?? ?? ?? ?? 2B 84 24 ?? ?? ?? ?? C5 F8",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
	    print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("FMV2")
	
	if HackTool:SignatureScan("C5 C2 5E 0D ?? ?? ?? ?? C5 FA ?? ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
	    print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("FMVFRTIME1")
	
	if HackTool:SignatureScan("8B 1D ?? ?? ?? ?? 44 0F B6 ?? ?? ?? ?? ?? 8B 88",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
	    print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("FMVFRTIME2")
	
	if HackTool:SignatureScan("B8 ?? ?? ?? ?? 3B D8 0F 4F D8 65 48 8B",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
	    print( tAddress:GetInfo(TYPE_ADDRESS) )
	end	
	
	return true

end


function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("DefaultAR")
	Variables:PushInt("FMVFrameTime")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["DefaultAR"]:WriteFloat(fDefaultAspectRatio)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			;movss [(allocation)Variables->FOVIn],xmm0
			;addss xmm0,[(allocation)Variables->AdditionalFOV]
			;movss [(allocation)Variables->FOVOut],xmm0
			%originalcode%
			movss xmm15,[rdi+0x50]
			movss [(allocation)Variables->FOVIn],xmm15
			addss xmm15,[(allocation)Variables->AdditionalFOV]
			movss [(allocation)Variables->FOVOut],xmm15
			movss [rdi+0x50],xmm15
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)FMV1,FMV1_cc:
			fild dword ptr [rsp+0x11C]
			fmul dword ptr [(allocation)Variables->DefaultAR]
			fistp dword ptr [rsp+0x118]
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)FMVFRTIME1,FMVFRTIME1_cc:
			%originalcode%
			mov [(allocation)Variables->FMVFrameTime],$$1			$context=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)FMVFRTIME2,FMVFRTIME2_cc:
			mov $$1,[(allocation)Variables->FMVFrameTime]			$context=1
			jmp %returnaddress%
			%end%	
			
		(codecave)FOV2,FOV2_cc:
			vdivss xmm1,xmm6,[(allocation)Variables->DefaultAR]
			%end%
			
		(codecave)FMV2,FMV2_cc:
			vdivss xmm1,xmm7,[(allocation)Variables->DefaultAR]
			%end%
		
		(codecave)AR,AR_cc:
			vsubss xmm1,xmm0,[(allocation)Variables->DefaultAR]
			%end%

	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("FOV2_cc",bFixEnabled)
		Toggle_CodeCave("FMV1_cc",bFMV)
		Toggle_CodeCave("FMV2_cc",bFMV)
		Toggle_CodeCave("FMVFRTIME1_cc",bFrameTime)
		Toggle_CodeCave("FMVFRTIME2_cc",bFrameTime)
		Toggle_CodeCave("AR_cc",bAR)
		
		local tAddress = HackTool:GetAddress("AR2")
		if tAddress then
			local AspectRatios = HackTool:GetAddress("AR2")
			if AspectRatios and AspectRatios["16x9"] then
				AspectRatios["16x9"]:WriteFloat(DisplayInfo:GetAspectRatio())
				AspectRatios["21x9"]:WriteFloat(DisplayInfo:GetAspectRatio())
			end
		end		
	end	

	Write_FOV()
	
end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["FMVFrameTime"] and Variables["AdditionalFOV"] then
		fFrameTime = Variables["FMVFrameTime"]:ReadInt()
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nGame FOV In : %.0f - FOV Out : %.0f\r\nGame Frametime : %d fps", fFOVIn, fFOVOut, fFrameTime))
	end
end

function FOVSlider_Changed(Sender)
	local Variables = HackTool:GetAllocation("Variables")

	fFOV = (Sender:GetScaledFloat(3))
	lblFOVSlider.Caption:SetCaption( string.format("Additional FOV : %.2f",fFOV) )
	Variables["AdditionalFOV"]:WriteFloat(fFOV)
	ForceUpdate()
	
end

function Disable_Inject()
	
	local AspectRatios = HackTool:GetAddress("AR2")
	if AspectRatios and AspectRatios["16x9"] then
		AspectRatios["16x9"]:WriteFloat(Default_16x9)
		AspectRatios["21x9"]:WriteFloat(Default_21x9)
	end
	
	CleanUp()
	
end

function Write_FOV()

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

end

function CKFOVFix_Changed(Sender)

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

function CKMarkersFix_Changed(Sender)

	bAR = Toggle_CheckFix(Sender)
	Toggle_CodeCave("AR_cc",bAR)
	ForceUpdate()
	
end

function CKFMVFix_Changed(Sender)

	bFMV = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FMV1_cc",bFMV)
	Toggle_CodeCave("FMV2_cc",bFMV)
	ForceUpdate()
	
end


function CKFMVFRTIMEFix_Changed(Sender)

	bFrameTime = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FMVFRTIME1_cc",bFrameTime)
	Toggle_CodeCave("FMVFRTIME2_cc",bFrameTime)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
