// **********************************************************************************
// * "Binary.c"																		*
// * Written by :	Mihaela Tancu													*
// **********************************************************************************


#include "stdafx.h"

#include <io.h>
#include <sys/stat.h>
#include  <stdlib.h>
#include  <string.h>
#include  <stdio.h>

/*
=======================================================================================
DEFINES
=======================================================================================
*/
#include  "Binary.h"
#include  "IniData.h"
/*
=======================================================================================
GLOBALS
=======================================================================================
*/
char		SAVE_g_szDirName[ _MAX_PATH ];
char		SAVE_g_szTargetDirName[ _MAX_PATH ];
unsigned char	g_ucChoice;
/*
=======================================================================================
=======================================================================================
FUNCTIONS
=======================================================================================
=======================================================================================
*/
//************************************************************************
void fn_vGetFileExtension( char *szFileName , char *a4_cExtend )
{
   char *pdest;
   int result;
   int  ch = '.';
   /* Search forward. */
   pdest = strchr( szFileName, ch);
   if (pdest !=0)
   {
	   result = pdest - szFileName + 1;

	   a4_cExtend[0] = toupper (szFileName [result++]);
	   a4_cExtend[1] = toupper (szFileName [result++]);
	   a4_cExtend[2] = toupper (szFileName [result++]);
	   a4_cExtend[3] = 0;
   }
   else
   {
	   a4_cExtend[0] = 0;
	   a4_cExtend[1] = 0;
	   a4_cExtend[2] = 0;
	   a4_cExtend[3] = 0;
   }

}
/*
=======================================================================================
Directory Functions
=======================================================================================
*/

/*
----------------------------------------------------------------------------------------
Description   : create a directory
szDirName           -> name of directory
Returns (BOOL ) true if sucess
----------------------------------------------------------------------------------------
*/
BOOL SAVE_fn_bCreateDirectory( char *szDirName )
{
	if ( (!CreateDirectory( szDirName, NULL )) && (GetLastError() != ERROR_ALREADY_EXISTS) )
	{
		char *p_szCur;
		
		p_szCur = strchr( szDirName, '\\');
		while (p_szCur != NULL)
		{
			*p_szCur = 0;
			if ((!CreateDirectory (szDirName, NULL)) && (GetLastError() != ERROR_ALREADY_EXISTS) )
			{
				char szMessage[ 512 ];
				sprintf( szMessage, "Can't create local path %s", szDirName );
				return FALSE;
			}
			*p_szCur++ = '\\';
			p_szCur = strchr( p_szCur, '\\');
		};
		
		if ((!CreateDirectory (szDirName, NULL)) && (GetLastError() != ERROR_ALREADY_EXISTS) )
		{
			char szMessage[ 512 ];
			sprintf( szMessage, "Can't create local path %s", szDirName );
			return FALSE;
		}
	}
	return TRUE;
}

