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

--GAME VARS

--ControlVars
bFixEnabled = true
bCamera = true
bFPS = true
bCinematics = true

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

--INJECTION BEHAVIOUR
InjectDelay = 1000
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_CAMERA","Camera fine adjustment",15,70,210,17)
	DefaultControls.AddFixedFOVSlider("CAMERASlider","CAMERASlider_Changed",55,100,125,35,0,120,0,1)
	CAMERASlider:SetTickFrequency(10)
	CAMERASlider:SetLabel1Text("-20")
	CAMERASlider:SetLabel2Text("100")
	
	DefaultControls.AddFixToggle("CKCAMERAFix_Enable","Camera distance fix","CKCAMERAFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKFPSFix_Enable","In game frame fix","CKFPSFix_Changed",255,121,180,14)
	DefaultControls.AddFixToggle("CKCINEMATICSFPSFix_Enable","Cutscenes frame fix","CKCINEMATICSFPSFix_Changed",255,141,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("CAMERADISTANCE")
	
	if HackTool:SignatureScan("8B 88 ?? ?? ?? ?? 49 8B ?? ?? ?? ?? ?? F3 0F ?? ?? ?? ?? ?? ?? 48",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Ronin.exe+2BC551 - F3 0F 10 15 37 D7 47 04  - movss xmm2,[Ronin.exe+4739C90]
		--Ronin.exe+2BC559 - F3 0F 5C CA              - subss xmm1,xmm2
		--Ronin.exe+2BC55D - 8B 88 44 02 00 00        - mov ecx,[rax+00000244]
		--Ronin.exe+2BC563 - 49 8B 81 C8 03 00 00     - mov rax,[r9+000003C8]
		--Ronin.exe+2BC56A - F3 0F 5E 0D 5A DA 47 04  - divss xmm1,[Ronin.exe+4739FCC]
	end
	
	local tAddress = HackTool:AddAddress("FPS")
	
	if HackTool:SignatureScan("48 ?? ?? 48 89 ?? ?? ?? 48 63 ?? ?? ?? ?? ?? B9 3C 00 00 00",tAddress,PAGE_EXECUTE_READ,0x0f,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Ronin.exe+2A29F2 - 48 89 44 24 10        - mov [rsp+10],rax
		--Ronin.exe+2A29F7 - 48 63 05 BE 94 C7 04  - movsxd  rax,dword ptr [Ronin.exe+4F1BEBC]
		--Ronin.exe+2A29FE - B9 3C 00 00 00        - mov ecx,0000003C
		--Ronin.exe+2A2A03 - C7 04 24 1E 00 00 00  - mov [rsp],0000001E
		--Ronin.exe+2A2A0A - 89 4C 24 04           - mov [rsp+04],ecx
	end
	
	local tAddress = HackTool:AddAddress("CINEMATICSFRAMERATE")
	
	if HackTool:SignatureScan("89 97 ?? ?? ?? ?? 89 87 ?? ?? ?? ?? E8 ?? ?? ?? ?? 89 87",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--Ronin.exe+2A2AB1 - 8B 87 48 07 00 00        - mov eax,[rdi+00000748]
		--Ronin.exe+2A2AB7 - 8B 97 40 07 00 00        - mov edx,[rdi+00000740]
		--Ronin.exe+2A2ABD - 89 97 3C 07 00 00        - mov [rdi+0000073C],edx
		--Ronin.exe+2A2AC3 - 89 87 44 07 00 00        - mov [rdi+00000744],eax
		--Ronin.exe+2A2AC9 - E8 16 FF FF FF           - call Ronin.exe+2A29E4
		--Ronin.exe+2A2ACE - 89 87 40 07 00 00        - mov [rdi+00000740],eax
	end
	
	return true
	
end

function Enable_Inject()

	local Variables = HackTool:AllocateMemory("Variables", 0)
	Variables:PushInt("CameraDistance")	
	Variables:Allocate()	
	
	ResolutionChanged()	

	local asm = [[		

		(codecave:jmp)CAMERADISTANCE,CAMERADISTANCE_cc:
			$$0 $$1,[(allocation)Variables->CameraDistance]				$ctx=1
			jmp %returnaddress%
			%end%
			
		(codecave:jmp)CINEMATICSFRAMERATE,CINEMATICSFRAMERATE_cc:
			$$0 $$2,0xff												$ctx=1 ; Push FPS in cutscenes up to 255
			$$0 eax,$$2													$ctx=1
			%originalcode%
			jmp %returnaddress%
			%end%

		(codecave)FPS,FPS_cc:
			$$0 $$1,0xff												$ctx=1 ; Push FPS up to 255
			%end%
			
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("CAMERADISTANCE_cc",bCamera)
		Toggle_CodeCave("FPS_cc",bFPS)
		Toggle_CodeCave("CINEMATICSFRAMERATE_cc",bCinematics)
	end	
			
	Write_Camera()
	
end

function Periodic()	
end

function CAMERASlider_Changed(Sender)

	fCameraDistance = Sender:GetPosition() - 20
	lblCAMERASlider.Caption:SetCaption( string.format("Camera: %.0f",fCameraDistance) )
		
	Write_Camera()	
	ForceUpdate()

end

function Write_Camera()	

	local Variables = HackTool:GetAllocation("Variables")
	if Variables and Variables["CameraDistance"] then
		Variables["CameraDistance"]:WriteInt(fCameraDistance)
	end
	
end

function CKCAMERAFix_Changed(Sender)

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

function CKFPSFix_Changed(Sender)

	bFPS = Toggle_CheckFix(Sender)
	Toggle_CodeCave("FPS_cc",bFPS)
	ForceUpdate()

end

function CKCINEMATICSFPSFix_Changed(Sender)

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

function Disable_Inject()
	
	CleanUp()
	
end

function ResolutionChanged() 

	SyncDisplayDetection()
	
end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
