Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void PatchMgr::InitializePatchList()
- {
- // load patches
- Log.Debug("PatchMgr", "Starting up.");
- #ifdef WIN32
- Log.Debug("PatchMgr", "Main function has been called.");
- Log.Notice("PatchMgr", "Loading Patches...");
- char Buffer[MAX_PATH*10];
- char Buffer2[MAX_PATH*10];
- char Buffer3[MAX_PATH*10];
- WIN32_FIND_DATA fd;
- HANDLE fHandle;
- MD5Hash md5;
- Patch * pPatch;
- DWORD size,sizehigh;
- HANDLE hFile;
- uint32 srcversion;
- char locality[5];
- uint32 i;
- if(!GetCurrentDirectory(MAX_PATH*10, Buffer))
- return;
- strcpy(Buffer2,Buffer);
- strcat(Buffer, "\\ClientPatches\\*.*");
- fHandle = FindFirstFile(Buffer, &fd);
- if(fHandle == INVALID_HANDLE_VALUE)
- return;
- do
- {
- snprintf(Buffer3,MAX_PATH*10,"%s\\ClientPatches\\%s",Buffer2,fd.cFileName);
- if(sscanf(fd.cFileName,"%4s%u.", locality, &srcversion) != 2)
- {
- Log.Notice("Found Incorrect : %s %s", locality, fd.cFileName);
- continue;
- }
- else
- {
- Log.Notice("Found Correct : %s %s", locality, fd.cFileName);
- }
- hFile = CreateFile(Buffer3, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);
- if(hFile == INVALID_HANDLE_VALUE)
- continue;
- Log.Notice("PatchMgr", "Found patch for %u locale `%s`.", srcversion,locality);
- pPatch = new Patch;
- size = GetFileSize(hFile, &sizehigh);
- pPatch->FileSize = size;
- pPatch->Data = new uint8[size];
- pPatch->Version = srcversion;
- for(i = 0; i < 4; ++i)
- pPatch->Locality[i] = static_cast<char>(tolower(locality[i]));
- pPatch->Locality[4] = 0;
- pPatch->uLocality = *(uint32*)pPatch->Locality;
- if(pPatch->Data== NULL)
- {
- // shouldn't really happen
- delete pPatch;
- CloseHandle(hFile);
- continue;
- }
- // read the whole file
- ASSERT(ReadFile(hFile, pPatch->Data, pPatch->FileSize, &size, NULL));
- ASSERT(size == pPatch->FileSize);
- // close the handle, no longer needed
- CloseHandle(hFile);
- // md5hash the file
- md5.Initialize();
- md5.UpdateData(pPatch->Data, pPatch->FileSize);
- md5.Finalize();
- memcpy(pPatch->MD5, md5.GetDigest(), MD5_DIGEST_LENGTH);
- // add the patch to the patchlist
- m_patches.push_back(pPatch);
- } while(FindNextFile(fHandle,&fd));
- FindClose(fHandle);
- #else
- /*
- *nix patch loader
- */
- Log.Notice("PatchMgr", "Loading Patches...");
- char Buffer[MAX_PATH*10];
- char Buffer2[MAX_PATH*10];
- char Buffer3[MAX_PATH*10];
- struct dirent ** list;
- int filecount;
- int read_fd;
- MD5Hash md5;
- Patch * pPatch;
- int size;
- uint32 srcversion;
- char locality[5];
- uint32 i;
- struct stat sb;
- strcpy(Buffer, "./ClientPatches");
- strcpy(Buffer2,Buffer);
- filecount = scandir("./ClientPatches", &list, 0, 0);
- if(filecount <= 0 || list== NULL)
- {
- Log.Error("PatchMgr", "No patches found.");
- return;
- }
- while(filecount--)
- {
- snprintf(Buffer3,MAX_PATH*10,"./ClientPatches/%s",list[filecount]->d_name);
- if(sscanf(list[filecount]->d_name,"%4s%u.", locality, &srcversion) != 2)
- continue;
- read_fd = open(Buffer3, O_RDONLY);
- if(read_fd <= 0)
- {
- LOG_ERROR("Cannot open %s", Buffer3);
- continue;
- }
- if(fstat(read_fd, &sb) < 0)
- {
- LOG_ERROR("Cannot stat %s", Buffer3);
- continue;
- }
- Log.Notice("PatchMgr", "Found patch for b%u locale `%s` (%u bytes).", srcversion,locality, sb.st_size);
- pPatch = new Patch;
- size = sb.st_size;
- pPatch->FileSize = size;
- pPatch->Data = new uint8[size];
- pPatch->Version = srcversion;
- for(i = 0; i < 4; ++i)
- pPatch->Locality[i] = tolower(locality[i]);
- pPatch->Locality[4] = 0;
- pPatch->uLocality = *(uint32*)pPatch->Locality;
- if(pPatch->Data== NULL)
- {
- // shouldn't really happen
- delete pPatch;
- close(read_fd);
- continue;
- }
- // read the whole file
- ASSERT(read(read_fd, pPatch->Data, size) == size);
- // close handle
- close(read_fd);
- // md5hash the file
- md5.Initialize();
- md5.UpdateData(pPatch->Data, pPatch->FileSize);
- md5.Finalize();
- memcpy(pPatch->MD5, md5.GetDigest(), MD5_DIGEST_LENGTH);
- // add the patch to the patchlist
- m_patches.push_back(pPatch);
- free(list[filecount]);
- }
- free(list);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment