/*=========================================================================
* CSLoad.c : Loading CS 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
#define PCS_FRIEND
#define PCS_MAIN
#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 "ErmPO.h"

// global for CS Link Table
#if !defined(U64)
SCR_tdst_Link_Table CS_g_stLinkTable;
#endif /* U64 */

 /*-----------------------------------------------------------------------------
 *  Description : initialize and close link table for CS
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author : 
 *  Modifications : 
 *---------------------------------------------------------------------------*/
#if !defined(U64)
SCR_tdst_Link_Table *CS_fn_p_stGetLinkTable(void)
{
 return(&CS_g_stLinkTable);
}
#endif /* U64 */

void CS_fn_vInitLinkTable(void)
{
#if !defined(U64)
	SCR_fn_v_Link_InitTable(&CS_g_stLinkTable);
#endif /* U64 */
}

void CS_fn_vCloseLinkTable(void)
{
#if !defined(U64)
	SCR_fn_v_Link_CloseTable(&CS_g_stLinkTable);
#endif /* U64 */
}

/*-----------------------------------------------------------------------------
 *  Description : Used to register all script callback functions
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
void CS_fn_vRegisterAllScriptSections(void)
{
#if !defined(U64)
	SCR_fn_v_RdL0_RegisterCallback(C_Section_CSHeader,  CS_fn_eScriptCallBackHeader,  SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_CSGeneral, CS_fn_eScriptCallBackGeneral, SCR_CRC_c_RdL0_ForSection);

/*	SCR_fn_v_RdL0_RegisterCallback(C_Section_CSHeader,  CS_fn_eScriptCallBackBinHeader,  SCR_CRC_c_RdL0_ForBinSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_CSGeneral, CS_fn_eScriptCallBackBinGeneral, SCR_CRC_c_RdL0_ForBinSection);
*/
#endif /* U64 */
}


/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of CS script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
#if !defined(U64)
SCR_tde_Anl_ReturnValue CS_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_wCSFileVersion = (short)fn_lAToI( _ap_szParams[0] );
		}
	}
	
	return ( eReturnValue );
}
#endif /* U64 */


