/*=========================================================================
* Load.c : Loading IPO Files
* 
* Version              1.0
* Creation date        29/04/97
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi Studios 1997
*=======================================================================*/
 
#define	 IPO_FRIEND
#include "ACP_Base.h"
 
#include "SCR.h"
#include "DPT.h"
#include "GEO.h"
#include "GLI.h"         
#include "SPO.h"
#include "PCS.h"
#include "PO.h"
/*#include "BIN.h"*/
 
#include "IPO.h"
#include "ISI.h"
 
 
 
 
/* global for IPO Link Table*/
SCR_tdst_Link_Table IPO_g_stLinkTable;
 
 /*-----------------------------------------------------------------------------
 *  Description : initialize and close link table for IPO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 29/04/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------
 *  Modification date :                 Modification Author : 
 *  Modifications : 
 *---------------------------------------------------------------------------*/
SCR_tdst_Link_Table *IPO_fn_p_stGetLinkTable(void)
{
 return(&IPO_g_stLinkTable);
}
 
void IPO_fn_vInitLinkTable(void)
{
	SCR_fn_v_Link_InitTable(&IPO_g_stLinkTable);
}
 
void IPO_fn_vCloseLinkTable(void)
{
	SCR_fn_v_Link_CloseTable(&IPO_g_stLinkTable);
}
 
/*-----------------------------------------------------------------------------
 *  Description : Used to register all script callback functions
 *-----------------------------------------------------------------------------
 *  Creation date : 30/04/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
void IPO_fn_vRegisterAllScriptSections(void)
{
	SCR_fn_v_RdL0_RegisterCallback(C_Section_IPOHeader, IPO_fn_eScriptCallBackHeader,SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_IPOGeneral, IPO_fn_eScriptCallBackGeneral,SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_IPO_MirrorGeneral, IPO_fn_eScriptCallBackGeneral,SCR_CRC_c_RdL0_ForSection);
 
/*BEGIN OS 23-Mar-98*/
/*	SCR_fn_v_RdL0_RegisterCallback(C_Section_IPOHeader, IPO_fn_eScriptCallBackBinHeader,SCR_CRC_c_RdL0_ForBinSection);*/
/*	SCR_fn_v_RdL0_RegisterCallback(C_Section_IPOGeneral, IPO_fn_eScriptCallBackBinGeneral,SCR_CRC_c_RdL0_ForBinSection);*/
/*	SCR_fn_v_RdL0_RegisterCallback(C_Section_IPO_MirrorGeneral, IPO_fn_eScriptCallBackBinGeneral,SCR_CRC_c_RdL0_ForBinSection);*/
/*END OS 23-Mar-98*/
 
}
 
 
/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of IPO script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 30/04/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue IPO_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_wIPOFileVersion = (short)fn_lAToI( _ap_szParams[0] );*/
		}
	}
	
	return ( eReturnValue );
}
 
/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of IPO script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 30/04/96            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue IPO_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;
	IPO_tdxHandleToInstanciatedPhysicalObject _hInstanciatedPhysicalObject;
	SCR_tdst_Cxt_Values *p_stVal;
	static char sz_LongPathForLinkTable[256];
 
	SCR_fn_v_RdL0_SetPriorityPath(fn_szGetLevelsDataPath());
 
	if ( M_IsTitle )	
	{
		_hInstanciatedPhysicalObject = IPO_fn_hAllocInstanciatedPhysicalObject();
		SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hInstanciatedPhysicalObject);
 
		SCR_M_RdL0_ComputeOpenSectionNameR(0,sz_LongPathForLinkTable);
		SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(&IPO_g_stLinkTable,sz_LongPathForLinkTable,(unsigned long)_hInstanciatedPhysicalObject),1,strlen(sz_LongPathForLinkTable)-strlen(SCR_M_RdL0_GetCompleteSectionNameR(0)));	
	}
	else if ( M_IsEntry )
	{
		if ( M_ActionIs( C_Entry_PO ) )
		{
			SCR_M_RdL0_GetSectionLong(C_ThisContext,0,IPO_tdxHandleToInstanciatedPhysicalObject,_hInstanciatedPhysicalObject);
			_hInstanciatedPhysicalObject->_hPhysicalObject=CS_fn_xLoadPhysicalObject(_ap_szParams[0]);
		}
		else if ( M_ActionIs( C_Entry_RLI ) )
		{
			SCR_M_RdL0_GetSectionLong(C_ThisContext,0,IPO_tdxHandleToInstanciatedPhysicalObject,_hInstanciatedPhysicalObject);
			p_stVal = SCR_fnp_st_RdL0_AnalyseSection(_ap_szParams[0], SCR_CDF_uw_Anl_Normal);
			_hInstanciatedPhysicalObject->_hRadiosity = (ACP_tdxHandleToRadiosity)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
		}
	}
	
	return ( eReturnValue );
}
 
