
///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_PSP_SCEUtility_LoadListHandler.cpp
// Author : Andy Tate
// Date   : 6-Oct-2005
//
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Include files...
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header...
#include "PSPEmul/SCEUtility/Asura_PSP_SCEUtility_LoadListHandler.h"

// Asura independent header-files...
#include "Core/Asura_Checksum.h"

// Asura platform header-files...

// Game independent header-files...

// Game platform header-files...


////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Static variable declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Private functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Protected functions...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtility_LoadListHandler::OnSelect
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Handles the saving of the specified slot upon selection.
//
// Parameters   : A reference to the slot data to use.
//
// Return value : "true" if successful, "false" if failed.
////////////////////////////////////////////////////////////////////////////////

bool Asura_PSP_SCEUtility_LoadListHandler::OnSelect (Asura_PSP_SCEUtility_FileData& xFileData)
{
	const u_int	uTotalSize	= m_uBufferSize + Asura_Checksum::CHECKSUM_LENGTH;
	u_char*		pucTemp		= new u_char [uTotalSize];
	u_int		uActualSize	= uTotalSize;
	bool		bSuccess	= xFileData.ReadData( uActualSize, pucTemp );

	// Did we read the data successfully?
	Asura_Assert( bSuccess, "Cannot read data from file." );

	if( bSuccess )
	{
		// Yes - check that we've read as much data as expected...
		bSuccess	= ( uTotalSize == uActualSize );

		Asura_Assert( bSuccess, "Mismatch in actual size of data loaded compared to that expected.");

		if( bSuccess )
		{
			u_char	aucActualChecksum[ Asura_Checksum::CHECKSUM_LENGTH ];

			// Yes - calculate the actual checksum...
			Asura_Checksum::EncodeFromBuffer( &pucTemp[ Asura_Checksum::CHECKSUM_LENGTH ], m_uDataSize, aucActualChecksum );

			// Compare the checksums...
			bSuccess	= Asura_Checksum::Compare( aucActualChecksum, pucTemp );

			Asura_Assert( bSuccess, "File data failed checksum test.");

			if( bSuccess )
			{
				// Yes - do we need to validate the data?
				if( NULL != m_pfnValidation )
				{
					// Yes - do so...
					bSuccess	= m_pfnValidation( &pucTemp[ Asura_Checksum::CHECKSUM_LENGTH ], m_uDataSize, m_uBufferSize );

					Asura_Assert( bSuccess, "File data vaildation failed.");
				}

				// Have we finished our validation?
				if( bSuccess )
				{
					// Yes - copy the data to the destination...
					memcpy( m_pvData, &pucTemp[ Asura_Checksum::CHECKSUM_LENGTH ], m_uDataSize );
				}
			}
		}
	}

	// Delete the temporary buffer and return...
	delete[]( pucTemp );
	return( bSuccess);
}

/// End of function 'Asura_PSP_SCEUtility_LoadListHandler::OnSelect'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtility_LoadListHandler::RenderEmptySlot
// Author       : Andy Tate
// Date         : 5-Oct-2005
//
// Description  : Renders the data for this empty slot.
//
// Parameters   : A reference to the current slot data and the screen-scale to
//				  use when calculating positions.
//
// Return value : None.
////////////////////////////////////////////////////////////////////////////////

void Asura_PSP_SCEUtility_LoadListHandler::RenderEmptySlot (const float fScreenScale,
															const Asura_PSP_SCEUtility_FileData& xFileData) const
{
	// Should never get here!!!
	Asura_Assert	(false, "We should never have empty slots in the load-list.");
}

/// End of function 'Asura_PSP_SCEUtility_LoadListHandler::RenderEmptySlot'. ///



////////////////////////////////////////////////////////////////////////////////
// Public functions...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtility_LoadListHandler::Asura_PSP_SCEUtility_LoadListHandler
// Author       : Andy Tate
// Date         : 6-Oct-2005
//
// Description  : Constructor function for the save-list handler.
//
// Parameters   : 
//
// Return value : N/A.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtility_LoadListHandler::Asura_PSP_SCEUtility_LoadListHandler (const int iMaxSaveSlots,
																			void* pvData,
																			const u_int uDataSize,
																			const u_int uBufferSize,
																			ValidationFunction* pfnValidation) :
	Asura_PSP_SCEUtility_FileListHandler(L"Load Data from Slot", iMaxSaveSlots, false),
	m_pvData(pvData),
	m_uDataSize(uDataSize),
	m_uBufferSize(uBufferSize),
	m_pfnValidation(pfnValidation)
{
	// Nothing more to initialsie...
}

/// End of function 'Asura_PSP_SCEUtility_LoadListHandler::Asura_PSP_SCEUtility_LoadListHandler'. ///


/// End of file. ///
