#include "main.h" #include "../src/GwCrc32.h" #include #include "xentax.h" GwArchive gGwArchive; bool gIgnoreFileWarning=false; unsigned char* CheckSpecial(int type, bool compressed, unsigned char* pFile_data, unsigned char* pDecmpBuf, unsigned long& rCompressedSize, unsigned long& rDecompressedSize) { //Check types for special needs if (type == ATEX || type == ATTX) { unsigned long ds=0; memcpy(&ds, pDecmpBuf+12, 4); ds+=12; rDecompressedSize=ds; rCompressedSize=ds; return pDecmpBuf; } else if (type == FFNA && compressed) { if (gFsConfig.disableArchiveFFNA) { return NULL; } else { unsigned long ds=0; memcpy(&ds, pDecmpBuf+9, 4); ds=5; rCompressedSize=ds; rDecompressedSize=ds; return pFile_data; } } return pFile_data; } //Loads file data from disk or if pFileName==NULL tries to load it from the gw.dat file unsigned char* Load(char* pFileName, unsigned long hash, unsigned long& rCompressedSize, unsigned long& rDecompressedSize) { //If the data we are loading has a filename associated with it. if (pFileName[0] > 0) { //Load the file into memory so we can calculate the size and CRC std::ifstream fin(pFileName, std::ios::binary); if (fin.fail()) { return NULL; } //Calculate the size of the file int begin=fin.tellg(); fin.seekg(0, ios::end); int end=fin.tellg(); unsigned long size=end-begin; fin.seekg(0, ios::beg); //Load the file data unsigned char* pFile_data=new unsigned char[size]; fin.read((char*)pFile_data, size); fin.close(); rCompressedSize=size; rDecompressedSize=0; return pFile_data; } //Load the data from the Guild Wars data file specified else { MftEntry mft; unsigned char* pFile_data=gGwArchive.GetFile(hash, &mft); if (pFile_data == NULL){return NULL;} bool not_processed=true; if(mft.IsCompressed() && (mft.type == ATEX || mft.type == ATTX || mft.type == FFNA)) { //check for exceptions to the special rule if(false && !gFsConfig.disableArchiveFFNA && mft.type == FFNA) { } else { //Decompress the data from the archive unsigned char* pData=NULL; int pData_size=0; UnpackGWDat(pFile_data, mft.compressedSize+8, pData, pData_size); if (pData == NULL){return NULL;} //Check for special data size processing unsigned char* pRet=NULL; if ((pRet=CheckSpecial(mft.type, true, pFile_data, pData, rCompressedSize, rDecompressedSize)) != pFile_data) { delete [] pFile_data; pFile_data=pRet; } else { delete [] pData; } not_processed=false; } } //If we did not process the file stuff in the above if (not_processed) { rCompressedSize=mft.compressedSize; rDecompressedSize=mft.uncompressedSize; unsigned char* pRet=NULL; if ((pRet=CheckSpecial(mft.type, false, pFile_data, pFile_data, rCompressedSize, rDecompressedSize)) != pFile_data) { delete [] pFile_data; pFile_data=pRet; } } return pFile_data; } return NULL; } void FreeFile(unsigned long hash) { hash_map::iterator itr; itr=gFiles.find(hash); if (itr != gFiles.end()) { FsFile* pFile=&itr->second; if (pFile->pFileData != NULL) { delete [] pFile->pFileData; pFile->pFileData=NULL; } } } void RegisterFile(char* pFileName, unsigned long hash, unsigned long decompressedSize) { FsFile file; //Appends the "Files/" directory to before the file name if not set the name to zeroes if (pFileName != NULL) { strcpy(file.fileName, "Files/"); strcat(file.fileName, pFileName); } else { memset(file.fileName, 0, 150); } //Set up a new file file.hash=hash; file.pFileData=NULL; //Load the file's data unsigned char* pFile_data=Load(file.fileName, file.hash, file.compressedSize, file.decompressedSize); if (pFile_data == NULL) { if (!gIgnoreFileWarning && file.hash > 0){gCoreLog.error("Could not load data for file hash: 0x%X", file.hash);} return; } //We loaded data from a file so we must calculate the sizes if (file.decompressedSize == 0) { //The file registration specified a decompressed file size if (decompressedSize > 0) { file.decompressedSize=decompressedSize; file.compressedSize-=8; } //If it did not, assume the decompressed size is the same as the compressed else { file.decompressedSize=file.compressedSize; } file.archiveFile=false; } else { file.archiveFile=true; } if (pFile_data == NULL){return;} //If we are dealing with uncompressed data, calculate the file's CRC if (file.compressedSize == file.decompressedSize) { file.crc32=CalculateGwCrc32(&file.hash, 4, CalculateGwCrc32(pFile_data, file.decompressedSize, 0)); } //If we are dealing with compressed data we need to decompress it before calculating the CRC else { unsigned char* pData=NULL; int out_size=0; UnpackGWDat(pFile_data, file.compressedSize+8, pData, out_size); if (pData == NULL) { return; } file.crc32=CalculateGwCrc32(&file.hash, 4, CalculateGwCrc32(pData, out_size, 0)); delete [] pData; } delete [] pFile_data; char message[250]; char compression[50]; if (file.compressedSize == file.decompressedSize) { sprintf(compression, "UNCMP"); } else { sprintf(compression, "CMP"); } sprintf(message, "Registered File 0x%X with CRC 0x%X (%s)", file.hash, file.crc32, compression); if (!gFsConfig.disableFileLog){gCoreLog.notice(message);} //Add the file to the hash map typedef pair fpair; gFiles.insert(fpair(hash,file)); } void RegisterFile(char* pFileName, unsigned long hash) { RegisterFile(pFileName, hash, 0); } FsFile* LoadFile(unsigned long hash) { bool checked_archive=false; bool checked_anet=false; functionstart: //Make sure we have a file registered with this hash hash_map::iterator itr; itr=gFiles.find(hash); if (itr != gFiles.end()) { //File isn't loaded if (itr->second.pFileData == NULL) { FsFile* pFile=&itr->second; unsigned long t, tt=0; pFile->pFileData=Load(pFile->fileName, hash, t, tt); if (pFile->pFileData == NULL){return NULL;} return &itr->second; } //File is loaded else { return &itr->second; } } //If we are using an archive back end check for the file in that else if (!checked_archive && gFsConfig.useArchive) { gIgnoreFileWarning=true; RegisterFile(NULL, hash); checked_archive=true; gIgnoreFileWarning=false; goto functionstart; } else if (!checked_anet && gFsConfig.useArenaNet) { GetFileFromAnet(hash); checked_anet=true; goto functionstart; } return NULL; } int RegisterFiles() { gIgnoreFileWarning=false; ifstream fin("FileList.ini"); if (fin.fail()) { return -1; } while (!fin.eof()) { char hash_string[20]; unsigned long hash=0; char filename[150]; unsigned long decomp_size=0; fin >> hash_string; fin >> filename; fin >> decomp_size; hash=Util::HexToDecimal(hash_string); RegisterFile(filename, hash, decomp_size); } fin.close(); return 0; }