///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_Platform_Core.cpp
// Author : Kevin Floyer-Lea
// Date   : 21-Jun-2005
//
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

/******************************************************************************/
/* Included Files                                                             */
/******************************************************************************/

#include "Core/Asura.h"

#include "Core/Asura_Core.h"

// Asura Platform-independent
#include "Core/Memory/Asura_Mem.h"

#if defined ASURA_USE_PROFILING_SYSTEM
	#include "Debugging/Asura_CPUProfile_Renderer.h"
	#include "Debugging/Asura_Platform_GPUProfile.h"
#endif

#if defined( ASURA_SUPPORT_MULTITHREADED_JOB_QUEUE)
	#include "Jobs/Asura_Job_Manager.h"
#endif

// Misc
#include <time.h>
#include <xbdm.h>
#include <xtl.h>

/******************************************************************************/
/* Global Variables                                                           */
/******************************************************************************/

static DWORD g_ulMainThreadID = 0;

/******************************************************************************/
/* Asura_Core Static Class Functions                                          */
/******************************************************************************/

void Asura_Core::PlatformInitialise()
{
	// Note that the memory manager tries to create a log file before
	// this is called, so early memory allocations won't be logged

#if defined ASURA_LOGFILES_ENABLED
	DmMapDevkitDrive();
#endif

	// init the RNG with a seed using time()
	Asura_Random::SetSeed(static_cast<u_int>(time(NULL)));

#if defined(_DEBUG)
	// Hack to enable Xbox360 builds to run in Debug...
	D3D__DisableConstantOverwriteCheck	= true;
#endif	//defined(_DEBUG)

g_ulMainThreadID = GetCurrentThreadId();

#if defined ASURA_USE_PROFILING_SYSTEM
	Asura_CPUProfile_Renderer::Initialise();
	Asura_GPUProfile_Renderer::Initialise();
#endif

#if defined( ASURA_SUPPORT_MULTITHREADED_JOB_QUEUE)
	Asura_Job_Manager::Initialise();
#endif
	
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	// must call XOnlineStartup before calling any Xbox functions, explicitly mentioned in docs for 
	// XShowSigninUI (although that seems to work regardless). Invitations don't seem to work.
	// XOnlineStartup calls XNetStartup and WSAStartup internally
	// However, if we want to bypass secure networking we have to do that first.
#ifdef ASURA_XBOX360_BYPASS_NETWORK_SECURITY
	// must be done before XOnlineStartup()
	XNetStartupParams xXNSP;
	Asura_Mem::Set(&xXNSP, 0, sizeof(xXNSP));
	xXNSP.cfgSizeOfStruct = sizeof(XNetStartupParams);
	xXNSP.cfgFlags = XNET_STARTUP_BYPASS_SECURITY;

#if defined(ASURA_ASSERT)
	const INT iResult =
#endif
		XNetStartup(&xXNSP);
	Asura_Assertf( iResult == ERROR_SUCCESS, ("Error %d from XNetStartup", iResult));
#endif // #ifdef ASURA_XBOX360_BYPASS_NETWORK_SECURITY

#if defined(ASURA_ASSERT)
	const DWORD uResult = 
#endif	//defined(ASURA_ASSERT)
		XOnlineStartup();
	Asura_Assertf( uResult == ERROR_SUCCESS, ("Error %u from XOnlineStartup", uResult));
#endif // #ifdef ASURA_SUPPORT_EXTERNAL_NETWORK

}

void Asura_Core::PlatformShutdown()
{
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
#if defined(ASURA_ASSERT)
	int iResult = 
#endif
		XOnlineCleanup();
	Asura_Assertf( iResult == NO_ERROR, ("Error %d from XOnlineCleanup", iResult));
#endif // #ifdef ASURA_SUPPORT_EXTERNAL_NETWORK

#if defined( ASURA_SUPPORT_MULTITHREADED_JOB_QUEUE)
	Asura_Job_Manager::Shutdown();
#endif
#if defined ASURA_USE_PROFILING_SYSTEM
	Asura_GPUProfile_Renderer::Shutdown();
	Asura_CPUProfile_Renderer::Shutdown();
#endif
}

void Asura_Core::PlatformReset()
{
}

bool Asura_Core::Platform_IsMainThread()
{
	return GetCurrentThreadId() == g_ulMainThreadID;
}
