#include "ToolsCPA.h"
 
#include "Structur/ErrGame.h"
#include "Structur/MemGame.h"
#include "Structur/GameScpt.h"
#include "Structur/EngMode.h"
 
#include "Options/ActBlMal.h"
#include "LSMem.h"
 
#include "Basic.h"
#include "ZeMem.h"
 
#ifdef GAM_USE_SNA
#include "sna.h"
#endif /* GAM_USE_SNA */
 
/*XB980825*/
#ifdef U64
extern unsigned long g_ulTotalMemoryUsed;
#endif /* U64 */
/*End XB*/
 
#include "ldt.h"
 
 
/*****************************************************************
Function name   : fn_iLoadMemoryDescription
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadMemoryDescription( LDT_tdst_Link *pLink )
{
	LDT_tdeParseResult result=ParseResult_BeginSection;
 
	static int iFirst=0;
	int i;
	void *vTab[50];
	short wNbMalloc=0;
 
	if (SNA_M_bTestSaveGameMemDesc() || SNA_M_bTestSaveLevelMemDesc() )
		SNA_fn_vWriteNoParamEntryToCurrentDscFile( SNA_C_ul_MemoryDescTitle );
 
	while((result = LDT_GetNextEntry()) != ParseResult_EndSection )
	{
	  	switch( result )
  		{
  		case ParseResult_Entry: /* an entry */
			{
				char *szEntry=LDT_szGetEntryName();
				if(!strcmp(szEntry,"Gap"))
				{
					vTab[wNbMalloc]=GlobalAlloc(GMEM_FIXED,(unsigned long)atol(LDT_szGetParam(1)));
					wNbMalloc++;
				}
				else
					switch(*(long*)(szEntry+1))
					{
					case 'Fema': /*"GameFixMemorySize"*/
						Mmg_M_InitBlock(Game,E_ucGameFixMemory,(unsigned long)fn_lAToI(LDT_szGetParam(1)));
						GAM_fn_vSetGameMemoryInFix();
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_GAMFixMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'Lema': /*"GameLevelMemorySize"*/
						if((iFirst<2)||(SNA_fn_ucGetLoadType()==SNA_LOAD_SCRIPT))
							Mmg_M_InitBlock(Game,E_ucGameLevelMemory,(unsigned long)fn_lAToI(LDT_szGetParam(1)));
						if(iFirst>0)
							GAM_fn_vSetGameMemoryInLevel();
 
						if ( SNA_M_bTestSaveLevelMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_GAMLevelMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'Mtno': /*"FontMemorySize"*/
						FON_fn_vFirstInitFONMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_FontMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'iFIA': /*"SAIFixMemorySize"*/
						SAI_fn_vInitFixMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
						SAI_fn_vSAIUseFixMemory();
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_SAIFixMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'eMIA': /*"SAIMemorySize"*/
						SAI_fn_vInitLevelMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
						SAI_fn_vSAIUseLevelMemory();
 
						if ( SNA_M_bTestSaveLevelMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_SAILevelMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'iFPM': /*"TMPFixMemory"*/
						TMP_fn_vCreateFixMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
/*						TMP_fn_vSetInFix();*/
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_TMPFixMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
/*
					case 'eLPM': //"TMPLevelMemory"
						TMP_fn_vCreateLevelMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
						TMP_fn_vSetInLevel();
 
						if ( SNA_M_bTestSaveLevelMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_TMPLevelMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
*/
					case 'xiFI': /*"AIFixMemory"*/
						fn_vInitAIFixMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
						AI_fn_vAIUseFixMemory();
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_AIFixMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'meMI': /*"AIMemory"*/
						if((iFirst<2)||(SNA_fn_ucGetLoadType()==SNA_LOAD_SCRIPT))
							fn_vInitAILevelMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
						AI_fn_vAIUseLevelMemory();
 
						if ( SNA_M_bTestSaveLevelMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_AILevelMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'eTPC': /*"ACPTextMemory"*/
						GEO_xCreateMemoryChannel(0,(unsigned long)fn_lAToI(LDT_szGetParam(1)));
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_ACPTextMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'iFPC': /*"ACPFixMemory"*/
						GEO_xCreateMemoryChannel(ACP_FixMemoryChannel,(unsigned long)fn_lAToI(LDT_szGetParam(1)));
						GEO_xSelectMemoryChannel(ACP_FixMemoryChannel);
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_ACPFixMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'eMPC': /*"ACPMemory"*/
						if((iFirst<2)||(SNA_fn_ucGetLoadType()==SNA_LOAD_SCRIPT))
							GEO_xCreateMemoryChannel(ACP_LevelMemoryChannel,(unsigned long)fn_lAToI(LDT_szGetParam(1)));
						if(iFirst>0)
							GEO_xSelectMemoryChannel(ACP_LevelMemoryChannel);
 
						if ( SNA_M_bTestSaveLevelMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_ACPLevelMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'tiso': /*"PositionMemorySize"*/
						POS_fn_vInitPosMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_PositionMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'pirc': /*"ScriptMemorySize"*/
						g_SCR_a9MemTable[fn_lAToI(LDT_szGetParam(1))] = fn_lAToI(LDT_szGetParam(2));
						SCR_fn_v_Mem_InitWithMemLevel(g_SCR_a9MemTable);
 
						if ( SNA_M_bTestSaveGameMemDesc() || SNA_M_bTestSaveLevelMemDesc() )
						{
							SNA_fn_vWrite2LongEntryToCurrentDscFile
							 ( 
								SNA_C_ul_ScriptMemory, 
								(unsigned long)fn_lAToI(LDT_szGetParam(1)),
								(unsigned long)fn_lAToI(LDT_szGetParam(2))
							 );
						}
						break;
					case 'Sspi': /*"LipsSynchMemory"*/
						LSY_fn_vFirstInitLSYMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
						break;
					case 'eMTP': /*"IPTMemorySize"	*/
						IPT_fn_vInitMemory((unsigned long)fn_lAToI(LDT_szGetParam(1)));
 
						if ( SNA_M_bTestSaveGameMemDesc() )
							SNA_fn_vWriteLongEntryToCurrentDscFile ( SNA_C_ul_IPTMemory, (unsigned long)fn_lAToI(LDT_szGetParam(1)) );
						break;
					case 'eMes': /*"UseMemorySnapshot"*/
						SNA_fn_vSetLoadType((unsigned char)fn_lAToI(LDT_szGetParam(1)));
						break;
					default:
						M_GameInformationError(E_uwGameScriptCallBackDescriptionUnknownAction);
						break;
					}
			}
		}
	}
   
	for(i=0;i<wNbMalloc;i++)
	{
		GlobalFree(vTab[i]);
	}
	wNbMalloc=0;
	iFirst++;
	if( (iFirst>1)&&(SNA_fn_ucGetLoadType()==SNA_LOAD_SNAPSHOT) )
	{
		AI_fn_vAIUseLevelMemory();
		GEO_xSelectMemoryChannel(ACP_LevelMemoryChannel);
		GAM_fn_vSetGameMemoryInLevel();
	}
	
    if ( SNA_M_bTestSaveGameMemDesc() || SNA_M_bTestSaveLevelMemDesc() )
        SNA_fn_vWriteNoParamEntryToCurrentDscFile ( SNA_C_ulEndOfDescSection );
	
	return 0;
}
