//
// Asura_PS2_TOCCache.h
//
// Created by Pete Ellacott - 14/01/2003
// 
// Copyright (C) 2003 Rebellion, All Rights Reserved.
//

#ifndef __ASURA_PS2_TOCCACHE_H__
#define __ASURA_PS2_TOCCACHE_H__

#ifdef ASURA_PS2_CDEMUL
#define ASURA_PS2_USE_TOC_CACHE 1
#else
#define ASURA_PS2_USE_TOC_CACHE 0
#endif

#include <libcdvd.h>

#if ASURA_PS2_USE_TOC_CACHE


#include "Core/Asura_Hash.h"

// This class cache the table of contents from the cd and the provides an sceCdSearchFile replacement
// that doesn't require any disk accessing
// (MP - 07.05.2003) You can override this limit in your project's
// Asura_EngineConfiguration.h.
#ifndef ASURA_PS2_MAX_TOC_ENTRIES
	#define ASURA_PS2_MAX_TOC_ENTRIES (512)
#endif

// C L A S S E S /////////////////////////////////////////////////////////////

class Asura_PS2_TOCCache
{

	public:

        // T Y P E S /////////////////////////////////////////////////////////

        struct Entry
        {
            Asura_Hash_ID m_uFilenameHash;
            u_int         m_uFirstSector;
            u_int         m_uLengthInBytes;

            union
            {
                char  m_szExtension[4];
                u_int m_uExtAsUint;
            };
        };

        // C O N S T A N T S /////////////////////////////////////////////////

        enum
        {
            EXE_NAME_LEN      = 8 + 1 + 2,                         // S???_NNN.NN
            ISO_TIMESTAMP_LEN = 4 + 1 + 2 + 1 + 2 + 3 + 2 + 1 + 2, // YYYY-MM-DD @ hh:mm
        };

        // C L A S S   M E T H O D S /////////////////////////////////////////

		static int CdSearchFile( sceCdlFILE* pxFileInfo, const char* szName );
        
        static const Entry* GetEntryList( void ) { return s_axTOCCache; }
        static u_int GetNumEntries( void ) { return s_uNumEntries; }

        static const char* GetExecutableName( void ) { return( s_szExecutableName[0] ? s_szExecutableName : NULL ); }
        static const char* GetISOTimestamp( void ) { return( s_szISOTimestamp[0] ? s_szISOTimestamp : NULL ); }

	private:

        // F R I E N D S /////////////////////////////////////////////////////
        
        friend class Asura_Core;

        // T Y P E S /////////////////////////////////////////////////////////

        struct Dir;

        // C L A S S   M E T H O D S /////////////////////////////////////////

        // For use by Asura_Core only.
		static void ReadTOC( void );

        static void ProcessPVD( sceCdRMode* pxReadMode, Dir* pxRootDir );

        // C L A S S   S T A T E /////////////////////////////////////////////

        static Entry s_axTOCCache[ ASURA_PS2_MAX_TOC_ENTRIES ];
        static u_int s_uNumEntries;
        static char  s_szExecutableName[EXE_NAME_LEN + 1];
        static char  s_szISOTimestamp[ISO_TIMESTAMP_LEN + 1];

};

#else // #if ASURA_PS2_USE_TOC_CACHE

class Asura_PS2_TOCCache
{

	public:
		
		static int CdSearchFile( sceCdlFILE* pxFileInfo, const char* szName );
};

#endif // #else not #if ASURA_PS2_USE_TOC_CACHE
    
#endif // #ifdef __ASURA_PS2_TOCCACHE_H__
