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


UINT fn_uiThreadAnims( LPVOID _p_vParam )
{
	BOOL                  bOptimizedAnims;
	HANDLE                hFindhandle;
	WIN32_FIND_DATA       stFindData;
	CString               csFind;
	tdeStatus             eStatus;
	char                  szMakeAnimExeName[512];
	char                  szMakeAnimCommandLine[512];
	char                  szTblName[512];
	int                   iNbProcesses = 0;
	PROCESS_INFORMATION   a2_stMakeanimProcesses[2];
	const char            *a_szProcessNames[] = { "Makeanim", "Makeanim" };
	int                   iWaitReturn;


	bOptimizedAnims = *(BOOL *)_p_vParam;

	fn_vOutputLine( "Making all anims...\r\n" );

	sprintf( szMakeAnimExeName, "%s\\makeanim.exe", (LPCTSTR)g_stTheGlobalData.csMainDirectory );

	// No families found, nothing to do...
	csFind = g_stTheGlobalData.csMainDirectory + "\\Gamedata\\World\\Levels\\_Common\\Families\\*.*";
	if( (hFindhandle = FindFirstFile( csFind, &stFindData )) == INVALID_HANDLE_VALUE )
		return STATUS_C_OK;

	// For each family in gamedata, we execute makeanim on it.
	do
	{
		// Skip . and .. 
		if( (stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
		    && ( (strcmp( stFindData.cFileName, ".") == 0)
		         || strcmp( stFindData.cFileName, "..") == 0) )
			continue;

		if( (stFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
		{
			csFind = "\t";
			csFind += stFindData.cFileName;
			csFind += "\r\n";
			fn_vOutputLine( csFind );
			for( int i=0; i<g_stTheGlobalData.a_csVersionDirectories.GetSize(); i++ )
			{
				sprintf( 
				    szTblName, 
				    "%s\\%s\\World\\Levels\\_Common\\Families\\%s\\%s.tbl",
				    (LPCTSTR)g_stTheGlobalData.csMainDirectory,
				    (LPCTSTR)g_stTheGlobalData.a_csVersionDirectories[i],
				    stFindData.cFileName,
				    stFindData.cFileName );

				if( GetFileAttributes( szTblName ) != 0xffffffff )
				{
					if( bOptimizedAnims )
						sprintf( szMakeAnimCommandLine, "Makeanim.exe -z -b %s", stFindData.cFileName );
					else
						sprintf( szMakeAnimCommandLine, "Makeanim.exe -b %s", stFindData.cFileName );

					// Spawn process Makeanim
					if( ! fn_bSpawnProcess( szMakeAnimExeName, szMakeAnimCommandLine, &a2_stMakeanimProcesses[iNbProcesses] ) )
					{
						FindClose( hFindhandle );
						AfxEndThread( STATUS_C_Error );
					}

					// increment spawned process count
					iNbProcesses++;

					// If we have 2 processes running, wait for end of one of them.
					if( iNbProcesses > 1 )
					{
						if( (eStatus = 
						    fn_eWaitForMultipleProcesses( iNbProcesses, a2_stMakeanimProcesses, a_szProcessNames, &iWaitReturn ) )
						    != STATUS_C_OK )
						{
							FindClose( hFindhandle );
							AfxEndThread( eStatus );
						}
						iNbProcesses--;
						if( iWaitReturn == 0 )
						{
							a2_stMakeanimProcesses[0] = a2_stMakeanimProcesses[1];
						}
					}


					break;

				}
			}
		}

	} while( FindNextFile( hFindhandle, &stFindData ) );

	FindClose( hFindhandle );

	fn_vOutputLine( "\tMakeAnim complete !\r\n" );

	AfxEndThread( STATUS_C_OK );
	return STATUS_C_OK;
}