/*-----------------------------------------------------------------------------
 *  Description :   function to load a IPO (script or binary load)
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
IPO_tdxHandleToInstanciatedPhysicalObject IPO_fn_xLoadInstanciatedPhysicalObject(char* _szSectionName)
{
	SCR_tdst_Cxt_Values *p_stValues;
	SCR_tdst_Link_Value *p_stLinkValue;
	IPO_tdxHandleToInstanciatedPhysicalObject hInstanciatedPhysicalObject=NULL;
	unsigned int uiPos;
	
	
	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value, uiPos, p_stLinkValue,SCR_M_st_Link_GetDynamicArray(&IPO_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(&IPO_g_stLinkTable));
	}
 
	/*	p_stLinkValue=BIN_fn_p_stSearchShortKey(&IPO_g_stLinkTable,_szSectionName);*/
	if(p_stLinkValue==NULL)
	{
		p_stValues = SCR_fnp_st_RdL0_AnalyseSection(_szSectionName, SCR_CDF_uw_Anl_Normal);
		hInstanciatedPhysicalObject = (IPO_tdxHandleToInstanciatedPhysicalObject)SCR_M_ul_RdL0_ExtractLongValue(p_stValues,0);
		return hInstanciatedPhysicalObject;
	}
	else
		return (IPO_tdxHandleToInstanciatedPhysicalObject)p_stLinkValue->ulValue;
}
 
/*-----------------------------------------------------------------------------
 *  Description :   function to prepare the binary save process
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void IPO_fn_vPrepareSaveBinaryBloc()
{
	BIN_fn_vCreateBinaryIds(&IPO_g_stLinkTable,BIN_C_IPOID);
	PO_fn_vPrepareSaveBinaryBloc();
	ISI_fn_vPrepareSaveBinaryBloc();
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write a binary IPO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long IPO_fn_ulWriteBinaryBloc(unsigned long _ulStructAddress, char* _p_cDestBuffer)
{
	IPO_tdxHandleToInstanciatedPhysicalObject hIPO=(IPO_tdxHandleToInstanciatedPhysicalObject)_ulStructAddress;
	char* p_cBufferPointer=_p_cDestBuffer;
 
	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,
									(unsigned long)IPO_fn_hGetPhysicalObject(hIPO),
									&PO_g_stLinkTable);
 
	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,
									(unsigned long)IPO_fn_hGetRadiosity(hIPO),
									ISI_fn_pGetLinkTable());
 
	return p_cBufferPointer-_p_cDestBuffer;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write all the binary IPO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void IPO_fn_vWriteAllIPOBinaryBlocs(char* _szBinaryFileName)
{
	IPO_fn_vPrepareSaveBinaryBloc();
	BIN_fn_vPutBinaryDataIntoFileFromLinkTable(_szBinaryFileName,&IPO_g_stLinkTable,
													2*sizeof(unsigned long),IPO_fn_ulWriteBinaryBloc);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary IPO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long IPO_fn_ulReadBinaryBloc(char* _p_cLoadedBuffer,unsigned long _ulLoadedBufferSize)
{
	unsigned long ulAddress;
	IPO_tdxHandleToInstanciatedPhysicalObject hIPO;
	char* p_cBufferPointer=_p_cLoadedBuffer;
 
	// Allocation 
	hIPO = IPO_fn_hAllocInstanciatedPhysicalObject();
 
	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&ulAddress,&PO_g_stLinkTable);
	IPO_fn_vSetPhysicalObject(hIPO,(PO_tdxHandleToPhysicalObject)ulAddress);
	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&ulAddress,ISI_fn_pGetLinkTable());
	IPO_fn_vSetRadiosity(hIPO,(ACP_tdxHandleToRadiosity)ulAddress);
 
	return (unsigned long)hIPO;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read all the binary blocs fort the IPO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void IPO_fn_vReadAllIPOBinaryBlocs(char* _szBinaryFileName)
{
	BIN_fn_vReadDataFromLinkTable(&IPO_g_stLinkTable,IPO_fn_ulReadBinaryBloc,2*sizeof(unsigned long),_szBinaryFileName);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary IPO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void IPO_fn_vResolveBinaryBloc(unsigned long _ulAddress)
{
	IPO_tdxHandleToInstanciatedPhysicalObject hIPO;
 
	hIPO=(IPO_tdxHandleToInstanciatedPhysicalObject)_ulAddress;
 
	BIN_fn_p_cGetPointer((char*)&hIPO->_hPhysicalObject,(unsigned long*)&hIPO->_hPhysicalObject,&PO_g_stLinkTable);
	BIN_fn_p_cGetPointer((char*)&hIPO->_hRadiosity,(unsigned long*)&hIPO->_hRadiosity,ISI_fn_pGetLinkTable());
}
*/
/*
char* IPO_fn_p_cGenericAllocationFunction(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 IPO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void IPO_fn_vReadAllIPOBinaryBlocs2(char* _szBinaryFileName)
{
	BIN_fn_vReadDataFromLinkTableWithoutAllocation(&IPO_g_stLinkTable,IPO_fn_vResolveBinaryBloc,
													2*sizeof(unsigned long),_szBinaryFileName,
													IPO_fn_p_cGenericAllocationFunction);
}
*/
/*
BEGIN OS 06.03.98
  Callbacks for bin files.
*/
 
