/*=========================================================================
* POLoad.c : Loading PO 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 "BIN.h"

#include "PO.h"


#define D_PO_VariableDefine

#include "ErmPO.h"

#ifdef __cplusplus
extern "C" {
#endif

void PO_fn_vFirstInit(void)
{
 Erm_M_InitErrMsg(PO);
}

#ifdef __cplusplus
} //extern "C"
#endif


// global for PO Link Table
SCR_tdst_Link_Table PO_g_stLinkTable;

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

void PO_fn_vInitLinkTable(void)
{
	SCR_fn_v_Link_InitTable(&PO_g_stLinkTable);
}

void PO_fn_vCloseLinkTable(void)
{
	SCR_fn_v_Link_CloseTable(&PO_g_stLinkTable);
}


/*-----------------------------------------------------------------------------
 *  Description : Used to register all script callback functions
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
void PO_fn_vRegisterAllScriptSections(void)
{
	SCR_fn_v_RdL0_RegisterCallback(C_Section_POHeader, PO_fn_eScriptCallBackHeader,SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback(C_Section_POGeneral, PO_fn_eScriptCallBackGeneral,SCR_CRC_c_RdL0_ForSection);

//  SCR_fn_v_RdL0_RegisterCallback(C_Section_POHeader, PO_fn_eScriptCallBackBinHeader,SCR_CRC_c_RdL0_ForBinSection);
//	SCR_fn_v_RdL0_RegisterCallback(C_Section_POGeneral, PO_fn_eScriptCallBackBinGeneral,SCR_CRC_c_RdL0_ForBinSection);

}


/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of PO script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue PO_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_wPOFileVersion = (short)fn_lAToI( _ap_szParams[0] );
		}
	}
	
	return ( eReturnValue );
}

/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of PO script file
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : 05/05/97            Author : Sebastien DAVID (Gïzmo)
 *-----------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue PO_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;
    PO_tdxHandleToPhysicalObject _hPhysicalObject;
	SCR_tdst_Cxt_Values *p_stVal;
	char sz_LongPathForLinkTable[MAX_PATH];
	char sFile[MAX_PATH], sAction[MAX_PATH], sIdent[MAX_PATH];
	char sz_SectionNameForCS[MAX_PATH];
	//char s[80];
	
	if ( M_IsTitle )	
	{
		_hPhysicalObject = PO_fn_hAllocPhysicalObject();
		SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hPhysicalObject);

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

		//		SCR_fn_v_RdL0_ComputeSectionName(sz_LongPathForLinkTable,SCR_M_RdL0_GetOpenFileNameR(0),C_Section_POGeneral,_p_szName);
		//SCR_M_RdL0_GetFileNameR(_Offset)
	/*
		strcpy(sz_LongPathForLinkTable,fn_szGetLevelsDataPath());
		strcat(sz_LongPathForLinkTable,"\\");
		strcat(sz_LongPathForLinkTable,SCR_M_RdL0_GetCompleteSectionNameR(0));
		*/
		//SCR_fnp_st_Link_SetValue(&PO_g_stLinkTable, sz_LongPathForLinkTable, (unsigned long)_hPhysicalObject);
		/*
		strcpy(s,SCR_M_RdL0_GetCompleteSectionNameR(0));
		strcat(s,"\n");
    OutputDebugString(s);
		*/
	}
	else if ( M_IsEntry )
	{
		if ( M_ActionIs( C_Entry_VS ) )
		{
			SCR_M_RdL0_GetSectionLong(C_ThisSection,0,PO_tdxHandleToPhysicalObject,_hPhysicalObject);
			
			p_stVal = SCR_fnp_st_RdL0_AnalyseSection(_ap_szParams[0], SCR_CDF_uw_Anl_Normal);
			_hPhysicalObject->_hVisualSet = (GEO_tdxHandleToVisualSet)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);

			HIE_fn_vComputeOnePoBoundingVolume(_hPhysicalObject);
		}
		else if ( M_ActionIs( C_Entry_CS ) )
		{
			SCR_M_RdL0_GetSectionLong(C_ThisSection,0,PO_tdxHandleToPhysicalObject,_hPhysicalObject);
			
			SCR_fn_v_RdL0_SplitSectionName(_ap_szParams[0], sFile, sAction, sIdent);
			strcpy(sz_SectionNameForCS,sFile);
			strcat(sz_SectionNameForCS,"^");
			strcat(sz_SectionNameForCS,C_Section_AllCollideSets);
			strcat(sz_SectionNameForCS,"^");
			strcat(sz_SectionNameForCS,sAction);
			strcat(sz_SectionNameForCS,":");
			strcat(sz_SectionNameForCS,sIdent);

			p_stVal = SCR_fnp_st_RdL0_AnalyseSection(sz_SectionNameForCS, SCR_CDF_uw_Anl_Normal);
			_hPhysicalObject->_hCollideSet = (PCS_tdxHandleToPhysicalCollSet)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
		}
		else if ( M_ActionIs( C_Entry_BV ) )
		{
/*
			SCR_M_RdL0_GetSectionLong(C_ThisContext,0,PO_tdxHandleToPhysicalObject,_hPhysicalObject);
			
			p_stVal = SCR_fnp_st_RdL0_AnalyseSection(_ap_szParams[0], SCR_CDF_uw_Anl_Normal);
			_hPhysicalObject->_hBoundingVolume = (ACP_tdxHandleOfObject)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
*/
		}
	}
	
	return ( eReturnValue );
}

