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

--GAME VARS
fDefaultFOV = 85
fAdditionalFOV = 0
fAspectRatio = DisplayInfo:GetWidth() / DisplayInfo:GetHeight()

--ControlVars
bFixEnabled = true
bFOV = true
bAspect = true

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

--INJECTION BEHAVIOUR
InjectDelay = 200
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.AddFixToggle("CKFOVFix_Enable","FOV fix","CKFOVFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKASPECTFix_Enable","Aspect ratio fix","CKASPECTFix_Changed",255,121,180,14)

end

function Configure_SignatureScan() 
	
	local tAddress = HackTool:AddAddress("FOV")
	
	if HackTool:SignatureScan("F3 0F 10 ?? ?? ?? ?? ?? F3 0F 5E ?? ?? ?? ?? ?? F3 0F 59 ?? ?? ?? ?? ?? C3",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--BladesOfFire.exe+C0895F - CC                    	- int 3 
		--BladesOfFire.exe+C08960 - F3 0F 10 05 60 DD 2E 01 - movss xmm0,[BladesOfFire.exe+1EF66C8]
		--BladesOfFire.exe+C08968 - F3 0F 5E 05 08 DF 2E 01 - divss xmm0,[BladesOfFire.exe+1EF6878]
		--BladesOfFire.exe+C08970 - F3 0F 59 81 08 01 00 00 - mulss xmm0,[rcx+00000108]
		--BladesOfFire.exe+C08978 - C3                    	- ret 
	end
		
	local tAddress = HackTool:AddAddress("ASPECT")
	
	if HackTool:SignatureScan("F3 48 ?? ?? ?? F3 0F ?? ?? E8 ?? ?? ?? ?? F3 48 ?? ?? ?? 89 83 ?? ?? ?? ?? EB ?? 89",tAddress,PAGE_EXECUTE_READ,0x0,Process_EXEName) == 0 then
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else	
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		--BladesOfFire.exe+FD3BC0 - 8B 83 04 01 00 00      - mov eax,[rbx+00000104]
		--BladesOfFire.exe+FD3BC6 - F3 48 0F 2A C0         - cvtsi2ss xmm0,rax
		--BladesOfFire.exe+FD3BCB - F3 0F 59 C6            - mulss xmm0,xmm6
		--BladesOfFire.exe+FD3BCF - E8 48 48 0F 00         - call BladesOfFire.exe+10C841C
		--BladesOfFire.exe+FD3BD4 - F3 48 0F 2C C0         - cvttss2si rax,xmm0
	end
	
	return true
	
end

function Enable_Inject()
	local NearTo = HackTool:GetModuleAddress(Process_EXEName)
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("FOVIn")
	Variables:PushFloat("FOVOut")
	Variables:PushFloat("AdditionalFOV")
	Variables:PushFloat("ScreenRatio")
	
	Variables:Allocate(NearTo)
	Variables["FOVIn"]:WriteFloat(fDefaultFOV)
	Variables["FOVOut"]:WriteFloat(fDefaultFOV)
	Variables["AdditionalFOV"]:WriteFloat(fAdditionalFOV)
	Variables["ScreenRatio"]:WriteFloat(fAspectRatio)
	
	FOVCalculator = HackTool:InjectFOVCalculator("FOVCalculator")	
	
	ResolutionChanged()	
		
	local asm = [[		

		(codecave:jmp)FOV,FOV_cc:
			%originalcode%
		    addss $$1,[(allocation)Variables->AdditionalFOV]			$ctx=1
			jmp %returnaddress%
			%end%
								
		(codecave:jmp)ASPECT,ASPECT_cc:
			%originalcode%
			movss xmm6,[(allocation)Variables->ScreenRatio]
			jmp %returnaddress%
			%end%
	
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes",NearTo) == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("FOV_cc",bFOV)
		Toggle_CodeCave("ASPECT_cc",bAspect)
	end	
			
	Write_FOV()
	
end

function Periodic()

	local Variables = HackTool:GetAllocation("Variables")
	
	if Variables and Variables["FOVIn"] and Variables["AdditionalFOV"] then
		local fFOVIn = Variables["FOVIn"]:ReadFloat()
		local fFOVOut = Variables["FOVOut"]:ReadFloat()
		
		PluginViewport:AppendStatusMessage( string.format("\r\n===== Fix informations =====\r\nFOV In: %.2f, Out : %.2f", fFOVIn, fFOVOut))
	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 Disable_Inject()
	
	CleanUp()
	
end

function CKFOVFix_Changed(Sender)

	bFOV = Toggle_CheckFix(Sender)
	local fFOV = (bFOV or bAspect)
	Write_FOV()
	Toggle_CodeCave("FOV_cc",fFOV)
	ForceUpdate()
	
end

function CKASPECTFix_Changed(Sender)

	bAspect = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ASPECT_cc",bAspect)
	ForceUpdate()
	
end

function ResolutionChanged()

end

function Init()

	Init_BaseControls()
	Init_Controls()

end

function DeInit()

	DisableFix()

end
