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

--ControlVars
bFixEnabled = true
bFOV = true
bHUDSafeZone = true
bSaveEverywhere = true

--GAME VARS
fAdditionalFOV = 0
fAdditionalFOVVehicle = 0
fAdditionalFOVOther = 0
fHUDSafeZOneLeft = 0
fHUDSafeZoneRight = 100

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Days Gone"
Process_ClassName = "UnrealWindow"
Process_EXEName = "*"

--INJECTION BEHAVIOUR
InjectDelay = 500 
WriteInterval = 500
SearchInterval = 1000
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","General FOV fine adjustment",15,70,210,17)	
	DefaultControls.AddFOVSlider("FOVSlider","FOVSlider_Changed",55,100,125,35)
	FOVSlider:SetTickFrequency(25)
	DefaultControls.AddHeader("Header_FOV2","Vehicle FOV fine adjustment",15,160,210,17)	
	DefaultControls.AddFOVSlider("FOVSliderVehicle","FOVSliderVehicle_Changed",55,190,125,35)
	FOVSliderVehicle:SetTickFrequency(25)
	DefaultControls.AddHeader("Header_FOV3","Other FOV fine adjustment",245,160,210,17)	
	DefaultControls.AddFOVSlider("FOVSliderOther","FOVSliderOther_Changed",285,190,125,35)
	FOVSliderOther:SetTickFrequency(25)
	
	DefaultControls.AddHeader("Header_HUD_SZ_LEFT","HUD safe zone left",15,250,210,17)	
	DefaultControls.AddFixedFOVSlider("HUD_SZ_LEFT","HUDSZLSlider_Changed",55,280,125,35,0,40,0,1)
	HUD_SZ_LEFT:SetTickFrequency(5)
	HUD_SZ_LEFT:SetLabel1Text("0")
	HUD_SZ_LEFT:SetLabel2Text("40")
	DefaultControls.AddHeader("Header_HUDSZRIGHT","HUD safe zone right",245,250,210,17)	
	DefaultControls.AddFixedFOVSlider("HUD_SZ_RIGHT","HUDSZRSlider_Changed",285,280,125,35,0,40,0,1)
	HUD_SZ_RIGHT:SetTickFrequency(5)
	HUD_SZ_RIGHT:SetLabel1Text("0")
	HUD_SZ_RIGHT:SetLabel2Text("40")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,60,14)
	DefaultControls.AddFixToggle("CKHUDFix_Enable","HUD safe zone fix","CKHUDFix_Changed",325,101,100,14)
	DefaultControls.AddFixToggle("CKSaveFix_Enable","Save everywhere","CKSaveFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("FOV")
	if HackTool:SignatureScan("CC 48 8B ?? ?? F3 0F 10 80 ?? ?? ?? ?? F3 0F",tAddress,PAGE_EXECUTE_READ,0x0d,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--DaysGone.exe+4375A0 - 48 8B 41 28           		- mov rax,[rcx+28]
		--DaysGone.exe+4375A4 - F3 0F 10 80 68 03 00 00   	- movss xmm0,[rax+00000368]
		--DaysGone.exe+4375AC - F3 0F 59 41 4C         		- mulss xmm0,[rcx+4C]
		--DaysGone.exe+4375B1 - C3                    		- ret 
		--DaysGone.exe+4375B2 - CC                    		- int 3 
	end
		
	local tAddress = HackTool:AddAddress("HUDSAFEZONE")
	if HackTool:SignatureScan("48 ?? ?? 48 ?? ?? 66 48 ?? ?? ?? FF ?? 48 8B ?? ?? ?? 0F ?? ??",tAddress,PAGE_EXECUTE_READ,0x18,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--DaysGone.exe+1FBA9E3 - 0F 10 00               	- movups xmm0,[rax]
		--DaysGone.exe+1FBA9E6 - 48 8B C7              		- mov rax,rdi
		--DaysGone.exe+1FBA9E9 - 0F 11 07               	- movups [rdi],xmm0
		--DaysGone.exe+1FBA9EC - 48 83 C4 30           		- add rsp,30
		--DaysGone.exe+1FBA9F0 - 5F                    		- pop rdi
	end
	
	local tAddress = HackTool:AddAddress("SAVE")
	if HackTool:SignatureScan("83 B9 ?? ?? ?? ?? ?? 7E ?? 0F B6 ?? ?? ?? ?? ?? EB ??",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--DaysGone.exe+7234DC - 45 32 F6              	- xor r14b,r14b
		--DaysGone.exe+7234DF - 48 8B D9              	- mov rbx,rcx
		--DaysGone.exe+7234E2 - 83 B9 D4 06 00 00 00    - cmp dword ptr [rcx+000006D4],00
		--DaysGone.exe+7234E9 - 7E 09                 	- jle DaysGone.exe+7234F4
		--DaysGone.exe+7234EB - 0F B6 A9 D8 06 00 00    - movzx ebp,byte ptr [rcx+000006D8]
	end
	
	
	return true
	
end



function Enable_Inject() 

	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("BaseFOV")
	Variables:PushFloat("BaseFOVScale")
	Variables:PushFloat("GeneralFOV")
	Variables:PushFloat("VehicleFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("FOVVehicleIn")
	Variables:PushFloat("FOVVehicleOut")
	Variables:PushFloat("FOVOtherIn")
	Variables:PushFloat("FOVOtherOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AdditionalVehicleFOV")
	Variables:PushFloat("AdditionalOtherFOV")
	Variables:PushFloat("HUDSafeZoneLeft")
	Variables:PushFloat("HUDSafeZoneTop")
	Variables:PushFloat("HUDSafeZoneRight")
	Variables:PushFloat("HUDSafeZoneBottom")
	Variables:Allocate()
	
	--Initialize left, top, right and bottom safe zone (regular values)
	Variables["HUDSafeZoneLeft"]:WriteFloat(0)
	Variables["HUDSafeZoneTop"]:WriteFloat(0)
	Variables["HUDSafeZoneRight"]:WriteFloat(1)
	Variables["HUDSafeZoneBottom"]:WriteFloat(1)
	Variables["GeneralFOV"]:WriteFloat(70)
	Variables["VehicleFOV"]:WriteFloat(90)
	
	asm = [[	
			
		(codecave:jmp)FOV,FOV_cc:
			movss xmm15,[rax+0x368]
			movss [(allocation)Variables->BaseFOVScale],xmm15
			%originalcode%
			movss xmm15,[$$2]										$ctx=1
			movss [(allocation)Variables->FOVIn],$$1				$ctx=1
			movss [(allocation)Variables->BaseFOV],xmm15
			pushfq
			comiss xmm15,[(allocation)Variables->GeneralFOV]			   ; Test if FOV is general
			jne testVehicleFOV
			
			addss $$1,[(allocation)Variables->AdditionalFOV]		$ctx=1
			jmp exit			
			
			testVehicleFOV:
			comiss xmm15,[(allocation)Variables->VehicleFOV]			   ; Test if FOV is Vehicle
			jne otherFOV
			
			addss $$1,[(allocation)Variables->AdditionalVehicleFOV]	$ctx=1
			jmp exit

			otherFOV:
			addss $$1,[(allocation)Variables->AdditionalOtherFOV]	$ctx=1
			
			exit:
			popfq
			movss [(allocation)Variables->FOVOut],$$1				$ctx=1
			jmp %returnaddress%			
			%end%
			
		(codecave:jmp)HUDSAFEZONE,HUDSAFEZONE_cc:
			%originalcode%
			$$0 $$2,[(allocation)Variables->HUDSafeZoneLeft]		$ctx=1 ; Will copy all safe zone offsets
			$$0 [$$1],$$2											$ctx=1
			jmp %returnaddress%			
			%end%
			
		(codecave:jmp)SAVE,SAVE_cc:
			mov word [$$1],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("HUDSAFEZONE_cc",bHUDSafeZone)
		Toggle_CodeCave("SAVE_cc",bSaveEverywhere)
	end

	Write_FOV()
	Write_Vehicle_FOV()
	Write_Other_FOV()
	Write_HUD_Safe_Zone_Left()
	Write_HUD_Safe_Zone_Right()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		fBaseFOVScale = Variables["BaseFOVScale"]:ReadFloat()
		fBaseFOV = Variables["BaseFOV"]:ReadFloat()
		fFOVIn = Variables["FOVIn"]:ReadFloat()
		fFOVOut = Variables["FOVOut"]:ReadFloat()		
		fVehicleFOVIn = Variables["FOVVehicleIn"]:ReadFloat()
		fVehicleFOVOut = Variables["FOVVehicleOut"]:ReadFloat()
		fOtherFOVIn = Variables["FOVOtherOut"]:ReadFloat()
		fOtherFOVOut = Variables["FOVOtherOut"]:ReadFloat()
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nBase FOV: %0.f, Base FOV Scale: %0.2f\r\nFOV In: %0.f, FOV Out : %.0f", fBaseFOV, fBaseFOVScale, fFOVIn, fFOVOut))
	end
	
end

function Disable_Inject()

	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

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

function FOVSlider_Changed(Sender)

	fAdditionalFOV = ((Sender:GetScaledFloat(2)) * 0.7) + 15
	lblFOVSlider.Caption:SetCaption( string.format("+ %.0f",fAdditionalFOV) )
	Write_FOV()
	ForceUpdate()
	
end

function FOVSliderVehicle_Changed(Sender)

	fAdditionalFOVVehicle = ((Sender:GetScaledFloat(2)) * 0.7) + 15
	lblFOVSliderVehicle.Caption:SetCaption( string.format("+ %.0f",fAdditionalFOVVehicle) )
	Write_Vehicle_FOV()
	ForceUpdate()
	
end

function FOVSliderOther_Changed(Sender)

	fAdditionalFOVOther = ((Sender:GetScaledFloat(2)) * 0.7) + 15
	lblFOVSliderOther.Caption:SetCaption( string.format("+ %.0f",fAdditionalFOVOther) )
	Write_Other_FOV()
	ForceUpdate()
	
end

function CKHUDFix_Changed(Sender)

	bHUDSafeZone = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUDSAFEZONE_cc",bHUDSafeZone)
	ForceUpdate()

end

function HUDSZLSlider_Changed(Sender)
	
	fHUDSafeZOneLeft = Sender:GetPosition()
	lblHUD_SZ_LEFT.Caption:SetCaption( string.format("+ %.0f %%",fHUDSafeZOneLeft) )
	Write_HUD_Safe_Zone_Left()
	ForceUpdate()
	
end

function HUDSZRSlider_Changed(Sender)

	fHUDSafeZoneRight = Sender:GetPosition()
	lblHUD_SZ_RIGHT.Caption:SetCaption( string.format("+ %.0f %%",fHUDSafeZoneRight) )
	Write_HUD_Safe_Zone_Right()
	ForceUpdate()
	
end

function CKSaveFix_Changed(Sender)

	bSaveEverywhere = Toggle_CheckFix(Sender)
	Toggle_CodeCave("SAVE_cc",bSaveEverywhere)
	ForceUpdate()
	
end

function Write_FOV()

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

end

function Write_Vehicle_FOV()

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

end

function Write_Other_FOV()

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

end

function Write_HUD_Safe_Zone_Left()
	
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["HUDSafeZoneLeft"] then
		Variables["HUDSafeZoneLeft"]:WriteFloat(fHUDSafeZOneLeft / 100)
	end
	
end

function Write_HUD_Safe_Zone_Right()
	
	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["HUDSafeZoneRight"] then
		Variables["HUDSafeZoneRight"]:WriteFloat(1 - (fHUDSafeZoneRight / 100))
	end
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end