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

--GAME VARS
fAdditionalHUDWidth = 1
fAdditionalHUDHeight = 1

--ControlVars
bFixEnabled = true
bHUDSafeZone = true
bArFix = true
bArFix2 = true
bArFix3 = true

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

--INJECTION BEHAVIOUR
InjectDelay = 200
WriteInterval = 5000
SearchInterval = 1000
SuspendThread = true

--Name                         Manual/Auto/Hybrid  		Steam/Origin/Any                IncludeFile:Configure;Enable;Periodic;Disable;
SupportedVersions = { 		
{"Automatically Detect",       "Manual",  			"Any",	                         "Configure_SignatureScan;Enable_Inject;Periodic;Disable_Inject;"},
}

function Init_Controls()
	DefaultControls.AddHeader("Header_FOV","HUD safe zone adjustment",15,70,210,17)
	DefaultControls.AddFOVSlider("HUDWidthSlider","HUD_Width_SafeZone_Changed",55,100,125,35)		
	DefaultControls.AddFOVSlider("HUDHeightSlider","HUD_Height_SafeZone_Changed",55,160,125,35)		
	DefaultControls.AddHeader("Ind_fixes","Individual fixes",245,70,210,17)
	DefaultControls.AddFixToggle("CKARatio","Aspect ratio","CKARatio_Changed",250,95,180,14)
	DefaultControls.AddFixToggle("CKHUD","HUD safe zones","CKHUD_Changed",250,115,180,14)
end

function Configure_SignatureScan() 
	local tAddress = HackTool:AddAddress("HUDHeightFix")
	if HackTool:SignatureScan("488B88A08700000F28DC",tAddress,PAGE_EXECUTE_READ,0x00,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS))
	end		
	
	local tAddress = HackTool:AddAddress("HUDWidthFix")
	if HackTool:SignatureScan("488B88A087000048894D67",tAddress,PAGE_EXECUTE_READ,0x00,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS))
	end
	
	local tAddress = HackTool:AddAddress("ArFix")
	if HackTool:SignatureScan("0F2FB0????????0F92",tAddress,PAGE_EXECUTE_READ,0x00,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS))
	end
	
	local tAddress = HackTool:AddAddress("ArFix2")
	if HackTool:SignatureScan("C780DC00000000000000E86C",tAddress,PAGE_EXECUTE_READ,0x00,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS))
	end
	
	local tAddress = HackTool:AddAddress("ArFix3")
	if HackTool:SignatureScan("C780DC000000398EE33F",tAddress,PAGE_EXECUTE_READ,0x00,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS))
	end
	
	
	return true
end