/*-----------------------------------------------------------------------------
 *  Description :   function to load a IPO (script or binary load)
 *-----------------------------------------------------------------------------*/
/*
IPO_tdxHandleToInstanciatedPhysicalObject IPO_fn_xLoadBinInstanciatedPhysicalObject(char* _szSectionName)
{
	SCR_tdst_Cxt_Values *p_stValues;
	SCR_tdst_Link_Value *p_stLinkValue;
	IPO_tdxHandleToInstanciatedPhysicalObject hInstanciatedPhysicalObject=NULL;
 
	p_stLinkValue=BIN_fn_p_stSearchShortKey(&IPO_g_stLinkTable,_szSectionName);
	if(p_stLinkValue==NULL)
	{
		p_stValues = SCR_fnp_st_Bin_GetREFERENCE( _szSectionName );
		hInstanciatedPhysicalObject = (IPO_tdxHandleToInstanciatedPhysicalObject)SCR_M_ul_RdL0_ExtractLongValue(p_stValues,0);
		return hInstanciatedPhysicalObject;
	}
	else
		return (IPO_tdxHandleToInstanciatedPhysicalObject)p_stLinkValue->ulValue;
}
*/
 
 
 
/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of IPO script file
 *-----------------------------------------------------------------------------*/
/*
SCR_tde_Anl_ReturnValue IPO_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);
  
  SCR_fn_us_Bin_GetUSHORT( _p_stFile ); //VersionNumber
 
  SCR_fn_v_Bin_EndSection(_p_stFile);
 
	
	return ( eReturnValue );
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of IPO script file
 *-----------------------------------------------------------------------------*/
/*
SCR_tde_Anl_ReturnValue IPO_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;
  IPO_tdxHandleToInstanciatedPhysicalObject _hInstanciatedPhysicalObject;
	SCR_tdst_Cxt_Values *p_stVal;
	char sz_LongPathForLinkTable[256], szP0[MAX_PATH];
 
 
  
  if( _eAction != SCR_EA_Anl_BeginSection )
  {
	  // do whatever ... 
	  return SCR_ERV_Anl_NormalReturn;
  }
 
  SCR_fn_v_Bin_BeginSection(_p_stFile);
 
	_hInstanciatedPhysicalObject = IPO_fn_hAllocInstanciatedPhysicalObject();
	SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hInstanciatedPhysicalObject);
 
	SCR_M_RdL0_ComputeOpenSectionNameR(0,sz_LongPathForLinkTable);
	SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(&IPO_g_stLinkTable,sz_LongPathForLinkTable,(unsigned long)_hInstanciatedPhysicalObject),1,strlen(sz_LongPathForLinkTable)-strlen(SCR_M_RdL0_GetCompleteSectionNameR(0)));
  
  // PO
  SCR_fn_sz_Bin_GetREFERENCE2Buffer(  _p_stFile, szP0 );
	_hInstanciatedPhysicalObject->_hPhysicalObject=CS_fn_xLoadBinPhysicalObject(szP0);  
  
  // RLI
  SCR_fn_sz_Bin_GetREFERENCE2Buffer(  _p_stFile, szP0 );
  p_stVal = SCR_fnp_st_Bin_GetREFERENCE( szP0 );
  _hInstanciatedPhysicalObject->_hRadiosity = (ACP_tdxHandleToRadiosity)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
  
  SCR_fn_v_Bin_EndSection(_p_stFile);
	
	return ( eReturnValue );
}
*/
 
/*
END OS 06.03.98
  Callbacks for bin files.
*/
