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

--GAME VARS
fDefaultAspectRatio = 1.777777777
fDefaultAspectScale = 3

--ControlVars
bFixEnabled = true
bAspectFix = true
bMoviesFix = true

--PROCESS VARS
Process_FriendlyName = Module:GetFriendlyName()
Process_WindowName = "Wolfenstein%R: The New Order"
Process_ClassName = "Wolfenstein The New Order"
Process_EXEName = "WolfNewOrder_x64.exe"

--INJECTION BEHAVIOUR
InjectDelay = 1500
WriteInterval = 100
SearchInterval = 500
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("CKAspectFix_Enable","Aspect Fix","CKAspectFix_Changed",255,101,180,14)
	DefaultControls.AddFixToggle("CKMoviesFix_Enable","Movies Fix","CKMoviesFix_Changed",255,120,180,14)
end


function Configure_SignatureScan() 

	local tAddress = HackTool:AddAddress("AspectFix")
	if HackTool:SignatureScan("F3 0F 5E ?? 75 ?? 0F 2F ?? 76 ?? 0F 28 ?? EB ?? F3",tAddress,PAGE_EXECUTE_READ,0x10,Process_EXEName) == 0 then	
		return ErrorOccurred(string.format(SigScanError,tAddress:GetName()))
	else
		print( tAddress:GetInfo(TYPE_ADDRESS) )
		
		local OriginalAddress = HackTool:AddAddress("OriginalAspect",tAddress)
		OriginalAddress:AcquireAddress(0x4)
		print( OriginalAddress:GetInfo(TYPE_FLOAT) )
	end	
	
	local tAddress = HackTool:AddAddress("MoviesFix")
	if HackTool:SignatureScan("D1 ?? 85 ?? 0F 4F ?? 33 ?? 8B ?? E8",tAddress,PAGE_EXECUTE_READ,0xB,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("AspectScale")
	Variables:PushInt("Movies_Left")
	Variables:PushInt("Movies_Width")
	Variables:PushInt("Movies_Height")
	
	Variables:Allocate()	
	
	ResolutionChanged()

	local asm = [[	
	
		(codecave:jmp)MoviesFix,MoviesFix_cc:
			mov ecx, [(allocation)Variables->Movies_Left]
			mov r8d, [(allocation)Variables->Movies_Width]
			mov r9d, [(allocation)Variables->Movies_Height]
			%originalcode%
			jmp %returnaddress%
			%end%
			
		(codecave)AspectFix,AspectFix_cc:
			$$0 $$1, [(allocation)Variables->AspectScale]
			%end%
	]]
	
	if HackTool:CompileAssembly(asm,"Fixes") == nil then
		return ErrorOccurred("Assembly compilation failed...")
	else
		Toggle_CodeCave("AspectFix_cc",bAspectFix)
		Toggle_CodeCave("MoviesFix_cc",bMoviesFix)
	end		

end

function Periodic()

	local OriginalAspect = HackTool:GetAddress("OriginalAspect")
	local Variables = HackTool:GetAllocation("Variables")
	if OriginalAspect and Variables then
	
		if bAspectFix then
			PluginViewport:AppendStatusMessage( string.format("\r\n     (AspectRatio) Default=%.2f, New=%.2f",
												OriginalAspect:ReadFloat(),Variables["AspectScale"]:ReadFloat()) )		
		end

		if bMoviesFix then
			PluginViewport:AppendStatusMessage( string.format("\r\n     (Movies) Left=%d, Width=%d, Height=%d",
												Variables["Movies_Left"]:ReadInt(),Variables["Movies_Width"]:ReadInt(),Variables["Movies_Height"]:ReadInt()) )		
		end
		
	end

end

function Disable_Inject()

	CleanUp()

end


function ResolutionChanged() 

	local Variables = HackTool:GetAllocation("Variables")
	if Variables then
		Variables["Movies_Left"]:WriteInt(DisplayInfo:GetiOffsetX())
		Variables["Movies_Width"]:WriteInt(DisplayInfo:GetiOffsetWidth())
		Variables["Movies_Height"]:WriteInt(DisplayInfo:GetiOffsetHeight())
		Variables["AspectScale"]:WriteFloat((DisplayInfo:GetAspectRatio() / fDefaultAspectRatio) * fDefaultAspectScale)
	end
	
end

function CKAspectFix_Changed(Sender)

	bAspectFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("AspectFix_cc",bAspectFix)
	ForceUpdate()		
	
end

function CKMoviesFix_Changed(Sender)

	bMoviesFix = Toggle_CheckFix(Sender)
	Toggle_CodeCave("MoviesFix_cc",bMoviesFix)
	ForceUpdate()		
	
end

function Init()	
	Init_BaseControls()
	Init_Controls()
end

function DeInit()
	DisableFix()
end