///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_Streaming_HandlerReplacement.cpp
// Author : Martin G. Harris
// Date   : 23-May-2006
//
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

// RJT NOTE: Required for GCC builds as the file list generation doesn't account for VC project exclusion from build
#ifdef ASURA_USE_STREAMING

#include "Core/Asura.h"
#include "Streaming/Asura_Streaming_HandlerReplacement.h"

#include "AI/Asura_AI_Navigation.h"
#include "Animation/Asura_Animation.h"
#include "Cutscene/Asura_Cutscene_System.h"
#include "Entity/Asura_StaticMessage_System.h"
#include "Mesh/Asura_Env_System.h"
#include "Rendering/Asura_Module.h"
#include "Sound/Asura_MetaMusic_System.h"

////////////////////////////////////////
// Asura_Streaming_HandlerReplacement //
////////////////////////////////////////
// Replace all relevant handlers, which means most of them.
Asura_Streaming_HandlerReplacement::Asura_Streaming_HandlerReplacement(const Asura_Hash_ID uZoneHash,
																	   const Asura_Hash_ID uContainingZoneHash)

: m_pxAINavHandler(&Asura_AI_Navigation_System::GetCurrentHandler())
{
	Asura_Assert(uZoneHash != ASURA_HASH_ID_UNSET, "Unset master zone");
	//Asura_Assert(uContainingZoneHash != ASURA_HASH_ID_UNSET, "Unknown containing zone. AI navigation not set.");

	// Set the new handlers...
	if (uContainingZoneHash != ASURA_HASH_ID_UNSET)
	{
		Asura_AI_Navigation_System::SetCurrentHandler(uContainingZoneHash);
		if (&Asura_AI_Navigation_System::GetCurrentHandler() == 0)
		{
			//Asura_Assert(0, "Unloaded containing zone. AI navigation not set.");
			Asura_AI_Navigation_System::SetCurrentHandler(m_pxAINavHandler);
		}
	}
}

Asura_Streaming_HandlerReplacement::~Asura_Streaming_HandlerReplacement()
{
	// Restore the old handlers.
	Asura_AI_Navigation_System::SetCurrentHandler(m_pxAINavHandler);
}

///////////////////////////////////////////////
// Asura_Streaming_TexAnimHandlerReplacement //
///////////////////////////////////////////////
// Replace texture animation handler.
Asura_Streaming_TexAnimHandlerReplacement::Asura_Streaming_TexAnimHandlerReplacement(const Asura_Hash_ID uZoneHash)
: m_pxTexAnimHandler(&Asura_TextureAnimation_System::GetCurrentHandler())
{
	Asura_Assert(uZoneHash != ASURA_HASH_ID_UNSET, "Unset zone");
	if (uZoneHash != ASURA_HASH_ID_UNSET)
	{
		Asura_TextureAnimation_System::SetCurrentHandler(uZoneHash);
		if (&Asura_TextureAnimation_System::GetCurrentHandler() == 0)
		{
			Asura_Assert(0, "Failed to set texture animation handler");
			// Restore the old handler.
			Asura_TextureAnimation_System::SetCurrentHandler(m_pxTexAnimHandler);
		}
	}
}

Asura_Streaming_TexAnimHandlerReplacement::~Asura_Streaming_TexAnimHandlerReplacement()
{
	Asura_TextureAnimation_System::SetCurrentHandler(m_pxTexAnimHandler);
}

//////////////////////////////////////////////
// Asura_Streaming_ModuleHandlerReplacement //
//////////////////////////////////////////////
// Replace only the module and environment handlers.
Asura_Streaming_ModuleHandlerReplacement::Asura_Streaming_ModuleHandlerReplacement()
: m_pxOldModuleHandler(&Asura_Module_System::GetCurrentHandler())
, m_pxOldEnvironmentHandler(&Asura_Environment_System::GetCurrentEnvironment())
{
}

Asura_Streaming_ModuleHandlerReplacement::~Asura_Streaming_ModuleHandlerReplacement()
{
	Asura_Module_System::SetCurrentHandler(m_pxOldModuleHandler);
	Asura_Environment_System::SetCurrentEnvironment(m_pxOldEnvironmentHandler);
}

bool Asura_Streaming_ModuleHandlerReplacement::IsLoaded()
{
	// Are both the module and environment handlers loaded?
	return (&Asura_Module_System::GetCurrentHandler() != 0 &&
			&Asura_Environment_System::GetCurrentEnvironment() != 0);
}

// Set module and environment handlers from zone hash.
void Asura_Streaming_ModuleHandlerReplacement::SetCurrent(const Asura_Hash_ID uContainingZoneHash)
{
	Asura_Module_System::SetCurrentHandler(uContainingZoneHash);
	Asura_Environment_System::SetCurrentEnvironment(uContainingZoneHash);
}

#endif	// ASURA_USE_STREAMING
