stoneharry

Untitled

Apr 29th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1.     /*
  2.      *nix patch loader
  3.      */
  4.     Log.Notice("PatchMgr", "Loading Patches...");
  5.     char Buffer[MAX_PATH*10];
  6.     char Buffer2[MAX_PATH*10];
  7.     char Buffer3[MAX_PATH*10];
  8.  
  9.     struct dirent ** list;
  10.     int filecount;
  11.     int read_fd;
  12.     MD5Hash md5;
  13.     Patch * pPatch;
  14.     int size;
  15.     uint32 srcversion;
  16.     char locality[5];
  17.     uint32 i;
  18.     struct stat sb;
  19.  
  20.     strcpy(Buffer, "./ClientPatches");
  21.     strcpy(Buffer2,Buffer);
  22.  
  23.     filecount = scandir("./ClientPatches", &list, 0, 0);
  24.     if(filecount <= 0 || list== NULL)
  25.     {
  26.         Log.Error("PatchMgr", "No patches found.");
  27.         return;
  28.     }
  29.  
  30.     while(filecount--)
  31.     {
  32.         snprintf(Buffer3,MAX_PATH*10,"./ClientPatches/%s",list[filecount]->d_name);
  33.         if(sscanf(list[filecount]->d_name,"%4s%u.", locality, &srcversion) != 2)
  34.             continue;
  35.  
  36.         read_fd = open(Buffer3, O_RDONLY);
  37.         if(read_fd <= 0)
  38.         {
  39.             LOG_ERROR("Cannot open %s", Buffer3);
  40.             continue;
  41.         }
  42.  
  43.         if(fstat(read_fd, &sb) < 0)
  44.         {
  45.             LOG_ERROR("Cannot stat %s", Buffer3);
  46.             continue;
  47.         }
  48.  
  49.         Log.Notice("PatchMgr", "Found patch for b%u locale `%s` (%u bytes).", srcversion,locality, sb.st_size);
  50.         pPatch = new Patch;
  51.         size = sb.st_size;
  52.         pPatch->FileSize = size;
  53.         pPatch->Data = new uint8[size];
  54.         pPatch->Version = srcversion;
  55.         for(i = 0; i < 4; ++i)
  56.             pPatch->Locality[i] = tolower(locality[i]);
  57.         pPatch->Locality[4] = 0;
  58.         pPatch->uLocality = *(uint32*)pPatch->Locality;
  59.  
  60.         if(pPatch->Data== NULL)
  61.         {
  62.             // shouldn't really happen
  63.             delete pPatch;
  64.             close(read_fd);
  65.             continue;
  66.         }
  67.  
  68.         // read the whole file
  69.         ASSERT(read(read_fd, pPatch->Data, size) == size);
  70.  
  71.         // close handle
  72.         close(read_fd);
  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.         free(list[filecount]);
  83.     }
  84.     free(list);
Advertisement
Add Comment
Please, Sign In to add comment