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

--GAME VARS
fDefaultAspectRatio = 1.7777777777
fDEfaultAspectRatioVH = 0.5625
fDefaultAspectRatioHV = 1.7777777777
fDefaultHUDReference = 1280
fNewAspectRatioVH = 0.5625
fNewAspectRatioHV = 1.7777777777
fNewHUDReference = 1280


--ControlVars
bFixEnabled = true
bHUDFix = true
bFOVFix = true
bRemoveLetterbox = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Assassin's Creed IV Black Flag"
Process_ClassName = "ScimitarEngineWindowClass"
Process_EXEName = "*"

--INJECTION BEHAVIOUR
InjectDelay = 3000
WriteInterval = 100
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.AddFixToggle("CKRemoveLetterbox_Enable","Remove Letterbox","CKRemoveLetterbox_Changed",255,101,180,14)
end


function Configure_SignatureScan() 

	if Process_EXEName == "AC4BFMP.exe" then
		
		local tAddress = HackTool:AddAddress("AspectRatioVH")		
		if HackTool:SignatureScan("0F 28 ?? F3 0F 59 ?? ?? ?? ?? ?? 0F 2F ?? 77",tAddress,PAGE_EXECUTE_READ,0x3,Process_EXEName) == 0 then	
			return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
		else
			tAddress:AcquireAddress(0x4)	
			tAddress:SetAutoUnprotect(true)
			fDefaultAspectRatioVH = tAddress:ReadFloat()
			print( tAddress:GetInfo(TYPE_FLOAT) )		
		end	
		
		local tAddress = HackTool:AddAddress("AspectRatioHV")
		if HackTool:SignatureScan("77 ?? 0F 28 ?? F3 0F 59 ?? ?? ?? ?? ?? EB ?? 0F 28",tAddress,PAGE_EXECUTE_READ,0x5,Process_EXEName) == 0 then	
			return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
		else
			tAddress:AcquireAddress(0x4)	
			tAddress:SetAutoUnprotect(true)
			fAspectRatioHV = tAddress:ReadFloat()
			print( tAddress:GetInfo(TYPE_FLOAT) )		
		end			
	else
		local tAddress = HackTool:AddAddress("AspectRatioVH")
		if HackTool:SignatureScan("0F 28 ?? EB ?? F3 0F 10 ?? ?? ?? ?? ?? F3 0F 5E ?? F3 0F 59 ?? 0F",tAddress,PAGE_EXECUTE_READ,0x5,Process_EXEName) == 0 then	
			return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
		else
			tAddress:AcquireAddress(0x4)	
			tAddress:SetAutoUnprotect(true)
			fDefaultAspectRatioVH = tAddress:ReadFloat()
			print( tAddress:GetInfo(TYPE_FLOAT) )		
			
			
			local tAddress = HackTool:AddAddress("HUDReferenceWidth", tAddress)
			tAddress:OffsetAddress(-(SIZE_FLOAT*3))
			tAddress:SetAutoUnprotect(true)
			fDefaultHUDReference = tAddress:ReadFloat()
			print( tAddress:GetInfo(TYPE_FLOAT) )
		end		

		local tAddress = HackTool:AddAddress("AspectRatioHV")
		if HackTool:SignatureScan("77 ?? 0F 28 ?? F3 0F 59 ?? F3 0F 59 ?? ?? ?? ?? ?? EB",tAddress,PAGE_EXECUTE_READ,0x9,Process_EXEName) == 0 then	
			return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
		else
			tAddress:AcquireAddress(0x4)	
			tAddress:SetAutoUnprotect(true)
			fAspectRatioHV = tAddress:ReadFloat()
			print( tAddress:GetInfo(TYPE_FLOAT) )		
		end			
		
	end



	return true

end




function Enable_Inject() 
	
	ResolutionChanged()
	
end

function Periodic()

	if bRemoveLetterbox then
		local AspectRatioVH = HackTool:GetAddress("AspectRatioVH")
		if AspectRatioVH then
			AspectRatioVH:WriteFloat(fNewAspectRatioVH)
		end
		
		local AspectRatioHV = HackTool:GetAddress("AspectRatioHV")
		if AspectRatioHV then
			AspectRatioHV:WriteFloat(fNewAspectRatioHV)
		end	
		
		local HUDReferenceWidth = HackTool:GetAddress("HUDReferenceWidth")
		if HUDReferenceWidth then
			HUDReferenceWidth:WriteFloat(fNewHUDReference)
		end	
	end
				
end

function Disable_Inject()
	
	CleanUp()
	
end


function ResolutionChanged() 

	local HUDWidth = DisplayInfo:GetfOffsetWidth()
	local HUDHeight = DisplayInfo:GetfOffsetHeight()
	local HUDAspect = HUDWidth / HUDHeight	
	
	fNewAspectRatioVH = HUDHeight / HUDWidth
	fNewAspectRatioHV = HUDWidth / HUDHeight
	fNewHUDReference = fDefaultHUDReference / (fDefaultAspectRatioHV / fNewAspectRatioHV)
	
end

function CKRemoveLetterbox_Changed(Sender)

	bRemoveLetterbox = Toggle_CheckFix(Sender)
	if bRemoveLetterbox == false then
		RestoreAspectRatio()
	end
	ForceUpdate()	
	
end

function RestoreAspectRatio() 

	local AspectRatioVH = HackTool:GetAddress("AspectRatioVH")
	if AspectRatioVH then
		AspectRatioVH:WriteFloat(fDefaultAspectRatioVH)
	end
	
	local AspectRatioHV = HackTool:GetAddress("AspectRatioHV")
	if AspectRatioHV then
		AspectRatioHV:WriteFloat(fDefaultAspectRatioHV)
	end	
	
	local HUDReferenceWidth = HackTool:GetAddress("HUDReferenceWidth")
	if HUDReferenceWidth then
		HUDReferenceWidth:WriteFloat(fDefaultHUDReference)
	end		

end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end