//----------------------------------------------------------------------------- // Purpose: Writes out a new .dem file based on the existing dem file with new camera positions saved into the dem file // Note: The new file is named filename_smooth.dem // Input : *filename - // smoothing - //----------------------------------------------------------------------------- void SaveSmoothingInfo( char const *filename, CSmoothingContext& smoothing ) { // Nothing to do int c = smoothing.smooth.Count(); if ( !c ) return; IBaseFileSystem *fs = g_pFileSystem; FileHandle_t infile, outfile; infile = fs->Open( filename, "rb", "GAME" ); if ( infile == FILESYSTEM_INVALID_HANDLE ) return; int filesize = fs->Size( infile ); char outfilename[ 512 ]; Q_StripExtension( filename, outfilename, sizeof( outfilename ) ); Q_strncat( outfilename, "_smooth", sizeof(outfilename), COPY_ALL_CHARACTERS ); Q_DefaultExtension( outfilename, ".dem", sizeof( outfilename ) ); outfile = fs->Open( outfilename, "wb", "GAME" ); if ( outfile == FILESYSTEM_INVALID_HANDLE ) { fs->Close( infile ); return; } int i; // The basic algorithm is to seek to each sample and "overwrite" it during copy with the new data... int lastwritepos = 0; for ( i = 0; i < c; i++ ) { demosmoothing_t *p = &smoothing.smooth[ i ]; int copyamount = p->file_offset - lastwritepos; COM_CopyFileChunk( outfile, infile, copyamount ); fs->Seek( infile, p->file_offset, FILESYSTEM_SEEK_HEAD ); // wacky hacky overwriting fs->Write( &p->info, sizeof( democmdinfo_t ), outfile ); lastwritepos = fs->Tell( outfile ); fs->Seek( infile, p->file_offset + sizeof( democmdinfo_t ), FILESYSTEM_SEEK_HEAD ); } // Copy the final bit of data, if any... int final = filesize - lastwritepos; COM_CopyFileChunk( outfile, infile, final ); fs->Close( outfile ); fs->Close( infile ); }
/* <2a121> ../engine/hashpak.c:1060 */ void HPAK_CreatePak(char *pakname, struct resource_s *pResource, void *pData, FileHandle_t fpSource) { char name[MAX_PATH]; int32 curpos; FileHandle_t fp; hash_pack_entry_t *pCurrentEntry; byte md5[16]; MD5Context_t ctx; byte *pDiskData; if ((!fpSource && !pData) || (fpSource && pData)) { Con_Printf("HPAK_CreatePak, must specify one of pData or fpSource\n"); return; } Q_snprintf(name, ARRAYSIZE(name), "%s", pakname); #ifdef REHLDS_FIXES name[ARRAYSIZE(name) - 1] = 0; #endif // REHLDS_FIXES COM_DefaultExtension(name, HASHPAK_EXTENSION); Con_Printf("Creating HPAK %s.\n", name); fp = FS_Open(name, "wb"); if (!fp) { Con_Printf("ERROR: couldn't open new .hpk, check access rights to %s.\n", name); return; } Q_memset(&ctx, 0, sizeof(MD5Context_t)); MD5Init(&ctx); if (pData) MD5Update(&ctx, (byte *)pData, pResource->nDownloadSize); else { curpos = FS_Tell(fpSource); pDiskData = (byte *)Mem_Malloc(pResource->nDownloadSize + 1); Q_memset(pDiskData, 0, pResource->nDownloadSize); FS_Read(pDiskData, pResource->nDownloadSize, 1, fp); FS_Seek(fpSource, curpos, FILESYSTEM_SEEK_HEAD); MD5Update(&ctx, pDiskData, pResource->nDownloadSize); Mem_Free(pDiskData); } MD5Final(md5, &ctx); if (Q_memcmp(pResource->rgucMD5_hash, md5, sizeof(md5)) != 0) { Con_Printf("HPAK_CreatePak called with bogus lump, md5 mismatch\n"); Con_Printf("Purported: %s\n", MD5_Print(pResource->rgucMD5_hash)); Con_Printf("Actual : %s\n", MD5_Print(md5)); Con_Printf("Ignoring lump addition\n"); return; } Q_memset(&hash_pack_header, 0, sizeof(hash_pack_header_t)); Q_memcpy(hash_pack_header.szFileStamp, "HPAK", sizeof(hash_pack_header.szFileStamp)); hash_pack_header.version = HASHPAK_VERSION; hash_pack_header.nDirectoryOffset = 0; FS_Write(&hash_pack_header, sizeof(hash_pack_header_t), 1, fp); Q_memset(&hash_pack_dir, 0, sizeof(hash_pack_directory_t)); hash_pack_dir.nEntries = 1; hash_pack_dir.p_rgEntries = (hash_pack_entry_t *)Mem_Malloc(sizeof(hash_pack_entry_t)); Q_memset(hash_pack_dir.p_rgEntries, 0, sizeof(hash_pack_entry_t) * hash_pack_dir.nEntries); pCurrentEntry = &hash_pack_dir.p_rgEntries[0]; Q_memcpy(&pCurrentEntry->resource, pResource, sizeof(resource_t)); pCurrentEntry->nOffset = FS_Tell(fp); pCurrentEntry->nFileLength = pResource->nDownloadSize; if (pData) FS_Write(pData, pResource->nDownloadSize, 1, fp); else COM_CopyFileChunk(fp, fpSource, pResource->nDownloadSize); curpos = FS_Tell(fp); FS_Write(&hash_pack_dir.nEntries, 4, 1, fp); FS_Write(hash_pack_dir.p_rgEntries, sizeof(hash_pack_entry_t), 1, fp); if (hash_pack_dir.p_rgEntries) { Mem_Free(hash_pack_dir.p_rgEntries); hash_pack_dir.p_rgEntries = NULL; } hash_pack_dir.nEntries = 0; hash_pack_header.nDirectoryOffset = curpos; FS_Seek(fp, 0, FILESYSTEM_SEEK_HEAD); FS_Write(&hash_pack_header, sizeof(hash_pack_header_t), 1, fp); FS_Close(fp); }
/* <2a974> ../engine/hashpak.c:598 */ void HPAK_RemoveLump(char *pakname, resource_t *pResource) { FileHandle_t fp; FileHandle_t tmp; char szTempName[MAX_PATH]; char szOriginalName[MAX_PATH]; hash_pack_directory_t olddir; hash_pack_directory_t newdir; hash_pack_entry_t *oldentry; hash_pack_entry_t *newentry; int n; int i; if (pakname == NULL || *pakname == '\0' || pResource == NULL) { Con_Printf(__FUNCTION__ ": Invalid arguments\n"); return; } HPAK_FlushHostQueue(); #ifdef REHLDS_FIXES Q_strncpy(szOriginalName, pakname, ARRAYSIZE(szOriginalName) - 1); szOriginalName[ARRAYSIZE(szOriginalName) - 1] = 0; COM_DefaultExtension(szOriginalName, HASHPAK_EXTENSION); #else //TODO: Not sure why Cmd_Argv(1) is used since function receives pakname parameter char name[MAX_PATH]; Q_snprintf(name, ARRAYSIZE(name), "%s", Cmd_Argv(1)); COM_DefaultExtension(name, HASHPAK_EXTENSION); Q_strncpy(szOriginalName, name, ARRAYSIZE(szOriginalName) - 1); szOriginalName[ARRAYSIZE(szOriginalName) - 1] = 0; #endif // REHLDS_FIXES fp = FS_Open(szOriginalName, "rb"); if (!fp) { Con_Printf("Error: couldn't open HPAK file %s for removal.\n", szOriginalName); return; } COM_StripExtension(szOriginalName, szTempName); COM_DefaultExtension(szTempName, ".hp2"); tmp = FS_Open(szTempName, "w+b"); if (!tmp) { FS_Close(fp); Con_Printf("ERROR: couldn't create %s.\n", szTempName); return; } FS_Seek(fp, 0, FILESYSTEM_SEEK_HEAD); FS_Seek(tmp, 0, FILESYSTEM_SEEK_HEAD); FS_Read(&hash_pack_header, sizeof(hash_pack_header_t), 1, fp); FS_Write(&hash_pack_header, sizeof(hash_pack_header_t), 1, tmp); if (Q_strncmp(hash_pack_header.szFileStamp, "HPAK", sizeof(hash_pack_header.szFileStamp))) { FS_Close(fp); FS_Close(tmp); FS_Unlink(szTempName); Con_Printf("%s is not an HPAK file\n", szOriginalName); return; } if (hash_pack_header.version != HASHPAK_VERSION) { FS_Close(fp); FS_Close(tmp); FS_Unlink(szTempName); Con_Printf("ERROR: HPAK version outdated\n"); return; } FS_Seek(fp, hash_pack_header.nDirectoryOffset, FILESYSTEM_SEEK_HEAD); FS_Read(&olddir.nEntries, 4, 1, fp); if (olddir.nEntries < 1 || (unsigned int)olddir.nEntries > MAX_FILE_ENTRIES) { FS_Close(fp); FS_Close(tmp); FS_Unlink(szTempName); Con_Printf("ERROR: HPAK had bogus # of directory entries: %i\n", olddir.nEntries); return; } if (olddir.nEntries == 1) { FS_Close(fp); FS_Close(tmp); FS_Unlink(szOriginalName); FS_Unlink(szTempName); Con_Printf("Removing final lump from HPAK, deleting HPAK:\n %s\n", szOriginalName); return; } olddir.p_rgEntries = (hash_pack_entry_t *)Mem_Malloc(sizeof(hash_pack_entry_t) * olddir.nEntries); FS_Read(olddir.p_rgEntries, sizeof(hash_pack_entry_t) * olddir.nEntries, 1, fp); newdir.nEntries = olddir.nEntries - 1; newdir.p_rgEntries = (hash_pack_entry_t *)Mem_Malloc(sizeof(hash_pack_entry_t) * newdir.nEntries); if (!HPAK_FindResource(&olddir, pResource->rgucMD5_hash, NULL)) { FS_Close(fp); FS_Close(tmp); FS_Unlink(szTempName); Mem_Free(olddir.p_rgEntries); Mem_Free(newdir.p_rgEntries); Con_Printf("ERROR: HPAK doesn't contain specified lump: %s\n", pResource->szFileName); return; } Con_Printf("Removing %s from HPAK %s.\n", pResource->szFileName, szOriginalName); for (i = 0, n = 0; i < olddir.nEntries; i++) { oldentry = &olddir.p_rgEntries[i]; if (Q_memcmp(olddir.p_rgEntries[i].resource.rgucMD5_hash, pResource->rgucMD5_hash, 16)) { newentry = &newdir.p_rgEntries[n++]; Q_memcpy(newentry, oldentry, sizeof(hash_pack_entry_t)); newentry->nOffset = FS_Tell(tmp); FS_Seek(fp, oldentry->nOffset, FILESYSTEM_SEEK_HEAD); COM_CopyFileChunk(tmp, fp, newentry->nFileLength); } } hash_pack_header.nDirectoryOffset = FS_Tell(tmp); FS_Write(&newdir.nEntries, 4, 1, tmp); for (i = 0; i < newdir.nEntries; i++) FS_Write(&newdir.p_rgEntries[i], sizeof(hash_pack_entry_t), 1, tmp); FS_Seek(tmp, 0, FILESYSTEM_SEEK_HEAD); FS_Write(&hash_pack_header, sizeof(hash_pack_header_t), 1, tmp); FS_Close(fp); FS_Close(tmp); FS_Unlink(szOriginalName); FS_Rename(szTempName, szOriginalName); Mem_Free(olddir.p_rgEntries); Mem_Free(newdir.p_rgEntries); }
/* <2a494> ../engine/hashpak.c:322 */ void HPAK_AddLump(qboolean bUseQueue, char *pakname, struct resource_s *pResource, void *pData, FileHandle_t fpSource) { FileHandle_t iRead; FileHandle_t iWrite; char name[MAX_PATH]; char szTempName[MAX_PATH]; char szOriginalName[MAX_PATH]; hash_pack_directory_t olddirectory; hash_pack_directory_t newdirectory; hash_pack_entry_t *pNewEntry; byte md5[16]; MD5Context_t ctx; byte *pDiskData; if (pakname == NULL) { Con_Printf("HPAK_AddLump called with invalid arguments: no .pak filename\n"); return; } if (!pResource) { Con_Printf("HPAK_AddLump called with invalid arguments: no lump to add\n"); return; } if (!pData && !fpSource) { Con_Printf("HPAK_AddLump called with invalid arguments: no file handle\n"); return; } if (pResource->nDownloadSize < 1024 || (unsigned int)pResource->nDownloadSize > MAX_FILE_SIZE) { Con_Printf("HPAK_AddLump called with bogus lump, size: %i\n", pResource->nDownloadSize); return; } Q_memset(&ctx, 0, sizeof(MD5Context_t)); MD5Init(&ctx); if (pData) MD5Update(&ctx, (byte *)pData, pResource->nDownloadSize); else { pDiskData = (byte *)Mem_Malloc(pResource->nDownloadSize + 1); Q_memset(pDiskData, 0, pResource->nDownloadSize); FS_Read(pDiskData, pResource->nDownloadSize, 1, fpSource); FS_Seek(fpSource, FS_Tell(fpSource), FILESYSTEM_SEEK_HEAD); MD5Update(&ctx, pDiskData, pResource->nDownloadSize); Mem_Free(pDiskData); } MD5Final(md5, &ctx); if (Q_memcmp(pResource->rgucMD5_hash, md5, sizeof(md5)) != 0) { Con_Printf("HPAK_AddLump called with bogus lump, md5 mismatch\n"); Con_Printf("Purported: %s\n", MD5_Print(pResource->rgucMD5_hash)); Con_Printf("Actual : %s\n", MD5_Print(md5)); Con_Printf("Ignoring lump addition\n"); return; } if (bUseQueue) { HPAK_AddToQueue(pakname, pResource, pData, fpSource); return; } Q_snprintf(name, ARRAYSIZE(name), "%s", pakname); #ifdef REHLDS_FIXES name[ARRAYSIZE(name) - 1] = 0; #endif // REHLDS_FIXES COM_DefaultExtension(name, HASHPAK_EXTENSION); COM_FixSlashes(name); Q_strncpy(szOriginalName, name, ARRAYSIZE(szOriginalName) - 1); szOriginalName[ARRAYSIZE(szOriginalName) - 1] = 0; iRead = FS_Open(name, "rb"); if (!iRead) { HPAK_CreatePak(pakname, pResource, pData, fpSource); return; } COM_StripExtension(name, szTempName); COM_DefaultExtension(szTempName, ".hp2"); iWrite = FS_Open(szTempName, "w+b"); if (!iWrite) { FS_Close(iRead); Con_Printf("ERROR: couldn't open %s.\n", szTempName); return; } FS_Read(&hash_pack_header, sizeof(hash_pack_header_t), 1, iRead); if (hash_pack_header.version != HASHPAK_VERSION) { FS_Close(iRead); FS_Close(iWrite); FS_Unlink(szTempName); Con_Printf("Invalid .hpk version in HPAK_AddLump\n"); return; } FS_Seek(iRead, 0, FILESYSTEM_SEEK_HEAD); COM_CopyFileChunk(iWrite, iRead, FS_Size(iRead)); FS_Seek(iRead, hash_pack_header.nDirectoryOffset, FILESYSTEM_SEEK_HEAD); FS_Read(&olddirectory.nEntries, 4, 1, iRead); if (olddirectory.nEntries < 1 || (unsigned int)olddirectory.nEntries > MAX_FILE_ENTRIES) { FS_Close(iRead); FS_Close(iWrite); FS_Unlink(szTempName); Con_Printf("ERROR: .hpk had bogus # of directory entries: %i\n", olddirectory.nEntries); return; } olddirectory.p_rgEntries = (hash_pack_entry_t *)Mem_Malloc(sizeof(hash_pack_entry_t) * olddirectory.nEntries); FS_Read(olddirectory.p_rgEntries, sizeof(hash_pack_entry_t) * olddirectory.nEntries, 1, iRead); FS_Close(iRead); if (HPAK_FindResource(&olddirectory, pResource->rgucMD5_hash, NULL) != FALSE) { FS_Close(iWrite); FS_Unlink(szTempName); Mem_Free(olddirectory.p_rgEntries); return; } newdirectory.nEntries = olddirectory.nEntries + 1; newdirectory.p_rgEntries = (hash_pack_entry_t *)Mem_Malloc(sizeof(hash_pack_entry_t) * newdirectory.nEntries); Q_memset(newdirectory.p_rgEntries, 0, sizeof(hash_pack_entry_t) * newdirectory.nEntries); Q_memcpy(newdirectory.p_rgEntries, olddirectory.p_rgEntries, sizeof(hash_pack_entry_t) * olddirectory.nEntries); pNewEntry = NULL; for (int i = 0; i < olddirectory.nEntries; i++) { if (Q_memcmp(pResource->rgucMD5_hash, olddirectory.p_rgEntries[i].resource.rgucMD5_hash, 16) >= 0) { pNewEntry = &newdirectory.p_rgEntries[i]; #ifndef REHLDS_FIXES while (i < olddirectory.nEntries) { Q_memcpy(&newdirectory.p_rgEntries[i + 1], &olddirectory.p_rgEntries[i], sizeof(hash_pack_entry_t)); i++; } #else Q_memcpy(&newdirectory.p_rgEntries[i + 1], &olddirectory.p_rgEntries[i], (olddirectory.nEntries - i) * sizeof(hash_pack_entry_t)); #endif break; } } if (pNewEntry == NULL) { pNewEntry = &newdirectory.p_rgEntries[newdirectory.nEntries - 1]; } Q_memset(pNewEntry, 0, sizeof(hash_pack_entry_t)); FS_Seek(iWrite, hash_pack_header.nDirectoryOffset, FILESYSTEM_SEEK_HEAD); Q_memcpy(&pNewEntry->resource, pResource, sizeof(resource_t)); pNewEntry->nOffset = FS_Tell(iWrite); pNewEntry->nFileLength = pResource->nDownloadSize; if (pData) FS_Write(pData, pResource->nDownloadSize, 1, iWrite); else COM_CopyFileChunk(iWrite, fpSource, pResource->nDownloadSize); hash_pack_header.nDirectoryOffset = FS_Tell(iWrite); FS_Write(&newdirectory.nEntries, 4, 1, iWrite); for (int j = 0; j < newdirectory.nEntries; j++) FS_Write(&newdirectory.p_rgEntries[j], sizeof(hash_pack_entry_t), 1, iWrite); if (newdirectory.p_rgEntries) Mem_Free(newdirectory.p_rgEntries); if (olddirectory.p_rgEntries) Mem_Free(olddirectory.p_rgEntries); FS_Seek(iWrite, 0, FILESYSTEM_SEEK_HEAD); FS_Write(&hash_pack_header, sizeof(hash_pack_header_t), 1, iWrite); FS_Close(iWrite); FS_Unlink(szOriginalName); FS_Rename(szTempName, szOriginalName); }