static int SFileAddInternalListFile( TMPQArchive * ha, HANDLE hMpq) { TMPQArchive * haMpq = (TMPQArchive *)hMpq; TMPQHash * pFirstHash; TMPQHash * pHash; HANDLE hListFile; LCID lcSaveLocale = lcFileLocale; int nError = ERROR_SUCCESS; // If there is hash table, we need to support multiple listfiles // with different locales (BrooDat.mpq) if(haMpq->pHashTable != NULL) { pFirstHash = pHash = GetFirstHashEntry(haMpq, LISTFILE_NAME); while(nError == ERROR_SUCCESS && pHash != NULL) { // Set the prefered locale to that from list file SFileSetLocale(pHash->lcLocale); if(SFileOpenFileEx(hMpq, LISTFILE_NAME, 0, &hListFile)) { // Add the data from the listfile to MPQ nError = SFileAddArbitraryListFile(ha, hListFile); SFileCloseFile(hListFile); } // Restore the original locale SFileSetLocale(lcSaveLocale); // Move to the next hash pHash = GetNextHashEntry(haMpq, pFirstHash, pHash); } } else { // Open the external list file if(SFileOpenFileEx(hMpq, LISTFILE_NAME, 0, &hListFile)) { // Add the data from the listfile to MPQ // The function also closes the listfile handle nError = SFileAddArbitraryListFile(ha, hListFile); SFileCloseFile(hListFile); } } // Return the result of the operation return nError; }
LCID WINAPI SFileSetLocale_stub(LCID nNewLocale) { LoadSFMpqDll(); if (hSFMpq) { *(FARPROC *)&SFileSetLocale = GetProcAddress(hSFMpq,"SFileSetLocale"); if (SFileSetLocale) return SFileSetLocale(nNewLocale); } return 0; }
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------- int ExtractWmo(const std::vector<std::string>& pArchiveNames) { char* szListFile = ""; char szLocalFile[MAX_PATH] = ""; HANDLE hMpq = ""; BOOL bResult = FALSE; //const char* ParsArchiveNames[] = {"patch-2.MPQ", "patch.MPQ", "common.MPQ", "expansion.MPQ"}; int nError = ERROR_SUCCESS; if(szListFile == NULL || *szListFile == 0) szListFile = NULL; //char tmp[1024]; //for (size_t i=0; i<4; i++) for (size_t i=0; i<pArchiveNames.size(); i++) { //sprintf(tmp,"%s\\%s", input_path, ParsArchiveNames[i]); //if(!SFileOpenArchive(tmp, 0, 0, &hMpq)) if(!SFileOpenArchive(pArchiveNames[i].c_str(), 0, 0, &hMpq)) printf("NOT open!!! %s\n",pArchiveNames[i].c_str()); // Copy files from archive if(nError == ERROR_SUCCESS) { SFILE_FIND_DATA wf; HANDLE hFind = SFileFindFirstFile(hMpq,"*.wmo*", &wf, szListFile); bResult = TRUE; while(hFind != NULL && bResult == TRUE) { ShowProcessedFile(wf.cFileName); SFileSetLocale(wf.lcLocale); sprintf(szLocalFile, "%s\\%s", szWorkDirWmo, GetPlainName(wf.cFileName)); fixnamen(szLocalFile,strlen(szLocalFile)); FILE * n; if ((n = fopen(szLocalFile, "rb"))== NULL) { int p = 0; //Select root wmo files const char * rchr = strrchr(GetPlainName(wf.cFileName),0x5f); if(rchr != NULL) { char cpy[4]; strncpy((char*)cpy,rchr,4); for (int i=0;i<4;i++) { int m = cpy[i]; if(isdigit(m)) p++; } } if(p != 3) { //printf("RootWmo!\n"); string s = wf.cFileName; WMORoot * froot = new WMORoot(s); if(!froot->open()) { printf("Not open RootWmo!!!\n"); bResult = SFileFindNextFile(hFind, &wf); continue; } FILE *output=fopen(szLocalFile,"wb"); froot->ConvertToVMAPRootWmo(output); int Wmo_nVertices = 0; if(froot->nGroups !=0) { for (int i=0; i<froot->nGroups; i++) { char temp[512]; strcpy(temp, wf.cFileName); temp[strlen(wf.cFileName)-4] = 0; char groupFileName[512]; sprintf(groupFileName,"%s_%03d.wmo",temp, i); printf("%s\n",groupFileName); //printf("GroupWmo!\n"); string s = groupFileName; WMOGroup * fgroup = new WMOGroup(s); if(!fgroup->open()) { printf("Not all open Group file for: %s\n",GetPlainName(wf.cFileName)); bResult = SFileFindNextFile(hFind, &wf); break; } Wmo_nVertices += fgroup->ConvertToVMAPGroupWmo(output, preciseVectorData); } } fseek(output, 8, SEEK_SET); // store the correct no of vertices fwrite(&Wmo_nVertices,sizeof(int),1,output); fclose(output); } } else { fclose(n); } wf.dwFileFlags &= ~MPQ_FILE_HAS_EXTRA; wf.dwFileFlags &= ~MPQ_FILE_EXISTS; // Find the next file bResult = SFileFindNextFile(hFind, &wf); } // Delete the extracted file in the case of an error if(nError != ERROR_SUCCESS) DeleteFile(szLocalFile); // Close the search handle if(hFind != NULL) SFileFindClose(hFind); } } // Close both archives if(hMpq != NULL) //SFileCloseArchive(hMpq); if(nError == ERROR_SUCCESS) printf("\nExtract wmo complete (No errors)\n"); return nError; }
int main(int argc, char** argv) try { po::options_description desc("Required options"); desc.add_options() ("help,h", "produce help message") ("force,f", "overwrite existing files") ("mpq", po::value<std::string>(), "the mpq to create") ("files", po::value<std::vector<std::string> >(), "input files"); po::positional_options_description p; p.add("mpq", 1); p.add("files", -1); po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); po::notify(vm); if (!vm.count("files") || !vm.count("mpq") || vm.count("help")) { std::cout << "usage: <mpq> [<files> ...]" << std::endl << std::endl << desc << std::endl; return 1; } std::vector<std::string> files(vm["files"].as< std::vector<std::string> >()); std::vector<FileEntry> toAdd; fs::path mpqPath(vm["mpq"].as<std::string>()); if(fs::exists(mpqPath)) { if(vm.count("force")) fs::remove(mpqPath); else throw std::runtime_error("mpq does already exist"); } for(std::vector<std::string>::iterator path = files.begin(); path != files.end(); ++path) { if(fs::is_regular_file(*path)) toAdd.push_back(FileEntry(*path, *path)); if(!fs::is_directory(*path)) //no symlinks etc continue; for(fs::recursive_directory_iterator file(*path), end; file != end; ++file) if(fs::is_regular_file(file->path())) toAdd.push_back(FileEntry(file->path(), makeRelative(*path, file->path()))); } for(std::vector<FileEntry>::iterator it = toAdd.begin(); it != toAdd.end(); ++it) std::cout << it->realPath << " >> " << it->mpqPath << std::endl; HANDLE mpq; if(!SFileCreateArchive(mpqPath.string().c_str(), MPQ_CREATE_ARCHIVE_V2, toAdd.size(), &mpq)) throw std::runtime_error("couldn't create mpq"); SFileSetLocale(0); size_t counter(0); for(std::vector<FileEntry>::iterator it = toAdd.begin(); it != toAdd.end(); ++it) { loadbar(++counter, toAdd.size()); if(!SFileAddFileEx(mpq, it->realPath.string().c_str(), it->mpqPath.string().c_str(), MPQ_FILE_COMPRESS, MPQ_COMPRESSION_BZIP2, MPQ_COMPRESSION_BZIP2)) std::cout << "couldn't add file " << it->realPath << std::endl; } std::cout << std::endl; SFileCompactArchive(mpq, NULL, false); SFileFlushArchive(mpq); SFileCloseArchive(mpq); return 0; } catch (const std::exception& e) { std::cerr << "error: " << e.what() << "\n"; }
int SAttrFileSaveToMpq(TMPQArchive * ha) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD dwToWrite; DWORD dwWritten; LCID lcSave = lcLocale; char szAttrFile[MAX_PATH]; int nError = ERROR_SUCCESS; // If there are no attributes, do nothing if(ha->pAttributes == NULL) return ERROR_SUCCESS; // Create the local attributes file if(nError == ERROR_SUCCESS) { GetAttributesFileName(ha, szAttrFile); hFile = CreateFile(szAttrFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if(hFile == INVALID_HANDLE_VALUE) nError = GetLastError(); } // Write the content of the attributes to the file if(nError == ERROR_SUCCESS) { // Write the header of the attributes file dwToWrite = sizeof(DWORD) + sizeof(DWORD); WriteFile(hFile, ha->pAttributes, dwToWrite, &dwWritten, NULL); if(dwWritten != dwToWrite) nError = ERROR_DISK_FULL; } // Write the array of CRC32 if(nError == ERROR_SUCCESS && ha->pAttributes->pCrc32 != NULL) { dwToWrite = sizeof(TMPQCRC32) * ha->pHeader->dwBlockTableSize; WriteFile(hFile, ha->pAttributes->pCrc32, dwToWrite, &dwWritten, NULL); if(dwWritten != dwToWrite) nError = ERROR_DISK_FULL; } // Write the array of FILETIMEs if(nError == ERROR_SUCCESS && ha->pAttributes->pFileTime != NULL) { dwToWrite = sizeof(TMPQFileTime) * ha->pHeader->dwBlockTableSize; WriteFile(hFile, ha->pAttributes->pFileTime, dwToWrite, &dwWritten, NULL); if(dwWritten != dwToWrite) nError = ERROR_DISK_FULL; } // Write the array of MD5s if(nError == ERROR_SUCCESS && ha->pAttributes->pMd5 != NULL) { dwToWrite = sizeof(TMPQMD5) * ha->pHeader->dwBlockTableSize; WriteFile(hFile, ha->pAttributes->pMd5, dwToWrite, &dwWritten, NULL); if(dwWritten != dwToWrite) nError = ERROR_DISK_FULL; } // Add the attributes into MPQ if(nError == ERROR_SUCCESS) { SFileSetLocale(LANG_NEUTRAL); nError = AddFileToArchive(ha, hFile, ATTRIBUTES_NAME, MPQ_FILE_COMPRESS | MPQ_FILE_REPLACEEXISTING, 0, SFILE_TYPE_DATA, NULL); lcLocale = lcSave; } // Close the temporary file and delete it. // There is no FILE_FLAG_DELETE_ON_CLOSE on LINUX. if(hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); DeleteFile(szAttrFile); return nError; }