/*=========================================================================
* VSLoad.c : Loading VS Files
* 
* Version              1.0
* Creation date        05/05/97
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1997
*=======================================================================*/

#define PO_FRIEND
#include "ACP_Base.h"

#include "SCR.h"
#include "DPT.h"
#include "GEO.h"
#include "GLI.h"     
#include "COL.h"       
#include "SPO.h"
#include "PCS.h"
#include "PO.h"
//#include "BIN.h"
#include "GLIGLOU\MULTIDRV\inc\VisuS_st.h" // Must be defined as friend

#define VS_C_MaxStructureSize 32*1024

// global for VS Link Table
SCR_tdst_Link_Table VS_g_stLinkTable;

 /*-----------------------------------------------------------------------------
 *  Description : initialize and close link table for VS
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author : 
 *  Modifications : 
 *---------------------------------------------------------------------------*/
SCR_tdst_Link_Table *VS_fn_p_stGetLinkTable(void)
{
 return(&VS_g_stLinkTable);
}

void VS_fn_vInitLinkTable(void)
{
	SCR_fn_v_Link_InitTable(&VS_g_stLinkTable);
}

void VS_fn_vCloseLinkTable(void)
{
	SCR_fn_v_Link_CloseTable(&VS_g_stLinkTable);
}

/*-----------------------------------------------------------------------------
 *  Description : Used to register all script callback functions
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
void VS_fn_vRegisterAllScriptSections(void)
{
	SCR_fn_v_RdL0_RegisterCallback(C_Section_VSHeader,  VS_fn_eScriptCallBackHeader,  SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_VSGeneral, VS_fn_eScriptCallBackGeneral, SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(C_SubSection_LOD,    VS_fn_eScriptCallBackLOD,     SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_VSRLI,     GLI_fn_xLoadRLI,     SCR_CRC_c_RdL0_ForSection);
/*	SCR_fn_v_RdL0_RegisterCallback(C_Section_VSHeader,  VS_fn_eScriptCallBackBinHeader,  SCR_CRC_c_RdL0_ForBinSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_VSGeneral, VS_fn_eScriptCallBackBinGeneral, SCR_CRC_c_RdL0_ForBinSection);
	SCR_fn_v_RdL0_RegisterCallback(C_SubSection_LOD,    VS_fn_eScriptCallBackBinLOD,     SCR_CRC_c_RdL0_ForBinSection);
*/
}


/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of VS script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue VS_fn_eScriptCallBackHeader(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
	
	if ( M_IsEntry )
	{
		if ( M_ActionIs( C_Entry_FileVersion ) )
		{
			// not used yet
			//gs_wVSFileVersion = (short)fn_lAToI( _ap_szParams[0] );
		}
	}
	
	return ( eReturnValue );
}


/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of VS script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue VS_fn_eScriptCallBackGeneral(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
  GEO_tdxHandleToVisualSet _hVisualSet;
	char sz_LongPathForLinkTable[256];
	//char s[80];
	

	if ( M_IsTitle )	
	{
		_hVisualSet = GLI_p_stCreateVisualSet(atol(_ap_szParams[0]));
		SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hVisualSet);

		SCR_M_RdL0_ComputeOpenSectionNameR(0,sz_LongPathForLinkTable);
		SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(&VS_g_stLinkTable,sz_LongPathForLinkTable,(unsigned long)_hVisualSet),1,strlen(sz_LongPathForLinkTable)-strlen(SCR_M_RdL0_GetCompleteSectionNameR(0)));

		/*
		strcpy(sz_LongPathForLinkTable,fn_szGetLevelsDataPath());
		strcat(sz_LongPathForLinkTable,"\\");
		strcat(sz_LongPathForLinkTable,SCR_M_RdL0_GetCompleteSectionNameR(0));
		SCR_fnp_st_Link_SetValue(&VS_g_stLinkTable, sz_LongPathForLinkTable, (unsigned long)_hVisualSet);
		*/
		/*
		strcpy(s,SCR_M_RdL0_GetCompleteSectionNameR(0));
		strcat(s,"\n");
    OutputDebugString(s);
		*/
	}
	else if ( M_IsBegSubSection )
	{
		SCR_M_RdL0_GetSectionLong(C_ThisContext,0,GEO_tdxHandleToVisualSet,_hVisualSet);
		SCR_M_RdL0_SetContextLong(C_ChildContext,0,_hVisualSet);
		SCR_M_RdL0_SetContextLong(C_ChildContext,1,atol(_ap_szParams[1])-1);
		SCR_M_RdL0_SetContextDouble(C_ChildContext,0,0.0); //init Threshold=0
  }
	
	return ( eReturnValue );
}


