// supervis.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "supervis.h"
#include "SNDinc.h"
#include "snddll.h"

#include "snddbg.h"
#include "sndplay.h"
#include "sndvect.h"
#include "sndres.h"
#include "sndini.h"
#include "sndtheme.h"
#include "sndvirt.h"
#include "sndevent.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CSupervisApp

BEGIN_MESSAGE_MAP(CSupervisApp, CWinApp)
	//{{AFX_MSG_MAP(CSupervisApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSupervisApp construction

CSupervisApp::CSupervisApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CSupervisApp object

CSupervisApp theApp;


BOOL CSupervisApp::InitInstance() 
{

	//Set the Key for registry. We have to init the function SetRegistrey() in
	//InitInstance() because it is a protected member function of CWinApp
	SetRegistryKey( "Ubisoft" );
	
	//Get the registry configuration
	m_cLCBPath = GetProfileString("Settings", "LCBPath", "NoLCBPath");
	m_cLCBFileTitle = GetProfileString("Settings", "LCBFileTitle", "NoLCBFileTitle");

	//Initialise
	m_bSuperVisaterActive = FALSE;


	return CWinApp::InitInstance();
}


/*
 *=============================================================================
 *  Description : Launch the Supervisater PropertySheet dialog
  *=============================================================================
 *  Input  : SND_tdstInitStruct		*pInitStruct	
 *
 *  Output : Void.
 *=============================================================================
 *  Creation date : 23/03/99            Author : Pascal Lalancette            
 *=============================================================================
 *  Modification date :				    Author : 
 *  Modifications :
 *=============================================================================
 */
void SND_CALL SND_fn_vLaunchSupervisater(SND_tdstInitStruct *pInitStruct)
{

#ifdef SUPERVISATER
#if defined(_DLL_COMPILATION_MODE)
    SND_DllInitEntryPoints_StaticFunctions((HMODULE)pInitStruct->hProcessInstance );
#endif



	AFX_MANAGE_STATE(AfxGetStaticModuleState())
	
	TRY
	{

		CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

		if(pSuperVisApp->m_bSuperVisaterActive)
		{

			
			//We start an UI Thread for the supervisater Interface
			pSuperVisApp->m_SuperVisThread = (CSuperVisThread*)
			AfxBeginThread(RUNTIME_CLASS(CSuperVisThread), THREAD_PRIORITY_NORMAL,
					0, CREATE_SUSPENDED);

			pSuperVisApp->m_SuperVisThread->ResumeThread();
		
		}
		
	}
	END_TRY
	
#endif
}



/*
 *=============================================================================
 *  Description : Close the Supervisater PropertySheet dialog
  *=============================================================================
 *  Input  : Void
 *
 *  Output : Void.
 *=============================================================================
 *  Creation date : 23/03/99            Author : Pascal Lalancette            
 *=============================================================================
 *  Modification date :				    Author : 
 *  Modifications :
 *=============================================================================
 */
void SND_CALL SND_fn_vKillSupervisater()
{
#ifdef SUPERVISATER



	AFX_MANAGE_STATE(AfxGetStaticModuleState())
	
	TRY
	{

		CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
		
		if(pSuperVisApp->m_bSuperVisaterActive)
		{

			if(pSuperVisApp->m_SuperVisThread->m_bIsPSheettActive)
			{
				pSuperVisApp->m_SuperVisThread->SuperVisSheet->SendDestroyMsg();

			}

		}


	}
	END_TRY


#endif

}


/*
 *=============================================================================
 *  Description : Set the Supervisater as Active
 *					
 *=============================================================================
 *  Input  : void
 *
 *  Output : Void.
 *=============================================================================
 *  Creation date : 08/04/99            Author : Pascal Lalancette            
 *=============================================================================
 *  Modification date :				    Author : 
 *  Modifications :
 *=============================================================================
 */
void SND_CALL SND_fn_vSetSupervisaterActive(void)
{
	
	AFX_MANAGE_STATE(AfxGetStaticModuleState())
	
	TRY
	{

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	pSuperVisApp->m_bSuperVisaterActive = TRUE;

	}
	END_TRY
}


/*
 *=============================================================================
 *  Description : Retrieve the Supervisater state
 *					
 *=============================================================================
 *  Input  : void
 *
 *  Output : SndBool
 *=============================================================================
 *  Creation date : 08/04/99            Author : Pascal Lalancette            
 *=============================================================================
 *  Modification date :				    Author : 
 *  Modifications :
 *=============================================================================
 */
SndBool SND_CALL SND_fn_bIsSupervisaterActive(void)
{

	
	AFX_MANAGE_STATE(AfxGetStaticModuleState())
	
	TRY
	{

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	return pSuperVisApp->m_bSuperVisaterActive;

	}
	END_TRY
}



