/*---------------------------------------------------------------------------* Project: Horizon File: tpl_ReadTexturePackage.cpp Copyright (C)2009 Nintendo Co., Ltd. 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. $Rev: 19746 $ *---------------------------------------------------------------------------*/ #include #include #include #include #include "tpl_crc32.h" #include "tpl_Internal.h" namespace nn { namespace tpl { namespace CTR { // テクスチャインデックス取得関数 s16 GetTextureIndex(const void* pTexPackage, const char* texPath) { if(!pTexPackage || !texPath) { NN_TASSERT_((pTexPackage != 0) && (texPath != 0)); return -1; } // CRC32のハッシュコードを作成。 int fileNameLen = (int)::std::strlen(texPath); unsigned int crc32key = calculate_crc32(texPath, fileNameLen); const CtrTexturePackageHeader &header = *reinterpret_cast(pTexPackage); const CtrTextureHash *texHash = reinterpret_cast ((const char*)pTexPackage + (header.texHashOffset)); // パッケージ内のテクスチャ情報と比較して一致するものを探す。 TPK_SEARCH_DATA data = { texPath, fileNameLen, pTexPackage }; int t = search_crc32_data( texHash, header.numTexture, sizeof(CtrTextureHash), crc32key, &data); if(t < 0) return -1; return texHash[t].index; } // テクスチャ取得関数 void* GetTexture(s32* mipLevel, u32* mipmapSize, const void* pTexPackage, const s16 texIndex) { if(!pTexPackage) { NN_TASSERT_(pTexPackage != 0); return 0; } // 指定された番号がテクスチャの枚数に収まっているか確認。 const CtrTexturePackageHeader *header = reinterpret_cast(pTexPackage); NN_TASSERT_((0 <= texIndex) && (texIndex < header->numTexture)); if((texIndex < 0) || (header->numTexture <= texIndex)) { return 0; } const CtrTextureInfo *texInfo = reinterpret_cast (((const char*)pTexPackage) + (sizeof(CtrTexturePackageHeader)) + (sizeof(CtrTextureInfo) * texIndex)); if(!texInfo) { return 0; } // get texture size. const unsigned int *Size = reinterpret_cast (((const char*)pTexPackage) + (4 * texInfo->bitmapSizeOffset)); *mipLevel = static_cast(texInfo->mipLevel); for(int i=0; i < *mipLevel; i++) { mipmapSize[i] = Size[i]; } return (void*)(((const char*)pTexPackage) + (header->texDataOffset + texInfo->texDataOffset)); } // テクスチャ情報取得関数 bool GetTextureInfo(CtrTextureInfo* pTexInfo, const void* pTexPackage, const s16 texIndex) { if(!pTexPackage || !pTexInfo) { NN_TASSERT_((pTexPackage != 0) && (pTexInfo != 0)); return false; } // 指定された番号がテクスチャの枚数に収まっているか確認。 const CtrTexturePackageHeader *header = reinterpret_cast(pTexPackage); NN_TASSERT_((0 <= texIndex) && (texIndex < header->numTexture)); if((texIndex < 0) || (header->numTexture <= texIndex)) { return false; } const CtrTextureInfo *texInfo = reinterpret_cast (((const char*)pTexPackage) + (sizeof(CtrTexturePackageHeader)) + (sizeof(CtrTextureInfo) * texIndex)); pTexInfo->filePathOffset = texInfo->filePathOffset; pTexInfo->texDataSize = texInfo->texDataSize; pTexInfo->texDataOffset = texInfo->texDataOffset; pTexInfo->texFormat = texInfo->texFormat; pTexInfo->width = texInfo->width; pTexInfo->height = texInfo->height; pTexInfo->mipLevel = texInfo->mipLevel; pTexInfo->type = texInfo->type; pTexInfo->cubeDir = texInfo->cubeDir; pTexInfo->bitmapSizeOffset = texInfo->bitmapSizeOffset; pTexInfo->srcFileTime = texInfo->srcFileTime; return true; } // テクスチャパッケージヘッダーチェック関数(ヘルパー関数) bool CheckTexturePackageHeader(const void* pTexPackage) { if(!pTexPackage) { NN_TASSERT_(pTexPackage != 0); return false; } // 指定された番号がテクスチャの枚数に収まっているか確認。 const CtrTexturePackageHeader *header = reinterpret_cast(pTexPackage); NN_TASSERT_(header); // ヘッダのマジックコードを確認。 if(::std::strncmp(header->magic, "CTPK", 4) != 0) { NN_LOG("ERROR: The texture package's magic code isn't 'CTPK'.\n"); return false; } if(header->version != CTR_TEXTURE_PACKAGE_VERSION) { NN_LOG("ERROR: The texture package's version code differs.\n"); return false; } return true; } }}}