/*-----------------------------------------------------------------------------
 *  Description :   call-back for LODs sections of VS script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue VS_fn_eScriptCallBackLOD(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
  GEO_tdxHandleToVisualSet _hVisualSet;
	//SCR_tdst_Cxt_Values *p_stVal;
	ACP_tdxHandleOfObject   _hGeometricObject;
	long	l_NumberOfLOD;
	double lf_Tmp;
	MTH_tdxReal xThreshold;
	
	if ( M_IsEntry )
	{
		if ( M_ActionIs( C_Entry_LOD_Threshold ) )
		{
			SCR_M_RdL0_SetContextDouble(C_ThisContext,0,atof(_ap_szParams[0]));
		}
		else if ( M_ActionIs( C_Entry_FichierMOD ) )
		{
			SCR_M_RdL0_GetContextLong(C_ThisContext,0,GEO_tdxHandleToVisualSet,_hVisualSet);
			SCR_M_RdL0_GetContextLong(C_ThisContext,1,long,l_NumberOfLOD);
			SCR_M_RdL0_GetContextDouble(C_ThisContext,0,lf_Tmp); xThreshold = (float)lf_Tmp;
			
			//p_stVal = SCR_fnp_st_RdL0_AnalyseSection(_ap_szParams[0], SCR_CDF_uw_Anl_Normal);
			//_hGeometricObject = (ACP_tdxHandleOfObject)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
			_hGeometricObject = GLI_pLoadGeometricInFile(_ap_szParams[0]);
			GLI_vSetVisualSetLOD(_hVisualSet,l_NumberOfLOD,xThreshold,_hGeometricObject);
		}
//ANNECY JMD 17/02/98 {
		else if ( M_ActionIs ( C_Entry_FichierRLI ) ) {
			long lNumberOfTable ;
			ACP_tdxHandleToRadiosity *d_hRLI ;

			SCR_M_RdL0_GetContextLong(C_ThisContext,0,GEO_tdxHandleToVisualSet,_hVisualSet);
			d_hRLI = GLI_vVisualSetLoadRLI (_ap_szParams[0], &lNumberOfTable ) ;
			GLI_vVisualSetSetRLITable(_hVisualSet, d_hRLI, lNumberOfTable) ;
		}
//END ANNECY JMD }
	}

	return ( eReturnValue );
}

/*-----------------------------------------------------------------------------
 *  Description :   function to load a Visual Set (script or binary load)
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
GEO_tdxHandleToVisualSet VS_fn_xLoadVisualSet(char* _szSectionName)
{
	SCR_tdst_Cxt_Values *p_stValues;
	SCR_tdst_Link_Value *p_stLinkValue;
	GEO_tdxHandleToVisualSet hVisualSet=NULL;
	unsigned int uiPos;


	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value, uiPos, p_stLinkValue,SCR_M_st_Link_GetDynamicArray(&VS_g_stLinkTable));
	while(p_stLinkValue)
	{
        if(!strcmpi(SCR_M_p_sz_Link_GetKey(p_stLinkValue) + SCR_M_ul_Link_GetAdditionalLong(p_stLinkValue,1),_szSectionName))
			break;
        uiPos++;
        SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value, uiPos, p_stLinkValue,SCR_M_st_Link_GetDynamicArray(&VS_g_stLinkTable));
	}
//	p_stLinkValue=BIN_fn_p_stSearchShortKey(&VS_g_stLinkTable,_szSectionName);
	if(p_stLinkValue==NULL)
	{
		p_stValues = SCR_fnp_st_RdL0_AnalyseSection(_szSectionName, SCR_CDF_uw_Anl_Normal);
		hVisualSet = (GEO_tdxHandleToVisualSet)SCR_M_ul_RdL0_ExtractLongValue(p_stValues,0);
		return hVisualSet;
	}
	else
		return (GEO_tdxHandleToVisualSet)p_stLinkValue->ulValue;
}

/*-----------------------------------------------------------------------------
 *  Description :   function to prepare the binary save process
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void VS_fn_vPrepareSaveBinaryBloc()
{
	BIN_fn_vCreateBinaryIds(&VS_g_stLinkTable,BIN_C_VSID);
	GEO_fn_vPrepareSaveGeometricObjects();
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write a binary visual set
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long VS_fn_ulWriteBinaryBloc(unsigned long _ulStructAddress, char* _p_cDestBuffer)
{
	GEO_tdstVisualSet* p_stVisualSet=(GEO_tdstVisualSet*)_ulStructAddress;
	char* p_cBufferPointer=_p_cDestBuffer;
	ACP_tdxIndex xI;

	p_cBufferPointer=BIN_fn_p_cPutReal(p_cBufferPointer,p_stVisualSet->xLastDistance);
	p_cBufferPointer=BIN_fn_p_cPutLong(p_cBufferPointer,p_stVisualSet->xNbLodDefinitions);
	p_cBufferPointer=BIN_fn_p_cPutLong(p_cBufferPointer,0); // place for the pointer
	p_cBufferPointer=BIN_fn_p_cPutLong(p_cBufferPointer,0); // place for the pointer

	p_cBufferPointer=BIN_fn_p_cPutStruct(p_cBufferPointer,(char*)p_stVisualSet->d_xThresholdsTable,sizeof(MTH_tdxReal)*p_stVisualSet->xNbLodDefinitions);
	for(xI=0;xI<p_stVisualSet->xNbLodDefinitions;++xI)
		p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,(unsigned long)p_stVisualSet->d_p_stLodDefinitions[xI],GLI_p_stGetLinkTableOfGeometric());

	return p_cBufferPointer-_p_cDestBuffer;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write all the binary visual sets
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void VS_fn_vWriteAllVSBinaryBlocs(char* _szBinaryFileName)
{
	VS_fn_vPrepareSaveBinaryBloc();
	BIN_fn_vPutBinaryDataIntoFileFromLinkTable(_szBinaryFileName,&VS_g_stLinkTable,
													VS_C_MaxStructureSize,VS_fn_ulWriteBinaryBloc);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary visual set
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long VS_fn_ulReadBinaryBloc(char* _p_cLoadedBuffer,unsigned long _ulLoadedBufferSize)
{
	GEO_tdstVisualSet* p_stVisualSet;
	ACP_tdxIndex xNbLOD;
	char* p_cBufferPointer=_p_cLoadedBuffer;
	ACP_tdxIndex xI;

	p_cBufferPointer=BIN_fn_p_cGetLong(p_cBufferPointer,&xNbLOD);
	p_stVisualSet = GLI_p_stCreateVisualSet(xNbLOD);
	p_cBufferPointer=BIN_fn_p_cGetReal(p_cBufferPointer,&p_stVisualSet->xLastDistance);
	p_cBufferPointer=BIN_fn_p_cGetStruct(p_cBufferPointer,(char*)p_stVisualSet->d_xThresholdsTable,sizeof(MTH_tdxReal)*p_stVisualSet->xNbLodDefinitions);
	for(xI=0;xI<p_stVisualSet->xNbLodDefinitions;++xI)
		p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&p_stVisualSet->d_p_stLodDefinitions[xI],GLI_p_stGetLinkTableOfGeometric());

	return (unsigned long)p_stVisualSet;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read all the binary blocs fort the visual sets
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void VS_fn_vReadAllVSBinaryBlocs(char* _szBinaryFileName)
{
	BIN_fn_vReadDataFromLinkTable(&VS_g_stLinkTable,VS_fn_ulReadBinaryBloc,VS_C_MaxStructureSize,_szBinaryFileName);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary visual set
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void VS_fn_vResolveBinaryBloc(unsigned long _ulAddress)
{
	GEO_tdstVisualSet* p_stVisualSet;
	ACP_tdxIndex xI;

	p_stVisualSet=(GEO_tdstVisualSet*)_ulAddress;

	p_stVisualSet->d_xThresholdsTable=(MTH_tdxReal*)((char*)p_stVisualSet+sizeof(GEO_tdstVisualSet));
	p_stVisualSet->d_p_stLodDefinitions=(ACP_tdxHandleOfObject *)((char*)p_stVisualSet+
																			sizeof(GEO_tdstVisualSet)+
																			p_stVisualSet->xNbLodDefinitions*sizeof(MTH_tdxReal));

	for(xI=0;xI<p_stVisualSet->xNbLodDefinitions;++xI)
		BIN_fn_p_cGetPointer((char*)&p_stVisualSet->d_p_stLodDefinitions[xI],(unsigned long*)&p_stVisualSet->d_p_stLodDefinitions[xI],GLI_p_stGetLinkTableOfGeometric());
}

char* VS_fn_p_cAllocateFunction(unsigned long _ulSize)
{
	char* p_cBuffer;
    GEO_M_CPAMalloc(p_cBuffer ,char* ,_ulSize ,E_uwGEONotEnoughtMemory );
	return p_cBuffer;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read all the binary blocs fort the visual sets
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void VS_fn_vReadAllVSBinaryBlocs2(char* _szBinaryFileName)
{
	BIN_fn_vReadDataFromLinkTableWithoutAllocation(&VS_g_stLinkTable,VS_fn_vResolveBinaryBloc,
													VS_C_MaxStructureSize,_szBinaryFileName,
													VS_fn_p_cAllocateFunction);
}
*/
/*
BEGIN OS 02.03.98
  Callbacks for bin files.
*/