//ajout d'un couple evt-objet
void SND_CALL dbgSND_fn_vAddEventToObject(long indice,long type,long asso,SND_tduRefEvt evt)
{
/*	dbgSND_tdstObjectStruct* obj=NULL;
	int i,iIns;
	BOOL obj_found=FALSE;
	LV_ITEM lvi;
	char texte[80];
	char info[10];

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		if (!dbg_objects_init)
		{
			dbg_objects_init=TRUE;
			SNDLST2_M_DynamicInitAnchor(&dbg_objects.pList);
		}

		SNDLST2_M_DynamicForEachElementOf(&dbg_objects.pList,obj,i)
		{
			if (obj->indice==indice) 
			{
				obj_found=TRUE;
				break;
			}
		}

		if (obj_found)
		{
			add_evt(obj,asso,evt);
		}
		else
		{
			obj=GlobalAlloc(GMEM_FIXED,sizeof(dbgSND_tdstObjectStruct));
			memset(obj,0,sizeof(dbgSND_tdstObjectStruct));
			SNDLST2_M_DynamicInitElement(obj);
			obj->indice=indice;
			SNDLST2_M_DynamicInitAnchor(&obj->evts.pList);
			SNDLST2_M_DynamicAddTail(&dbg_objects.pList,obj);

			add_evt(obj,asso,evt);

			//raj liste
			if (hwndSupervisater)
			{
				memset(&lvi,0,sizeof(lvi));
				lvi.mask=LVIF_TEXT|LVIF_PARAM; 
				lvi.iItem=0; 
				lvi.iSubItem=0;
				dbgSND_fn_vGetInfoForObjectSound(indice,type,info,sizeof(info)-1);info[sizeof(info)-1]=0;
				sprintf(texte,"0x%x (%s)",obj->indice,info);
				lvi.pszText=texte;
				lvi.lParam=obj->indice;
 				iIns=ListView_InsertItem(GetDlgItem(hwndSupervisater,IDC_LISTOBJECT),&lvi);
			}
		}

		if (hMutex) ReleaseMutex(hMutex);
	}

*/
}



// destruction d'un couple evt-couple
void SND_CALL dbgSND_fn_vKillEventToObjectPrivate(long indice,long asso)
{
/*	dbgSND_tdstObjectStruct* obj=NULL;
	int i,iFind,iDel;
	BOOL obj_found=FALSE;
	LV_FINDINFO lvf;

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		SNDLST2_M_DynamicForEachElementOf(&dbg_objects.pList,obj,i)
		{
			if (obj->indice==indice) 
			{
				obj_found=TRUE;
				break;
			}
		}

		if (obj_found)
		{
			del_evt(obj,asso);

			if (!SNDLST2_M_DynamicGetNumberOfElements(&obj->evts.pList) && delete_objects)
			{
				//destruction
				if (hwndSupervisater)
				{
					memset(&lvf,0,sizeof(LV_FINDINFO));
					lvf.flags=LVFI_PARAM;
					lvf.lParam=indice;
					iDel=ListView_DeleteItem(GetDlgItem(hwndSupervisater,IDC_LISTOBJECT)
						,iFind=ListView_FindItem(GetDlgItem(hwndSupervisater,IDC_LISTOBJECT),-1,&lvf)
						);
				}
				SNDLST2_M_DynamicIsolate(obj);
				GlobalFree(obj);
			}
		}

		if (hMutex) ReleaseMutex(hMutex);
	}

*/
}




//raj des paramètres d'un couple
void SND_CALL dbgSND_fn_vSetPosObject(long indice,SoundParam* par)
{
/*	dbgSND_tdstObjectStruct* obj=NULL;
	int i;
	BOOL obj_found=FALSE;

	if (bSupervisaterActive)
	{
		SNDLST2_M_DynamicForEachElementOf(&dbg_objects.pList,obj,i)
		{
			if (obj->indice==indice) 
			{
				obj_found=TRUE;
				break;
			}
		}

		if (obj_found)
		{
			memcpy(&obj->par,par,sizeof(SoundParam));
		}

  }
*/
}

	
//add a micro
void SND_CALL dbgSND_fn_vAddMicro(long indice)
{
/*	pdbgMICRO_DESCRIPTION mic;
	int iIns;
	LV_ITEM lvi;
	char texte[80];

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		if (!dbg_micros_init)
		{
			dbg_micros_init=TRUE;
			SNDLST2_M_DynamicInitAnchor(&dbg_micros.pList);
		}

		mic=GlobalAlloc(GMEM_FIXED,sizeof(dbgMICRO_DESCRIPTION));
		memset(mic,0,sizeof(dbgMICRO_DESCRIPTION));
		SNDLST2_M_DynamicInitElement(mic);
		mic->Id=indice;
		SNDLST2_M_DynamicAddTail(&dbg_micros.pList,mic);
		//raj liste
		if (hwndSupervisater)
		{
			memset(&lvi,0,sizeof(lvi));
			lvi.mask=LVIF_TEXT|LVIF_PARAM; 
			lvi.iItem=0; 
			lvi.iSubItem=0;
			sprintf(texte,"0x%x",mic->Id);
			lvi.pszText=texte;
			lvi.lParam=mic->Id;
 			iIns=ListView_InsertItem(GetDlgItem(hwndSupervisater,IDC_LISTMICRO),&lvi);
		}

		if (hMutex) ReleaseMutex(hMutex);
	}

*/
}


//destroy z micro
void SND_CALL dbgSND_fn_vKillMicro(long indice)
{
/*	BOOL mic_found=FALSE;
	pdbgMICRO_DESCRIPTION mic;
	int i,iDel,iFind;
	LV_FINDINFO lvf;

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		SNDLST2_M_DynamicForEachElementOf(&dbg_micros.pList,mic,i)
		{
			if (mic->Id==indice) 
			{
				mic_found=TRUE;
				break;
			}
		}

		if (mic_found)
		{
			//raj liste
			if (hwndSupervisater)
			{
				memset(&lvf,0,sizeof(LV_FINDINFO));
				lvf.flags=LVFI_PARAM;
				lvf.lParam=indice;
				iDel=ListView_DeleteItem(GetDlgItem(hwndSupervisater,IDC_LISTMICRO)
					,iFind=ListView_FindItem(GetDlgItem(hwndSupervisater,IDC_LISTMICRO),-1,&lvf)
					);
			}
			SNDLST2_M_DynamicIsolate(mic);
			GlobalFree(mic);
		}

		if (hMutex) ReleaseMutex(hMutex);
	}

*/
}


