
#include "stdafx.h"
//#include <stdio.h>
#include "util.h"
#include "GlobalData.h"
#include "file.h"
#include "Process.h"
#include "Threads.h"
#include "scripts.h"


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


tdeStatus fn_e_TASK_Binarize( CByteArray &a_cSelectedLevels )
{
	if( ! fn_bCheckFileExistanceInStartupDir( "crb.exe" ) )
		return STATUS_C_Error;

	// Reset list of binarized levels
	for( int i=g_stTheGlobalData.a_cBinarizedLevels.GetSize()-1; i>=0; i-- )
	{
		g_stTheGlobalData.a_cBinarizedLevels.SetAt( i, 0 );
	}

	// Before binarizing, we disable BigFile for reloc tables,
	// because user must regenerate it after binarizing
	fn_vDisableRelocTableBigFile();

	// Start binarization !
	tdeStatus eStatus = fn_eSpawnThreadAndWaitWithInputEvents( fn_uiThreadBinarize, &a_cSelectedLevels );


	if( eStatus == STATUS_C_OK )
	{
		// If no error, update startprg.ini
	//			fn_vUpdateStartPrg();
		fn_vUpdateStartPrg_old();
	}

	return eStatus;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


tdeStatus fn_e_TASK_TestBinarizedMaps( CByteArray &a_cBinarizedLevels )
{
	CString       csOutput, csSequence;
	BOOL          bAllMaps;
	int           iSequenceNumber, iSizeOfSequence;
	int           iNumberOfMap=0, iFirstMap=-1;
	CString       csFileName = g_stTheGlobalData.csMainDirectory + "\\Test.ini";
	tdeStatus     eStatus;

	fn_vOutputLine( "Testing levels...\r\n" );

	bAllMaps = TRUE;
	iSequenceNumber = 1;
	iSizeOfSequence = 0;
	csSequence = "SEQUENCE1=";
	for( int i=0; i<a_cBinarizedLevels.GetSize(); i++ )
	{
		if( a_cBinarizedLevels[i] != 0 )
		{
			iNumberOfMap++;
			if( iFirstMap == -1 )
			{
				// Get first map number.
				iFirstMap = i;

				csOutput = "\t" + g_stTheGlobalData.a_csLevelNames[i] + "\r\n";
				fn_vOutputLine( csOutput );
			}
			else
			{
				// Insert level into a sequence (except the first one)
				if( iSizeOfSequence > 250 )
				{
					char szSeq[5];
					iSequenceNumber ++;
					iSizeOfSequence = 0;
					sprintf( szSeq, "%i", iSequenceNumber );
					csSequence += "\nSEQUENCE";
					csSequence += szSeq;
					csSequence += "=";
				}

				csSequence += g_stTheGlobalData.a_csLevelNames[i] + " ";
				iSizeOfSequence += g_stTheGlobalData.a_csLevelNames[i].GetLength() + 1;

				csOutput = "\t" + g_stTheGlobalData.a_csLevelNames[i] + "\r\n";
				fn_vOutputLine( csOutput );
			}
		}
		else
			bAllMaps = FALSE;
	}
	csSequence += "\n";

	// if nothing to do...
	if( iNumberOfMap == 0 )
		return STATUS_C_OK;

	// if there is only one map, no test.ini
	if( iNumberOfMap > 1 )
	{
		CStdioFile    fTestIni;

		// Generate Test.ini
		if( ! fTestIni.Open( csFileName, CFile::modeWrite | CFile::modeCreate ) )
		{
			csOutput = "\tError: Can't create '" + csFileName + "'";
			fn_vOutputLine( csOutput );
			return STATUS_C_Error;
		}

		if( bAllMaps )
			fTestIni.WriteString( "[TEST]\nMODE=ALLMAPSONLY\nWAITMIN=35\nWAITMAX=45\nWITHRESTART=0\nSTAT=1\n" );
		else
		{
			fTestIni.WriteString( "[TEST]\nMODE=SEQUENCE\nWAITMIN=35\nWAITMAX=45\nWITHRESTART=0\nSTAT=1\n" );
			fTestIni.WriteString( csSequence );
		}

		fTestIni.Close();
	}

	char szExe[512];
	char szCommandLine[128];
	
	sprintf( szExe, "%s\\%s", g_stTheGlobalData.csMainDirectory, g_stTheGlobalData.csExeName );
	sprintf( szCommandLine, "%s -level:%s", szExe, g_stTheGlobalData.a_csLevelNames[iFirstMap] );

	eStatus = fn_eSpawnAndWaitForProcessWithInputEvents( szExe, szCommandLine );

	if( iNumberOfMap > 1 )
		DeleteFile( csFileName );

	if( eStatus == STATUS_C_OK )
	{
		fn_vOutputLine( "\tTest is sucessful !\r\n\r\n" );
	}
	else
	{
		fn_vOutputLine( "\r\n" );
	}

	return eStatus;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


tdeStatus fn_e_TASK_Version()
{
	char       szExeName[512];
	char       szCommandLine[1024];
	int        i;
	tdeStatus  eStatus = STATUS_C_OK;

	fn_vOutputLine( "\r\n* * Making version * * * * * * * * * * * * * * * \r\n" );

	if(    ! fn_bCheckFileExistanceInStartupDir( "rm2sndparser.exe" )
	    || ! fn_bCheckFileExistanceInStartupDir( "convimg.exe" )
	    || ! fn_bCheckFileExistanceInStartupDir( "concat.exe" )
	    || ! fn_bCheckFileExistanceInStartupDir( "crb.exe" ) )
		return STATUS_C_Error;

	if( ! fn_bValidateMainDirectorySetting() )
		return STATUS_C_Error;

	if( ! fn_bValidateBinDirectorySetting() )
		return STATUS_C_Error;

	if( ! fn_bValidateGameExeSetting() )
		return STATUS_C_Error;

	// Select all levels for binarization.
	int iSize   = g_stTheGlobalData.a_csLevelNames.GetSize();
	for( i=0; i<iSize; i++ )
	{
		g_stTheGlobalData.a_cLevelsSelectedForBinarization.SetAt( i, 1 );
	}

	// Now it's time to make the sound banks.
	fn_vOutputLine( "Making sound banks...\r\n" );
	sprintf( szExeName, "%s\\rm2SndParser.exe", (LPCTSTR)g_stTheGlobalData.csStartupDirectory );

	fn_vMakeSndParserCommandLine( szCommandLine );
	strcat( szCommandLine, " -ParseAllMaps -ParseFix -GenerateBanks" );

	eStatus = fn_eSpawnAndWaitForProcessWithInputEvents( szExeName, szCommandLine );
	if( eStatus != STATUS_C_OK )
		return eStatus;

	// Then, we copy the sound directory
	eStatus = fn_eSpawnThreadAndWaitWithInputEvents( fn_uiThreadCopySoundDirectory, NULL );

	if( eStatus != STATUS_C_OK )
		return eStatus;

	// Texture time...
	eStatus = fn_eSpawnThreadAndWaitWithInputEvents( 
			fn_uiThreadMakeBigFileTextures, 
			(void *)(LPCTSTR)g_stTheGlobalData.csTempTextureDirectory );

	if( eStatus != STATUS_C_OK )
		return eStatus;


	// Now we binarize all levels.
	eStatus = fn_e_TASK_Binarize( g_stTheGlobalData.a_cLevelsSelectedForBinarization );
	if( eStatus != STATUS_C_OK )
		return eStatus;

	// Now we test the binarized maps.
	if( g_stTheGlobalData.bAutoTestMaps )
	{
		fn_e_TASK_TestBinarizedMaps( g_stTheGlobalData.a_cBinarizedLevels );
	}

	return STATUS_C_OK;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

