//////////////////////////////////////////////////////////////////////////////
//
// Asura_PS2_NetGui.cpp
//
// Copyright (c) 2004 Rebellion. All rights reserved.
//
// Description:
//
//     interface for the NetGui spawning process
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

// this file's header
#include "Network/Asura_PS2_NetGui.h"


// Stdlib independent.
#include <string.h>

// Stdlib platform-specific.
#include <eekernel.h>
#include <libcdvd.h>
#include <sifcmd.h>					// For sceSifExitCmd().
#include <libpad.h>					// For scePadEnd().

// Engine independent
#include "Core/Asura_Core.h"
#include "Core/Asura_String.h"		// for asura_printftobfr
#include "Debugging/Asura_Logfile.h"

// Engine platform-specific.
#include "Core/Asura_PS2_OS.h"
#include "FileAccess/Asura_PS2_TOCCache.h"
#include "Rendering/Asura_PS2_DeferredRendering.h"
#include "Rendering/Asura_PS2_GS.h"

// middleware
#include "Sony/multistream/ee/sound.h"

void Asura_PS2_NetGui::LaunchNetGui(const char *szFilename, int argc, const char *argv, bool bForceHost0 /* = false */)
{
#ifdef ASURA_PS2_CDEMUL
    int iErr;
	int iResult;

	const char* szFullpath = Asura_PS2_OS::GetFullPath(szFilename, true);

	static char szExecutableName[32];
	Asura_PrintfToBfr(szExecutableName, "cdrom0:\\%s;1", 32, true, Asura_PS2_TOCCache::GetExecutableName());
	// construct argv
	static char *netguiArgv[] = 
	{ 
		szExecutableName, 
		"", 
		"" 
	};

	// wait for DMA
	Asura_PS2_DeferredRendering::WaitForDeferredRendering();
	Asura_PS2_BetterGsSyncPath(0,0);

	// Shutdown the poweroff thread
#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
	Asura_PS2_OS::UndoPowerOff();
#endif

	// shutdown multistream therad
	SOUND_CloseFastLoad();

	sceCdInit(SCECdEXIT);

	scePadEnd();

	Asura_OutputToDebugger_Retail(("calling LoadExecPS2 with arg %s\n", szFullpath));

	// put printf before this as it uses SIF to send text to IOP
	sceSifExitCmd();

	LoadExecPS2(szFullpath, sizeof(netguiArgv)/sizeof(char *), netguiArgv);

#else
	// netgui needs to load from cd, so do nothing here
#endif
}
