changeset 2175:5261002b7d39

autosave continue
author Ritor1
date Tue, 21 Jan 2014 18:32:23 +0600
parents daa61976637d
children 2f9689a85c31
files LOD.cpp
diffstat 1 files changed, 147 insertions(+), 149 deletions(-) [+]
line wrap: on
line diff
--- 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;i<uNumSubDirs; i++)
-            {
-            comp_res=_stricmp(pSubIndices[i].pFilename,pDir->pFilename);
-            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].uNumSubIndices<pDir->uNumSubIndices)
-                        {
-                        if (pSubIndices[i].word_00001E<pDir->word_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_index<uNumSubDirs))
-        {
-        for(int i=uNumSubDirs;i>insert_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<uNumSubDirs; i++)
-            {
-            pSubIndices[i].uOfsetFromSubindicesStart=offset_to_data;
-            offset_to_data+=pSubIndices[i].uDataSize;
-            }
-        }
+        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;
 
-    //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) --------------------------------------------------------