//raj micro
void SND_CALL dbgSND_fn_vSetPosMicro(long indice,MicroParam* par)
{
/*	BOOL mic_found=FALSE;
	pdbgMICRO_DESCRIPTION mic;
	int i;

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		SNDLST2_M_DynamicForEachElementOf(&dbg_micros.pList,mic,i)
		{
			if (mic->Id==indice) 
			{
				mic_found=TRUE;
				break;
			}
		}

		if (mic_found)
		{
			memcpy(&mic->par,par,sizeof(MicroParam));
		}

		if (hMutex) ReleaseMutex(hMutex);
	}

  */
}

/*
 *=============================================================================
 *  Description : Exported Events Historic function. Add the events in
 *					the Historic list
 *=============================================================================
 *  Input  : SND_tduRefEvt			evt	
 *			 long					indice
 *			 long					type_obj
 *			 long					asso
 *			 tdstBlockResourceMem*	res
 *
 *  Output : Void.
 *=============================================================================
 *  Creation date : 23/03/99            Author : Pascal Lalancette            
 *=============================================================================
 *  Modification date :				    Author : 
 *  Modifications :
 *=============================================================================
 */
void SND_CALL dbgSND_fn_vAddHistoric(SND_tduRefEvt evt,long indice,long type_obj,long asso,tdstBlockResourceMem* res)
{


	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	TRY
	{

		char buffer[50], buffer2[50];

		CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

		if(pSuperVisApp->m_bSuperVisaterActive)
		{
		
		
			//Allocate memomry for a new hisoric element. Will be free when the apps quit.
			EventsHistElement *evtHisto = new EventsHistElement;
			ResourceElement *supervisRes = new ResourceElement;

			SCR_tdst_Cxt_Values *pScriptContext;

			
			//
			//We add the Event data in the Event History page
			//

			//Fill the new element with data
			GetLocalTime(&evtHisto->time);
			evtHisto->evt_pstPtr = evt.pstPtr;
			evtHisto->evt_Id = evt.pstPtr->Id;
			evtHisto->evt_eType = evt.pstPtr->eType;
			evtHisto->res_pstPtr = res;
			evtHisto->asso = asso;


			pSuperVisApp->m_SuperVisThread->m_lEventToAddBankId = (0xFFFF0000 & evtHisto->evt_Id) >> 16;
			pSuperVisApp->m_SuperVisThread->m_lEventToAddId = (0x0000FFFF & evtHisto->evt_Id);
			
			//We find the bank name...
			//We analyzing the LCB header to find the CSB file
			strcpy(buffer, (LPCSTR)(pSuperVisApp->m_cLCBFileTitle));
			strcat(buffer, ".lcb^SndEventGroupList");
			//pScriptContext = SCR_fnp_st_RdL0_AnalyseSection("playmo_b.lcb^SndEventGroupList", SCR_CDF_uw_Anl_ForceAnalyse);
			pScriptContext = SCR_fnp_st_RdL0_AnalyseSection(buffer, SCR_CDF_uw_Anl_ForceAnalyse);
			evtHisto->cEvtBankName = pSuperVisApp->m_SuperVisThread->m_cEventToAddBankName;


			strcpy(buffer, (LPCSTR)(pSuperVisApp->m_SuperVisThread->m_cEventToAddBankName));
			strcat(buffer, ".csb^SndEventE:");
			itoa(pSuperVisApp->m_SuperVisThread->m_lEventToAddId, buffer2, 10);
			strcat(buffer, buffer2 );

			//We find the Event name...
			//We analyzing the CSB header
			pScriptContext = SCR_fnp_st_RdL0_AnalyseSection(buffer, SCR_CDF_uw_Anl_ForceAnalyse);
			evtHisto->cEvtName = pSuperVisApp->m_SuperVisThread->m_cEventToAddName;

			//
			//We add the events name in the list
			//
			
			//Add Envent history element in the global pointer Array
			 long lEventIndex = pSuperVisApp->m_SuperVisThread->m_apEventHistoArray.Add(evtHisto);


			//Are we paused?...
			if(!pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_EventsHistoryPage.m_bPauseProcess)
			{
				//Add the new  Envent history element in the history list control of the EventHistoryPage
				pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_EventsHistoryPage.FilterAndDisplayEvent(lEventIndex);
			}



			//
			//We add the ressources data in the mixer page
			//

			//Fill the new element with data, dont do anything if res == NULL.
			//Its a STOP_EVENT with no resource
			if(res != NULL)
			{

				pSuperVisApp->m_SuperVisThread->m_lResToAddBankId = (0xFFFF0000 & res->Id) >> 16;
				pSuperVisApp->m_SuperVisThread->m_lResToAddId = (0x0000FFFF & res->Id);

				GetLocalTime(&supervisRes->time);
				supervisRes->ulId = res->Id;
				supervisRes->tdeType = res->eType;
				supervisRes->ucVolume  = res->ucVolume;
				supervisRes->res  = res;
				

				//We find the bank name...
				//We analyzing the LCB header to find the CSB file
				strcpy(buffer, (LPCSTR)(pSuperVisApp->m_cLCBFileTitle));
				strcat(buffer, ".lcb^SndEventGroupList");
				pScriptContext = SCR_fnp_st_RdL0_AnalyseSection(buffer, SCR_CDF_uw_Anl_ForceAnalyse);
				supervisRes->cResBankName = pSuperVisApp->m_SuperVisThread->m_cResToAddBankName;
				
				strcpy(buffer, (LPCSTR)(pSuperVisApp->m_SuperVisThread->m_cResToAddBankName));
				strcat(buffer, ".csb^SndResourceE:");
				itoa(pSuperVisApp->m_SuperVisThread->m_lResToAddId, buffer2, 10);
				strcat(buffer, buffer2 );

				//We find the Res name...
				//We analyzing the CSB header
				pScriptContext = SCR_fnp_st_RdL0_AnalyseSection(buffer, SCR_CDF_uw_Anl_ForceAnalyse);
				supervisRes->cResName = pSuperVisApp->m_SuperVisThread->m_cResToAddName;

				//Add Resource element in the global pointer Array
				long lResIndex = pSuperVisApp->m_SuperVisThread->m_apResources.Add(supervisRes);

				if(!pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_MixerPage.m_bPauseProcess)
				{
					//Add the new  Envent history element in the history list control of the EventHistoryPage
					pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_MixerPage.FilterAndDisplayRes(lResIndex);

				}



	//			res->ucVolume = 0;
				
			}

		}

	}
	END_TRY

}