function Enable_Inject()
	local Variables = HackTool:AllocateMemory("Variables",0)
	Variables:PushFloat("HUDRender_Width")
	Variables:PushFloat("HUDRender_Height")
	Variables:PushFloat("AspectRatio")
	Variables:PushFloat("AspectRatioZero")
	Variables:PushFloat("GameWidth")
	Variables:PushFloat("GameHeight")
	Variables:PushFloat("AspectRatioPointer")
	Variables:Allocate()

	ResolutionChanged()
	
	local asm = [[	
		(codecave:jmp)HUDWidthFix,HUDWidthFix_cc:
			mov rcx,[(allocation)Variables->HUDRender_Width]
			jmp %returnaddress%		
			%end%	
			
				;	MassEffectAndromeda.exe+11EE59A: 45 85 FF                 - test r15d,r15d
				;	MassEffectAndromeda.exe+11EE59D: 74 3C                    - je MassEffectAndromeda.exe+11EE5DB
				;	MassEffectAndromeda.exe+11EE59F: 48 8B 05 A2 82 0E 03     - mov rax,[MassEffectAndromeda.exe+42D6848]
				; ---------- INJECTING HERE ----------
				;	MassEffectAndromeda.exe+11EE5A6: 48 8B 88 A0 87 00 00     - mov rcx,[rax+000087A0]
				; ---------- DONE INJECTING  ----------
				;	MassEffectAndromeda.exe+11EE5AD: 48 89 4D 67              - mov [rbp+67],rcx
				;	MassEffectAndromeda.exe+11EE5B1: F3 0F 10 35 07 FC E7 01  - movss xmm6,[MassEffectAndromeda.exe+306E1C0]
				;	MassEffectAndromeda.exe+11EE5B9: F3 0F 5C 75 67           - subss xmm6,[rbp+67]
			
		(codecave:jmp)HUDHeightFix,HUDHeightFix_cc:
			mov rcx,[(allocation)Variables->HUDRender_Width]
			movss [(allocation)Variables->GameWidth],xmm4
			movss [(allocation)Variables->GameHeight],xmm0
			jmp %returnaddress%		
			%end%

				;	MassEffectAndromeda.exe+A824F8: F3 0F 10 35 C0 BC 5E 02        - movss xmm6,[MassEffectAndromeda.exe+306E1C0]
				;	MassEffectAndromeda.exe+A82500: 48 8B 05 41 43 85 03           - mov rax,[MassEffectAndromeda.exe+42D6848]
				;	MassEffectAndromeda.exe+A82507: 48 8B 3D 62 81 86 03           - mov rdi,[MassEffectAndromeda.exe+42EA670]
				; ---------- INJECTING HERE ----------
				;	MassEffectAndromeda.exe+A8250E: 48 8B 88 A0 87 00 00           - mov rcx,[rax+000087A0]
				; ---------- DONE INJECTING  ----------
				;	MassEffectAndromeda.exe+A82515: 0F 28 DC                       - movaps xmm3,xmm4
				;	MassEffectAndromeda.exe+A82518: 0F 28 D0                       - movaps xmm2,xmm0
				;	MassEffectAndromeda.exe+A8251B: 44 0F 28 CE                    - movaps xmm9,xmm6
			
		(codecave:jmp)ArFix,ArFix_cc:
			push rbx
			mov rbx,[(allocation)Variables->AspectRatioZero]
			cmp [(allocation)Variables->AspectRatioPointer],rbx
			jne ARFix
			mov [(allocation)Variables->AspectRatioPointer],rax
			
			ARFix:
			sub esp, 16										; allocate space on stack
			movdqu dqword [esp], xmm0						; push xmm0
			sub esp, 16										; allocate space on stack
			movdqu dqword [esp], xmm1						; push xmm1
			movss xmm0,[(allocation)Variables->GameWidth]
			movss xmm1,[(allocation)Variables->GameHeight]
			divss xmm0,xmm1									; calculate aspect ratio based on game window resolution
			movss [(allocation)Variables->AspectRatio],xmm0
			movdqu xmm1, dqword [esp]						; pop xmm1
			add esp, 16										; re-align stack
			movdqu xmm0, dqword [esp]						; pop xmm0
			add esp, 16										; re-align stack	
			mov rbx,[(allocation)Variables->AspectRatio]
			mov [rax+0x000000DC],rbx
			pop rbx
			comiss xmm6,[rax+0x000000DC]
			jmp %returnaddress%
			%end%
			
				;	MassEffectAndromeda.exe+F28B5E: 48 89 05 2B B9 4D 03     - mov [MassEffectAndromeda.exe+4404490],rax
				;	MassEffectAndromeda.exe+F28B65: 48 85 C0                 - test rax,rax
				;	MassEffectAndromeda.exe+F28B68: 74 1F                    - je MassEffectAndromeda.exe+F28B89
				; ---------- INJECTING HERE ----------
				;MassEffectAndromeda.exe+F28B6A: 0F 2F B0 DC 00 00 00     - comiss xmm6,[rax+000000DC]
				; ---------- DONE INJECTING  ----------
				;	MassEffectAndromeda.exe+F28B71: 0F 92 C1                 - setb cl
				;	MassEffectAndromeda.exe+F28B74: 3A 8B 56 F8 01 00        - cmp cl,[rbx+0001F856]
				;	MassEffectAndromeda.exe+F28B7A: 88 8B 56 F8 01 00        - mov [rbx+0001F856],cl
			
		(codecave)ArFix2,ArFix2_cc:
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop			
			%end%
			
				;	MassEffectAndromeda.exe+5BBA4F: 48 8B D3                       - mov rdx,rbx
				;	MassEffectAndromeda.exe+5BBA52: 48 8B CF                       - mov rcx,rdi
				; ---------- INJECTING HERE ----------
				;	MassEffectAndromeda.exe+5BBA55: C7 80 DC 00 00 00 00 00 00 00  - mov [rax+000000DC],00000000
				; ---------- DONE INJECTING  ----------
				;	MassEffectAndromeda.exe+5BBA5F: E8 6C 52 FF FF                 - call MassEffectAndromeda.exe+5B0CD0
				;	MassEffectAndromeda.exe+5BBA64: 48 85 C0                       - test rax,rax
			
		(codecave)ArFix3,ArFix3_cc:
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop
			nop			
			%end%
			
				;	MassEffectAndromeda.exe+5BB9B7: 48 8B 0D E2 09 AF 03           - mov rcx,[MassEffectAndromeda.exe+40AC3A0]
				;	MassEffectAndromeda.exe+5BB9BE: E8 ED B9 CE FF                 - call MassEffectAndromeda.ANTITAMPER_TESTVALUE_CODEMARKER+2120
				; ---------- INJECTING HERE ----------
				;	MassEffectAndromeda.exe+5BB9C3: C7 80 DC 00 00 00 39 8E E3 3F  - mov [rax+000000DC],3FE38E39  (16/9 ratio)
				; ---------- DONE INJECTING  ----------
				;	MassEffectAndromeda.exe+5BB9CD: 48 8B 0D DC 0C D1 03           - mov rcx,[MassEffectAndromeda.exe+42CC6B0]
				;	MassEffectAndromeda.exe+5BB9D4: 48 8B D3                       - mov rdx,rbx
			
	]]

	if HackTool:CompileAssembly(asm,"HUDWidthFix") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("HUDWidthFix_cc",bHUDSafeZone)
		Toggle_CodeCave("HUDHeightFix_cc",bHUDSafeZone)
		Toggle_CodeCave("ArFix_cc",bArFix)
		Toggle_CodeCave("ArFix2_cc",bArFix2)
		Toggle_CodeCave("ArFix3_cc",bArFix3)
	end
