/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     m2d_resG2dLoader.cpp

  Copyright (C)2007-2009 Nintendo  All Rights Reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law. They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Revision: 1758 $
 *-------------------------------------------------------------------------*/

#include <nnsys/g2d.h>
#include <ntmv/m2d/resG2dLoader.h>
#include <ntmv/m2d/common.h>

namespace ntmv
{
namespace m2d
{
namespace detail
{

namespace
{

inline void*
GetResource(const char* resPath)
{
    return NNS_FndGetArchiveFileByName(resPath);
}

}   // namespace

/*---------------------------------------------------------------------------*
  Name:         LoadNCER

  Description:  Allocates a buffer internally and reads a cell data bank from an NCER file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the cell data bank becomes unnecessary.
                

  Arguments:    ppCellBank: Pointer to a pointer to the buffer with the cell data bank.
                            
                fileName:   Name of the NCER file where the cell data bank is stored.

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNCER(NNSG2dCellDataBank** ppCellBank, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppCellBank);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedCellBank(pFile, ppCellBank))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintCellBank(*ppCellBank);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNANR

  Description:  Allocates a buffer internally and reads an animation data bank from an NANR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the animation data bank becomes unnecessary.
                

  Arguments:    ppAnimBank: Pointer that gets the pointer to the buffer with the animation data bank.
                            
                fileName:   Name of the NANR file where the animation data bank is stored.
                            

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNANR(NNSG2dAnimBankData** ppAnimBank, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppAnimBank);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedAnimBank(pFile, ppAnimBank))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintAnimBank(*ppAnimBank);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}
/*---------------------------------------------------------------------------*
  Name:         LoadNMCR

  Description:  Allocates a buffer internally and reads a multi-cell data bank from an NMCR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the multi-cell data bank becomes unnecessary.
                

  Arguments:    ppMCBank:   Pointer to a pointer to the buffer with the multi-cell data bank.
                            
                fileName:   Name of the NMCR file where the multi-cell data bank is stored.
                            

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNMCR(NNSG2dMultiCellDataBank** ppMCBank, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppMCBank);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedMultiCellBank(pFile, ppMCBank))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintMultiCellBank (*ppMCBank);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNMAR

  Description:  Allocates a buffer internally and reads a multi-cell animation data bank from an NMAR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the multi-animation data bank becomes unnecessary.
                

  Arguments:    ppMCABank: Pointer to a pointer to the buffer with the multi-cell animation data bank.
                           
                fileName:  Name of the NMAR file where the multi-cell animation data bank is stored.
                           

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNMAR(NNSG2dMultiCellAnimBankData** ppMCABank, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppMCABank);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedMCAnimBank(pFile, ppMCABank))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintAnimBank(*ppMCABank);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNCGR

  Description:  Allocates a buffer internally and reads character data in the NITRO-CHARACTER format from an NCGR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the character data becomes unnecessary.
                

  Arguments:    ppCharData: Pointer to a pointer to the buffer with the character data.
                            
                fileName:   Name of NCGR file where the character data is stored.

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNCGR(NNSG2dCharacterData** ppCharData, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppCharData);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedCharacterData(pFile, ppCharData))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintCharacterData(*ppCharData);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNCGREx

  Description:  Allocates a buffer internally and reads character data in the NITRO-CHARACTER format from an NCGR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the character data becomes unnecessary.
                

  Arguments:    ppCharData:    Pointer to a pointer to the buffer with the character data.
                               
                ppCharPosInfo: Pointer to a pointer to the buffer with position data for character loads.
                               
                
                fileName:      Name of NCGR file where the character data is stored.

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNCGREx(
    NNSG2dCharacterData**       ppCharData, 
    NNSG2dCharacterPosInfo**    ppCharPosInfo,
    const char*                 fileName
/*, NNSFndAllocator* pAllocator */
)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppCharData);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedCharacterData(pFile, ppCharData))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintCharacterData(*ppCharData);
            #endif
            
            if (NNS_G2dGetUnpackedCharacterPosInfo(pFile, ppCharPosInfo))
            {
                #if defined(NNS_DEBUG)
                    NNS_G2dPrintCharacterPosInfo(*ppCharPosInfo);
                #endif

                return pFile;
            }
        }    
    }
    
    // NNS_FndFreeToAllocator(pAllocator, pFile);
    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNCLR

  Description:  Allocates a buffer internally and reads palette data from an NCLR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the palette data becomes unnecessary.
                

  Arguments:    ppPltData: Pointer to a pointer to the buffer with the palette data.
                           
                fileName:  Name of NCLR file where the palette data is stored.

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNCLR(NNSG2dPaletteData** ppPltData, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppPltData);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedPaletteData(pFile, ppPltData))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintPaletteData(*ppPltData);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNENR

  Description:  Allocates a buffer internally and reads an entity data bank from an NENR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the entity data bank becomes unnecessary.
                

  Arguments:    ppEntityBank: Pointer to a pointer to the buffer with the entity data bank.
                              
                fileName:     Name of the NENR file where the entity data bank is stored.
                              

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNENR(NNSG2dEntityDataBank** ppEntityBank, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppEntityBank);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedEntityBank(pFile, ppEntityBank))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintEntityDataBank(*ppEntityBank);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNCBR

  Description:  Allocates a buffer internally and reads character data in bitmap format from an NCBR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the character data becomes unnecessary.
                

  Arguments:    ppCharData: Pointer that gets the pointer to the buffer with the character data.
                            
                fileName:   Name of NCBR file where the character data is stored.

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNCBR(NNSG2dCharacterData** ppCharData, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppCharData);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedCharacterData(pFile, ppCharData))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintCharacterData(*ppCharData);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

/*---------------------------------------------------------------------------*
  Name:         LoadNSCR

  Description:  Allocates a buffer internally and reads character data in bitmap format from an NCBR file.
                
                After the function succeeds, you must use NNS_FndFreeToAllocator to release the buffer when the character data becomes unnecessary.
                

  Arguments:    ppCharData: Pointer that gets the pointer to the buffer with the character data.
                            
                fileName:   Name of NCBR file where the character data is stored.

  Returns:      Returns a pointer to an internally allocated buffer with the file data when it was loaded successfully.
                
                Returns NULL if it failed to load.
 *---------------------------------------------------------------------------*/
void*
LoadNSCR(NNSG2dScreenData** ppScreenData, const char* fileName/* , NNSFndAllocator* pAllocator */)
{
    void*   pFile = NULL;

    SDK_NULL_ASSERT(ppScreenData);
    SDK_NULL_ASSERT(fileName);

    pFile = GetResource(fileName);
    if (pFile != NULL)
    {
        if (NNS_G2dGetUnpackedScreenData(pFile, ppScreenData))
        {
            #if defined(NNS_DEBUG)
                NNS_G2dPrintScreenData(*ppScreenData);
            #endif

            return pFile;
        }

        // NNS_FndFreeToAllocator(pAllocator, pFile);
    }

    return NULL;
}

}   // namespace detail
}   // namespace m2d
}   // namespace ntmv
