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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAdditionalCinematicsFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()
fDefaultAspectRatio = 1.777777791
fCamera = 0.8
fSharpening = 0

--ControlVars
bFixEnabled = true
bFOV = true
bCinematicsFOV = true
bAspect = true
bDOF = true
bCamera = true
bSharpening = true
bFog = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "*"
Process_ClassName = "UnrealWindow"
Process_EXEName = "JediSurvivor.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.AddFixedFOVSlider("CinematicsFOVSlider","CinematicsFOVSlider_Changed",55,160,125,35,0,70,0,1)
	CinematicsFOVSlider:SetTickFrequency(10)
	DefaultControls.AddHeader("Header_CAMERA","Camera fine adjustment",15,230,210,17)
	DefaultControls.AddFixedFOVSlider("CAMERASlider","CAMERASlider_Changed",55,260,125,35,0,300,0,1)
	CAMERASlider:SetTickFrequency(30)
	CAMERASlider:SetLabel1Text("0")
	CAMERASlider:SetLabel2Text("3")
	DefaultControls.AddHeader("Header_Sharpening","Image sharpening adjustment",15,320,210,17)
	DefaultControls.AddFixedFOVSlider("SharpeningSlider","SharpeningSlider_Changed",55,350,125,35,0,100,0,1)
	SharpeningSlider:SetTickFrequency(10)
	SharpeningSlider:SetLabel1Text("0")
	SharpeningSlider:SetLabel2Text("10")

	DefaultControls.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKCinematicsFOVFix_Enable","Cinematics FOV fix","CKCinematicsFOVFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKARFix_Enable","Aspect ratio fix","CKARFix_Changed",255,141,180,14)
	DefaultControls.AddFixToggle("CKDOFFix_Enable","Depth of field fix","CKDOFFix_Changed",255,161,180,14)
	DefaultControls.AddFixToggle("CKCAMERAFix_Enable","Camera distance","CKCAMERAFix_Changed",255,181,180,14)
	DefaultControls.AddFixToggle("CKSHARPENINGFix_Enable","Sharpening fix","CKSHARPENINGFix_Changed",255,201,180,14)
	DefaultControls.AddFixToggle("CKFOGFix_Enable","Fog removal fix","CKFOGFix_Changed",255,221,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("77 ?? 48 ?? ?? FF 90 ?? ?? ?? ?? F3 0F ?? ?? ?? 48",tAddress,PAGE_EXECUTE_READ,0x0b,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--JediSurvivor.exe+37CC034 - 48 8B 01              - mov rax,[rcx]
		--JediSurvivor.exe+37CC037 - FF 90 F0 06 00 00     - call qword ptr [rax+000006F0]
		--JediSurvivor.exe+37CC03D - F3 0F 10 40 18        - movss xmm0,[rax+18]
		--JediSurvivor.exe+37CC042 - 48 83 C4 28           - add rsp,28
		--JediSurvivor.exe+37CC046 - C3                    - ret 
	end
	
	local tAddress = HackTool:AddAddress("ASPECTS")
	
	if HackTool:SignatureScan("F3 0F ?? ?? ?? ?? ?? ?? E8 ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? EB ?? 0F",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		local Aspect_1 = HackTool:AddAddress("ASPECT1", tAddress)
		local Aspect_2 = HackTool:AddAddress("ASPECT2", tAddress)
		local CinematicsFOV = HackTool:AddAddress("CinematicsFOV", tAddress)
		Aspect_2:OffsetAddress(0x2F)
		CinematicsFOV:OffsetAddress(0xd)

		print( Aspect_1:GetInfo(TYPE_ADDRESS) )	
		print( Aspect_2:GetInfo(TYPE_ADDRESS) )
		print( CinematicsFOV:GetInfo(TYPE_ADDRESS) )
		--JediSurvivor.exe+3117BA9 - F3 0F 10 05 73 38 C0 01  - movss xmm0,[JediSurvivor.exe+4D1B424]
		--JediSurvivor.exe+3117BB1 - F3 0F 5E C1              - divss xmm0,xmm1
		--JediSurvivor.exe+3117BB5 - F3 0F 59 83 AC 09 00 00  - mulss xmm0,[rbx+000009AC]
		--JediSurvivor.exe+3117BBD - E8 D1 26 A8 01           - call JediSurvivor.exe+4B9A293
		--JediSurvivor.exe+3117BC2 - F3 0F 59 05 36 B5 D5 01  - mulss xmm0,[JediSurvivor.exe+4E73100]
	end
	
	local tAddress = HackTool:AddAddress("DOF")
	
	if HackTool:SignatureScan("8B ?? ?? E8 ?? ?? ?? ?? 8B ?? E8 ?? ?? ?? ?? 84 ?? 74 ?? 48",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--JediSurvivor.exe+2511F30 - 48 8B 05 B1 83 47 04  - mov rax,[JediSurvivor.exe+698A2E8]
		--JediSurvivor.exe+2511F37 - 48 8B CB              - mov rcx,rbx
		--JediSurvivor.exe+2511F3A - 8B 78 04              - mov edi,[rax+04]
		--JediSurvivor.exe+2511F3D - E8 4E CE 38 01        - call JediSurvivor.exe+389ED90
		--JediSurvivor.exe+2511F42 - 8B C8                 - mov ecx,eax
	end
	
	local tAddress = HackTool:AddAddress("CAMERA")
	
	if HackTool:SignatureScan("72 ?? 0F ?? ?? F3 0F ?? ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 0F ?? ?? F3 0F ?? ?? 0F 54",tAddress,PAGE_EXECUTE_READ,0x05,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--JediSurvivor.exe+1A5DE3B - 72 0B                   - jb JediSurvivor.exe+1A5DE48
		--JediSurvivor.exe+1A5DE3D - 0F 28 F0                - movaps xmm6,xmm0
		--JediSurvivor.exe+1A5DE40 - F3 0F 5D 35 E8 D5 2B 03 - minss xmm6,[JediSurvivor.exe+4D1B430]
		--JediSurvivor.exe+1A5DE48 - F3 0F 10 3D DC D5 2B 03 - movss xmm7,[JediSurvivor.exe+4D1B42C]
		--JediSurvivor.exe+1A5DE50 - 0F 28 C6                - movaps xmm0,xmm6
	end
	
	local tAddress = HackTool:AddAddress("SHARPENING")
	
	if HackTool:SignatureScan("F3 44 ?? ?? ?? ?? 45 0F ?? ?? 73 ?? 45 0F ?? ?? EB ?? F3 44 0F ?? ?? ?? ?? ?? ?? F3",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--JediSurvivor.exe+25B1200 - F3 0F 11 85 B8 06 00 00  - movss [rbp+000006B8],xmm0
		--JediSurvivor.exe+25B1208 - 48 8B 05 99 92 3E 04     - mov rax,[JediSurvivor.exe+699A4A8]
		--JediSurvivor.exe+25B120F - F3 44 0F 10 60 04        - movss xmm12,[rax+04]
		--JediSurvivor.exe+25B1215 - 45 0F 2F E2              - comiss xmm12,xmm10
		--JediSurvivor.exe+25B1219 - 73 06                    - jae JediSurvivor.exe+25B1221
	end
	
	local tAddress = HackTool:AddAddress("FOG1")
	
	if HackTool:SignatureScan("75 ?? B3 ?? EB ?? 32 ?? 48 8B ?? ?? ?? 48 85 ?? 74 ?? E8 ?? ?? ?? ?? 84",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--JediSurvivor.exe+233590D - 48 8B 05 64 2E 60 04  - mov rax,[JediSurvivor.exe+6938778]
		--JediSurvivor.exe+2335914 - 83 78 04 01           - cmp dword ptr [rax+04],01
		--JediSurvivor.exe+2335918 - 75 04                 - jne JediSurvivor.exe+233591E
		--JediSurvivor.exe+233591A - B3 01                 - mov bl,01
		--JediSurvivor.exe+233591C - EB 02                 - jmp JediSurvivor.exe+2335920
	end

	local tAddress = HackTool:AddAddress("FOG2")
	
	if HackTool:SignatureScan("74 ?? F7 47 ?? ?? ?? ?? ?? 74 ?? 83 BB",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--JediSurvivor.exe+283AF1D - 74 41                 - je JediSurvivor.exe+283AF60
		--JediSurvivor.exe+283AF1F - 83 3D 92 16 C8 03 00  - cmp dword ptr [JediSurvivor.exe+64BC5B8],00
		--JediSurvivor.exe+283AF26 - 74 38                 - je JediSurvivor.exe+283AF60
		--JediSurvivor.exe+283AF28 - F7 47 2C 00 00 00 02  - test [rdi+2C],02000000
		--JediSurvivor.exe+283AF2F - 74 2F                 - je JediSurvivor.exe+283AF60
	end

	return true
	
end

function Enable_Inject()
	local NearTo = HackTool:GetModuleAddress(Process_EXEName)
	local Variables = HackTool:AllocateMemory("Variables",50)
	Variables:PushFloat("CompensatedFOV")
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("CinematicsFOVIn")
	Variables:PushFloat("CinematicsFOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("AdditionalCinematicsFOV")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("ScreenRatio")
	Variables:PushFloat("GameAspect")
	Variables:PushFloat("Camera")
	Variables:PushFloat("Sharpening")
	
	Variables:Allocate(NearTo)
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["AdditionalCinematicsFOV"]:WriteFloat(fAdditionalCinematicsFOV)
	Variables["AspectRatio"]:WriteFloat(fDefaultAspectRatio)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	Variables["GameAspect"]:WriteFloat(0)
	Variables["Camera"]:WriteFloat(fCamera)
	Variables["Sharpening"]:WriteFloat(fSharpening)
	
	ResolutionChanged()	
							
	local asm = [[		

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
			$$0 [(allocation)Variables->FOVIn],$$1							$ctx=1
			addss $$1,[(allocation)Variables->AdditionalFOV]				$ctx=1
			$$0 [(allocation)Variables->FOVOut],$$1							$ctx=1
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)CinematicsFOV,CinematicsFOV_cc:
			%originalcode%
			movss [(allocation)Variables->CinematicsFOVIn],$$1				$ctx=1
			addss $$1,[(allocation)Variables->AdditionalCinematicsFOV]		$ctx=1
			movss [(allocation)Variables->CinematicsFOVOut],$$1				$ctx=1
			jmp %returnaddress%
			%end%
		
		(codecave:jmp)ASPECT1,ASPECT1_cc:
		    fld dword ptr [$$2+4]											$ctx=1
		    fmul dword ptr [(allocation)Variables->ScreenRatio]
		    fstp dword ptr [(allocation)Variables->GameAspect]
			$$0 $$1,[(allocation)Variables->GameAspect]						$ctx=1
			jmp %returnaddress%
			%end%
						
		(codecave:jmp)ASPECT2,ASPECT2_cc:
		    fld dword ptr [$$2+4]											$ctx=1
		    fmul dword ptr [(allocation)Variables->ScreenRatio]
		    fstp dword ptr [(allocation)Variables->GameAspect]
			$$0 $$1,[(allocation)Variables->GameAspect]						$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)CAMERA,CAMERA_cc:
			movss $$1,[(allocation)Variables->Camera]						$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)SHARPENING,SHARPENING_cc:
			$$0 $$1,[(allocation)Variables->Sharpening]						$ctx=1
			$$0 [$$2],$$1													$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave)DOF,DOF_cc:
			xor $$1,$$1														$ctx=1
			nop
			%end%
			
		(codecave)FOG1,FOG1_cc:
			jmp $$1															$ctx=1
			%end%
			
		(codecave)FOG2,FOG2_cc:
			jmp $$1															$ctx=1
			%end%
			
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes",NearTo) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("CinematicsFOV_cc",bCinematicsFOV)
		Toggle_CodeCave("ASPECT1_cc",bAspect)
		Toggle_CodeCave("ASPECT2_cc",bAspect)
		Toggle_CodeCave("DOF_cc",bDOF)
		Toggle_CodeCave("CAMERA_cc",bCamera)
		Toggle_CodeCave("SHARPENING_cc",bSharpening)
		Toggle_CodeCave("FOG1_cc",bFog)
		Toggle_CodeCave("FOG2_cc",bFog)
	end	
			
	Write_Camera()
	Write_FOV()
	Write_Cinematics_FOV()
	Write_Sharpening()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fCompensatedFOV = Variables["CompensatedFOV"]:ReadFloat()
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		local fCinematicsFOVIn = Variables["CinematicsFOVIn"]:ReadFloat()
		local fCinematicsFOVOut = Variables["CinematicsFOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Out : %.2f -- Cinematics FOV In: %.2f, Out: %.2f", fFOVIn, fFOVOut, fCinematicsFOVIn, fCinematicsFOVOut))
	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 CinematicsFOVSlider_Changed(Sender)
	
	fAdditionalCinematicsFOV = Sender:GetPosition() - 20
	lblCinematicsFOVSlider.Caption:SetCaption( string.format("Cinematics FOV: %.0f",fAdditionalCinematicsFOV) )
	Write_Cinematics_FOV()	
	ForceUpdate()

end

function Write_Cinematics_FOV()

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

function CAMERASlider_Changed(Sender)

	fCamera = Sender:GetPosition()
	lblCAMERASlider.Caption:SetCaption( string.format("Value: %.2f",fCamera / 100) )
	Write_Camera()
	ForceUpdate()
	
end

function Write_Camera()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["Camera"] then
		if bCamera == true then
			local Camera = fCamera / 100
			Variables["Camera"]:WriteFloat(Camera)
		else
			Variables["Camera"]:WriteFloat(0)
		end
	end

end

function SharpeningSlider_Changed(Sender)

	fSharpening = Sender:GetPosition() / 10
	lblSharpeningSlider.Caption:SetCaption( string.format("Value: %.2f",fSharpening) )
	Write_Sharpening()
	ForceUpdate()
	
end

function Write_Sharpening()

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["Sharpening"] then
		if bSharpening == true then
			Variables["Sharpening"]:WriteFloat(fSharpening)
		else
			Variables["Sharpening"]: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 CKCinematicsFOVFix_Changed(Sender)

	bCinematicsFOV = Toggle_CheckFix(Sender)
	Write_Cinematics_FOV()
	Toggle_CodeCave("CinematicsFOV_cc",bCinematicsFOV)
	ForceUpdate()

end

function CKARFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ASPECT1_cc",bAspect)
	Toggle_CodeCave("ASPECT2_cc",bAspect)
	ForceUpdate()
	
end

function CKDOFFix_Changed(Sender)

	bDOF = Toggle_CheckFix(Sender)
	Toggle_CodeCave("DOF_cc",bDOF)
	ForceUpdate()
	
end

function CKCAMERAFix_Changed(Sender)

	bCamera = Toggle_CheckFix(Sender)
	Write_Camera()
	Toggle_CodeCave("CAMERA_cc",bCamera)
	ForceUpdate()
	
end

function CKSHARPENINGFix_Changed(Sender)

	bSharpening = Toggle_CheckFix(Sender)
	Write_Sharpening()
	Toggle_CodeCave("SHARPENING_cc",bSharpening)
	ForceUpdate()
	
end

function CKFOGFix_Changed(Sender)

	bFog = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FOG1_cc",bFog)
	Toggle_CodeCave("FOG2_cc",bFog)
	ForceUpdate()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