//----ligne
void SND_CALL dbgSND_fn_vKillVolumeLine(long line)
{
/*	pdbgVOLUMELINE_DESCRIPTION dbgpvol,dbgpvolnext;
	int i;

	SNDLST2_M_DynamicForEachMovingElementOf(&dbgVolumeLineSnd.pList,dbgpvol,dbgpvolnext,i)
	{
		if (dbgpvol->id==line)
		{
			SNDLST2_M_DynamicIsolate(dbgpvol);
			GlobalFree(dbgpvol);
		}
	}
*/
}


void SND_CALL dbgSND_fn_vAddVolumeLine(long line)
{
/*	int iIns;
	LV_ITEM lvi;
	LV_COLUMN lvc;
	char texte[80];
	pdbgVOLUMELINE_DESCRIPTION dbgpvol;

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		if (!dbgVolumeLineInit)
		{
			SNDLST2_M_DynamicInitAnchor(&dbgVolumeLineSnd.pList);
			dbgVolumeLineInit=TRUE;
		}

		dbgpvol=GlobalAlloc(GMEM_FIXED,sizeof(dbgVOLUMELINE_DESCRIPTION));
		memset(dbgpvol,0,sizeof(dbgVOLUMELINE_DESCRIPTION));
		SNDLST2_M_DynamicInitElement(dbgpvol);
		SNDLST2_M_DynamicAddTail(&dbgVolumeLineSnd.pList,dbgpvol);
		dbgpvol->id=line;
		dbgpvol->vol=127;

		//raj liste
		if (hwndDiversSnd)
		{
			memset(&lvi,0,sizeof(lvi));
			lvi.mask=LVIF_TEXT|LVIF_PARAM; 
			lvi.iItem=0; 
			lvi.iSubItem=0;
			sprintf(texte,"0x%x",line);
			lvi.pszText=texte;
			lvi.lParam=line;
 			iIns=ListView_InsertItem(GetDlgItem(hwndDiversSnd,IDC_LISTE),&lvi);

			memset(&lvc,0,sizeof(lvc));
			sprintf(texte,"%d",127);
			lvc.pszText=texte;
			lvc.cx=diverssnd_column[1].width;
			lvc.iSubItem=1;
			lvc.mask=LVCF_TEXT|LVCF_SUBITEM|LVCF_WIDTH; 
			diverssnd_column[1].id_col=ListView_InsertColumn(GetDlgItem(hwndDiversSnd,IDC_LISTE),1,&lvc);
		}

		if (hMutex) ReleaseMutex(hMutex);
	}
*/}


void SND_CALL dbgSND_fn_vSetVolumeLine(long line,long vol)
{
/*	pdbgVOLUMELINE_DESCRIPTION dbgpvol;
	int i;

	SNDLST2_M_DynamicForEachElementOf(&dbgVolumeLineSnd.pList,dbgpvol,i)
	{
		if (dbgpvol->id==line)
		{
			dbgpvol->vol=vol;
		}
	}

	refresh_volume_line();

*/
}
void SND_CALL dbgSND_fn_vLoadBank(int iId)
{
}

void SND_CALL dbgSND_fn_vUnLoadBank(int iId)
{
}



