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

--GAME VARS
fDefaultFOV = 0
fAdditionalFOV = 0
fFOV = 0
fCinematicsFOV = 0
fAspectRatio = 1.7777778
fHUDLimit = 0.25

--ControlVars
bFixEnabled = true
bFOV = true
bCinematics = true
bHUDLimits = true

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

--INJECTION BEHAVIOUR
InjectDelay = 100
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.AddHeader("Header_FOV","FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	DefaultControls.AddHeader("Header_FOV2","Cinematics FOV fine adjustment",15,160,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider2","CinematicsFOVSlider_Changed",55,190,125,35)
	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKCinematicsFix_Enable","Cutscenes AR + FOV fix","CKCinematicsFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKHUDLimits_Enable","HUD Safe zone fix","CKHUDLimits_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("48 ?? ?? ?? 48 ?? ?? 0F 29 ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 33 ??",tAddress,PAGE_EXECUTE_READ,0x0C,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		--DeadIsland-Win64-Shipping.exe+16D4D46: 48 8B D9                 - mov rbx,rcx
		--DeadIsland-Win64-Shipping.exe+16D4D49: 0F 29 74 24 20           - movaps [rsp+20],xmm6
		--DeadIsland-Win64-Shipping.exe+16D4D4E: F3 0F 10 B1 28 35 00 00  - movss xmm6,[rcx+00003528]
		--DeadIsland-Win64-Shipping.exe+16D4D56: 33 D2                    - xor edx,edx
		--DeadIsland-Win64-Shipping.exe+16D4D58: 8B 89 20 38 00 00        - mov ecx,[rcx+00003820]
		print( tAddress:GetInfo(TYPE_ADDRESS) )
	end
	
	local tAddress = HackTool:AddAddress("CinematicsFix")
	
	if HackTool:SignatureScan("74 ?? 8B ?? ?? 48 ?? ?? 89 ?? ?? E8 ?? ?? ?? ?? F3 0F ?? ?? ?? ??",tAddress,PAGE_EXECUTE_READ,0x10,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		--DeadIsland-Win64-Shipping.exe+16F2186 - 89 46 3C                 - mov [rsi+3C],eax
		--DeadIsland-Win64-Shipping.exe+16F2189 - E8 B2 2B FE FF           - call DeadIsland-Win64-Shipping.exe+16D4D40
		--DeadIsland-Win64-Shipping.exe+16F218E - F3 0F 11 46 28           - movss [rsi+28],xmm0
		--DeadIsland-Win64-Shipping.exe+16F2193 - 48 8B 97 08 35 00 00     - mov rdx,[rdi+00003508]
		--DeadIsland-Win64-Shipping.exe+16F219A - 48 8B 8A 38 08 00 00     - mov rcx,[rdx+00000838]
		print( tAddress:GetInfo(TYPE_ADDRESS) )	
	end
	
	local tAddress = HackTool:AddAddress("HUDLimits")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ??  48 8B ?? ?? ?? ?? ?? 8B ?? ?? ?? ?? ?? 89",tAddress,PAGE_EXECUTE_READ,0x00,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		--DeadIsland-Win64-Shipping.exe+199DA99: 48 8B 87 F0 01 00 00     - mov rax,[rdi+000001F0]
		--DeadIsland-Win64-Shipping.exe+199DAA0: F3 0F 5F C1              - maxss xmm0,xmm1
		--DeadIsland-Win64-Shipping.exe+199DAA4: F3 0F 11 90 AC 00 00 00  - movss [rax+000000AC],xmm2
		--DeadIsland-Win64-Shipping.exe+199DAAC: F3 0F 11 80 A8 00 00 00  - movss [rax+000000A8],xmm0
		--DeadIsland-Win64-Shipping.exe+199DAB4: 48 8B 8F F0 01 00 00     - mov rcx,[rdi+000001F0]
		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("FOVCinIn")
	Variables:PushFloat("FOVCinOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AdditionalCinematicsFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("HUDLimit")
	Variables:Allocate()
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["HUDLimit"]:WriteFloat(fHUDLimit)
	
	ResolutionChanged()	
	
	local asm = [[	

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			movss [(allocation)Variables->FOVIn],$$1						$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]				$ctx=1
			movss [(allocation)Variables->FOVOut],$$1						$ctx=1
			jmp %returnaddress%
			%end%			
			
		(codecave:jmp)CinematicsFix,CinematicsFix_cc:
			mov eax,[(allocation)Variables->AspectRatio]
			mov [rsi+0x3C],eax
			movss [(allocation)Variables->FOVCinIn],$$2						$ctx=1
			addss $$2,[(allocation)Variables->AdditionalCinematicsFOV]		$ctx=1
			movss [(allocation)Variables->FOVCinOut],$$2					$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%	
						
		(codecave:jmp)HUDLimits,HUDLimits_cc:
			movss xmm0,[(allocation)Variables->HUDLimit]
			movss xmm2,[(allocation)Variables->HUDLimit]
			%originalcode%
			jmp %returnaddress%
			%end%

	]]

	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("CinematicsFix_cc",bCinematics)
		Toggle_CodeCave("HUDLimits_cc",bHUDLimits)
	end	
			
	Write_FOV()
	Write_CinematicsFOV()
	
end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()
		if fFOVIn < 10 then
			PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: --, FOV Out : --", fFOVIn, fFOVOut))
		else
			PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, FOV Out : %.2f", fFOVIn, fFOVOut))
		end
		
		if bCinematics and Variables["AspectRatio"] then
			fAspectRatio = DisplayInfo:GetfOffsetWidth() / DisplayInfo:GetfOffsetHeight()
			Variables["AspectRatio"]:WriteFloat(fAspectRatio)
		end
		
	end
end

function FOVSlider_Changed(Sender)

	fFOV = (Sender:GetScaledFloat(3)) + 18.33
	lblFOVSlider.Caption:SetCaption( string.format("Additional FOV : %.2f",fFOV) )
	Write_FOV()
	ForceUpdate()
	
end

function CinematicsFOVSlider_Changed(Sender)

	fCinematicsFOV = (Sender:GetScaledFloat(3)) + 18.33
	lblFOVSlider2.Caption:SetCaption( string.format("Additional Cinematics FOV : %.2f",fCinematicsFOV) )
	Write_CinematicsFOV()
	ForceUpdate()

end

function Write_FOV()

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

end

function Write_CinematicsFOV()

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

end

function Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function CKCinematicsFix_Changed(Sender)

	bCinematics = Toggle_CheckFix(Sender)
	Toggle_CodeCave("CinematicsFix_cc",bCinematics)
	ForceUpdate()
	
end

function CKHUDLimits_Changed(Sender)

	bHUDLimits = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUDLimits_cc",bHUDLimits)
	ForceUpdate()

end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end