/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of CS script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
#if !defined(U64)
SCR_tde_Anl_ReturnValue CS_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;
  PCS_tdxHandleToPhysicalCollSet _hCollideSet;
	char sz_LongPathForLinkTable[256];
	//SCR_tdst_Cxt_Values *p_stVal;
	ACP_tdxHandleOfObject   _hGeometricObject;
	//char s[80];
	

	if ( M_IsTitle )	
	{
		_hCollideSet = PCS_fn_hCreatePhysicalCollideSet(1);
		SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hCollideSet);

		SCR_M_RdL0_ComputeOpenSectionNameR(0,sz_LongPathForLinkTable);
		SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(&CS_g_stLinkTable,sz_LongPathForLinkTable,(unsigned long)_hCollideSet),1,strlen(sz_LongPathForLinkTable)-strlen(SCR_M_RdL0_GetCompleteSectionNameR(0)));
		
		//SCR_fn_v_RdL0_ComputeSectionName(sz_LongPathForLinkTable,SCR_M_RdL0_GetOpenFileNameR(0),C_Section_CSGeneral,_p_szName);
			/*
		strcpy(sz_LongPathForLinkTable,fn_szGetLevelsDataPath());
		strcat(sz_LongPathForLinkTable,"\\");
		strcat(sz_LongPathForLinkTable,SCR_M_RdL0_GetCompleteSectionNameR(0));
		*/
		//SCR_fnp_st_Link_SetValue(&CS_g_stLinkTable, sz_LongPathForLinkTable, (unsigned long)_hCollideSet);

		/*
		strcpy(s,SCR_M_RdL0_GetCompleteSectionNameR(0));
		strcat(s,"\n");
    OutputDebugString(s);
		*/
	}
	else if ( M_IsEntry )
	{
		if ( M_ActionIs( C_Entry_ZDR ) )
		{
			SCR_M_RdL0_GetSectionLong(C_ThisContext,0,PCS_tdxHandleToPhysicalCollSet,_hCollideSet);
			
			/*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]);

   if (  _hGeometricObject->p_stOctree!=NULL&&GEO_fn_ucGetBlocNumberOf(_hGeometricObject)!=GEO_fn_ucGetBlocNumberOf(_hGeometricObject->p_stOctree)
       ||_hGeometricObject->p_stOctree==NULL&&GEO_fn_ucGetBlocNumberOf(_hGeometricObject)!=GEO_M_ucGetMemMallocMode())
   {
    M_POWarningErrorWithMessage(E_uwPOOctreeMustBeInFix,_p_stFile->a_szFileName);

    _hGeometricObject->p_stOctree = NULL;
   }
   /* Computing Octree on ZDR (JO 23/09/97) */
   COL_fn_bCreateOctree(_hGeometricObject,10,5,4);
			PCS_fn_vSetZdrGeoObjOfPhysicalCollSet(_hCollideSet,_hGeometricObject);

		}
		else if ( M_ActionIs( C_Entry_ZDE ) )
		{
			SCR_M_RdL0_GetSectionLong(C_ThisContext,0,PCS_tdxHandleToPhysicalCollSet,_hCollideSet);
			
			/*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]);
			PCS_fn_vSetZdeGeoObjOfPhysicalCollSet(_hCollideSet,_hGeometricObject);

		}
		else if ( M_ActionIs( C_Entry_ZDD ) )
		{
			SCR_M_RdL0_GetSectionLong(C_ThisContext,0,PCS_tdxHandleToPhysicalCollSet,_hCollideSet);
			
			/*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]);
			PCS_fn_vSetZddGeoObjOfPhysicalCollSet(_hCollideSet,_hGeometricObject);

		}
	}	
	return ( eReturnValue );
}
#endif /* U64 */

/*-----------------------------------------------------------------------------
 *  Description :   function to load a Collide Set (script or binary load)
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
PCS_tdxHandleToPhysicalCollSet CS_fn_xLoadCollideSet(char* _szSectionName)
{
	PCS_tdxHandleToPhysicalCollSet hCollideSet=NULL;
#if !defined(U64)
	SCR_tdst_Cxt_Values *p_stValues;
	SCR_tdst_Link_Value *p_stLinkValue;
	unsigned int uiPos;

	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value, uiPos, p_stLinkValue,SCR_M_st_Link_GetDynamicArray(&CS_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(&CS_g_stLinkTable));
	}
//	p_stLinkValue=BIN_fn_p_stSearchShortKey(&CS_g_stLinkTable,_szSectionName);
	if(p_stLinkValue==NULL)
	{
		p_stValues = SCR_fnp_st_RdL0_AnalyseSection(_szSectionName, SCR_CDF_uw_Anl_Normal);
		hCollideSet = (PCS_tdxHandleToPhysicalCollSet)SCR_M_ul_RdL0_ExtractLongValue(p_stValues,0);
		return hCollideSet;
	}
	else
		return (PCS_tdxHandleToPhysicalCollSet)p_stLinkValue->ulValue;
#else /* U64 */
		return hCollideSet;
#endif /* U64 */
}

/*-----------------------------------------------------------------------------
 *  Description :   function to prepare the binary save process
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void CS_fn_vPrepareSaveBinaryBloc()
{
#if !defined(U64)
	BIN_fn_vCreateBinaryIds(&CS_g_stLinkTable,BIN_C_CSID);
#endif // U64 
	GEO_fn_vPrepareSaveGeometricObjects();
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write a binary collide set
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long CS_fn_ulWriteBinaryBloc(unsigned long _ulStructAddress, char* _p_cDestBuffer)
{
	char* p_cBufferPointer=_p_cDestBuffer;
#if !defined(U64)
	PCS_tdxHandleToPhysicalCollSet hCollideSet=(PCS_tdxHandleToPhysicalCollSet)_ulStructAddress;

	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,(unsigned long)PCS_fn_hGetZdmGeoObjOfPhysicalCollSet(hCollideSet),GLI_p_stGetLinkTableOfGeometric());
	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,(unsigned long)PCS_fn_hGetZddGeoObjOfPhysicalCollSet(hCollideSet),GLI_p_stGetLinkTableOfGeometric());
	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,(unsigned long)PCS_fn_hGetZdeGeoObjOfPhysicalCollSet(hCollideSet),GLI_p_stGetLinkTableOfGeometric());
	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,(unsigned long)PCS_fn_hGetZdrGeoObjOfPhysicalCollSet(hCollideSet),GLI_p_stGetLinkTableOfGeometric());

#endif // U64 
	return p_cBufferPointer-_p_cDestBuffer;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write all the binary collide sets
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void CS_fn_vWriteAllCSBinaryBlocs(char* _szBinaryFileName)
{
	CS_fn_vPrepareSaveBinaryBloc();
#if !defined(U64)
	BIN_fn_vPutBinaryDataIntoFileFromLinkTable(_szBinaryFileName,&CS_g_stLinkTable,
													4*sizeof(ACP_tdxHandleOfObject),CS_fn_ulWriteBinaryBloc);
#endif // U64 
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary collide set
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long CS_fn_ulReadBinaryBloc(char* _p_cLoadedBuffer,unsigned long _ulLoadedBufferSize)
{
	ACP_tdxHandleOfObject hHandle;
	PCS_tdxHandleToPhysicalCollSet hCollideSet;
	char* p_cBufferPointer=_p_cLoadedBuffer;

	// Allocation 
	hCollideSet = PCS_fn_hCreatePhysicalCollideSet();

#if !defined(U64)
	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&hHandle,GLI_p_stGetLinkTableOfGeometric());
	PCS_fn_vSetZdmGeoObjOfPhysicalCollSet(hCollideSet,hHandle);
	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&hHandle,GLI_p_stGetLinkTableOfGeometric());
	PCS_fn_vSetZddGeoObjOfPhysicalCollSet(hCollideSet,hHandle);
	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&hHandle,GLI_p_stGetLinkTableOfGeometric());
	PCS_fn_vSetZdeGeoObjOfPhysicalCollSet(hCollideSet,hHandle);
	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&hHandle,GLI_p_stGetLinkTableOfGeometric());
	PCS_fn_vSetZdrGeoObjOfPhysicalCollSet(hCollideSet,hHandle);
#endif // U64 

	return (unsigned long)hCollideSet;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read all the binary blocs fort the visual sets
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
 /*
void CS_fn_vReadAllCSBinaryBlocs(char* _szBinaryFileName)
{
#if !defined(U64)
	BIN_fn_vReadDataFromLinkTable(&CS_g_stLinkTable,CS_fn_ulReadBinaryBloc,4*sizeof(ACP_tdxHandleOfObject),_szBinaryFileName);
#endif // U64 
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary collide set
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void CS_fn_vResolveBinaryBloc(unsigned long _ulAddress)
{
	tdstPhysicalCollSet* hCollideSet;

	hCollideSet=(tdstPhysicalCollSet*)_ulAddress;

#if !defined(U64)
	BIN_fn_p_cGetPointer((char*)&hCollideSet->hZdm,(unsigned long*)&hCollideSet->hZdm,GLI_p_stGetLinkTableOfGeometric());
	BIN_fn_p_cGetPointer((char*)&hCollideSet->hZdd,(unsigned long*)&hCollideSet->hZdd,GLI_p_stGetLinkTableOfGeometric());
	BIN_fn_p_cGetPointer((char*)&hCollideSet->hZde,(unsigned long*)&hCollideSet->hZde,GLI_p_stGetLinkTableOfGeometric());
	BIN_fn_p_cGetPointer((char*)&hCollideSet->hZdr,(unsigned long*)&hCollideSet->hZdr,GLI_p_stGetLinkTableOfGeometric());
#endif // U64 
}

char* CS_fn_p_cGeneralAllocationFunction(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 CS_fn_vReadAllCSBinaryBlocs2(char* _szBinaryFileName)
{
#if !defined(U64)
	BIN_fn_vReadDataFromLinkTableWithoutAllocation(&CS_g_stLinkTable,CS_fn_vResolveBinaryBloc,
													4*sizeof(ACP_tdxHandleOfObject),_szBinaryFileName,
													CS_fn_p_cGeneralAllocationFunction);
#endif // U64 
}
*/
/*
BEGIN OS 02.03.98
  Callbacks for bin files.
*/

/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of CS script file
 *-----------------------------------------------------------------------------*/
/*
#if !defined(U64)
SCR_tde_Anl_ReturnValue CS_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_BeginSection )
	{
		// do whatever ... 
		return SCR_ERV_Anl_NormalReturn;
	}
  
  SCR_fn_v_Bin_BeginSection(_p_stFile);

  // not used yet
	//gs_wCSFileVersion = (short)fn_lAToI( _ap_szParams[0] );
	SCR_fn_us_Bin_GetUSHORT( _p_stFile );

  SCR_fn_v_Bin_EndSection(_p_stFile);

	return ( eReturnValue );
}
#endif // U64 
*/

/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of CS script file
 *-----------------------------------------------------------------------------*/
/*
#if !defined(U64)
SCR_tde_Anl_ReturnValue CS_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;
  PCS_tdxHandleToPhysicalCollSet _hCollideSet;
	char sz_LongPathForLinkTable[256];
	//SCR_tdst_Cxt_Values *p_stVal;
	ACP_tdxHandleOfObject   _hGeometricObject;
	char chTag;

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

	_hCollideSet = PCS_fn_hCreatePhysicalCollideSet(1);
	SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hCollideSet);

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

  SCR_fn_v_Bin_BeginSection(_p_stFile);

  do{
    chTag=SCR_fn_ch_Bin_GetCHAR( _p_stFile );
	  switch (chTag)
	  { 
	    case 0:  //ZDR
	    {
			  char szP0[MAX_PATH];
        SCR_fn_sz_Bin_GetREFERENCE2Buffer(  _p_stFile, szP0 );
			  _hGeometricObject = GLI_pLoadBinGeometricInFile(szP0);

        if (  _hGeometricObject->p_stOctree!=NULL&&GEO_fn_ucGetBlocNumberOf(_hGeometricObject)!=GEO_fn_ucGetBlocNumberOf(_hGeometricObject->p_stOctree)
           ||_hGeometricObject->p_stOctree==NULL&&GEO_fn_ucGetBlocNumberOf(_hGeometricObject)!=GEO_M_ucGetMemMallocMode())
        {
        M_POWarningErrorWithMessage(E_uwPOOctreeMustBeInFix,_p_stFile->a_szFileName);

        _hGeometricObject->p_stOctree = NULL;
        }
        // Computing Octree on ZDR (JO 23/09/97) 
        COL_fn_bCreateOctree(_hGeometricObject,10,5,4);
        PCS_fn_vSetZdrGeoObjOfPhysicalCollSet(_hCollideSet,_hGeometricObject);

		  }
	  	break;
	    case 1:  //ZDE
	    {
			  char szP0[MAX_PATH];
        SCR_fn_sz_Bin_GetREFERENCE2Buffer(  _p_stFile, szP0 );

			  _hGeometricObject = GLI_pLoadBinGeometricInFile(szP0);
			  PCS_fn_vSetZdeGeoObjOfPhysicalCollSet(_hCollideSet,_hGeometricObject);

		  }
	  	break;
	    case 2:  //ZDD
	    {
			  char szP0[MAX_PATH];
        SCR_fn_sz_Bin_GetREFERENCE2Buffer(  _p_stFile, szP0 );

			  _hGeometricObject = GLI_pLoadBinGeometricInFile(szP0);
			  PCS_fn_vSetZddGeoObjOfPhysicalCollSet(_hCollideSet,_hGeometricObject);

		  }
	  	break;

    }
  }while (chTag!=-1);

  SCR_fn_v_Bin_EndSection(_p_stFile);
  
	return ( eReturnValue );
}
#endif // U64 
*/

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