void SND_CALL dbgSND_fn_vAddVoiceSample(long id,tduRefRes res,SampleParam* par)
{
/*	BOOL visusnd_found=FALSE;
	pdbgVISUSND_ELEMENT visusnd;
	int i,iIns;
	LV_ITEM lvi;
	char texte[80];

	if (bSupervisaterActive)
	{
		if (!dbg_visusnd_init)
		{
			dbg_visusnd_init=TRUE;
			SNDLST2_M_DynamicInitAnchor(&dbg_visusnd.pList);
		}

		SNDLST2_M_DynamicForEachElementOf(&dbg_visusnd.pList,visusnd,i)
		{
			if (visusnd->id==id) 
			{
				visusnd_found=TRUE;
				break;
			}
		}

		if (!visusnd_found)
		{
			visusnd=GlobalAlloc(GMEM_FIXED,sizeof(dbgVISUSND_ELEMENT));
			memset(visusnd,0,sizeof(dbgVISUSND_ELEMENT));
			SNDLST2_M_DynamicInitElement(visusnd);
			visusnd->id=id;
			SNDLST2_M_DynamicAddTail(&dbg_visusnd.pList,visusnd);
			//raj liste
			if (hwndVisuSnd)
			{
				memset(&lvi,0,sizeof(lvi));
				lvi.mask=LVIF_TEXT|LVIF_PARAM; 
				lvi.iItem=0; 
				lvi.iSubItem=0;
				sprintf(texte,"0x%x",visusnd->id);
				lvi.pszText=texte;
				lvi.lParam=visusnd->id;
 				iIns=ListView_InsertItem(GetDlgItem(hwndVisuSnd,IDC_LISTE),&lvi);
			}
		}

		visusnd->res_pstPtr=res.pstPtr;
		visusnd->res_Id=res.pstPtr->Id;
		memcpy(&visusnd->par,par,sizeof(SampleParam));

		if (hMutex) ReleaseMutex(hMutex);
	}
*/}

	
void SND_CALL dbgSND_fn_vSetParVoiceSample(long id,SampleParam* par)
{
/*	pdbgVISUSND_ELEMENT visusnd;
	int i;
	BOOL visusnd_found=FALSE;

	if (bSupervisaterActive)
	{
		SNDLST2_M_DynamicForEachElementOf(&dbg_visusnd.pList,visusnd,i)
		{
			if (visusnd->id==id) 
			{
				visusnd_found=TRUE;
				break;
			}
		}

		if (visusnd_found)
		{
			memcpy(&visusnd->par,par,sizeof(SampleParam));
		}
	}
*/}

	
void SND_CALL dbgSND_fn_vDelVoiceSample(long id)
{
/*	pdbgVISUSND_ELEMENT visusnd;
	BOOL visusnd_found=FALSE;
	int i,iDel,iFind;
	LV_FINDINFO lvf;

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		SNDLST2_M_DynamicForEachElementOf(&dbg_visusnd.pList,visusnd,i)
		{
			if (visusnd->id==id) 
			{
				visusnd_found=TRUE;
				break;
			}
		}

		if (visusnd_found)
		{
			if (hwndVisuSnd)
			{
				memset(&lvf,0,sizeof(LV_FINDINFO));
				lvf.flags=LVFI_PARAM;
				lvf.lParam=id;
				iDel=ListView_DeleteItem(GetDlgItem(hwndVisuSnd,IDC_LISTE)
					,iFind=ListView_FindItem(GetDlgItem(hwndVisuSnd,IDC_LISTE),-1,&lvf)
					);
			}
			SNDLST2_M_DynamicIsolate(visusnd);
			GlobalFree(visusnd);
		}

		if (hMutex) ReleaseMutex(hMutex);
	}

*/  
}


void SND_CALL dbgSND_fn_vPauseVoiceSample(long id)
{
/*	pdbgVISUSND_ELEMENT visusnd;
	BOOL visusnd_found=FALSE;
	int i;

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		SNDLST2_M_DynamicForEachElementOf(&dbg_visusnd.pList,visusnd,i)
		{
			if (visusnd->id==id) 
			{
				visusnd_found=TRUE;
				break;
			}
		}

		if (visusnd_found)
		{
			visusnd->bPaused=TRUE;
		}

		if (hMutex) ReleaseMutex(hMutex);
	}
*/
}


void SND_CALL dbgSND_fn_vResumeVoiceSample(long id)
{
/*	pdbgVISUSND_ELEMENT visusnd;
	BOOL visusnd_found=FALSE;
	int i;

	if (bSupervisaterActive)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

		SNDLST2_M_DynamicForEachElementOf(&dbg_visusnd.pList,visusnd,i)
		{
			if (visusnd->id==id) 
			{
				visusnd_found=TRUE;
				break;
			}
		}

		if (visusnd_found)
		{
			visusnd->bPaused=FALSE;
		}

		if (hMutex) ReleaseMutex(hMutex);
	}

*/
}


void SND_CALL dbgSND_fn_vSetProcessTimeSample(float ratio)
{
/*	char texte[256];

	if (bSupervisaterActive)
	{
		if (hwndVisuSnd)
		{
			sprintf(texte,"%d/1000",(int)(1000.0*ratio));
			SetDlgItemText(hwndVisuSnd,IDC_PROCESSTIME,texte);
		}
	}
*/
}


//fonctions pour éditeur
tdstBlockResourceMem* SND_CALL dbgSND_fn_pstGetSelectedResource(void)
{
/*	if (hwndHistoric)
		return res_for_editor;
	else
*/		return NULL;
}




SndBool SND_CALL dbgSND_fn_bDoesSupervisaterSelectResource(void)
{
/*	if (hwndHistoric)
	{
		if (SendDlgItemMessage(hwndHistoric,IDC_SELECTRESOURCE,BM_GETCHECK,0,0)==BST_CHECKED)
*/			return TRUE;
/*	}

	return FALSE;
*/}




