
#include "stdafx.h"
#include <io.h>
#include "util.h"
#include "file.h"
#include "process.h"
#include "globaldata.h"

UINT fn_uiThreadMakeGFTextures( LPVOID _p_vParam )
{
	CStringArray          a_csSourceDirectories;
	CStringArray          a_csDestDirectories;
	CString               csTmp, csTmp2;
	CString               csOutput;
	BOOL                  bKeepDisplayResults = FALSE;
	tdeStatus             eStatus;

	_p_vParam = _p_vParam;

	fn_vOutputLine( "Making gf textures...\r\n" );

	if( ! fn_bCheckFileExistanceInStartupDir( "convimg.exe" ) )
		AfxEndThread( STATUS_C_Error );

	// Build array of dirs to execute convimg on.
	a_csSourceDirectories.SetSize( 0, 5 );
	a_csDestDirectories.SetSize( 0, 5 );

	// First, the vignettes.
	a_csSourceDirectories.Add( "\\Gamedata\\Vignette" );
	a_csDestDirectories.Add( "\\Vignette" );

	// Then, specific textures (starting from last dir to first dir in version.ini)
	for( int i=g_stTheGlobalData.a_csVersionDirectories.GetSize()-1; i>=0; i-- )
	{
		csTmp = "\\" + g_stTheGlobalData.a_csVersionDirectories[i] + "\\World\\Graphics\\Textures";
		a_csSourceDirectories.Add( csTmp );

		a_csDestDirectories.Add( "\\World\\Graphics\\Textures" );
	}

	// Then, common textures.
	// Common textures will be copied AFTER, so they will be chosen BEFORE specific textures.
	a_csSourceDirectories.Add( "\\Gamedata\\World\\Graphics\\Textures" );
	a_csDestDirectories.Add( "\\World\\Graphics\\Textures" );

	char szComvImgExeName[512];
	char szComvImgCommandLine[512];

	sprintf( szComvImgExeName, "%s\\ConvImg.exe", (const char *)g_stTheGlobalData.csStartupDirectory );

	for( i=0; i<a_csSourceDirectories.GetSize(); i++ )
	{
		csTmp  = g_stTheGlobalData.csMainDirectory + a_csSourceDirectories[i];
		csTmp2 = g_stTheGlobalData.csBinDirectory + a_csDestDirectories[i];

		sprintf( 
		    szComvImgCommandLine, 
		    "ConvImg.exe -SrcPath:%s -DstPath:%s -BMP -TGA", 
		    (const char *)csTmp,
		    (const char *)csTmp );

		csOutput = "\tConverting '" + a_csSourceDirectories[i] + "'\r\n";
		fn_vOutputLine( csOutput );

		// We make the gf textures in the source directory (for engine)
		if( (eStatus = fn_eSpawnAndWaitForProcessWithInputEvents( szComvImgExeName, szComvImgCommandLine )) != STATUS_C_OK )
			AfxEndThread( eStatus );

		// Now, we copy them to bin directory
		csOutput = "\tCopying '" + a_csSourceDirectories[i] + "' to bin directory\r\n";
		fn_vOutputLine( csOutput );
		csTmp += "\\*.gf";
		if( (eStatus = fn_eRecursiveFileCopy( csTmp, csTmp2 )) != STATUS_C_OK )
			AfxEndThread( eStatus );

	} /* End loop on a_csSourceDirectories */

	fn_vOutputLine( "\tTexture copy is successful !\r\n\r\n" );

	AfxEndThread( (UINT)STATUS_C_OK );
	return STATUS_C_OK;
}


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

tdeStatus fn_eEditGameDscForBigFile()
{
	CString         csFileName;
	CString         csLine;
	CString         csOutput;
	CStdioFile      fGameDsc;
	DWORD           dwCurrentSeek;
	BOOL            bFound;
	CString         csLineToFind( "Textures(\"\\Textures.cnt\")" );
	CString         csEditYourself( "\tYou'll have to edit game.dsc for yourself...\r\n" );

	fn_vOutputLine( "\tEditing game.dsc\r\n" );

	csFileName = g_stTheGlobalData.csMainDirectory + "\\Gamedata\\Game.dsc";
	if( ! fGameDsc.Open( csFileName, CFile::modeReadWrite ) )
	{
		AfxMessageBox( "Unable to open game.dsc !", MB_OK | MB_ICONSTOP );
		fn_vOutputLine( "\tUnable to open game.dsc !\r\n" );
		fn_vOutputLine( csEditYourself );
		return STATUS_C_Error;
	}

	bFound = FALSE;

	while( !bFound )
	{
		dwCurrentSeek = fGameDsc.GetPosition();

		if( ! fGameDsc.ReadString( csLine ) )
		{
			csOutput = "\tThe line " + csLineToFind + " was not found in game.dsc.\r\n";
			fn_vOutputLine( csOutput );
			fn_vOutputLine( csEditYourself );
			return STATUS_C_Error;
		}

		if( csLine.Find( csLineToFind ) != -1 )
		{
			int iFind = csLine.Find( ';' );
			if( iFind != -1 )
			{
				csLine.SetAt( iFind, ' ' );
				fGameDsc.Seek( dwCurrentSeek, CFile::begin );
				fGameDsc.WriteString( csLine );
			}
			bFound = TRUE;
		}
		
	} // End while
	
	fGameDsc.Close();
	return STATUS_C_OK;
}


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


