# HG changeset patch # User Ritor1 # Date 1390307543 -21600 # Node ID 5261002b7d3923f7330a326c4ec45a3f2137a7fe # Parent daa61976637daf842129ef30400ee355c220c238 autosave continue diff -r daa61976637d -r 5261002b7d39 LOD.cpp --- a/LOD.cpp Tue Jan 21 00:59:21 2014 +0600 +++ b/LOD.cpp Tue Jan 21 18:32:23 2014 +0600 @@ -1501,163 +1501,161 @@ //----- (00461B48) -------------------------------------------------------- unsigned int LODWriteableFile::Write(const LOD::Directory *pDir, const void *pDirData, int a4) { - char Filename[256]; - char NewFilename[256]; - FILE *tmp_file; - int comp_res; - bool bRewrite_data; - int offset_to_data; - int total_data_size; - int size_correction; - int to_copy_size; - int read_size; - int curr_position; - int insert_index; - - //insert new data in sorted index lod file - bRewrite_data = false; - insert_index=-1; - if (!isFileOpened)//sometimes gives crash - return 1; - if ( !pSubIndices) - return 2; - if (!pIOBuffer||!uIOBufferSize) - return 3; - - for (int i=0;ipFilename); - if(comp_res==0) - { - insert_index=i; - if (a4==0) - { - bRewrite_data=true; + char Filename[256]; + char NewFilename[256]; + FILE *tmp_file; + int comp_res; + bool bRewrite_data; + int offset_to_data; + int total_data_size; + int size_correction; + int to_copy_size; + int read_size; + int curr_position; + int insert_index; - break; - } - if (a4==1) - { - if(pSubIndices[i].uNumSubIndicesuNumSubIndices) - { - if (pSubIndices[i].word_00001Eword_00001E) - return 4; - } - else - bRewrite_data=true; - break; - } - if (a4==2) - return 4; - } - else if (comp_res>0) - { - if (insert_index==-1) - { - insert_index=i; - break; - } - } - } - strcpy(Filename, "lod.tmp"); - tmp_file = fopen(Filename, "wb+"); - if ( !tmp_file ) - return 5; - if (!bRewrite_data) - size_correction=0; - else - size_correction=pSubIndices[insert_index].uDataSize; + //insert new data in sorted index lod file + bRewrite_data = false; + insert_index = -1; + if ( !isFileOpened )//sometimes gives crash + return 1; + if ( !pSubIndices ) + return 2; + if ( !pIOBuffer || !uIOBufferSize ) + return 3; - //create chapter index - LOD::Directory Lindx; - strcpy(Lindx.pFilename, "chapter"); - Lindx.dword_000018=0; - Lindx.word_00001E=0; - Lindx.uNumSubIndices= uNumSubDirs; - Lindx.uOfsetFromSubindicesStart=sizeof(LOD::FileHeader)+sizeof(LOD::Directory); - total_data_size=uLODDataSize+pDir->uDataSize-size_correction; - if (!bRewrite_data) + for ( int i = 0; i < uNumSubDirs; i++ ) + { + comp_res = _stricmp(pSubIndices[i].pFilename, pDir->pFilename); + if( comp_res == 0 ) + { + insert_index = i; + if ( a4 == 0 ) + { + bRewrite_data = true; + break; + } + if ( a4 == 1 ) + { + if ( pSubIndices[i].uNumSubIndices < pDir->uNumSubIndices ) { - total_data_size+=sizeof(LOD::Directory); - Lindx.uNumSubIndices++; - } - - Lindx.uDataSize=total_data_size; - uNumSubDirs= Lindx.uNumSubIndices; - //move indexes +1 after insert point - if (!bRewrite_data&&(insert_indexinsert_index; --i) - memcpy(&pSubIndices[i],&pSubIndices[i-1],sizeof(LOD::Directory)); - + if ( pSubIndices[i].word_00001E < pDir->word_00001E ) + return 4; } - //insert - memcpy(&pSubIndices[insert_index],pDir,sizeof(LOD::Directory)); - //correct offsets to data - if (uNumSubDirs>0) - { - offset_to_data=sizeof(LOD::Directory)*uNumSubDirs; - for (int i=0;i 0 ) + { + if ( insert_index == -1 ) + { + insert_index=i; + break; + } + } + } + strcpy(Filename, "lod.tmp"); + tmp_file = fopen(Filename, "wb+"); + if ( !tmp_file ) + return 5; + if (!bRewrite_data) + size_correction=0; + else + size_correction=pSubIndices[insert_index].uDataSize; - //construct lod file with added data - fwrite( &header, sizeof(LOD::FileHeader), 1, tmp_file); - fwrite(&Lindx, sizeof(LOD::Directory), 1, tmp_file); - fseek(pFile,Lindx.uOfsetFromSubindicesStart, SEEK_SET); - fwrite(pSubIndices, sizeof(LOD::Directory), uNumSubDirs, tmp_file); - - offset_to_data = sizeof(LOD::Directory) * uNumSubDirs; - if ( !bRewrite_data ) - offset_to_data -= sizeof(LOD::Directory); + //create chapter index + LOD::Directory Lindx; + strcpy(Lindx.pFilename, "chapter"); + Lindx.dword_000018 = 0; + Lindx.word_00001E = 0; + Lindx.uNumSubIndices = uNumSubDirs; + Lindx.uOfsetFromSubindicesStart = sizeof(LOD::FileHeader) + sizeof(LOD::Directory); + total_data_size = uLODDataSize + pDir->uDataSize-size_correction; + if (!bRewrite_data) + { + total_data_size += sizeof(LOD::Directory); + Lindx.uNumSubIndices++; + } - fseek(pFile, offset_to_data, SEEK_CUR); - //copy from open lod to temp lod first half - to_copy_size=pSubIndices[insert_index].uOfsetFromSubindicesStart-pSubIndices[0].uOfsetFromSubindicesStart; - while(to_copy_size>0) - { - read_size = uIOBufferSize; - if ( to_copy_size <= uIOBufferSize ) - read_size = to_copy_size; - fread(pIOBuffer, 1, read_size, pFile); - fwrite(pIOBuffer, 1, read_size, tmp_file); - to_copy_size-=read_size; - } - // add container data - fwrite(pDirData, 1, pDir->uDataSize, tmp_file); - if ( bRewrite_data ) - fseek(pFile,size_correction , SEEK_CUR); + Lindx.uDataSize = total_data_size; + uNumSubDirs = Lindx.uNumSubIndices; + //move indexes +1 after insert point + if ( !bRewrite_data && (insert_index < uNumSubDirs) ) + { + for( int i = uNumSubDirs; i > insert_index; --i ) + memcpy(&pSubIndices[i], &pSubIndices[i-1], sizeof(LOD::Directory)); + } + //insert + memcpy(&pSubIndices[insert_index], pDir, sizeof(LOD::Directory)); + //correct offsets to data + if (uNumSubDirs > 0) + { + offset_to_data = sizeof(LOD::Directory) * uNumSubDirs; + for ( int i = 0; i < uNumSubDirs; i++ ) + { + pSubIndices[i].uOfsetFromSubindicesStart = offset_to_data; + offset_to_data += pSubIndices[i].uDataSize; + } + } + + //construct lod file with added data + fwrite(&header, sizeof(LOD::FileHeader), 1, tmp_file); + fwrite(&Lindx, sizeof(LOD::Directory), 1, tmp_file); + fseek(pFile,Lindx.uOfsetFromSubindicesStart, SEEK_SET); + fwrite(pSubIndices, sizeof(LOD::Directory), uNumSubDirs, tmp_file); + + offset_to_data = sizeof(LOD::Directory) * uNumSubDirs; + if ( !bRewrite_data ) + offset_to_data -= sizeof(LOD::Directory); - //add remainng data last half - curr_position = ftell(pFile); - fseek(pFile, 0, SEEK_END); - to_copy_size = ftell(pFile) - curr_position; - fseek(pFile, curr_position, SEEK_SET); - while ( to_copy_size > 0 ) - { - read_size = uIOBufferSize; - if ( to_copy_size <= uIOBufferSize ) - read_size = to_copy_size; - fread(pIOBuffer, 1, read_size, pFile); - fwrite(pIOBuffer, 1, read_size, tmp_file); - to_copy_size-=read_size; - } - //replace old file by new with added data - strcpy(NewFilename, pLODName); - fclose(tmp_file); - CloseWriteFile(); //isFileOpened == false, current file - remove(NewFilename); - rename(Filename, NewFilename); - CloseWriteFile(); - //reload new - LoadFile(pLODName, 0);//isFileOpened == true, next file - if (isFileOpened == false) + fseek(pFile, offset_to_data, SEEK_CUR); + //copy from open lod to temp lod first half + to_copy_size = pSubIndices[insert_index].uOfsetFromSubindicesStart - pSubIndices[0].uOfsetFromSubindicesStart; + while(to_copy_size > 0) + { + read_size = uIOBufferSize; + if ( to_copy_size <= uIOBufferSize ) + read_size = to_copy_size; + fread(pIOBuffer, 1, read_size, pFile); + fwrite(pIOBuffer, 1, read_size, tmp_file); + to_copy_size -= read_size; + } + // add container data + fwrite(pDirData, 1, pDir->uDataSize, tmp_file); + if ( bRewrite_data ) + fseek(pFile,size_correction , SEEK_CUR); + + //add remainng data last half + curr_position = ftell(pFile); + fseek(pFile, 0, SEEK_END); + to_copy_size = ftell(pFile) - curr_position; + fseek(pFile, curr_position, SEEK_SET); + while ( to_copy_size > 0 ) + { + read_size = uIOBufferSize; + if ( to_copy_size <= uIOBufferSize ) + read_size = to_copy_size; + fread(pIOBuffer, 1, read_size, pFile); + fwrite(pIOBuffer, 1, read_size, tmp_file); + to_copy_size -= read_size; + } + //replace old file by new with added data + strcpy(NewFilename, pLODName); + fclose(tmp_file); + CloseWriteFile(); //isFileOpened == false, current file + remove(NewFilename); + rename(Filename, NewFilename); + CloseWriteFile(); + //reload new + LoadFile(pLODName, 0);//isFileOpened == true, next file + if (isFileOpened == false) __debugbreak(); - return 0; - } + return 0; +} //----- (00461A43) --------------------------------------------------------