void SND_CALL dbgSND_fn_vRajListesSxdVirtual2(SND_tdhVirtualModule p_virtual_module)
{
/*	virtual_voice_list* VV=&p_virtual_module->virtual_voices;
	audible_voice_list* VA=&p_virtual_module->audible_voices;
	dead_voice_list* VD=&p_virtual_module->dead_voices;
	p_virtual_voice v;
	pdbgVISUVIRT_ELEMENT dbgv,dbgvnext;
	SndBool bFound;
	int i,j;
	int id_module;

	if (hwndVisuVirt)
	{
		if (hMutex) WaitForSingleObject(hMutex,INFINITE);

*/		/*zz
		hwnd=GetDlgItem(hwndVisuVirt,IDC_LISTVV);
		*/
/*
		//recherche du bon module
		bFound=FALSE;
		for (id_module=0;id_module<iNbVirtModule;id_module++)
		{
			if (p_vv_list[id_module].iIdModule==(long)p_virtual_module)
			{
				bFound=TRUE;
				break;
			}
		}

		if (bFound && id_module==iCurrentVirtModule)
		{
*/			/*zz
			sprintf(texte,"%d/%d",SNDLST2_M_DynamicGetNumberOfElements(&p_virtual_module->virtual_voices.pList),p_virtual_module->g_lNbSxdAudible);
			SetDlgItemText(hwndVisuVirt,IDC_NBVOIX,texte);
			*/
/*
			//1:élimination des lignes obsolètes
			SNDLST2_M_DynamicForEachMovingElementOf(&p_vv_list[id_module].pList,dbgv,dbgvnext,i)
			{
				bFound=FALSE;
				SNDLST2_M_DynamicForEachElementOf(&VV->pList,v,j)
				{
					if (v==dbgv->id)
					{
						bFound=TRUE;
						break;
					}
				}
				if (!bFound)
				{//to be destroyed
*/					/*zz
					memset(&lvf,0,sizeof(LV_FINDINFO));
					lvf.flags=LVFI_PARAM;
					lvf.lParam=(long)dbgv;
					iFind=ListView_FindItem(hwnd,-1,&lvf);
					if (iFind!=-1)
						iDel=ListView_DeleteItem(hwnd,iFind);

					SNDLST2_M_DynamicIsolate(dbgv);
					GlobalFree(dbgv);
					*/
/*					dbgv->bStillActive=FALSE;
				}
			}
			//2:raj de anciennes lignes et ajoûts des nouvelles
			SNDLST2_M_DynamicForEachElementOf(&VV->pList,v,i)
			{//pour chacune voies actuelle
				bFound=FALSE;
				SNDLST2_M_DynamicForEachElementOf(&p_vv_list[id_module].pList,dbgv,j)
				{//vette voix éxistait-elle déjà
					if ((dbgv->id==v) && (dbgv->bStillActive))
					{
						bFound=TRUE;
						break;
					}
				}
				if (!bFound)
				{
					dbgv=GlobalAlloc(GMEM_FIXED,sizeof(*dbgv));
					SNDLST2_M_DynamicInitElement(dbgv);
					SNDLST2_M_DynamicAddTail(&p_vv_list[id_module].pList,dbgv);

					dbgv->bDisplayed=FALSE;
					dbgv->bStillActive=TRUE;
*//*zz
					memset(&lvi,0,sizeof(lvi));
					lvi.mask=LVIF_TEXT|LVIF_PARAM; 
					lvi.iItem=0; 
					lvi.iSubItem=0;
					sprintf(texte,"0x%x",dbgv);
					lvi.pszText=texte;
					lvi.lParam=(long)dbgv;
 					iIns=ListView_InsertItem(hwnd,&lvi);
					*/
/*				}
				dbgv->id=v;
				dbgv->rNorme=v->rNorme;
				dbgv->lInstance=v->lInstance;
				dbgv->audible=v->audible;
				dbgv->lVoice=v->lVoice;
				dbgv->bPaused=v->bPaused;
				dbgv->lResId=v->tduRes.pstPtr->Id;
				dbgv->lResRef=(long)v->tduRes.pstPtr;
				dbgv->bLoop=v->tduRes.pstPtr->uRes.stSample.bLoop;

*/				/*zz
				memset(&lvf,0,sizeof(LV_FINDINFO));
				lvf.flags=LVFI_PARAM;
				lvf.lParam=(long)dbgv;
				iFind=ListView_FindItem(hwnd,-1,&lvf);

				sprintf(texte,"%f",M_RealToDoubleSnd(dbgv->rNorme));
				ListView_SetItemText(hwnd,iFind,vv_column[VV_COL_NORME].id_col,texte);
				sprintf(texte,"%s",dbgv->bPaused?"P":" ");
				ListView_SetItemText(hwnd,iFind,vv_column[VV_COL_PAUSE].id_col,texte);
				sprintf(texte,"%d",dbgv->lInstance);
				ListView_SetItemText(hwnd,iFind,vv_column[VV_COL_INSTANCE].id_col,texte);
				if (dbgv->audible)
				{
					sprintf(texte,"0x%x",dbgv->audible);
					ListView_SetItemText(hwnd,iFind,vv_column[VV_COL_VA].id_col,texte);
					sprintf(texte,"0x%x",dbgv->lVoice);
					ListView_SetItemText(hwnd,iFind,vv_column[VV_COL_VH].id_col,texte);
				}
				else
				{
					sprintf(texte," ");
					ListView_SetItemText(hwnd,iFind,vv_column[VV_COL_VA].id_col,texte);
					sprintf(texte," ");
					ListView_SetItemText(hwnd,iFind,vv_column[VV_COL_VH].id_col,texte);
				}
				*/
/*			}
		}//if bFound
*/


/*		if (hMutex) ReleaseMutex(hMutex);
	}

*/
}

void SND_CALL dbgSND_fn_vInitListesSxdVirtual(long virtual_module,char* descrpition)
{
/*	if (hMutex) WaitForSingleObject(hMutex,INFINITE);

	if (iNbVirtModule)
		p_vv_list=GlobalReAlloc(p_vv_list,(iNbVirtModule+1)*sizeof(*p_vv_list),GMEM_MOVEABLE);
	else
		p_vv_list=GlobalAlloc(GMEM_FIXED,sizeof(*p_vv_list));

	p_vv_list[iNbVirtModule].iIdModule=virtual_module;
	strncpy(p_vv_list[iNbVirtModule].szDescription,descrpition,sizeof(p_vv_list[iNbVirtModule].szDescription)-1);

	iNbVirtModule++;

	if (hMutex) ReleaseMutex(hMutex);

*/
}



