diff options
author | Ladislav <Zezula> | 2014-03-27 17:21:38 +0100 |
---|---|---|
committer | Ladislav <Zezula> | 2014-03-27 17:21:38 +0100 |
commit | 27631209d90b8863db06b4c81c7b21efb32f2f2d (patch) | |
tree | a8680e8c5c4262c514b25d129eb1cd0f1ddf6178 /src/SFileListFile.cpp | |
parent | c8db90a9c4dcf30b626861bfe6efeec818a78719 (diff) |
+ Fixed crash when adding file after an empty archive has been flushed
Diffstat (limited to 'src/SFileListFile.cpp')
-rw-r--r-- | src/SFileListFile.cpp | 83 |
1 files changed, 46 insertions, 37 deletions
diff --git a/src/SFileListFile.cpp b/src/SFileListFile.cpp index b2db0c5..ed549d1 100644 --- a/src/SFileListFile.cpp +++ b/src/SFileListFile.cpp @@ -363,49 +363,58 @@ int SListFileSaveToMpq(TMPQArchive * ha) DWORD cbListFile = 0; int nError = ERROR_SUCCESS; - // Only save (listfile) if we should do so - if(ha->dwFileFlags1 == 0 || ha->dwMaxFileCount == 0) - return ERROR_SUCCESS; + // Only save the listfile if we should do so + if(ha->dwFileFlags1 != 0) + { + // At this point, we expect to have at least one reserved entry in the file table + assert(ha->dwFlags & MPQ_FLAG_LISTFILE_INVALID); + assert(ha->dwReservedFiles >= 1); + + // Create the raw data that is to be written to (listfile) + // Note: Creating the raw data before the (listfile) has been created in the MPQ + // causes that the name of the listfile will not be included in the listfile itself. + // That is OK, because (listfile) in Blizzard MPQs does not contain it either. + pbListFile = CreateListFile(ha, &cbListFile); + if(pbListFile != NULL) + { + // We expect it to be nonzero size + assert(cbListFile != 0); + + // Determine the real flags for (listfile) + if(ha->dwFileFlags1 == MPQ_FILE_EXISTS) + ha->dwFileFlags1 = GetDefaultSpecialFileFlags(cbListFile, ha->pHeader->wFormatVersion); + + // Create the listfile in the MPQ + nError = SFileAddFile_Init(ha, LISTFILE_NAME, + 0, + cbListFile, + LANG_NEUTRAL, + ha->dwFileFlags1 | MPQ_FILE_REPLACEEXISTING, + &hf); + + // Write the listfile raw data to it + if(nError == ERROR_SUCCESS) + { + // Write the content of the listfile to the MPQ + nError = SFileAddFile_Write(hf, pbListFile, cbListFile, MPQ_COMPRESSION_ZLIB); + SFileAddFile_Finish(hf); + } - // At this point, we expect to have at least one reserved entry in the file table - assert(ha->dwReservedFiles >= 1); - ha->dwReservedFiles--; + // Free the listfile buffer + STORM_FREE(pbListFile); + } + else + { + // If the list file is empty, we assume ERROR_SUCCESS + nError = (cbListFile == 0) ? ERROR_SUCCESS : ERROR_NOT_ENOUGH_MEMORY; + } - // Create the raw data that is to be written to (listfile) - // Note: Creating the raw data before the (listfile) has been created in the MPQ - // causes that the name of the listfile will not be included in the listfile itself. - // That is OK, because (listfile) in Blizzard MPQs does not contain it either. - pbListFile = CreateListFile(ha, &cbListFile); - if(pbListFile != NULL) - { - // We expect it to be nonzero size - assert(cbListFile != 0); - - // Determine the real flags for (listfile) - if(ha->dwFileFlags1 == MPQ_FILE_EXISTS) - ha->dwFileFlags1 = GetDefaultSpecialFileFlags(cbListFile, ha->pHeader->wFormatVersion); - - // Create the listfile in the MPQ - nError = SFileAddFile_Init(ha, LISTFILE_NAME, - 0, - cbListFile, - LANG_NEUTRAL, - ha->dwFileFlags1 | MPQ_FILE_REPLACEEXISTING, - &hf); - - // Write the listfile raw data to it + // If the save process succeeded, we clear the MPQ_FLAG_LISTFILE_INVALID flag if(nError == ERROR_SUCCESS) { - // Write the content of the listfile to the MPQ - nError = SFileAddFile_Write(hf, pbListFile, cbListFile, MPQ_COMPRESSION_ZLIB); - SFileAddFile_Finish(hf); - - // Clear the invalidate flag ha->dwFlags &= ~MPQ_FLAG_LISTFILE_INVALID; + ha->dwReservedFiles--; } - - // Free the listfile buffer - STORM_FREE(pbListFile); } return nError; |