/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of VS script file
 *---------------------------------------------------------------------------*/
/*
SCR_tde_Anl_ReturnValue VS_fn_eScriptCallBackBinHeader(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;

    
	if( _eAction == SCR_EA_Anl_AlreadyAnalysed )
	{
		// do whatever ... 
		return SCR_ERV_Anl_NormalReturn;
	}

    SCR_fn_v_Bin_BeginSection(_p_stFile);

	// not used yet
	//gs_wVSFileVersion = 
	SCR_fn_s_Bin_GetSHORT( _p_stFile );
	
	SCR_fn_v_Bin_EndSection(_p_stFile);
	
	return ( eReturnValue );
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of VS script file
 *-----------------------------------------------------------------------------*/
/*
SCR_tde_Anl_ReturnValue VS_fn_eScriptCallBackBinGeneral(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
  GEO_tdxHandleToVisualSet _hVisualSet;
	char sz_LongPathForLinkTable[256];
	
  switch( _eAction )
  {
  case SCR_EA_Anl_BeginSection:

	SCR_fn_v_Bin_BeginSection(_p_stFile);

	_hVisualSet = GLI_p_stCreateVisualSet(SCR_fn_s_Bin_GetSHORT( _p_stFile ));
	SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hVisualSet);

	SCR_fn_v_Bin_EndSection(_p_stFile);

	break;

  case SCR_EA_Anl_BeginSubSection:
	SCR_M_RdL0_ComputeOpenSectionNameR(0,sz_LongPathForLinkTable);
	SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(&VS_g_stLinkTable,sz_LongPathForLinkTable,(unsigned long)_hVisualSet),1,strlen(sz_LongPathForLinkTable)-strlen(SCR_M_RdL0_GetCompleteSectionNameR(0)));

	SCR_M_RdL0_SetContextLong(C_ChildContext,0,_hVisualSet);
	SCR_M_RdL0_SetContextLong(C_ChildContext,1,(short)atol(_ap_szParams[1]) - 1);
	SCR_M_RdL0_SetContextDouble(C_ChildContext,0,0.0); //init Threshold=0
	
	break;
 }
	
	return ( eReturnValue );
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   call-back for LODs sections of VS script file
 *-----------------------------------------------------------------------------*/