//redfresh Supervistaer display
void SND_CALL dbgSND_fn_vRefreshSupervisater(void)
{
/*	if (bSupervisaterActive)
	{
		if (hwndHistoric)
		{
			ListView_DeleteAllItems(GetDlgItem(hwndHistoric,IDC_LISTE));
			refresh_historic();
		}
	}
*/
}




void SND_CALL dbgSND_fn_vKillEventToObject(long indice,long asso)
{
//bgSND_fn_vKillEventToObjectPrivate(indice,asso);
}



//Used to get the message pump...
SndBool SND_CALL dbgSND_fn_bFilterDllMsg(LPMSG lpMsg)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	TRY
	{
		return AfxGetThread()->PreTranslateMessage(lpMsg);
	}
	END_TRY
	return FALSE;
}


int CSupervisApp::ExitInstance() 
{

	long i;
	EventsHistElement* tempEvt;
	ResourceElement* tempRes;


	//Save global app variable to registry
	WriteProfileString("Settings", "LCBPath", m_cLCBPath);
	WriteProfileString("Settings", "LCBFileTitle", m_cLCBFileTitle);


	//Free the events history array
	for (i= 0;  i <m_SuperVisThread->m_apEventHistoArray.GetSize() ;i++)
	{
		tempEvt = (EventsHistElement *)(m_SuperVisThread->m_apEventHistoArray.GetAt(i));
		delete tempEvt;
	}

	m_SuperVisThread->m_apEventHistoArray.RemoveAll();


	
	//Free the Resources array
	for (i= 0;  i <m_SuperVisThread->m_apResources.GetSize() ;i++)
	{
		tempRes = (ResourceElement *)(m_SuperVisThread->m_apResources.GetAt(i));
		delete tempRes;
	}

	m_SuperVisThread->m_apResources.RemoveAll();



	//Remove Ole object
	OleUninitialize();



	return CWinApp::ExitInstance();
}