/*-----------------------------------------------------------------------------
 *  Description :   function to load a PO (script or binary load)
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
PO_tdxHandleToPhysicalObject CS_fn_xLoadPhysicalObject(char* _szSectionName)
{
	SCR_tdst_Cxt_Values *p_stValues;
	SCR_tdst_Link_Value *p_stLinkValue;
	PO_tdxHandleToPhysicalObject hPhysicalObject=NULL;
	unsigned int uiPos;

	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value, uiPos, p_stLinkValue,SCR_M_st_Link_GetDynamicArray(&PO_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(&PO_g_stLinkTable));
	}

//	p_stLinkValue=BIN_fn_p_stSearchShortKey(&PO_g_stLinkTable,_szSectionName);
	if(p_stLinkValue==NULL)
	{
		p_stValues = SCR_fnp_st_RdL0_AnalyseSection(_szSectionName, SCR_CDF_uw_Anl_Normal);
		hPhysicalObject = (PO_tdxHandleToPhysicalObject)SCR_M_ul_RdL0_ExtractLongValue(p_stValues,0);
		return hPhysicalObject;
	}
	else
		return (PO_tdxHandleToPhysicalObject)p_stLinkValue->ulValue;
}

// Shaitan => module list in the level
/*-----------------------------------------------------------------------------
 *  Description :   function to load a PO (script or binary load)
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
PO_tdxHandleToPhysicalObject CS_fn_xFindPhysicalObject(char* _szSectionName)
{
	SCR_tdst_Link_Value *p_stLinkValue;
	PO_tdxHandleToPhysicalObject hPhysicalObject=NULL;
	unsigned int uiPos;

	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value, uiPos, p_stLinkValue,SCR_M_st_Link_GetDynamicArray(&PO_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(&PO_g_stLinkTable));
	}

	if(p_stLinkValue==NULL)
		return NULL;
	else
		return (PO_tdxHandleToPhysicalObject)p_stLinkValue->ulValue;
}
// End Shaitan => module list in the level

/*-----------------------------------------------------------------------------
 *  Description :   function to prepare the binary save process
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void PO_fn_vPrepareSaveBinaryBloc()
{
	BIN_fn_vCreateBinaryIds(&PO_g_stLinkTable,BIN_C_POID);
	CS_fn_vPrepareSaveBinaryBloc();
	VS_fn_vPrepareSaveBinaryBloc();
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write a binary PO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long PO_fn_ulWriteBinaryBloc(unsigned long _ulStructAddress, char* _p_cDestBuffer)
{
	PO_tdxHandleToPhysicalObject hPO=(PO_tdxHandleToPhysicalObject)_ulStructAddress;
	char* p_cBufferPointer=_p_cDestBuffer;

	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,
									(unsigned long)PO_fn_hGetVisualSet(hPO),
									&VS_g_stLinkTable);

	p_cBufferPointer=BIN_fn_p_cPutPointer(p_cBufferPointer,
									(unsigned long)PO_fn_hGetCollideSet(hPO),
									&CS_g_stLinkTable);

	p_cBufferPointer=BIN_fn_p_cPutLong(p_cBufferPointer,0);

	return p_cBufferPointer-_p_cDestBuffer;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to write all the binary PO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void PO_fn_vWriteAllPOBinaryBlocs(char* _szBinaryFileName)
{
	PO_fn_vPrepareSaveBinaryBloc();
	BIN_fn_vPutBinaryDataIntoFileFromLinkTable(_szBinaryFileName,&PO_g_stLinkTable,
													3*sizeof(unsigned long),PO_fn_ulWriteBinaryBloc);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary PO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
unsigned long PO_fn_ulReadBinaryBloc(char* _p_cLoadedBuffer,unsigned long _ulLoadedBufferSize)
{
	unsigned long ulAddress;
	PO_tdxHandleToPhysicalObject hPO;
	char* p_cBufferPointer=_p_cLoadedBuffer;
	long lCrap;

	// Allocation 
	hPO = PO_fn_hAllocPhysicalObject();

	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&ulAddress,&VS_g_stLinkTable);
	PO_fn_vSetVisualSet(hPO,(GEO_tdxHandleToVisualSet)ulAddress);
	p_cBufferPointer=BIN_fn_p_cGetPointer(p_cBufferPointer,(unsigned long*)&ulAddress,&CS_g_stLinkTable);
	PO_fn_vSetCollideSet(hPO,(PCS_tdxHandleToPhysicalCollSet)ulAddress);
	p_cBufferPointer=BIN_fn_p_cGetLong(p_cBufferPointer,&lCrap);

	HIE_fn_vComputeOnePoBoundingVolume(hPO);

	return (unsigned long)hPO;
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read all the binary blocs fort the visual sets
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
 /*
void PO_fn_vReadAllPOBinaryBlocs(char* _szBinaryFileName)
{
	BIN_fn_vReadDataFromLinkTable(&PO_g_stLinkTable,PO_fn_ulReadBinaryBloc,2*sizeof(unsigned long),_szBinaryFileName);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   function to read a binary PO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Aug 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void PO_fn_vResolveBinaryBloc(unsigned long _ulAddress)
{
	PO_tdxHandleToPhysicalObject hPO;

	hPO=(PO_tdxHandleToPhysicalObject)_ulAddress;

	BIN_fn_p_cGetPointer((char*)&hPO->_hVisualSet,(unsigned long*)&hPO->_hVisualSet,&VS_g_stLinkTable);
	BIN_fn_p_cGetPointer((char*)&hPO->_hCollideSet,(unsigned long*)&hPO->_hCollideSet,&CS_g_stLinkTable);
}

char* PO_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 PO_fn_vReadAllPOBinaryBlocs2(char* _szBinaryFileName)
{
	BIN_fn_vReadDataFromLinkTableWithoutAllocation(&PO_g_stLinkTable,PO_fn_vResolveBinaryBloc,
													3*sizeof(unsigned long),_szBinaryFileName,
													PO_fn_p_cGeneralAllocationFunction);
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   2nd pass to read a binary PO
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *  Creation date : Sep 97            Author : Alain Robin
 *-----------------------------------------------------------------------------*/