/*
----------------------------------------------------------------------------------------
Description   : delete temporary files
Returns (BOOL ) true if success
----------------------------------------------------------------------------------------
*/
BOOL fn_bDeleteTemporaryFiles( char *szDir, char *szLevelName )
{
	char			a4_cFileExtension[4];
	WIN32_FIND_DATA stFindData;
	HANDLE			hFind;
	char			szSrc[256];
	char			a256_cFileName[256];
	


	if ( _access( szDir, 0) != 0)
		return FALSE;
	
	
	strcpy(szSrc, szDir);
	strcat(szSrc, "\\*.*" );
	if( (hFind = FindFirstFile(szSrc, &stFindData )) == INVALID_HANDLE_VALUE)
		return TRUE;
	
	do
	{ 
		if ( !(stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
		{
			strcpy(a256_cFileName, _strupr(stFindData.cFileName));
			fn_vGetFileExtension( stFindData.cFileName , a4_cFileExtension);
			if ((strstr(a256_cFileName,"FIX")) || (strstr(a256_cFileName,szLevelName)))
			{
				strcpy(	a256_cFileName, szDir);
				strcat(	a256_cFileName, "\\");
				strcat(	a256_cFileName, stFindData.cFileName);
				DeleteFile(a256_cFileName);
			}
		}
	} while(FindNextFile( hFind, &stFindData ));
	
	FindClose( hFind );
	
	return TRUE;
}
/*
----------------------------------------------------------------------------------------
Description   : copy directory (source and target dir names are stored in global var
SAVE_g_szDirName and SAVE_g_szTargetDirName )
Returns (BOOL ) true if success
----------------------------------------------------------------------------------------
*/
BOOL fn_bCopyDirectory( BOOL _bRecursive, char *szExtendFile )
{
	char			a4_cFileExtension[4];
	WIN32_FIND_DATA stFindData;
	HANDLE			hFind;
	char			*p_szEndSource;
	char			*p_szEndTarget;
	BOOL			bAllFiles;
	

	if (strcmp(szExtendFile,"*.*")==0)
		bAllFiles = TRUE;
	else
		bAllFiles = FALSE;

	if ( _access( SAVE_g_szTargetDirName, 0) != 0)
		SAVE_fn_bCreateDirectory(SAVE_g_szTargetDirName);
	
	p_szEndSource = SAVE_g_szDirName + strlen( SAVE_g_szDirName );
	p_szEndTarget = SAVE_g_szTargetDirName + strlen( SAVE_g_szTargetDirName );
	
	strcpy( p_szEndSource, "\\*.*" );
	if( (hFind = FindFirstFile(SAVE_g_szDirName, &stFindData )) == INVALID_HANDLE_VALUE)
		return TRUE;
	*p_szEndSource = 0;
	
	do
	{
		*p_szEndTarget = *p_szEndSource = '\\';
		strcpy( p_szEndSource + 1, stFindData.cFileName );
		strcpy( p_szEndTarget + 1, stFindData.cFileName );
		
		if ( stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
		{
			if (_bRecursive)
			{
				if ( *stFindData.cFileName != '.')
				{
					if ( !fn_bCopyDirectory(_bRecursive, szExtendFile) )
						return FALSE;
				}
			}
		}
		else
		{
			fn_vGetFileExtension( stFindData.cFileName , a4_cFileExtension);
			if ((bAllFiles) || (strstr(szExtendFile,a4_cFileExtension)))
			{
				if ( !CopyFile(  SAVE_g_szDirName, SAVE_g_szTargetDirName, FALSE ) )
					return FALSE;
			}
		}
		*p_szEndSource = *p_szEndTarget = 0;
	} while(FindNextFile( hFind, &stFindData ));
	
	FindClose( hFind );
	
	return TRUE;
}


BOOL fn_bMoveDirectory( BOOL _bRecursive, char *szExtendFile )
{
	char			a4_cFileExtension[4];
	WIN32_FIND_DATA stFindData;
	HANDLE			hFind;
	char			*p_szEndSource;
	char			*p_szEndTarget;
	BOOL			bAllFiles;
	

	if (strcmp(szExtendFile,"*.*")==0)
		bAllFiles = TRUE;
	else
		bAllFiles = FALSE;

	if ( _access( SAVE_g_szTargetDirName, 0) != 0)
		SAVE_fn_bCreateDirectory(SAVE_g_szTargetDirName);
	
	p_szEndSource = SAVE_g_szDirName + strlen( SAVE_g_szDirName );
	p_szEndTarget = SAVE_g_szTargetDirName + strlen( SAVE_g_szTargetDirName );
	
	strcpy( p_szEndSource, "\\*.*" );
	if( (hFind = FindFirstFile(SAVE_g_szDirName, &stFindData )) == INVALID_HANDLE_VALUE)
		return TRUE;
	*p_szEndSource = 0;
	
	do
	{
		*p_szEndTarget = *p_szEndSource = '\\';
		strcpy( p_szEndSource + 1, stFindData.cFileName );
		strcpy( p_szEndTarget + 1, stFindData.cFileName );
		
		if ( stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
		{
			if (_bRecursive)
			{
				if ( *stFindData.cFileName != '.')
				{
					if ( !fn_bCopyDirectory(_bRecursive, szExtendFile) )
						return FALSE;
				}
			}
		}
		else
		{
			fn_vGetFileExtension( stFindData.cFileName , a4_cFileExtension);
			if ((bAllFiles) || (strstr(szExtendFile,a4_cFileExtension)))
			{
				if ( !stricmp(SAVE_g_szDirName, "game.mem") || 
					 !stricmp(SAVE_g_szDirName, "game.dsc") ||
					 !stricmp(SAVE_g_szDirName, "game.fix") ||
					 !stricmp(SAVE_g_szDirName, "level.mem")|| 
					 !stricmp(SAVE_g_szDirName, "default.pgb") || 
					 !stricmp(SAVE_g_szDirName, "game.pgb") || 
					 !stricmp(SAVE_g_szDirName, "game.rnd") || 
					 !strstr(SAVE_g_szDirName, ".tbl") || 
					 !strstr(SAVE_g_szDirName, ".a3i"))
				{	if ( !CopyFile(  SAVE_g_szDirName, SAVE_g_szTargetDirName, FALSE ) )
						return FALSE;
				}
				else if ( !MoveFileEx( SAVE_g_szDirName, SAVE_g_szTargetDirName, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING ))
					return 0;
			}
		}
		*p_szEndSource = *p_szEndTarget = 0;
	} while(FindNextFile( hFind, &stFindData ));
	
	FindClose( hFind );
	
	return TRUE;
}


/*
----------------------------------------------------------------------------------------
Description   : copy a directory into another one (recursivelly or not)
szSourceDir         -> source directory
szTargetDir         -> target directory
Returns (BOOL ) true if success
----------------------------------------------------------------------------------------
*/

BOOL SAVE_fn_bCopyDirectory( char *szSourceDir, char *szTargetDir, BOOL _bRecursive, char *szExtendFile)
{
	strcpy( SAVE_g_szTargetDirName, szTargetDir );
	strcpy( SAVE_g_szDirName, szSourceDir );
	
	return fn_bCopyDirectory(_bRecursive, szExtendFile);
}



BOOL SAVE_fn_bMoveDirectory( char *szSourceDir, char *szTargetDir, BOOL _bRecursive, char *szExtendFile)
{
	strcpy( SAVE_g_szTargetDirName, szTargetDir );
	strcpy( SAVE_g_szDirName, szSourceDir );
	
	return fn_bMoveDirectory(_bRecursive, szExtendFile);
}

//************************************************************************
char fn_cExecuteGame(char *szProgramName, char *szLevelName, char *szPass)
{
	char a256_cLineCommand[256];
	int  res, i;
	
	strcpy(a256_cLineCommand,szProgramName);
	strcat(a256_cLineCommand, " -level:");
	strcat(a256_cLineCommand, szLevelName);
	strcat(a256_cLineCommand, " -bin:2 ");
	strcat(a256_cLineCommand, " -mem:");
	strcat(a256_cLineCommand,  szPass);
	
	res=system(a256_cLineCommand);
	i=errno;
	return ((char)res);
}


//Mihaela Tancu - Auto
char fn_cExecuteMakeanim(char *szProgramName, BOOL b, BOOL a, BOOL z, char *szAnimName)
{
	char a256_cLineCommand[256];
	int  res;
	
	strcpy(a256_cLineCommand,szProgramName);
	if (b)
		strcat(a256_cLineCommand, " -b ");
	if (a)
		strcat(a256_cLineCommand, " -a ");
	if (z)
		strcat(a256_cLineCommand, " -z ");
	strcat(a256_cLineCommand, " ");
	strcat(a256_cLineCommand, szAnimName);
	
	res=system(a256_cLineCommand);
	return ((char)res);
}
//Mihaela Tancu - Auto


//Mihaela Tancu - Auto
// return the file name without extension
char *fn_p_cGetFileNameWithoutExt(char *_sFileName)
{
  char *p_char;
  static char sFileName[256];

  sprintf(sFileName, "%s", _sFileName);
  p_char = sFileName;
  if ((p_char=strstr(sFileName, "."))!=NULL)
	*p_char = 0;

  return sFileName;
}
//Mihaela Tancu - Auto


//Mihaela Tancu - Auto
char *fn_p_cGetPathFileName(char *_sFileName)
{
  char *p_char;
  static char sFileName[256];

  sprintf(sFileName, "%s", _sFileName);
  p_char = sFileName;
  if ((p_char=strrchr(sFileName, '\\'))!=NULL)
	*p_char = 0;

  return sFileName;
}
//Mihaela Tancu - Auto


//Mihaela Tancu - Auto
char fn_cExecuteConvImg(char *szProgramName, char * szSource, char *szDestBinDir, BOOL bBMP, BOOL bTGA, BOOL bConvert16, BOOL bConvert32, char *szTextureName)
{
	char a256_cLineCommand[256];
	int  res;
	char szCurrentSourcePath[_MAX_PATH], szCurrentDestPath[_MAX_PATH]/*, szCurrentDir[_MAX_PATH]*/;

	strcpy(szCurrentSourcePath, szSource);
	strcat(szCurrentSourcePath, "\\World\\Graphics\\Textures\\");
//here should be the name of the texture file to be converted not all the directory!!!
//	if (!bConvTextures)
		strcat(szCurrentSourcePath, fn_p_cGetPathFileName(szTextureName));
//	else
//		strcat(szCurrentSourcePath, szTextureName);
	
	strcpy(szCurrentDestPath, szDestBinDir);
	strcat(szCurrentDestPath, "\\World\\Graphics\\Textures\\");
	strcat(szCurrentDestPath, fn_p_cGetPathFileName(szTextureName));

	strcpy(a256_cLineCommand, szProgramName);
	strcat(a256_cLineCommand, " -SrcPath:");
	strcat(a256_cLineCommand, szCurrentSourcePath);
	strcat(a256_cLineCommand, " -DstPath:");
	strcat(a256_cLineCommand, szCurrentDestPath);
	if (bBMP) strcat(a256_cLineCommand, " -BMP");
	if (bTGA) strcat(a256_cLineCommand, " -TGA");
	if (bConvert16) strcat(a256_cLineCommand, " -Convert16");
	if (bConvert32) strcat(a256_cLineCommand, " -Convert32");

/*	strcpy(szIniFileName, "dfm.ini");
    if (bYes)
	{	getcwd(szCurrentDir,_MAX_PATH);
		WritePrivateProfileString("ConvertImg","SourcePath",szCurrentSourcePath,szIniFileName);
		WritePrivateProfileString("ConvertImg","DestPath",szCurrentDestPath,szIniFileName);
		sprintf(szTemp, "%d", bBMP);
		WritePrivateProfileString("ConvertImg","BMP",szTemp,szIniFileName);
		sprintf(szTemp, "%d", bTGA);
		WritePrivateProfileString("ConvertImg","TGA",szTemp,szIniFileName);
		sprintf(szTemp, "%d", bConvert16);
		WritePrivateProfileString("ConvertImg","Convert16",szTemp,szIniFileName);
		sprintf(szTemp, "%d", bConvert32);
		WritePrivateProfileString("ConvertImg","Convert32",szTemp,szIniFileName);
	}
*/

	if (!bBMP && !bTGA) //is not possible this combination!!!
	{	
		MessageBox(NULL,"Should select BMP ot TGA for conversion"," Error in command line",MB_OK);
		return 0;
	}

	res=system(a256_cLineCommand);
	return ((char)res);
}
//Mihaela Tancu - Auto

// GuS
char fn_cExecuteConvImgOnDirectory( char *szProgramName, char *szSourceDir, char *szDestDir, BOOL bBMP, BOOL bTGA, BOOL bConvert16, BOOL bConvert32 )
{
	char a256_cLineCommand[256];
	int  res;

	strcpy(a256_cLineCommand, szProgramName);
	strcat(a256_cLineCommand, " -SrcPath:");
	strcat(a256_cLineCommand, szSourceDir);
	strcat(a256_cLineCommand, " -DstPath:");
	strcat(a256_cLineCommand, szDestDir);
	if (bBMP) strcat(a256_cLineCommand, " -BMP");
	if (bTGA) strcat(a256_cLineCommand, " -TGA");
	if (bConvert16) strcat(a256_cLineCommand, " -Convert16");
	if (bConvert32) strcat(a256_cLineCommand, " -Convert32");

	if (!bBMP && !bTGA) // this combination is not possible !!!
	{
		MessageBox(NULL,"Should select BMP ot TGA for texture conversion"," Error in command line",MB_OK);
		return 0;
	}

	res=system(a256_cLineCommand);
	return ((char)res);
}


// GuS
void fn_vCopyTextures( char *_szBaseDir, char *_szDestinationDir, BOOL bConvertTextures )
{
	char szBinTexturesDirectory[_MAX_PATH];
	char szBinVignettesDirectory[_MAX_PATH];
	char szDirName[_MAX_PATH], szSaveDir[_MAX_PATH];

	GetCurrentDirectory( _MAX_PATH, szSaveDir);

	SetCurrentDirectory( _szBaseDir);
	sprintf( szBinTexturesDirectory, "%s\\World\\Graphics\\Textures", _szDestinationDir );
	SAVE_fn_bCreateDirectory( szBinTexturesDirectory );

	FILE *fdTest = fopen("version.ini", "rb");
	// Check  directories in version.ini and convert textures with the appropriate order.
	if (fdTest) 
	{
		char szOtherDataDirs[255];
		char *szToken;
		char szDelimiters[] = " ;,\t";
		char *a8_szDirectory[8];
		int iCountDir;

		fgets( szOtherDataDirs, 255, fdTest );
		// suppress last char (EOL)
		szOtherDataDirs[strlen(szOtherDataDirs)-2] = NULL;
		fclose(fdTest);
		// Mark all dirs
		szToken = strtok( szOtherDataDirs, szDelimiters );
		for( iCountDir = 0; szToken; szToken = strtok( NULL, szDelimiters ) )
			a8_szDirectory[iCountDir++] = szToken;

		for (int i=iCountDir-1; i>=0; i--)
		{
			sprintf( szDirName, "%s\\%s\\World\\Graphics\\Textures", _szBaseDir, a8_szDirectory[i] );
			// If the directory exists and we have permission to write, we convert the textures in it.
			if ( access( szDirName, 6) == 0 )
			{
				if( bConvertTextures )
					fn_cExecuteConvImgOnDirectory( "ConvImg.exe", szDirName, szDirName, bBMP, bTGA, bConvert16, bConvert32 );

				// Then, we copy the textures to binary directory
					SAVE_fn_bCopyDirectory(szDirName, szBinTexturesDirectory, TRUE, "GF");
			}
		}
	}

	// Take care of gamedata (witch is not in version.ini)
	sprintf( szDirName, "%s\\Gamedata\\World\\Graphics\\Textures", _szBaseDir );
	if( bConvertTextures )
		fn_cExecuteConvImgOnDirectory( "ConvImg.exe", szDirName, szDirName, bBMP, bTGA, bConvert16, bConvert32 );
	SAVE_fn_bCopyDirectory(szDirName, szBinTexturesDirectory, TRUE, "GF");

	sprintf( szBinVignettesDirectory, "%s\\Vignette", _szDestinationDir );
	sprintf( szDirName, "%s\\Gamedata\\Vignette", _szBaseDir );
	if( bConvertTextures )
		fn_cExecuteConvImgOnDirectory( "ConvImg.exe", szDirName, szDirName, bBMP, bTGA, bConvert16, bConvert32 );
	SAVE_fn_bCopyDirectory(szDirName, szBinVignettesDirectory, TRUE, "GF");

	SetCurrentDirectory(szSaveDir);
}


//Mihaela Tancu
char fn_cExecuteGameFirst(char *szProgramName, char *szLevelName, char *szPass)
{
	char a256_cLineCommand[256];
	int  res;
	
	strcpy(a256_cLineCommand,szProgramName);
	strcat(a256_cLineCommand, " -level:");
	strcat(a256_cLineCommand, szLevelName);
	strcat(a256_cLineCommand, " -bin:2 ");
	strcat(a256_cLineCommand, " -mem:");
	strcat(a256_cLineCommand,  szPass);
	strcat(a256_cLineCommand, " -dsc:");
	strcat(a256_cLineCommand,  "2");
	
	res=system(a256_cLineCommand);
	return ((char)res);
}
//Mihaela Tancu


//************************************************************************
char fn_cExecuteCRB(char *szProgramName, char *szLevelName)
{
	char a256_cLineCommand[256];
	int  res;
	
	strcpy(a256_cLineCommand,szProgramName);
	strcat(a256_cLineCommand, " ");
	strcat(a256_cLineCommand, szLevelName);
	strcat(a256_cLineCommand, " -s");

	res=system(a256_cLineCommand);
	return 1;
}


//************************************************************************
char fn_cCopyAnyFilesOfADirectory(char *szSrcDirectory,  char *szDestDirectory, char *szPass) 
{
	
	WIN32_FIND_DATA stFindData;
	HANDLE			hFind;
	char			a256_cFileFound[256];
	char			a256_cNewFile[256];
	char			a4_cFileExtension[4];
	char			a256_cSrcDirectory[256];
	char			a256_cFileName[256];
	char			*p_cPointer;
	
	strcpy(a256_cSrcDirectory,szSrcDirectory );
	strcat(a256_cSrcDirectory,"\\*.*");
	if( (hFind = FindFirstFile(a256_cSrcDirectory, &stFindData )) == INVALID_HANDLE_VALUE)
		return 0;
	
	do
	{
		if(! ( stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
		{	
			strcpy(a256_cFileName, _strupr(stFindData.cFileName));
			fn_vGetFileExtension( a256_cFileName, a4_cFileExtension);
			if (
				(strcmp(a4_cFileExtension,"SNA")==0) ||
				(strcmp(a4_cFileExtension,"GPT")==0) ||
				(strcmp(a4_cFileExtension,"SND")==0) ||
				(strcmp(a4_cFileExtension,"PTX")==0) ||
				(strcmp(a4_cFileExtension,"VGM")==0) ||
//Mihaela Tancu
				((strcmp(a4_cFileExtension,"DSB")==0) && bDsc) ||
//Mihaela Tancu
				(strcmp(a4_cFileExtension,"LSY")==0) 
				)
			{
				strcpy(a256_cFileFound, szSrcDirectory);
				strcat(a256_cFileFound,"\\");
				strcat(a256_cFileFound,a256_cFileName);
				
				strcpy(a256_cNewFile, szDestDirectory);
				strcat(a256_cNewFile,"\\");
				strcat(a256_cNewFile,a256_cFileName);
				p_cPointer = a256_cNewFile + ((strlen(a256_cNewFile)) -4);
				strcpy(p_cPointer, szPass);
				p_cPointer++;
				strcpy(p_cPointer, ".");
				p_cPointer++;
				strcpy(p_cPointer, a4_cFileExtension);
				
				if (!stricmp(a256_cFileFound, "game.mem") || !stricmp(a256_cFileFound, "game.dsc") 
					||!stricmp(a256_cFileFound, "game.fix") || !stricmp(a256_cFileFound, "level.mem")
					 || !stricmp(a256_cFileFound, "default.pgb") || !stricmp(a256_cFileFound, "game.pgb")
					 || !stricmp(a256_cFileFound, "game.rnd")|| strstr(a256_cFileFound, ".tbl")|| strstr(a256_cFileFound, ".a3i"))
				{	if ( !CopyFile(  a256_cFileFound, a256_cNewFile, FALSE ) )
						return 0;
				}
				else
					if ( !MoveFileEx( a256_cFileFound, a256_cNewFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING ))
						return 0;
	
			}
		}
	} while(FindNextFile( hFind, &stFindData ));
	
	FindClose( hFind );
	
	return 1;
}
//************************************************************************
char fn_cCopyBinarizedFiles(char *szSrcDirectory,  char *szDestDirectory, char *szLevel) 
{
	
	WIN32_FIND_DATA stFindData;
	HANDLE			hFind;
	char			a256_cFileFound[256];
	char			a256_cNewFile[256];
	char			a4_cFileExtension[256];
	char			a256_cSrcDirectory[256];
	char			a256_cTmpFile[256];
	char			a256_cFileName[256];

	BOOL			bFix= FALSE;
	
	strcpy(a256_cSrcDirectory,szSrcDirectory );
	strcat(a256_cSrcDirectory,"\\*.*");
	if( (hFind = FindFirstFile(a256_cSrcDirectory, &stFindData )) == INVALID_HANDLE_VALUE)
		return 0;

	strcpy(a256_cTmpFile,szLevel);

	if (strcmp(szLevel,"FIX")==0)
	{
		bFix = TRUE;	
	}

	strcat(a256_cTmpFile, "0");

	do
	{
		if(! ( stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
		{	
			strcpy(a256_cFileName, _strupr(stFindData.cFileName));
			fn_vGetFileExtension( a256_cFileName, a4_cFileExtension);
			if (
				(strcmp(a4_cFileExtension,"RTB")==0) ||
				(strcmp(a4_cFileExtension,"RTS")==0) ||
				(strcmp(a4_cFileExtension,"RTT")==0) ||
				(strcmp(a4_cFileExtension,"RTP")==0) ||
				(strcmp(a4_cFileExtension,"RTV")==0) ||
				(strcmp(a4_cFileExtension,"RTL")==0) 
				)
			{
				if (strstr(stFindData.cFileName, szLevel))
				{
					strcpy(a256_cFileFound, szSrcDirectory);
					strcat(a256_cFileFound,"\\");
					strcat(a256_cFileFound,a256_cFileName);

					strcpy(a256_cNewFile, szDestDirectory);
					strcat(a256_cNewFile,"\\");
					strcat(a256_cNewFile,a256_cFileName);

				if (!stricmp(a256_cFileFound, "game.mem") || !stricmp(a256_cFileFound, "game.dsc") 
					|| !stricmp(a256_cFileFound, "game.fix") || !stricmp(a256_cFileFound, "level.mem")
					 || !stricmp(a256_cFileFound, "default.pgb") || !stricmp(a256_cFileFound, "game.pgb")
					 || !stricmp(a256_cFileFound, "game.rnd")|| strstr(a256_cFileFound, ".tbl")|| strstr(a256_cFileFound, ".a3i"))
				{	if ( !CopyFile(  a256_cFileFound, a256_cNewFile, FALSE ) )
						return 0;
				}
				else
					if ( !MoveFileEx( a256_cFileFound, a256_cNewFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING ))
						return 0;

				}
				else
				{
					if (!bFix)
					{
						if (strcmp(a256_cFileName, "FIXLVL.RTB")==0)
						{
							strcpy(a256_cFileFound, szSrcDirectory);
							strcat(a256_cFileFound,"\\");
							strcat(a256_cFileFound,a256_cFileName);

							strcpy(a256_cNewFile, szDestDirectory);
							strcat(a256_cNewFile,"\\");
							strcat(a256_cNewFile,stFindData.cFileName);


				if (!stricmp(a256_cFileFound, "game.mem") || !stricmp(a256_cFileFound, "game.dsc") 
					||!stricmp(a256_cFileFound, "game.fix") || !stricmp(a256_cFileFound, "level.mem")
					 || !stricmp(a256_cFileFound, "default.pgb") || !stricmp(a256_cFileFound, "game.pgb")
					 || !stricmp(a256_cFileFound, "game.rnd")|| strstr(a256_cFileFound, ".tbl")|| strstr(a256_cFileFound, ".a3i"))
				{	if ( !CopyFile(  a256_cFileFound, a256_cNewFile, FALSE ) )
								return 0;
				}
				else
					if ( !MoveFileEx( a256_cFileFound, a256_cNewFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING ))
						return 0;

						}
					}
				}
			}
			else
			{
				if (
					(strcmp(a4_cFileExtension,"SNA")==0) ||
					(strcmp(a4_cFileExtension,"GPT")==0) ||
					(strcmp(a4_cFileExtension,"SND")==0) ||
					(strcmp(a4_cFileExtension,"PTX")==0) ||
					(strcmp(a4_cFileExtension,"VGM")==0) ||
//Mihaela Tancu
					((strcmp(a4_cFileExtension,"DSB")==0) && bDsc) ||
//Mihaela Tancu
					(strcmp(a4_cFileExtension,"LSY")==0) 
				)
				{
					strcpy(a256_cFileFound, szSrcDirectory);
					strcat(a256_cFileFound,"\\");
					strcat(a256_cFileFound,a256_cFileName);

					if (strstr(a256_cFileName, a256_cTmpFile))
					{
						strcpy(a256_cNewFile, szDestDirectory);
						strcat(a256_cNewFile,"\\");
						strcat(a256_cNewFile,szLevel);
						strcat(a256_cNewFile, ".");
						strcat(a256_cNewFile, a4_cFileExtension);
				
				if (!stricmp(a256_cFileFound, "game.mem") || !stricmp(a256_cFileFound, "game.dsc") 
					||!stricmp(a256_cFileFound, "game.fix") || !stricmp(a256_cFileFound, "level.mem")
					 || !stricmp(a256_cFileFound, "default.pgb") || !stricmp(a256_cFileFound, "game.pgb")
					 || !stricmp(a256_cFileFound, "game.rnd")|| strstr(a256_cFileFound, ".tbl")|| strstr(a256_cFileFound, ".a3i"))
				{	if ( !CopyFile(  a256_cFileFound, a256_cNewFile, FALSE ) )
						return 0;
				}
				else
					if ( !MoveFileEx( a256_cFileFound, a256_cNewFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING ))
						return 0;
					}
				}
			}
		}
	} while(FindNextFile( hFind, &stFindData ));
	
	FindClose( hFind );
	
	return 1;
}

//************************************************************************
void fn_vInitBinGame_old()
{
	char szLevelName[_MAX_PATH];
	char szLevelsDirectory[ _MAX_PATH ];
	char szProgramName[_MAX_PATH];
	char szBinDirectory[ _MAX_PATH ];
	char szCurrentDir[_MAX_PATH];

	char szFileName[_MAX_PATH];
	char szTempName[_MAX_PATH];
	char szTempDir[_MAX_PATH];
	char szDirName[_MAX_PATH];
//	char szSrcFile[_MAX_PATH];
//	char szDestFile[ _MAX_PATH ];

	char *p_szCommanLine;
	char cChar;
	char cFirst=1;
	char cDebug=1;
	
	GetCurrentDirectory(_MAX_PATH, szCurrentDir);
	p_szCommanLine= AfxGetApp()->m_lpCmdLine;

	/* directory of the binarized data */
	if(strstr(p_szCommanLine,"-dir:"))
		sscanf(strstr(p_szCommanLine,"-dir:")+5,"%s",szDirName);
	else 
	{
		MessageBox(NULL,"No directory defined","Error in command line",MB_OK);
		return;
	}

	/* the level that we want to binarize*/
	if(strstr(p_szCommanLine,"-level:"))
	{		
		sscanf(strstr(p_szCommanLine,"-level:")+7,"%s",szTempName);
		strcpy(szLevelName, _strupr(szTempName));
	}
	else 
	{
		MessageBox(NULL,"No level defined","Error in command line",MB_OK);
		return;
	}

	if(strstr(p_szCommanLine,"-first:"))
	{		
		sscanf(strstr(p_szCommanLine,"-first:")+7,"%c",&cChar);
		if (strcmp(&cChar, "0")==0)
			cFirst = 0;
	}

	if(strstr(p_szCommanLine,"-debug:"))
	{		
		sscanf(strstr(p_szCommanLine,"-debug:")+7,"%c",&cChar);
		if (strcmp(&cChar, "0")==0)
			cDebug = 0;
	}

	if(strstr(p_szCommanLine,"-version:"))
	{		
		sscanf(strstr(p_szCommanLine,"-version:")+9,"%s",szTempName);
		strcpy(szFileName,szCurrentDir);
		strcat(szFileName,"\\StartPrg.ini");
		if (cDebug)
			GetPrivateProfileString(szTempName,"Debug","",szProgramName,_MAX_PATH,szFileName);
		else
			GetPrivateProfileString(szTempName,"Release","",szProgramName,_MAX_PATH,szFileName);
	}
	else 
	{
		MessageBox(NULL,"No Version defined"," Error in command line",MB_OK);
		return;
	}

	/*
	* path of the binary directory
	*/
	strcpy(szBinDirectory, szCurrentDir);
	strcat(szBinDirectory, "\\");
	strcat(szBinDirectory, szDirName);

	/*
	* path of the levels directory (source)
	*/
	strcpy(szLevelsDirectory,szCurrentDir);
	strcat(szLevelsDirectory,"\\GameData\\World\\levels");
	
	/*	
	* First Pass
	*/
	strcpy(szTempName, szCurrentDir);
	strcat(szTempName, "\\");
	strcat(szTempName, szProgramName);
	if (fn_cExecuteGame(szTempName, szLevelName, C_BinPass1)!=0)
	{
		strcpy(szTempName, "Pass 1 of ");
		strcat(szTempName, szLevelName);		
		MessageBox(NULL,szTempName,"Error in Game execution",MB_OK);
		exit(-1);
	}
	
	/*
	* copy some files created in the level directory
	*/
	strcpy(szTempDir, szLevelsDirectory);
	strcat(szTempDir, "\\");
	strcat(szTempDir, szLevelName);
	fn_cCopyAnyFilesOfADirectory(szTempDir,szCurrentDir,"0");
	
	/*
	* copy some files created in the Gamedata directory
	*/
	fn_cCopyAnyFilesOfADirectory(szLevelsDirectory,szCurrentDir,"0");

	/*
	* Second pass
	*/
	if (fn_cExecuteGame(szTempName, szLevelName, C_BinPass2)!=0)
	{
		strcpy(szTempName, "Pass 2 of ");
		strcat(szTempName, szLevelName);		
		MessageBox(NULL,szTempName,"Error in Game execution",MB_OK);
		exit(-1);
	}
	
	/*
	* copy some files created in the level directory
	*/
	fn_cCopyAnyFilesOfADirectory(szTempDir,szCurrentDir,"1");

	/*
	* copy some files created in the Gamedata directory
	*/
	fn_cCopyAnyFilesOfADirectory(szLevelsDirectory,szCurrentDir,"1");

	strcpy(szTempName,szCurrentDir);
	strcat(szTempName, "\\");
	strcat(szTempName, C_CRBExecutable);
    /*
	* CRB on fix
	*/
	fn_cExecuteCRB(szTempName, "Fix");		
    /*
	* CRB on Level
	*/
	fn_cExecuteCRB(szTempName, szLevelName);		

	/*
	*  Create the bin directory and copy the binarized data
	*/
	SAVE_fn_bCreateDirectory(szBinDirectory);
	strcpy(szTempName,szBinDirectory);
	strcat(szTempName,"\\");
	strcat(szTempName,C_World);
	SAVE_fn_bCreateDirectory(szTempName);
	strcat(szTempName,"\\");
	strcat(szTempName,C_Levels);
	SAVE_fn_bCreateDirectory(szTempName);

	if (cFirst)
		fn_cCopyBinarizedFiles(szCurrentDir, szTempName,  "FIX");

	strcat(szTempName,"\\");
	strcat(szTempName,szLevelName);
	SAVE_fn_bCreateDirectory(szTempName);
	fn_cCopyBinarizedFiles(szCurrentDir, szTempName,  szLevelName);

	/*
	* copy files (*.dsc, *.ito, *.mdf, *.pgb) 
	*/
/* YLG Modif
	strcpy(szSrcFile,szLevelsDirectory);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, ".dsc");
	strcpy(szDestFile,szTempName);
	strcat(szDestFile, "\\");
	strcat(szDestFile, szLevelName);
	strcat(szDestFile, ".dsc");
	CopyFile(szSrcFile, szDestFile, FALSE);

	strcpy(szSrcFile,szLevelsDirectory);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, ".ito");
	strcpy(szDestFile,szTempName);
	strcat(szDestFile, "\\");
	strcat(szDestFile, szLevelName);
	strcat(szDestFile, ".ito");
	CopyFile(szSrcFile, szDestFile, FALSE);

	strcpy(szSrcFile,szLevelsDirectory);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, ".mdf");
	strcpy(szDestFile,szTempName);
	strcat(szDestFile, "\\");
	strcat(szDestFile, szLevelName);
	strcat(szDestFile, ".mdf");
	CopyFile(szSrcFile, szDestFile, FALSE);

	strcpy(szSrcFile,szLevelsDirectory);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, "\\");
	strcat(szSrcFile, szLevelName);
	strcat(szSrcFile, ".pgb");
	strcpy(szDestFile,szTempName);
	strcat(szDestFile, "\\");
	strcat(szDestFile, szLevelName);
	strcat(szDestFile, ".pgb");
	CopyFile(szSrcFile, szDestFile, FALSE);
*/

	if (cFirst)
	{
		strcpy(szTempDir,szBinDirectory);
		strcpy(szDirName,szCurrentDir);
		strcat(szDirName,"\\Gamedata");
		SAVE_fn_bCopyDirectory(szDirName, szTempDir, FALSE, "*.*");

		/*
		* Copy sound directory
		*/
		strcpy(szTempName,szBinDirectory);
		strcat(szTempName,"\\");
		strcat(szTempName,C_World);
		strcpy(szTempDir, szTempName);	
		strcat(szTempDir,"\\Sound");

		strcpy(szDirName,szCurrentDir);
		strcat(szDirName,"\\Gamedata\\World\\Sound");
		SAVE_fn_bCopyDirectory(szDirName, szTempDir, TRUE, "*.*");


		/*
		* Copy the graphics\Texture directory
		*/
		strcat(szTempName,"\\Graphics");
		SAVE_fn_bCreateDirectory(szTempName);
		strcat(szTempName,"\\Textures");
		strcpy(szDirName,szCurrentDir);
		strcat(szDirName,"\\GameData\\World\\\\Graphics\\Textures");
		SAVE_fn_bCopyDirectory(szDirName, szTempName, TRUE, "GF");

		/*
		* Copy Options directory
		*/
/* YLG modif
		strcpy(szTempName,szBinDirectory);
		strcat(szTempName,"\\Options");
		strcpy(szDirName,szCurrentDir);
		strcat(szDirName,"\\Gamedata\\Options");
		SAVE_fn_bCopyDirectory(szDirName, szTempName, TRUE,"*.*");
*/		
		/*
		* Copy Texts directory
		*/
/* YLG modif
		strcpy(szTempName,szBinDirectory);
		strcat(szTempName,"\\Texts");
		strcpy(szDirName,szCurrentDir);
		strcat(szDirName,"\\Gamedata\\Texts");
		SAVE_fn_bCopyDirectory(szDirName, szTempName, TRUE,"*.*");
*/

		/*
		* Copy Vignette directory
		*/
		strcpy(szTempName,szBinDirectory);
		strcat(szTempName,"\\Vignette");
		strcpy(szDirName,szCurrentDir);
		strcat(szDirName,"\\Gamedata\\Vignette");
		SAVE_fn_bCopyDirectory(szDirName, szTempName, TRUE,"*.*");
	}

	fn_bDeleteTemporaryFiles(szCurrentDir, szLevelName);
}
//************************************************************************