SCR_tde_Anl_ReturnValue fn_AnalyseLCB(
	SCR_tdst_File_Description		*_p_stFile,
	char 	           				*_p_szName,
	char             				*_ap_szParameters[],
	SCR_tde_Anl_Action				_eAction )
{



	unsigned long ulEventGroupId;

    switch (_eAction)
    {
		case SCR_EA_Anl_Entry:
			if (strcmp(_p_szName,"LoadEventGroup")==0)
			{
				if (SCR_fn_uc_RdL0_GetNumberOfParameters(_ap_szParameters) == 1)
					ulEventGroupId = 0;
				else
				{
					ulEventGroupId = atoi(_ap_szParameters[1]);
					//For event
					if ( ulEventGroupId == (unsigned long)((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_lEventToAddBankId)
						((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_cEventToAddBankName = _ap_szParameters[0];
					//For resource
					if ( ulEventGroupId == (unsigned long)((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_lResToAddBankId)
						((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_cResToAddBankName = _ap_szParameters[0];
				}

			}
			break;
	}
	return SCR_ERV_Anl_NormalReturn;

}




SCR_tde_Anl_ReturnValue fn_AnalyseCsbHeader(
	SCR_tdst_File_Description		*_p_stFile,
	char 	           				*_p_szName,
	char             				*_ap_szParameters[],
	SCR_tde_Anl_Action				_eAction )
{

	switch (_eAction)
    {

    case SCR_EA_Anl_AlreadyAnalysed:
		break;
	
	case SCR_EA_Anl_BeginSection:
		break;
	
	case SCR_EA_Anl_Entry:
		if (strcmp(_p_szName,"SetNextFreeResourceId")==0){
		}
		else if (strcmp(_p_szName,"SetNextFreeEventId")==0){
			
			//We need to know how many events there is in the banks
//			g_ulNbEventsInBank = *(unsigned long *)_ap_szParameters[0];
		}
		break;

	}

	return SCR_ERV_Anl_NormalReturn;

}


SCR_tde_Anl_ReturnValue fn_FindEventNameInCSB(
	SCR_tdst_File_Description		*_p_stFile,
	char							*_p_szName,
	char							*_ap_szParameters[],
	SCR_tde_Anl_Action				_eAction)
{
/*	static unsigned long ulNextEventBinId=0;
    SND_tdstBlockEvent *pEvent;
	char *pszFileName;
	char *szSectionName;
    unsigned long ulBinId,ulTemp;
	SCR_tdst_Link_Value   *p_stValue;
	char szTemp[MAX_PATH];
*/
  
	switch (_eAction)
    {

    case SCR_EA_Anl_AlreadyAnalysed:
/*		// Reanalysis of the event section: we recheck the pointer value,
		//	in case it changed:
		szSectionName=SCR_M_RdL0_GetCompleteSectionNameR(0);
		ulBinId=SND_fn_ulGetEventBinIdFromSectionName(szSectionName);
		if (HIWORD(ulBinId)==SND_EXTRA_BANKID) break;
		pEvent=SND_fn_pGetBinEvent(ulBinId);
		SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pEvent);
		break;
*/
	case SCR_EA_Anl_BeginSection:

		
		
		/*// Get current event section name:
		szSectionName=SCR_M_RdL0_GetCompleteSectionNameR(0);
		sprintf(szTemp,"\tLoadEvent(%s)",szSectionName);
		SND_fn_bLogSoundLine(szTemp);
		// Try and find this section in the binary bank list:
		p_stValue=SCR_fnp_st_Link_SearchKey(&SND_gs_BinEventConversionTable,(char*)szSectionName);
		if (p_stValue!=NULL)
		{	// Sound event is in a binary bank, try to get a pointer:
			ulBinId=SCR_M_ul_Link_GetValue(p_stValue);
			pEvent=SND_fn_pGetBinEvent(ulBinId);
			if ((pEvent==NULL)&& g_bForceLoadBanks)
			{	// Bank is not loaded: try to load it
				SND_fn_bLoadBinaryBank(HIWORD(ulBinId));
				pEvent=SND_fn_pGetBinEvent(ulBinId);
			}
			// Set pointer in section context (even if it's null, meaning bank isn't loaded):
			SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pEvent);
			// Stop this event's section analysis:
			return  SCR_ERV_Anl_TerminateCurrentSection;
		}
		else 
		{ // sound event is in no binary bank: load it in extra bank:
			// Create event:
			pEvent=(SND_tdstBlockEvent*)SND_fn_pvMallocSnd(sizeof(SND_tdstBlockEvent));
			memset(pEvent,0,sizeof(SND_tdstBlockEvent));

			SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pEvent);
			// Register this new event in the extra bank's link table:  
			ulTemp=(SND_EXTRA_BANKID<<16)+ulNextEventBinId;
			ulNextEventBinId++;
			SCR_fnp_st_Link_SetValue(&SND_gs_ExtraBankEventsTable,
									szSectionName,ulTemp);
			// Get event type:
			pEvent->eType=SND_fn_eGetEventTypeFromString(szParams[0]);

			// Get event editor Id (file id&section name):
			pszFileName=SCR_M_RdL0_GetFileNameR(0);
			ulTemp=SND_fn_ulGetEventGroupId(pszFileName);
			ulTemp<<=16;
			ulTemp+=atoi(szAction);
			pEvent->Id=ulTemp;
			SCR_M_RdL0_SetSectionLong(0,1,ulTemp);

		
		 }*/
		break;
	case SCR_EA_Anl_Entry:
		if (strcmp(_p_szName,"SetName")==0)
			{
				
			
			
			if (SCR_fn_uc_RdL0_GetNumberOfParameters(_ap_szParameters) == 1)
				((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_cEventToAddName = _ap_szParameters[0];
				else
				{
					((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_cEventToAddName = "No Event Name found";
				}

			}
			break;
	
	
	
	
	
	
	/*SCR_M_RdL0_GetSectionLong(0,0,SND_tdstBlockEvent *,pEvent);

		if (strcmp(szAction,"SetStopable")==0)  
			pEvent->bStopable=(SndBool)(*szParams[1]);

		else if (strcmp(szAction,"SetDynamic")==0)  
			pEvent->bDynamic=(SndBool)(*szParams[1]);

		else
		{
			switch (pEvent->eType)
			{
			case EVT_SON_PLAY: 
				 SND_fn_vAnalyseEventParamsPlay(szAction,szParams,pEvent);
				break;
			case EVT_SON_STOP:
				 SND_fn_vAnalyseEventParamsStop(szAction,szParams,pEvent);
				break;
			case EVT_SON_STOP_N_GO:
				 SND_fn_vAnalyseEventParamsStopNGo(szAction,szParams,pEvent);
					break;
			case EVT_SON_STOP_N_GO_CROSSFADE:
				 SND_fn_vAnalyseEventParamsStopNGoFade(szAction,szParams,pEvent);
					break;
			case EVT_SON_PITCH:
			case EVT_SON_VOLUME:
			case EVT_SON_PAN:
				 SND_fn_vAnalyseEventParamsXtraParam(szAction,szParams,pEvent);
					break;
			case EVT_SON_EXTRA:
				 SND_fn_vAnalyseEventParamsExtraAll(szAction,szParams,pEvent);
					break;
			case EVT_SON_CHANGE_VOLUME:
				 SND_fn_vAnalyseEventChangeVolume(szAction,szParams,pEvent);
					break;
			case EVT_SON_EFFET:
				 SND_fn_vAnalyseEventParamsEffect(szAction,szParams,pEvent);
					break;
			}
		}
		*/
    }        // end switch
  
	return  SCR_ERV_Anl_NormalReturn;

}


SCR_tde_Anl_ReturnValue fn_FindResNameInCSB(
	SCR_tdst_File_Description		*_p_stFile,
	char							*_p_szName,
	char							*_ap_szParameters[],
	SCR_tde_Anl_Action				_eAction)
{
  
	switch (_eAction)
    {

    case SCR_EA_Anl_AlreadyAnalysed:
	case SCR_EA_Anl_BeginSection:
	break;
	case SCR_EA_Anl_Entry:
		if (strcmp(_p_szName,"SetName")==0)
			{
				
			
			
			if (SCR_fn_uc_RdL0_GetNumberOfParameters(_ap_szParameters) == 1)
				((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_cResToAddName = _ap_szParameters[0];
				else
				{
					((CSupervisApp *)AfxGetApp())->m_SuperVisThread->m_cResToAddName = "No Resource Name found";
				}

			}
			break;
	
	}        // end switch
  
	return  SCR_ERV_Anl_NormalReturn;

}






void erreur(SCR_tdst_Err_GlobalError *test){

	//MessageBox(NULL, test->a_szBufferOut, test->p_szFileName, MB_OK);

}



