1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
DWORD LoadAssetsFromArchive(_In_ char* ArchiveName, _In_ char* AssetFileName, _In_ RESOURCE_TYPE ResourceType, _Inout_ void* Resource)
{
DWORD Error = ERROR_SUCCESS;
mz_zip_archive Archive = { 0 };
BYTE* DecompressedBuffer = NULL;
size_t DecompressedSize = 0;
BOOL FileFoundInArchive = FALSE;
if (mz_zip_reader_init_file(&Archive, ArchiveName, 0) == MZ_FALSE)
{
Error = mz_zip_get_last_error(&Archive);
LogMessageA(LL_ERROR, "[%s] mz_zip_reader_init_file failed with 0x%08lx on archive file %s Error: %s !", __FUNCTION__, Error, ArchiveName, mz_zip_get_error_string(Error));
goto Exit;
}
for (int FileIndex = 0; FileIndex < (int)mz_zip_reader_get_num_files(&Archive); FileIndex++)
{
mz_zip_archive_file_stat CompressedFileStatistics = { 0 };
if (mz_zip_reader_file_stat(&Archive, FileIndex, &CompressedFileStatistics) == MZ_FALSE)
{
Error = mz_zip_get_last_error(&Archive);
LogMessageA(LL_ERROR, "[%s] mz_zip_reader_file_stat failed with 0x%08lx Archive %s File %s Error: %s !", __FUNCTION__, Error, ArchiveName, AssetFileName, mz_zip_get_error_string(Error));
goto Exit;
}
if (_stricmp(CompressedFileStatistics.m_filename, AssetFileName) == 0)
{
FileFoundInArchive = TRUE;
if ((DecompressedBuffer = mz_zip_reader_extract_to_heap(&Archive, FileIndex, &DecompressedSize, 0)) == NULL)
{
Error = mz_zip_get_last_error(&Archive);
LogMessageA(LL_ERROR, "[%s] mz_zip_reader_extract_to_heap failed with 0x%08lx Archive %s File %s Error %s !", __FUNCTION__, Error, ArchiveName, AssetFileName, mz_zip_get_error_string(Error));
goto Exit;
}
break;
}
}
if (FileFoundInArchive == FALSE)
{
Error = ERROR_FILE_NOT_FOUND;
LogMessageA(LL_ERROR, "[%s] File %s was not found in archive %s ! 0x%08lx", __FUNCTION__, AssetFileName, ArchiveName, Error);
goto Exit;
}
switch (ResourceType)
{
case RESOURCE_TYPE_WAV:
{
Error = LoadWavFromMemory(DecompressedBuffer, Resource);
break;
}
case RESOURCE_TYPE_OGG:
{
Error = LoadOggFromMemory(DecompressedBuffer, (uint32_t)DecompressedSize, Resource);
break;
}
case RESOURCE_TYPE_TILEMAP:
{
Error = LoadTilemapFromMemory(DecompressedBuffer, (uint32_t)DecompressedSize, Resource);
break;
}
case RESOURCE_TYPE_BMPX:
{
Error = Load32BppBitmapFromMemory(DecompressedBuffer, Resource);
break;
}
default:
ASSERT(FALSE, "Unknown resource type !");
}
Exit:
mz_zip_reader_end(&Archive);
return(Error);
}
|