/*
void PO_fn_vResolveBinaryBlocPass2(unsigned long _ulAddress)
{
	PO_tdxHandleToPhysicalObject hPO;

	hPO=(PO_tdxHandleToPhysicalObject)_ulAddress;
	HIE_fn_vComputeOnePoBoundingVolume(hPO);
}
*/
/*
BEGIN OS 02.03.98
  Callbacks for bin files.
*/

/*-----------------------------------------------------------------------------
 *  Description :   call-back for header section of PO script file
 *-----------------------------------------------------------------------------*/
/*
SCR_tde_Anl_ReturnValue PO_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;
    short sNotUsed;
	
	// not used yet
	//gs_wPOFileVersion = (short)fn_lAToI( _ap_szParams[0] );

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

	sNotUsed = SCR_fn_s_Bin_GetSHORT( _p_stFile );

  SCR_fn_v_Bin_EndSection(_p_stFile);

	return ( eReturnValue );
}
*/
/*-----------------------------------------------------------------------------
 *  Description :   call-back for general section of PO script file
 *-----------------------------------------------------------------------------*/
/*
SCR_tde_Anl_ReturnValue PO_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;
    PO_tdxHandleToPhysicalObject _hPhysicalObject;
	SCR_tdst_Cxt_Values *p_stVal;
	char sz_LongPathForLinkTable[MAX_PATH];
	char sFile[MAX_PATH], sAction[MAX_PATH], sIdent[MAX_PATH];
	char sz_SectionNameForCS[MAX_PATH];
	//char s[80];
  char szP0[MAX_PATH], szP1[MAX_PATH];
	
	if( _eAction != SCR_EA_Anl_BeginSection )
	{
		// do whatever ... 
		return SCR_ERV_Anl_NormalReturn;
	}


  SCR_fn_v_Bin_BeginSection(_p_stFile);

	_hPhysicalObject = PO_fn_hAllocPhysicalObject();
	SCR_M_RdL0_SetSectionLong(C_ThisContext,0,_hPhysicalObject);
		
	SCR_M_RdL0_ComputeOpenSectionNameR(0,sz_LongPathForLinkTable);
	SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(&PO_g_stLinkTable,sz_LongPathForLinkTable,(unsigned long)_hPhysicalObject),1,strlen(sz_LongPathForLinkTable)-strlen(SCR_M_RdL0_GetCompleteSectionNameR(0)));

  // VS
	
  SCR_fn_sz_Bin_GetREFERENCE2Buffer( _p_stFile, szP0 );
	p_stVal = SCR_fnp_st_Bin_GetREFERENCE( szP0 );
	_hPhysicalObject->_hVisualSet = (GEO_tdxHandleToVisualSet)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	HIE_fn_vComputeOnePoBoundingVolume(_hPhysicalObject);


  // CS
	
  SCR_fn_sz_Bin_GetREFERENCE2Buffer( _p_stFile, szP1 );
	SCR_fn_v_RdL0_SplitSectionName(szP1, sFile, sAction, sIdent);
	strcpy(sz_SectionNameForCS,sFile);
	strcat(sz_SectionNameForCS,"^");
	strcat(sz_SectionNameForCS,C_Section_AllCollideSets);
	strcat(sz_SectionNameForCS,"^");
	strcat(sz_SectionNameForCS,sAction);
	strcat(sz_SectionNameForCS,":");
	strcat(sz_SectionNameForCS,sIdent);

	p_stVal = SCR_fnp_st_Bin_GetREFERENCE( sz_SectionNameForCS );
	_hPhysicalObject->_hCollideSet = (PCS_tdxHandleToPhysicalCollSet)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);


  SCR_fn_v_Bin_EndSection(_p_stFile);

	return ( eReturnValue );
}
*/

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


/*
BEGIN OS 05.03.98
  Called functions by callbacks for bin files.
*/


/*-----------------------------------------------------------------------------
 *  Description :   function to load a PO (script or binary load)
 *-----------------------------------------------------------------------------*/
/*
PO_tdxHandleToPhysicalObject CS_fn_xLoadBinPhysicalObject(char* _szSectionName)
{
	SCR_tdst_Cxt_Values *p_stValues;
	SCR_tdst_Link_Value *p_stLinkValue;
	PO_tdxHandleToPhysicalObject hPhysicalObject=NULL;

	p_stLinkValue=BIN_fn_p_stSearchShortKey(&PO_g_stLinkTable,_szSectionName);
	if(p_stLinkValue==NULL)
	{
		p_stValues = SCR_fnp_st_Bin_GetREFERENCE( _szSectionName );
		hPhysicalObject = (PO_tdxHandleToPhysicalObject)SCR_M_ul_RdL0_ExtractLongValue(p_stValues,0);
		return hPhysicalObject;
	}
	else
		return (PO_tdxHandleToPhysicalObject)p_stLinkValue->ulValue;
}
*/
/*
END OS 05.03.98
  Called functions by callbacks for bin files.
*/
