
#include "stdafx.h"
//#include "GlobalData.h"
#include "/cpa/public/scr.h"

#define C_GrandChildSection    ( 2)
#define C_ChildSection         ( 1)
#define C_ThisSection          ( 0)
#define C_ParentSection        (-1)
#define C_GrandParentSection   (-2)
 
#define C_GrandChildContext    ( 2)
#define C_ChildContext         ( 1)
#define C_ThisContext          ( 0)
#define C_ParentContext        (-1)
#define C_GrandParentContext   (-2)

#define M_IsTitle         (_eAction==SCR_EA_Anl_BeginSection)
#define M_IsEnd           (_eAction==SCR_EA_Anl_EndSection)
#define M_IsBegSubSection (_eAction==SCR_EA_Anl_BeginSubSection)
#define M_IsEndSubSection (_eAction==SCR_EA_Anl_EndSubSection)
#define M_IsEntry         (_eAction==SCR_EA_Anl_Entry)
#define M_ActionIs(szActionAsked) (!strcmpi(_p_szName,szActionAsked))


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Callback function to analyse the section "LevelNames" in game.dsc
SCR_tde_Anl_ReturnValue fn_eScriptCallBackFirstLevel(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;
	CStringList *_p_oList;

	_p_stFile=_p_stFile;
	
	if (M_IsEntry)
	{
		SCR_M_RdL0_GetContextLong(C_ThisContext,0,CStringList*,_p_oList);

		if (M_ActionIs("LevelName"))
		{
			char *p_cDelimiter = strchr( _ap_szParams[0], '$' );
			if( p_cDelimiter ) *p_cDelimiter = 0;
			_p_oList -> AddTail (_ap_szParams[0]);			
		}
	}

	return(eReturnValue);
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Init acp modules for script parsing.

unsigned long g_SCR_a9MemTable[9] = {0,0,0,0,0,0,0,0,0};

void fn_vInitScr()
{
	Erm_M_InitErrMsg(Erm);
	Erm_M_InitErrMsg(Mmg);
	Mmg_fn_vFirstInitMmgModule(10);
	
	SCR_fn_v_RdL0_Init();
	g_SCR_a9MemTable[1] = 1000000;
	SCR_fn_v_Mem_InitWithMemLevel(g_SCR_a9MemTable);
	SCR_fn_v_RdL0_RegisterCallback( "FirstLevelDescription", fn_eScriptCallBackFirstLevel, SCR_CRC_c_RdL0_ForSection );
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

void fn_vDesinitScr()
{
//	SCR_fn_v_RdL0_DeleteRegisterCallback( "FirstLevelDescription", SCR_CRC_c_RdL0_ForSection ,  );
	SCR_fn_v_RdL0_Close();
	Mmg_fn_vStopMmgModule();
	MMG_fn_vDesinitMemoryRasterDeclaration ();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Analyse game.dsc
BOOL fn_bReadGameDsc(char *_szDataPath , CStringList *_p_oList)
{
	char szText[1024];
	char szFileName[512];
	char szSectionName[512];

	// Compute long name of game.dsc
	sprintf( szFileName, "%sGame.dsc", _szDataPath );

	// We need read acces on the file
	if( _access( szFileName, 04 ) == -1 )
	{
		sprintf( szText, "Can't read level list: '%s' is not accessible !\n(Does not exist or no read permission)", szFileName );
		AfxMessageBox( szText, MB_OK | MB_ICONSTOP );
		return FALSE;
	}

	// Analyse only the section describing the list of levels
	SCR_M_RdL0_SetContextLong(C_GrandChildContext, 0, (long)_p_oList);
	sprintf( szSectionName, "%s^FirstLevelDescription", szFileName );
	SCR_fnp_st_RdL0_AnalyseSection( szFileName, SCR_CDF_uw_Anl_ForceAnalyse );

	return TRUE;
}