UINT fn_uiThreadMakeBigFileTextures( LPVOID _p_vParam )
{
	CString               csSourceDirectory;
	CString               csDestDirectory;
	CString               csTmp, csTmp2;
	CString               csOutput;
	char                  szExeName[512];
	char                  szCommandLine[512];
	tdeStatus             eStatus;

	fn_vOutputLine( "Making gf textures bigfile...\r\n" );

	if(    ! fn_bCheckFileExistanceInStartupDir( "convimg.exe" )
	    || ! fn_bCheckFileExistanceInStartupDir( "concat.exe" ) )
		AfxEndThread( (UINT)STATUS_C_Error );

	csDestDirectory = (LPCTSTR)_p_vParam;

	// Get and test temp directory
	int iLength = csDestDirectory.GetLength() - 1;
	if( iLength > 0 && csDestDirectory[iLength] == '\\' )
		csDestDirectory.SetAt( iLength, 0 );

	if( _access( csDestDirectory, 06 ) == -1 )
	{
		csOutput = "Error: Temp directory is not valid !\n(The directory '" + csDestDirectory + "' is not accessible.)";
		AfxMessageBox( csOutput, MB_OK | MB_ICONSTOP );
		fn_vOutputLine( "\tInvalid temp directory\r\n" );
		AfxEndThread( STATUS_C_Error );
	}

	csDestDirectory += "\\textures";

	fn_vOutputLine( "\tDeleting previous textures in temp directory\r\n" );
	csTmp = csDestDirectory + "\\*.*";
	if( (eStatus = fn_eDeleteFileOrDirectory( csTmp )) == STATUS_C_Error )
	{
		fn_vOutputLine( "\tUnable to clean temp directory\r\n" );
		AfxEndThread( STATUS_C_Error );
	}
	if( eStatus == STATUS_C_UserBreak )
		AfxEndThread( eStatus );

	// First, convert and copy Gamedata\Vignette
	fn_vOutputLine( "\tConverting Vignettes\r\n" );
	sprintf( szExeName, "%s\\ConvImg.exe", (LPCTSTR)g_stTheGlobalData.csStartupDirectory );
	csTmp = g_stTheGlobalData.csMainDirectory + "\\Gamedata\\Vignette";
	sprintf( 
		  szCommandLine, 
		  "ConvImg.exe -SrcPath:%s -DstPath:%s -BMP -TGA", 
		  (LPCTSTR)csTmp,
		  (LPCTSTR)csTmp );

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

	csTmp += "\\*.gf";
	csTmp2 = g_stTheGlobalData.csBinDirectory + "\\Vignette";
	if( (eStatus = fn_eRecursiveFileCopy( csTmp, csTmp2 )) != STATUS_C_OK )
		AfxEndThread( eStatus );

	// Then convert all textures directories
	for( int i=g_stTheGlobalData.a_csVersionDirectories.GetSize()-1; i>=0; i-- )
	{
		csOutput = "\tConverting textures from '" + g_stTheGlobalData.a_csVersionDirectories[i] + "'\r\n";
		fn_vOutputLine( csOutput );

		csTmp = g_stTheGlobalData.csMainDirectory + "\\" + g_stTheGlobalData.a_csVersionDirectories[i] + "\\World\\Graphics\\Textures";
		sprintf( 
				szCommandLine, 
				"ConvImg.exe -SrcPath:%s -DstPath:%s -BMP -TGA", 
				(LPCTSTR)csTmp,
				(LPCTSTR)csDestDirectory );

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


	// Then, execute concat to make the bigfile.
	fn_vOutputLine( "\tMaking file Gamedata\\Textures.cnt\r\n" );
	sprintf( szExeName, "%s\\Concat.exe", (const char *)g_stTheGlobalData.csStartupDirectory );

	csTmp = g_stTheGlobalData.csMainDirectory + "\\GameData\\Textures.cnt";
	sprintf( 
			szCommandLine, 
			"Concat.exe -SrcPath:%s -DstFile:%s -SrcFlt:*.gf -Check -Xor", 
			(LPCTSTR)csDestDirectory,
			(LPCTSTR)csTmp );
	if( (eStatus = fn_eSpawnAndWaitForProcessWithInputEvents( szExeName, szCommandLine )) != STATUS_C_OK )
		AfxEndThread( eStatus );


	// Then, copy the bigfile into bin directory
	fn_vOutputLine( "\tCopying Bigfile to bin directory\r\n" );
	if( (eStatus = fn_eCopyFileOrDirectory( csTmp, g_stTheGlobalData.csBinDirectory )) != STATUS_C_OK )
		AfxEndThread( eStatus );

	// Clean the temporary directory.
	fn_vOutputLine( "\tCleaning temp directory\r\n" );
	csTmp = csDestDirectory + "\\*.*";
	if( (eStatus = fn_eDeleteFileOrDirectory( csTmp )) == STATUS_C_Error )
	{
		fn_vOutputLine( "\tUnable to clean up temp directory\r\n" );
		AfxEndThread( STATUS_C_Error );
	}
	if( eStatus == STATUS_C_UserBreak )
		AfxEndThread( eStatus );

	// At least, edit game.dsc to allow engine to load textures from bigfile.
	if( (eStatus = fn_eEditGameDscForBigFile()) == STATUS_C_Error )
		AfxEndThread( eStatus );

	fn_vOutputLine( "\tBigfile making is successful !\r\n\r\n" );

	AfxEndThread( STATUS_C_OK );
	return STATUS_C_OK;
}
