/*===========================================================================
 *
 * File:	Dfblocks.CPP
 * Author:	Dave Humphrey (uesp@m0use.net)
 * Created On:	Friday, June 29, 2001
 *
 * Implements the CDFBlocksFile for handling Daggerfall's BLOCKS.BSA file.
 *
 *=========================================================================*/

	/* Include Files */
#include "dfblocks.h"


/*===========================================================================
 *
 * Begin Local Variable Definitions
 *
 *=========================================================================*/
  DEFINE_FILE();
/*===========================================================================
 *		End of Local Variable Definitions
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Constructor
 *
 *=========================================================================*/
CDFBlocksFile::CDFBlocksFile() {
 }
/*===========================================================================
 *		End of Class CDFBlocksFile Constructor
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Destructor
 *
 *=========================================================================*/
void CDFBlocksFile::Destroy (void) {
  DEFINE_FUNCTION("CDFBlocksFile::Destroy()");
  
	/* Call base class method */
  CDFBsaFile::Destroy();
 }
/*===========================================================================
 *		End of Class CDFBlocksFile Destructor
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Method - 
 *		boolean GetRMBFile (ppObject, FileIndex, CharIndex, FileNumber);
 *
 * Attempts to create amd load a RMBFile into the given object as specified
 * by the input indices.  Returns FALSE on any error.
 *
 *=========================================================================*/
boolean CDFBlocksFile::LoadRMB (CDFRmbFile** ppObject, const int FileIndex, 
			        const int CharIndex,   const int FileNumber) {
  DEFINE_FUNCTION("CDFBlocksFile::LoadRMB()");
  boolean Result;
  char    Filename[16];
  char    FileString[8];
  char    FileChar[4];

	/* Ensure valid input */
  ASSERT(ppObject != NULL);
  ASSERT(FileNumber >= 0 && FileNumber < DFRMB_MAX_FILENUMBER);

	/* Find in the file character strings */
  Result = GetDFRmbFileChar(FileChar, CharIndex);
  if (Result) Result = GetDFRmbFilename(FileString, FileIndex);
  if (!Result) return (FALSE);
     
	/* Create the RMB filename to load */
  sprintf (Filename, "%4s%2s%02d.rmb", FileString, FileChar, FileNumber);
  //SystemLog.Printf ("CDFBlocksFile::ReadRMB(%d, 0x%02X, %d)", FileIndex, CharIndex, Number);

	/* Load the RMB file by BSA directory filename index */
  return LoadRMB(ppObject, Filename);
 }
/*===========================================================================
 *		End of Class Method CDFBlocksFile::LoadRMB()
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Method - boolean LoadRMB (ppObject, pFileName);
 *
 * Attempts to load a RMBFile into the given array object by its BSA 
 * filename.  Returns FALSE on any error.
 *
 *=========================================================================*/
boolean CDFBlocksFile::LoadRMB (CDFRmbFile** ppObject, const char* pFilename) {
  DEFINE_FUNCTION("CDFBlocksFile::LoadRMB()");
  int   Index;

	/* Find the record index of the specified file */
  Index = FindRecord(pFilename);
  if (Index < 0) return (FALSE);

	/* Read the RMB object from its BSA record index */
  return LoadRMB(ppObject, Index);
 }
/*===========================================================================
 *		End of Class Method CDFBlocksFile::LoadRMB()
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Method - boolean LoadRMB (ppObject, Index);
 *
 * Attempts to input a RMBFile into the given array object by its BSA 
 * record index.  Returns FALSE on any error.
 *
 *=========================================================================*/
boolean CDFBlocksFile::LoadRMB (CDFRmbFile** ppObject, const int Index) {
  DEFINE_FUNCTION("CDFBlocksFile::LoadRMB()");
  boolean Result;

	/* Ensure valid object state and input */
  ASSERT(IsOpen() && ppObject != NULL);

	/* Ensure a valid input index */
  if (!IsRMBFile(Index)) {
    ErrorHandler.AddError(DFERR_BLOCKS_NOTRMB);
    return (FALSE);
   }

	/* Move to the appropiate position in file and read */
  Result = Seek(GetRecordOffset(Index), SEEK_SET);

  if (Result) { 
    CreatePointer(*ppObject, CDFRmbFile);
    (*ppObject)->Attach(GetHandle());
    Result = (*ppObject)->ReadRMB(GetRecordSize(Index));
    (*ppObject)->Detach();
    if (!Result) DestroyPointer(*ppObject);
   }

  return (Result);
 }
/*===========================================================================
 *		End of Class Method CDFBlocksFile::LoadRMB()
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Method - boolean IsRMBFile (Index);
 *
 * Returns TRUE if the given BSA directory entry is a RMB type file.
 *
 *=========================================================================*/
boolean CDFBlocksFile::IsRMBFile (const int Index) const {

	/* Ensure a valid input index */
  if (!IsValidIndex(Index)) return (FALSE);

	/* Ensure a valid filename extension */
  return CompareExtension(GetRecordName(Index), "RMB");
 }
/*===========================================================================
 *		End of Class Method CDFBlocksFile::IsRMBFile()
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Method - boolean IsRDIFile (Index);
 *
 * Returns TRUE if the given BSA directory entry is a RDI type file.
 *
 *=========================================================================*/
boolean CDFBlocksFile::IsRDIFile (const int Index) const {

	/* Ensure a valid input index */
  if (!IsValidIndex(Index)) return (FALSE);

	/* Ensure a valid filename extension */
  return CompareExtension(GetRecordName(Index), "RDI");
 }
/*===========================================================================
 *		End of Class Method CDFBlocksFile::IsRDIFile()
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFBlocksFile Method - boolean IsRDBFile (Index);
 *
 * Returns TRUE if the given BSA directory entry is a RDB type file.
 *
 *=========================================================================*/
boolean CDFBlocksFile::IsRDBFile (const int Index) const {

	/* Ensure a valid input index */
  if (!IsValidIndex(Index)) return (FALSE);

	/* Ensure a valid filename extension */
  return CompareExtension(GetRecordName(Index), "RDB");
 }
/*===========================================================================
 *		End of Class Method CDFBlocksFile::IsRDBFile()
 *=========================================================================*/