stoneharry

Untitled

Apr 29th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. void PatchMgr::InitializePatchList()
  2. {
  3.     // load patches
  4.     Log.Debug("PatchMgr", "Starting up.");
  5. #ifdef WIN32
  6.     Log.Debug("PatchMgr", "Main function has been called.");
  7.     Log.Notice("PatchMgr", "Loading Patches...");
  8.     char Buffer[MAX_PATH*10];
  9.     char Buffer2[MAX_PATH*10];
  10.     char Buffer3[MAX_PATH*10];
  11.  
  12.     WIN32_FIND_DATA fd;
  13.     HANDLE fHandle;
  14.     MD5Hash md5;
  15.     Patch * pPatch;
  16.     DWORD size,sizehigh;
  17.     HANDLE hFile;
  18.     uint32 srcversion;
  19.     char locality[5];
  20.     uint32 i;
  21.  
  22.     if(!GetCurrentDirectory(MAX_PATH*10, Buffer))
  23.         return;
  24.  
  25.     strcpy(Buffer2,Buffer);
  26.     strcat(Buffer, "\\ClientPatches\\*.*");
  27.     fHandle = FindFirstFile(Buffer, &fd);
  28.     if(fHandle == INVALID_HANDLE_VALUE)
  29.         return;
  30.  
  31.     do
  32.     {
  33.         snprintf(Buffer3,MAX_PATH*10,"%s\\ClientPatches\\%s",Buffer2,fd.cFileName);
  34.         if(sscanf(fd.cFileName,"%4s%u.", locality, &srcversion) != 2)
  35.         {
  36.             Log.Notice("Found Incorrect : %s %s", locality, fd.cFileName);
  37.             continue;
  38.         }
  39.         else
  40.         {
  41.             Log.Notice("Found Correct : %s %s", locality, fd.cFileName);
  42.         }
  43.  
  44.         hFile = CreateFile(Buffer3, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);
  45.         if(hFile == INVALID_HANDLE_VALUE)
  46.             continue;
  47.  
  48.         Log.Notice("PatchMgr", "Found patch for %u locale `%s`.", srcversion,locality);
  49.         pPatch = new Patch;
  50.         size = GetFileSize(hFile, &sizehigh);
  51.         pPatch->FileSize = size;
  52.         pPatch->Data = new uint8[size];
  53.         pPatch->Version = srcversion;
  54.         for(i = 0; i < 4; ++i)
  55.             pPatch->Locality[i] = static_cast<char>(tolower(locality[i]));
  56.         pPatch->Locality[4] = 0;
  57.         pPatch->uLocality = *(uint32*)pPatch->Locality;
  58.  
  59.         if(pPatch->Data== NULL)
  60.         {
  61.             // shouldn't really happen
  62.             delete pPatch;
  63.             CloseHandle(hFile);
  64.             continue;
  65.         }
  66.  
  67.         // read the whole file
  68.         ASSERT(ReadFile(hFile, pPatch->Data, pPatch->FileSize, &size, NULL));
  69.         ASSERT(size == pPatch->FileSize);
  70.  
  71.         // close the handle, no longer needed
  72.         CloseHandle(hFile);
  73.  
  74.         // md5hash the file
  75.         md5.Initialize();
  76.         md5.UpdateData(pPatch->Data, pPatch->FileSize);
  77.         md5.Finalize();
  78.         memcpy(pPatch->MD5, md5.GetDigest(), MD5_DIGEST_LENGTH);
  79.        
  80.         // add the patch to the patchlist
  81.         m_patches.push_back(pPatch);
  82.  
  83.     } while(FindNextFile(fHandle,&fd));
  84.     FindClose(fHandle);
  85. #else
  86.     /*
  87.      *nix patch loader
  88.      */
  89.     Log.Notice("PatchMgr", "Loading Patches...");
  90.     char Buffer[MAX_PATH*10];
  91.     char Buffer2[MAX_PATH*10];
  92.     char Buffer3[MAX_PATH*10];
  93.  
  94.     struct dirent ** list;
  95.     int filecount;
  96.     int read_fd;
  97.     MD5Hash md5;
  98.     Patch * pPatch;
  99.     int size;
  100.     uint32 srcversion;
  101.     char locality[5];
  102.     uint32 i;
  103.     struct stat sb;
  104.  
  105.     strcpy(Buffer, "./ClientPatches");
  106.     strcpy(Buffer2,Buffer);
  107.  
  108.     filecount = scandir("./ClientPatches", &list, 0, 0);
  109.     if(filecount <= 0 || list== NULL)
  110.     {
  111.         Log.Error("PatchMgr", "No patches found.");
  112.         return;
  113.     }
  114.  
  115.     while(filecount--)
  116.     {
  117.         snprintf(Buffer3,MAX_PATH*10,"./ClientPatches/%s",list[filecount]->d_name);
  118.         if(sscanf(list[filecount]->d_name,"%4s%u.", locality, &srcversion) != 2)
  119.             continue;
  120.  
  121.         read_fd = open(Buffer3, O_RDONLY);
  122.         if(read_fd <= 0)
  123.         {
  124.             LOG_ERROR("Cannot open %s", Buffer3);
  125.             continue;
  126.         }
  127.  
  128.         if(fstat(read_fd, &sb) < 0)
  129.         {
  130.             LOG_ERROR("Cannot stat %s", Buffer3);
  131.             continue;
  132.         }
  133.  
  134.         Log.Notice("PatchMgr", "Found patch for b%u locale `%s` (%u bytes).", srcversion,locality, sb.st_size);
  135.         pPatch = new Patch;
  136.         size = sb.st_size;
  137.         pPatch->FileSize = size;
  138.         pPatch->Data = new uint8[size];
  139.         pPatch->Version = srcversion;
  140.         for(i = 0; i < 4; ++i)
  141.             pPatch->Locality[i] = tolower(locality[i]);
  142.         pPatch->Locality[4] = 0;
  143.         pPatch->uLocality = *(uint32*)pPatch->Locality;
  144.  
  145.         if(pPatch->Data== NULL)
  146.         {
  147.             // shouldn't really happen
  148.             delete pPatch;
  149.             close(read_fd);
  150.             continue;
  151.         }
  152.  
  153.         // read the whole file
  154.         ASSERT(read(read_fd, pPatch->Data, size) == size);
  155.  
  156.         // close handle
  157.         close(read_fd);
  158.  
  159.         // md5hash the file
  160.         md5.Initialize();
  161.         md5.UpdateData(pPatch->Data, pPatch->FileSize);
  162.         md5.Finalize();
  163.         memcpy(pPatch->MD5, md5.GetDigest(), MD5_DIGEST_LENGTH);
  164.  
  165.         // add the patch to the patchlist
  166.         m_patches.push_back(pPatch);
  167.         free(list[filecount]);
  168.     }
  169.     free(list);
  170. #endif
  171. }
Advertisement
Add Comment
Please, Sign In to add comment