end

function Periodic()
	local Variables = HackTool:GetAllocation("Variables")
	local HUD_Width_Safe_Zone = (fAdditionalHUDWidth + 75) / 100
	local HUD_Height_Safe_Zone = (fAdditionalHUDHeight + 80) / 100

	if Variables and Variables["HUDRender_Width"] and Variables["HUDRender_Height"] then
		Variables["HUDRender_Width"]:WriteFloat( HUD_Width_Safe_Zone )
		Variables["HUDRender_Height"]:WriteFloat( HUD_Height_Safe_Zone )
	end
end

function Disable_Inject()
	CleanUp()	
end

function ResolutionChanged() 
end

function HUDSafeZone()
end

function HK_IncreaseFOV()	
	HUDWidthSlider:OffsetPosition(1)
end

function HK_DecreaseFOV()	
	HUDWidthSlider:OffsetPosition(-1)
end

function HUD_Width_SafeZone_Changed(Sender)
	fAdditionalHUDWidth = Sender:GetScaledFloat(4)	
	fAdditionalHUDWidth = Round(fAdditionalHUDWidth,2)
	lblHUDWidthSlider.Caption:SetCaption( string.format("HUD width safe zone : %.2f",(fAdditionalHUDWidth + 75)))
	
	ForceUpdate()	
end

function HUD_Height_SafeZone_Changed(Sender)
	fAdditionalHUDHeight = Sender:GetScaledFloat(6)	
	fAdditionalHUDHeight = Sender:GetScaledFloat(5)	
	fAdditionalHUDHeight = Round(fAdditionalHUDHeight,2)
	lblHUDHeightSlider.Caption:SetCaption( string.format("HUD height safe zone : %.2f",fAdditionalHUDHeight + 80))
	
	ForceUpdate()	
end

function CKARatio_Changed(Sender)
	local Variables = HackTool:GetAllocation("Variables")
	bArFix = Toggle_CheckFix(Sender)
	bArFix2 = Toggle_CheckFix(Sender)
	bArFix3 = Toggle_CheckFix(Sender)
	Toggle_CodeCave("ArFix_cc",bArFix)
	Toggle_CodeCave("ArFix2_cc",bArFix2)
	Toggle_CodeCave("ArFix3_cc",bArFix3)
end

function CKHUD_Changed(Sender)
	bHUDSafeZone = Toggle_CheckFix(Sender)
	Toggle_CodeCave("HUDWidthFix_cc",bHUDSafeZone)
	Toggle_CodeCave("HUDHeightFix_cc",bHUDSafeZone)
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end