/*=========================================================================
*
* Anims.c	- Animation conversion
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include <Windows.h>
#include <stdio.h>

#include "Anims.h"

#include "print.h"
#include "system.h"

#include "SaveModifLst.h"		

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



/****************************************************************************
*		Description:	compute all the anims for all the family
*
*		Parameters:		sRawData	: raw  directory
*						sGameData	: game directory
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vComputeAllAnims (char *sRawData, char *sGameData)
{
  xString			sDirectory;
  HANDLE			hHandle;
  WIN32_FIND_DATA	stFindFileData;

  // change directory
  GetCurrentDirectory(256, sDirectory);
  SetCurrentDirectory(sRawData);

  // search all the anim's directories
  memset(&stFindFileData,0,sizeof(stFindFileData));
  hHandle = FindFirstFile("World/Graphics/Anims/*.*", &stFindFileData);
  if( hHandle == INVALID_HANDLE_VALUE)
  {
    SetCurrentDirectory(sDirectory);
    printf("\nNo anims in %s\n", sRawData);
    return;
  }

  do if(stFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
    if(strlen(stFindFileData.cFileName) == 0) break;
    if(strcmp(stFindFileData.cFileName, ".") == 0) continue;
    if(strcmp(stFindFileData.cFileName, "..") == 0) continue;

    // compute one animation
	MLT_vComputeOneAnim(stFindFileData.cFileName, sRawData, sGameData);
  }
  while(FindNextFile(hHandle, &stFindFileData));
  FindClose(hHandle);

  // restore directory
  SetCurrentDirectory(sDirectory);
}


/****************************************************************************
*		Description:	compute all the anims for one family
*
*		Parameters:		sAnimData	: anim directory 
*						sRawData	: raw  directory
*						sGameData	: game directory
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vComputeOneAnim (char *sAnimData, char *sRawData, char *sGameData)
{
  xString			sRawAnim, sGameAnim;
  xString			sDirectory;
  HANDLE			hHandle;
  WIN32_FIND_DATA	stFindFileData;

  // DISPLAY
  MLT_vOutput( C_ComRes_cTitleLine, "\nAnim %s", sAnimData);

  // init directories
  sprintf(sRawAnim, "%s\\World\\Graphics\\Anims\\%s", sRawData, sAnimData);
  sprintf(sGameAnim, "%s\\World\\Graphics\\Anims\\%s", sGameData, sAnimData);
  CreateDirectory(sGameAnim, NULL);

  // DISPLAY
  MLT_vOutput( C_ComRes_cNormalLine, "\n\tCopy...");

  // copy A3D files
  if (g_bStandardConfig)
	  MLT_vCopyFiles("*.a3d", sRawAnim, sGameAnim);
  else
  {	  
	  xString sAnim;
	  sprintf(sAnim, "%s\\%s", g_szAnims, sAnimData);
	  MLT_vCopyFiles("*.a3d", sAnim, sGameAnim);
  }
	

  // update paths
  MLT_vUpdatePathInModifFile(sGameAnim);

  // change directory
  GetCurrentDirectory(256, sDirectory);
  SetCurrentDirectory(sGameAnim);

  // search all the anim's directories
  memset(&stFindFileData,0,sizeof(stFindFileData));
  hHandle = FindFirstFile("*.a3b", &stFindFileData);
  if( hHandle == INVALID_HANDLE_VALUE)
  {
    SetCurrentDirectory(sDirectory);
	MLT_vOutput( C_ComRes_cResultLine, "\nAnim %s converted\n", sAnimData);

    return;
  }

  do if(stFindFileData.dwFileAttributes)
  {
    if(strlen(stFindFileData.cFileName) == 0) break;
    if(strcmp(stFindFileData.cFileName, ".") == 0) continue;
    if(strcmp(stFindFileData.cFileName, "..") == 0) continue;

    remove(stFindFileData.cFileName);
  }
  while(FindNextFile(hHandle, &stFindFileData));
  FindClose(hHandle);
  
  MLT_vOutput( C_ComRes_cResultLine, "\nAnim %s converted\n", sAnimData);

  // restore directory
  SetCurrentDirectory(sDirectory);
}