/*
SCR_tde_Anl_ReturnValue VS_fn_eScriptCallBackBinLOD(SCR_tdst_File_Description *_p_stFile,char *_p_szName,char *_ap_szParams[],SCR_tde_Anl_Action	_eAction)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
  GEO_tdxHandleToVisualSet _hVisualSet;
	//SCR_tdst_Cxt_Values *p_stVal;
	ACP_tdxHandleOfObject   _hGeometricObject;
	long	l_NumberOfLOD;
	double lf_Tmp;
	MTH_tdxReal xThreshold;
  char szP0[MAX_PATH];

  switch( _eAction )
  {
  case SCR_EA_Anl_BeginSection:

	SCR_fn_v_Bin_BeginSection(_p_stFile);

	// LOD_Threshold
	SCR_M_RdL0_SetContextDouble(C_ThisContext,0, SCR_fn_s_Bin_GetSHORT( _p_stFile ));

	// FichierMOD
	SCR_M_RdL0_GetContextLong(C_ThisContext,0,GEO_tdxHandleToVisualSet,_hVisualSet);
	SCR_M_RdL0_GetContextLong(C_ThisContext,1,long,l_NumberOfLOD);
	SCR_M_RdL0_GetContextDouble(C_ThisContext,0,lf_Tmp); xThreshold = (float)lf_Tmp;

	SCR_fn_sz_Bin_GetREFERENCE2Buffer(  _p_stFile, szP0 );

	_hGeometricObject = GLI_pLoadBinGeometricInFile(szP0);
	GLI_vSetVisualSetLOD(_hVisualSet,l_NumberOfLOD,xThreshold,_hGeometricObject);

	SCR_fn_v_Bin_EndSection(_p_stFile);
  break;
  }
	return ( eReturnValue );
}
*/

/*
END OS 02.03.98
  Callbacks for bin files.
*/
