You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
==========================
MAJOR FEATURES + CHANGES
==========================
Change 2877442 on 2016/02/23 by Graeme.Thornton@GThornton_DevCore
Allow 'dumpnonframe' stat command to also take an optional group name which will filter the output
Change 2879289 on 2016/02/24 by Graeme.Thornton@GThornton_DevCore
Removed "-" in switch string for load time tracker. Makes -NoLoadTrackClear cmd line param actually work
Change 2879323 on 2016/02/24 by Graeme.Thornton@GThornton_DevCore
PC builds now log engine initialization time with the load time tracker
Change 2879418 on 2016/02/24 by Graeme.Thornton@GThornton_DevCore
Optimization for FSignedArchiveReader
- Compute incoming data hash in parallel on another thread while we serialize and decrypt the signature on the chunk cache worker thread
Change 2879419 on 2016/02/24 by Graeme.Thornton@GThornton_DevCore
Extra stats for PakFile group
- Time reading from pak handles
- Number of open pak handles
Change 2881199 on 2016/02/25 by Graeme.Thornton@GThornton_DevCore
Use 32 bit CRCs to hash chunk contents rather than SHA1. Much faster to compute than SHA1, and the decrypting 4 bytes rather than 20 is also a lot faster
Change 2885687 on 2016/02/29 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Close disregard for GC pool when disabling it to leave it in a good state.
Change 2887358 on 2016/03/01 by Steve.Robb@Dev-Core
FlushCurrentThreadCache calls forwarded on from FMalloc wrappers.
Change 2887398 on 2016/03/01 by Steve.Robb@Dev-Core
Stats for MallocBinned2.
Change 2887682 on 2016/03/01 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Added code to catch game hangs. If game thread is stalled for more than 5 seconds we will catch that, report its callstack and crash.
Change 2889573 on 2016/03/02 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Renamed the new StackWalkAndDump to ThreadStackWalkAndDump to avoid name collisions and build errors.
Change 2889687 on 2016/03/02 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Send thread heartbeats during potentially slow tasks.
Change 2889963 on 2016/03/02 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Added the ability to query the name of a thread given its TLS id.
Change 2890439 on 2016/03/02 by Steve.Robb@Dev-Core
MallocBinned redirection header, to allow easy switching between MallocBinned and MallocBinned2.
Switch back to MallocBinned by default.
Change 2891799 on 2016/03/03 by Steve.Robb@Dev-Core
Shrunken GMallocBinned2MemSizeToIndex array.
Improvement of some shared templated alignment-related functions.
Change 2891812 on 2016/03/03 by Graeme.Thornton@GThornton_DevCore
Refactoring of pak signing to be more compatible with patching
- Store signatures in a block at the end of the pak file
- Optimize decryption of signatures by serializing them all when the pak is opened, and decrypting on the chunk cache worker thread between read requests.
Change 2891813 on 2016/03/03 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream2
Don't compile UnrealPak with ICU
Change 2892386 on 2016/03/03 by Steve.Robb@Dev-Core
GitHub #2070 - Fix incorrect FMallocDebug::Malloc alignment checking
Change 2894189 on 2016/03/04 by Steve.Robb@Dev-Core
Memswap fixes for sizes of 3, 5, 6 and 7, and early out on 0.
Change 2894427 on 2016/03/04 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Don't display message boxes in unattended mode when D3D device is removed.
Change 2896912 on 2016/03/07 by Graeme.Thornton@GThornton_DevCore
Disable reading of anything outside the pak file when in shipping builds
Change 2897053 on 2016/03/07 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
A few improvements to thread heartbeat thread:
- Don't check for heartbeat in debug builds
- Don't spawn the thread heartbeat thread for programs
- Ignore stalls when exiting
Change 2898752 on 2016/03/08 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Fixing race conditions when using malloc verify
Change 2899075 on 2016/03/08 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
UnrealPak will now accept hex values for p and q. Added the command line param to trust p and q.
Change 2899091 on 2016/03/08 by Steve.Robb@Dev-Core
Fix for TQueue::Peek const-correctness.
Change 2902775 on 2016/03/10 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
Adding code to test signature keys.
Change 2905040 on 2016/03/11 by Robert.Manuszewski@Robert.Manuszewski_NCL_Stream1
TBigInt Multiply is now 30x faster
Change 2905218 on 2016/03/11 by Steve.Robb@Dev-Core
New FCString::Strspn and FCString::Strcspn functions.
[CL 2921423 by Robert Manuszewski in Main branch]
995 lines
28 KiB
C++
995 lines
28 KiB
C++
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "PakFilePrivatePCH.h"
|
|
#include "IPlatformFilePak.h"
|
|
#include "SecureHash.h"
|
|
#include "FileManagerGeneric.h"
|
|
#include "ModuleManager.h"
|
|
#include "IPlatformFileModule.h"
|
|
#include "IOBase.h"
|
|
#include "BigInt.h"
|
|
#include "SignedArchiveReader.h"
|
|
#include "PublicKey.inl"
|
|
#include "AES.h"
|
|
#include "GenericPlatformChunkInstall.h"
|
|
|
|
DEFINE_LOG_CATEGORY(LogPakFile);
|
|
|
|
DEFINE_STAT(STAT_PakFile_Read);
|
|
DEFINE_STAT(STAT_PakFile_NumOpenHandles);
|
|
|
|
// Enable to stop the loading of any loose files outside of the pak file
|
|
static const bool GEnablePakFileSecurity = false;
|
|
|
|
/**
|
|
* Class to handle correctly reading from a compressed file within a compressed package
|
|
*/
|
|
class FPakSimpleEncryption
|
|
{
|
|
public:
|
|
enum
|
|
{
|
|
Alignment = FAES::AESBlockSize,
|
|
};
|
|
|
|
static FORCEINLINE int64 AlignReadRequest(int64 Size)
|
|
{
|
|
return Align(Size, Alignment);
|
|
}
|
|
|
|
static FORCEINLINE void DecryptBlock(void* Data, int64 Size)
|
|
{
|
|
#ifdef AES_KEY
|
|
FAES::DecryptData((uint8*)Data, Size);
|
|
#endif
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Thread local class to manage working buffers for file compression
|
|
*/
|
|
class FCompressionScratchBuffers : public TThreadSingleton<FCompressionScratchBuffers>
|
|
{
|
|
public:
|
|
FCompressionScratchBuffers()
|
|
: TempBufferSize(0)
|
|
, ScratchBufferSize(0)
|
|
{}
|
|
|
|
int64 TempBufferSize;
|
|
TAutoPtr<uint8> TempBuffer;
|
|
int64 ScratchBufferSize;
|
|
TAutoPtr<uint8> ScratchBuffer;
|
|
|
|
void EnsureBufferSpace(int64 CompressionBlockSize, int64 ScrachSize)
|
|
{
|
|
if(TempBufferSize < CompressionBlockSize)
|
|
{
|
|
TempBufferSize = CompressionBlockSize;
|
|
TempBuffer.Reset((uint8*)FMemory::Malloc(TempBufferSize));
|
|
}
|
|
if(ScratchBufferSize < ScrachSize)
|
|
{
|
|
ScratchBufferSize = ScrachSize;
|
|
ScratchBuffer.Reset((uint8*)FMemory::Malloc(ScratchBufferSize));
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Class to handle correctly reading from a compressed file within a pak
|
|
*/
|
|
template< typename EncryptionPolicy = FPakNoEncryption >
|
|
class FPakCompressedReaderPolicy
|
|
{
|
|
public:
|
|
class FPakUncompressTask : public FNonAbandonableTask
|
|
{
|
|
public:
|
|
uint8* UncompressedBuffer;
|
|
int32 UncompressedSize;
|
|
uint8* CompressedBuffer;
|
|
int32 CompressedSize;
|
|
ECompressionFlags Flags;
|
|
void* CopyOut;
|
|
int64 CopyOffset;
|
|
int64 CopyLength;
|
|
|
|
void DoWork()
|
|
{
|
|
// Decrypt and Uncompress from memory to memory.
|
|
int64 EncryptionSize = EncryptionPolicy::AlignReadRequest(CompressedSize);
|
|
EncryptionPolicy::DecryptBlock(CompressedBuffer, EncryptionSize);
|
|
FCompression::UncompressMemory(Flags, UncompressedBuffer, UncompressedSize, CompressedBuffer, CompressedSize, false);
|
|
if (CopyOut)
|
|
{
|
|
FMemory::Memcpy(CopyOut, UncompressedBuffer+CopyOffset, CopyLength);
|
|
}
|
|
}
|
|
|
|
FORCEINLINE TStatId GetStatId() const
|
|
{
|
|
// TODO: This is called too early in engine startup.
|
|
return TStatId();
|
|
//RETURN_QUICK_DECLARE_CYCLE_STAT(FPakUncompressTask, STATGROUP_ThreadPoolAsyncTasks);
|
|
}
|
|
};
|
|
|
|
FPakCompressedReaderPolicy(const FPakFile& InPakFile, const FPakEntry& InPakEntry, FArchive* InPakReader)
|
|
: PakFile(InPakFile)
|
|
, PakEntry(InPakEntry)
|
|
, PakReader(InPakReader)
|
|
{
|
|
}
|
|
|
|
/** Pak file that own this file data */
|
|
const FPakFile& PakFile;
|
|
/** Pak file entry for this file. */
|
|
const FPakEntry& PakEntry;
|
|
/** Pak file archive to read the data from. */
|
|
FArchive* PakReader;
|
|
|
|
FORCEINLINE int64 FileSize() const
|
|
{
|
|
return PakEntry.UncompressedSize;
|
|
}
|
|
|
|
void Serialize(int64 DesiredPosition, void* V, int64 Length)
|
|
{
|
|
const int32 CompressionBlockSize = PakEntry.CompressionBlockSize;
|
|
uint32 CompressionBlockIndex = DesiredPosition / CompressionBlockSize;
|
|
uint8* WorkingBuffers[2];
|
|
int64 DirectCopyStart = DesiredPosition % PakEntry.CompressionBlockSize;
|
|
FAsyncTask<FPakUncompressTask> UncompressTask;
|
|
FCompressionScratchBuffers& ScratchSpace = FCompressionScratchBuffers::Get();
|
|
bool bStartedUncompress = false;
|
|
|
|
int64 WorkingBufferRequiredSize = FCompression::CompressMemoryBound((ECompressionFlags)PakEntry.CompressionMethod,CompressionBlockSize);
|
|
WorkingBufferRequiredSize = EncryptionPolicy::AlignReadRequest(WorkingBufferRequiredSize);
|
|
ScratchSpace.EnsureBufferSpace(CompressionBlockSize, WorkingBufferRequiredSize*2);
|
|
WorkingBuffers[0] = ScratchSpace.ScratchBuffer;
|
|
WorkingBuffers[1] = ScratchSpace.ScratchBuffer + WorkingBufferRequiredSize;
|
|
|
|
while (Length > 0)
|
|
{
|
|
const FPakCompressedBlock& Block = PakEntry.CompressionBlocks[CompressionBlockIndex];
|
|
int64 Pos = CompressionBlockIndex * CompressionBlockSize;
|
|
int64 CompressedBlockSize = Block.CompressedEnd-Block.CompressedStart;
|
|
int64 UncompressedBlockSize = FMath::Min<int64>(PakEntry.UncompressedSize-Pos, PakEntry.CompressionBlockSize);
|
|
int64 ReadSize = EncryptionPolicy::AlignReadRequest(CompressedBlockSize);
|
|
int64 WriteSize = FMath::Min<int64>(UncompressedBlockSize - DirectCopyStart, Length);
|
|
PakReader->Seek(Block.CompressedStart);
|
|
PakReader->Serialize(WorkingBuffers[CompressionBlockIndex & 1],ReadSize);
|
|
if (bStartedUncompress)
|
|
{
|
|
UncompressTask.EnsureCompletion();
|
|
bStartedUncompress = false;
|
|
}
|
|
|
|
FPakUncompressTask& TaskDetails = UncompressTask.GetTask();
|
|
if (DirectCopyStart == 0 && Length >= CompressionBlockSize)
|
|
{
|
|
// Block can be decompressed directly into output buffer
|
|
TaskDetails.Flags = (ECompressionFlags)PakEntry.CompressionMethod;
|
|
TaskDetails.UncompressedBuffer = (uint8*)V;
|
|
TaskDetails.UncompressedSize = UncompressedBlockSize;
|
|
TaskDetails.CompressedBuffer = WorkingBuffers[CompressionBlockIndex & 1];
|
|
TaskDetails.CompressedSize = CompressedBlockSize;
|
|
TaskDetails.CopyOut = nullptr;
|
|
}
|
|
else
|
|
{
|
|
// Block needs to be copied from a working buffer
|
|
TaskDetails.Flags = (ECompressionFlags)PakEntry.CompressionMethod;
|
|
TaskDetails.UncompressedBuffer = (uint8*)ScratchSpace.TempBuffer;
|
|
TaskDetails.UncompressedSize = UncompressedBlockSize;
|
|
TaskDetails.CompressedBuffer = WorkingBuffers[CompressionBlockIndex & 1];
|
|
TaskDetails.CompressedSize = CompressedBlockSize;
|
|
TaskDetails.CopyOut = V;
|
|
TaskDetails.CopyOffset = DirectCopyStart;
|
|
TaskDetails.CopyLength = WriteSize;
|
|
}
|
|
|
|
if (Length == WriteSize)
|
|
{
|
|
UncompressTask.StartSynchronousTask();
|
|
}
|
|
else
|
|
{
|
|
UncompressTask.StartBackgroundTask();
|
|
}
|
|
bStartedUncompress = true;
|
|
V = (void*)((uint8*)V + WriteSize);
|
|
Length -= WriteSize;
|
|
DirectCopyStart = 0;
|
|
++CompressionBlockIndex;
|
|
}
|
|
|
|
if(bStartedUncompress)
|
|
{
|
|
UncompressTask.EnsureCompletion();
|
|
}
|
|
}
|
|
};
|
|
|
|
bool FPakEntry::VerifyPakEntriesMatch(const FPakEntry& FileEntryA, const FPakEntry& FileEntryB)
|
|
{
|
|
bool bResult = true;
|
|
if (FileEntryA.Size != FileEntryB.Size)
|
|
{
|
|
UE_LOG(LogPakFile, Error, TEXT("Pak header file size mismatch, got: %lld, expected: %lld"), FileEntryB.Size, FileEntryA.Size);
|
|
bResult = false;
|
|
}
|
|
if (FileEntryA.UncompressedSize != FileEntryB.UncompressedSize)
|
|
{
|
|
UE_LOG(LogPakFile, Error, TEXT("Pak header uncompressed file size mismatch, got: %lld, expected: %lld"), FileEntryB.UncompressedSize, FileEntryA.UncompressedSize);
|
|
bResult = false;
|
|
}
|
|
if (FileEntryA.CompressionMethod != FileEntryB.CompressionMethod)
|
|
{
|
|
UE_LOG(LogPakFile, Error, TEXT("Pak header file compression method mismatch, got: %d, expected: %d"), FileEntryB.CompressionMethod, FileEntryA.CompressionMethod);
|
|
bResult = false;
|
|
}
|
|
if (FMemory::Memcmp(FileEntryA.Hash, FileEntryB.Hash, sizeof(FileEntryA.Hash)) != 0)
|
|
{
|
|
UE_LOG(LogPakFile, Error, TEXT("Pak file hash does not match its index entry"));
|
|
bResult = false;
|
|
}
|
|
return bResult;
|
|
}
|
|
|
|
bool FPakPlatformFile::IsFilenameAllowed(const FString& InFilename)
|
|
{
|
|
if (bForceSecurityBypass || !bSecurityEnabled)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
FName FilenameHash(*InFilename);
|
|
|
|
#if PAKFILE_TRACK_SECURITY_EXCLUDED_FILES
|
|
if (SecurityExcludedFiles.Contains(FilenameHash))
|
|
{
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
if (AllowedExtensions.Num() == 0)
|
|
{
|
|
static const FName PakExtension(TEXT("pak"));
|
|
static const FName LogExtension(TEXT("log"));
|
|
AllowedExtensions.Add(PakExtension);
|
|
AllowedExtensions.Add(LogExtension);
|
|
}
|
|
|
|
static const FName IniExtension(TEXT("ini"));
|
|
|
|
FName ExtensionName = FName(*FPaths::GetExtension(InFilename));
|
|
bool bAllowed = false;
|
|
|
|
if (AllowedExtensions.Contains(ExtensionName))
|
|
{
|
|
bAllowed = true;
|
|
}
|
|
else if (ExtensionName == IniExtension)
|
|
{
|
|
// TODO: Filter out ini files that we don't trust
|
|
bAllowed = InFilename.Contains(TEXT("GameUserSettings"), ESearchCase::IgnoreCase);
|
|
}
|
|
|
|
#if PAKFILE_TRACK_SECURITY_EXCLUDED_FILES
|
|
UE_CLOG(!bAllowed, LogPakFile, Log, TEXT("Ignoring request for loose file '%s'"), *InFilename);
|
|
SecurityExcludedFiles.Add(FilenameHash);
|
|
#endif
|
|
|
|
return bAllowed;
|
|
}
|
|
|
|
FPakFile::FPakFile(const TCHAR* Filename, bool bIsSigned)
|
|
: PakFilename(Filename)
|
|
, bSigned(bIsSigned)
|
|
, bIsValid(false)
|
|
{
|
|
FArchive* Reader = GetSharedReader(NULL);
|
|
if (Reader)
|
|
{
|
|
Timestamp = IFileManager::Get().GetTimeStamp(Filename);
|
|
Initialize(Reader);
|
|
}
|
|
}
|
|
|
|
FPakFile::FPakFile(IPlatformFile* LowerLevel, const TCHAR* Filename, bool bIsSigned)
|
|
: PakFilename(Filename)
|
|
, bSigned(bIsSigned)
|
|
, bIsValid(false)
|
|
{
|
|
FArchive* Reader = GetSharedReader(LowerLevel);
|
|
if (Reader)
|
|
{
|
|
Timestamp = LowerLevel->GetTimeStamp(Filename);
|
|
Initialize(Reader);
|
|
}
|
|
}
|
|
|
|
FPakFile::FPakFile(FArchive* Archive)
|
|
: bSigned(false)
|
|
, bIsValid(false)
|
|
{
|
|
Initialize(Archive);
|
|
}
|
|
|
|
FPakFile::~FPakFile()
|
|
{
|
|
}
|
|
|
|
FArchive* FPakFile::CreatePakReader(const TCHAR* Filename)
|
|
{
|
|
FArchive* ReaderArchive = IFileManager::Get().CreateFileReader(Filename);
|
|
return SetupSignedPakReader(ReaderArchive, Filename);
|
|
}
|
|
|
|
FArchive* FPakFile::CreatePakReader(IFileHandle& InHandle, const TCHAR* Filename)
|
|
{
|
|
FArchive* ReaderArchive = new FArchiveFileReaderGeneric(&InHandle, Filename, InHandle.Size());
|
|
return SetupSignedPakReader(ReaderArchive, Filename);
|
|
}
|
|
|
|
FArchive* FPakFile::SetupSignedPakReader(FArchive* ReaderArchive, const TCHAR* Filename)
|
|
{
|
|
if (FPlatformProperties::RequiresCookedData())
|
|
{
|
|
#if !USING_SIGNED_CONTENT
|
|
if (bSigned || FParse::Param(FCommandLine::Get(), TEXT("signedpak")) || FParse::Param(FCommandLine::Get(), TEXT("signed")))
|
|
#endif
|
|
{
|
|
if (!Decryptor.IsValid())
|
|
{
|
|
Decryptor = new FChunkCacheWorker(ReaderArchive, Filename);
|
|
}
|
|
ReaderArchive = new FSignedArchiveReader(ReaderArchive, Decryptor);
|
|
}
|
|
}
|
|
return ReaderArchive;
|
|
}
|
|
|
|
void FPakFile::Initialize(FArchive* Reader)
|
|
{
|
|
if (Reader->TotalSize() < Info.GetSerializedSize())
|
|
{
|
|
UE_LOG(LogPakFile, Fatal, TEXT("Corrupted pak file (too short). Verify your installation."));
|
|
}
|
|
else
|
|
{
|
|
// Serialize trailer and check if everything is as expected.
|
|
Reader->Seek(Reader->TotalSize() - Info.GetSerializedSize());
|
|
Info.Serialize(*Reader);
|
|
UE_CLOG(Info.Magic != FPakInfo::PakFile_Magic, LogPakFile, Fatal, TEXT("Trailing magic number (%ud) is different than the expected one. Verify your installation."), Info.Magic);
|
|
UE_CLOG(!(Info.Version >= FPakInfo::PakFile_Version_Initial && Info.Version <= FPakInfo::PakFile_Version_Latest), LogPakFile, Fatal, TEXT("Invalid pak file version (%d). Verify your installation."), Info.Version);
|
|
|
|
LoadIndex(Reader);
|
|
// LoadIndex should crash in case of an error, so just assume everything is ok if we got here.
|
|
bIsValid = true;
|
|
}
|
|
}
|
|
|
|
void FPakFile::LoadIndex(FArchive* Reader)
|
|
{
|
|
if (Reader->TotalSize() < (Info.IndexOffset + Info.IndexSize))
|
|
{
|
|
UE_LOG(LogPakFile, Fatal, TEXT("Corrupted index offset in pak file."));
|
|
}
|
|
else
|
|
{
|
|
// Load index into memory first.
|
|
Reader->Seek(Info.IndexOffset);
|
|
TArray<uint8> IndexData;
|
|
IndexData.AddUninitialized(Info.IndexSize);
|
|
Reader->Serialize(IndexData.GetData(), Info.IndexSize);
|
|
FMemoryReader IndexReader(IndexData);
|
|
|
|
// Check SHA1 value.
|
|
uint8 IndexHash[20];
|
|
FSHA1::HashBuffer(IndexData.GetData(), IndexData.Num(), IndexHash);
|
|
if (FMemory::Memcmp(IndexHash, Info.IndexHash, sizeof(IndexHash)) != 0)
|
|
{
|
|
UE_LOG(LogPakFile, Fatal, TEXT("Corrupted index in pak file (CRC mismatch)."));
|
|
}
|
|
|
|
// Read the default mount point and all entries.
|
|
int32 NumEntries = 0;
|
|
IndexReader << MountPoint;
|
|
IndexReader << NumEntries;
|
|
|
|
MakeDirectoryFromPath(MountPoint);
|
|
// Allocate enough memory to hold all entries (and not reallocate while they're being added to it).
|
|
Files.Empty(NumEntries);
|
|
|
|
for (int32 EntryIndex = 0; EntryIndex < NumEntries; EntryIndex++)
|
|
{
|
|
// Serialize from memory.
|
|
FPakEntry Entry;
|
|
FString Filename;
|
|
IndexReader << Filename;
|
|
Entry.Serialize(IndexReader, Info.Version);
|
|
|
|
// Add new file info.
|
|
Files.Add(Entry);
|
|
|
|
// Construct Index of all directories in pak file.
|
|
FString Path = FPaths::GetPath(Filename);
|
|
MakeDirectoryFromPath(Path);
|
|
FPakDirectory* Directory = Index.Find(Path);
|
|
if (Directory != NULL)
|
|
{
|
|
Directory->Add(Filename, &Files.Last());
|
|
}
|
|
else
|
|
{
|
|
FPakDirectory NewDirectory;
|
|
NewDirectory.Add(Filename, &Files.Last());
|
|
Index.Add(Path, NewDirectory);
|
|
|
|
// add the parent directories up to the mount point
|
|
while (MountPoint != Path)
|
|
{
|
|
Path = Path.Left(Path.Len()-1);
|
|
int32 Offset = 0;
|
|
if (Path.FindLastChar('/', Offset))
|
|
{
|
|
Path = Path.Left(Offset);
|
|
MakeDirectoryFromPath(Path);
|
|
if (Index.Find(Path) == NULL)
|
|
{
|
|
FPakDirectory ParentDirectory;
|
|
Index.Add(Path, ParentDirectory);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Path = MountPoint;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
FArchive* FPakFile::GetSharedReader(IPlatformFile* LowerLevel)
|
|
{
|
|
uint32 Thread = FPlatformTLS::GetCurrentThreadId();
|
|
FArchive* PakReader = NULL;
|
|
{
|
|
FScopeLock ScopedLock(&CriticalSection);
|
|
TAutoPtr<FArchive>* ExistingReader = ReaderMap.Find(Thread);
|
|
if (ExistingReader)
|
|
{
|
|
PakReader = *ExistingReader;
|
|
}
|
|
}
|
|
if (!PakReader)
|
|
{
|
|
// Create a new FArchive reader and pass it to the new handle.
|
|
if (LowerLevel != NULL)
|
|
{
|
|
IFileHandle* PakHandle = LowerLevel->OpenRead(*GetFilename());
|
|
if (PakHandle)
|
|
{
|
|
PakReader = CreatePakReader(*PakHandle, *GetFilename());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
PakReader = CreatePakReader(*GetFilename());
|
|
}
|
|
if (!PakReader)
|
|
{
|
|
UE_LOG(LogPakFile, Fatal, TEXT("Unable to create pak \"%s\" handle"), *GetFilename());
|
|
}
|
|
{
|
|
FScopeLock ScopedLock(&CriticalSection);
|
|
ReaderMap.Emplace(Thread, PakReader);
|
|
}
|
|
}
|
|
return PakReader;
|
|
}
|
|
|
|
#if !UE_BUILD_SHIPPING
|
|
class FPakExec : private FSelfRegisteringExec
|
|
{
|
|
FPakPlatformFile& PlatformFile;
|
|
|
|
public:
|
|
|
|
FPakExec(FPakPlatformFile& InPlatformFile)
|
|
: PlatformFile(InPlatformFile)
|
|
{}
|
|
|
|
/** Console commands **/
|
|
virtual bool Exec( UWorld* InWorld, const TCHAR* Cmd, FOutputDevice& Ar ) override
|
|
{
|
|
if (FParse::Command(&Cmd, TEXT("Mount")))
|
|
{
|
|
PlatformFile.HandleMountCommand(Cmd, Ar);
|
|
return true;
|
|
}
|
|
if (FParse::Command(&Cmd, TEXT("Unmount")))
|
|
{
|
|
PlatformFile.HandleUnmountCommand(Cmd, Ar);
|
|
return true;
|
|
}
|
|
else if (FParse::Command(&Cmd, TEXT("PakList")))
|
|
{
|
|
PlatformFile.HandlePakListCommand(Cmd, Ar);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
static TAutoPtr<FPakExec> GPakExec;
|
|
|
|
void FPakPlatformFile::HandleMountCommand(const TCHAR* Cmd, FOutputDevice& Ar)
|
|
{
|
|
const FString PakFilename = FParse::Token(Cmd, false);
|
|
if (!PakFilename.IsEmpty())
|
|
{
|
|
const FString MountPoint = FParse::Token(Cmd, false);
|
|
Mount(*PakFilename, 0, MountPoint.IsEmpty() ? NULL : *MountPoint);
|
|
}
|
|
}
|
|
|
|
void FPakPlatformFile::HandleUnmountCommand(const TCHAR* Cmd, FOutputDevice& Ar)
|
|
{
|
|
const FString PakFilename = FParse::Token(Cmd, false);
|
|
if (!PakFilename.IsEmpty())
|
|
{
|
|
Unmount(*PakFilename);
|
|
}
|
|
}
|
|
|
|
void FPakPlatformFile::HandlePakListCommand(const TCHAR* Cmd, FOutputDevice& Ar)
|
|
{
|
|
TArray<FPakListEntry> Paks;
|
|
GetMountedPaks(Paks);
|
|
for (auto Pak : Paks)
|
|
{
|
|
Ar.Logf(TEXT("%s"), *Pak.PakFile->GetFilename());
|
|
}
|
|
}
|
|
#endif // !UE_BUILD_SHIPPING
|
|
|
|
FPakPlatformFile::FPakPlatformFile()
|
|
: LowerLevel(NULL)
|
|
, bSigned(false)
|
|
, bForceSecurityBypass(false)
|
|
{
|
|
|
|
#if PLATFORM_DESKTOP && UE_BUILD_SHIPPING
|
|
bool UsePakSecurity = true;
|
|
#else
|
|
bool UsePakSecurity = FParse::Param(FCommandLine::Get(), TEXT("PlatformFileSecurity"));
|
|
#endif
|
|
|
|
bSecurityEnabled = GEnablePakFileSecurity && UsePakSecurity;
|
|
}
|
|
|
|
FPakPlatformFile::~FPakPlatformFile()
|
|
{
|
|
FCoreDelegates::OnMountPak.Unbind();
|
|
FCoreDelegates::OnUnmountPak.Unbind();
|
|
|
|
// We need to flush async IO... if it hasn't been shut down already.
|
|
if (FIOSystem::HasShutdown() == false)
|
|
{
|
|
FIOSystem& IOSystem = FIOSystem::Get();
|
|
IOSystem.BlockTillAllRequestsFinishedAndFlushHandles();
|
|
}
|
|
|
|
{
|
|
FScopeLock ScopedLock(&PakListCritical);
|
|
for (int32 PakFileIndex = 0; PakFileIndex < PakFiles.Num(); PakFileIndex++)
|
|
{
|
|
delete PakFiles[PakFileIndex].PakFile;
|
|
PakFiles[PakFileIndex].PakFile = nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
void FPakPlatformFile::FindPakFilesInDirectory(IPlatformFile* LowLevelFile, const TCHAR* Directory, TArray<FString>& OutPakFiles)
|
|
{
|
|
// Helper class to find all pak files.
|
|
class FPakSearchVisitor : public IPlatformFile::FDirectoryVisitor
|
|
{
|
|
TArray<FString>& FoundPakFiles;
|
|
IPlatformChunkInstall* ChunkInstall;
|
|
public:
|
|
FPakSearchVisitor(TArray<FString>& InFoundPakFiles, IPlatformChunkInstall* InChunkInstall)
|
|
: FoundPakFiles(InFoundPakFiles)
|
|
, ChunkInstall(InChunkInstall)
|
|
{}
|
|
virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory)
|
|
{
|
|
if (bIsDirectory == false)
|
|
{
|
|
FString Filename(FilenameOrDirectory);
|
|
if (FPaths::GetExtension(Filename) == TEXT("pak"))
|
|
{
|
|
// if a platform supports chunk style installs, make sure that the chunk a pak file resides in is actually fully installed before accepting pak files from it
|
|
if (ChunkInstall)
|
|
{
|
|
FString ChunkIdentifier(TEXT("pakchunk"));
|
|
FString BaseFilename = FPaths::GetBaseFilename(Filename);
|
|
if (BaseFilename.StartsWith(ChunkIdentifier))
|
|
{
|
|
int32 DelimiterIndex = 0;
|
|
int32 StartOfChunkIndex = ChunkIdentifier.Len();
|
|
|
|
BaseFilename.FindChar(TEXT('-'), DelimiterIndex);
|
|
FString ChunkNumberString = BaseFilename.Mid(StartOfChunkIndex, DelimiterIndex-StartOfChunkIndex);
|
|
int32 ChunkNumber = 0;
|
|
TTypeFromString<int32>::FromString(ChunkNumber, *ChunkNumberString);
|
|
if (ChunkInstall->GetChunkLocation(ChunkNumber) == EChunkLocation::NotAvailable)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
FoundPakFiles.Add(Filename);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
// Find all pak files.
|
|
FPakSearchVisitor Visitor(OutPakFiles, FPlatformMisc::GetPlatformChunkInstall());
|
|
LowLevelFile->IterateDirectoryRecursively(Directory, Visitor);
|
|
}
|
|
|
|
void FPakPlatformFile::FindAllPakFiles(IPlatformFile* LowLevelFile, const TArray<FString>& PakFolders, TArray<FString>& OutPakFiles)
|
|
{
|
|
// Find pak files from the specified directories.
|
|
for (int32 FolderIndex = 0; FolderIndex < PakFolders.Num(); ++FolderIndex)
|
|
{
|
|
FindPakFilesInDirectory(LowLevelFile, *PakFolders[FolderIndex], OutPakFiles);
|
|
}
|
|
}
|
|
|
|
void FPakPlatformFile::GetPakFolders(const TCHAR* CmdLine, TArray<FString>& OutPakFolders)
|
|
{
|
|
#if !UE_BUILD_SHIPPING
|
|
// Command line folders
|
|
FString PakDirs;
|
|
if (FParse::Value(CmdLine, TEXT("-pakdir="), PakDirs))
|
|
{
|
|
TArray<FString> CmdLineFolders;
|
|
PakDirs.ParseIntoArray(CmdLineFolders, TEXT("*"), true);
|
|
OutPakFolders.Append(CmdLineFolders);
|
|
}
|
|
#endif
|
|
|
|
// @todo plugin urgent: Needs to handle plugin Pak directories, too
|
|
// Hardcoded locations
|
|
OutPakFolders.Add(FString::Printf(TEXT("%sPaks/"), *FPaths::GameContentDir()));
|
|
OutPakFolders.Add(FString::Printf(TEXT("%sPaks/"), *FPaths::GameSavedDir()));
|
|
OutPakFolders.Add(FString::Printf(TEXT("%sPaks/"), *FPaths::EngineContentDir()));
|
|
}
|
|
|
|
bool FPakPlatformFile::CheckIfPakFilesExist(IPlatformFile* LowLevelFile, const TArray<FString>& PakFolders)
|
|
{
|
|
TArray<FString> FoundPakFiles;
|
|
FindAllPakFiles(LowLevelFile, PakFolders, FoundPakFiles);
|
|
return FoundPakFiles.Num() > 0;
|
|
}
|
|
|
|
bool FPakPlatformFile::ShouldBeUsed(IPlatformFile* Inner, const TCHAR* CmdLine) const
|
|
{
|
|
#if !USING_SIGNED_CONTENT
|
|
bool Result = FParse::Param(CmdLine, TEXT("Pak")) || FParse::Param(CmdLine, TEXT("Signedpak")) || FParse::Param(CmdLine, TEXT("Signed"));
|
|
if (FPlatformProperties::RequiresCookedData() && !Result && !FParse::Param(CmdLine, TEXT("NoPak")))
|
|
{
|
|
TArray<FString> PakFolders;
|
|
GetPakFolders(CmdLine, PakFolders);
|
|
Result = CheckIfPakFilesExist(Inner, PakFolders);
|
|
}
|
|
return Result;
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
bool FPakPlatformFile::Initialize(IPlatformFile* Inner, const TCHAR* CmdLine)
|
|
{
|
|
// Inner is required.
|
|
check(Inner != NULL);
|
|
LowerLevel = Inner;
|
|
|
|
#if !USING_SIGNED_CONTENT
|
|
bSigned = FParse::Param(CmdLine, TEXT("Signedpak")) || FParse::Param(CmdLine, TEXT("Signed"));
|
|
if (!bSigned)
|
|
{
|
|
// Even if -signed is not provided in the command line, use signed reader if the hardcoded key is non-zero.
|
|
FEncryptionKey DecryptionKey;
|
|
DecryptionKey.Exponent.Parse(DECRYPTION_KEY_EXPONENT);
|
|
DecryptionKey.Modulus.Parse(DECRYPTION_KEY_MODULUS);
|
|
bSigned = !DecryptionKey.Exponent.IsZero() && !DecryptionKey.Modulus.IsZero();
|
|
}
|
|
#else
|
|
bSigned = true;
|
|
#endif
|
|
|
|
bool bMountPaks = true;
|
|
TArray<FString> PaksToLoad;
|
|
#if !UE_BUILD_SHIPPING
|
|
// Optionally get a list of pak filenames to load, only these paks will be mounted
|
|
FString CmdLinePaksToLoad;
|
|
if (FParse::Value(CmdLine, TEXT("-paklist="), CmdLinePaksToLoad))
|
|
{
|
|
CmdLinePaksToLoad.ParseIntoArray(PaksToLoad, TEXT("+"), true);
|
|
}
|
|
|
|
//if we are using a fileserver, then dont' mount paks automatically. We only want to read files from the server.
|
|
FString FileHostIP;
|
|
const bool bCookOnTheFly = FParse::Value(FCommandLine::Get(), TEXT("filehostip"), FileHostIP);
|
|
bMountPaks = !bCookOnTheFly;
|
|
#endif
|
|
|
|
if (bMountPaks)
|
|
{
|
|
// Find and mount pak files from the specified directories.
|
|
TArray<FString> PakFolders;
|
|
GetPakFolders(CmdLine, PakFolders);
|
|
TArray<FString> FoundPakFiles;
|
|
FindAllPakFiles(LowerLevel, PakFolders, FoundPakFiles);
|
|
// Sort in descending order.
|
|
FoundPakFiles.Sort(TGreater<FString>());
|
|
// Mount all found pak files
|
|
for (int32 PakFileIndex = 0; PakFileIndex < FoundPakFiles.Num(); PakFileIndex++)
|
|
{
|
|
const FString& PakFilename = FoundPakFiles[PakFileIndex];
|
|
bool bLoadPak = true;
|
|
if (PaksToLoad.Num() && !PaksToLoad.Contains(FPaths::GetBaseFilename(PakFilename)))
|
|
{
|
|
bLoadPak = false;
|
|
}
|
|
if (bLoadPak)
|
|
{
|
|
// hardcode default load ordering of game main pak -> game content -> engine content -> saved dir
|
|
// would be better to make this config but not even the config system is initialized here so we can't do that
|
|
uint32 PakOrder = 0;
|
|
if (PakFilename.StartsWith(FString::Printf(TEXT("%sPaks/%s-"), *FPaths::GameContentDir(), FApp::GetGameName())))
|
|
{
|
|
PakOrder = 4;
|
|
}
|
|
else if (PakFilename.StartsWith(FPaths::GameContentDir()))
|
|
{
|
|
PakOrder = 3;
|
|
}
|
|
else if (PakFilename.StartsWith(FPaths::EngineContentDir()))
|
|
{
|
|
PakOrder = 2;
|
|
}
|
|
else if (PakFilename.StartsWith(FPaths::GameSavedDir()))
|
|
{
|
|
PakOrder = 1;
|
|
}
|
|
|
|
Mount(*PakFilename, PakOrder);
|
|
}
|
|
}
|
|
}
|
|
|
|
#if !UE_BUILD_SHIPPING
|
|
GPakExec = new FPakExec(*this);
|
|
#endif // !UE_BUILD_SHIPPING
|
|
|
|
FCoreDelegates::OnMountPak.BindRaw(this, &FPakPlatformFile::HandleMountPakDelegate);
|
|
FCoreDelegates::OnUnmountPak.BindRaw(this, &FPakPlatformFile::HandleUnmountPakDelegate);
|
|
return !!LowerLevel;
|
|
}
|
|
|
|
bool FPakPlatformFile::Mount(const TCHAR* InPakFilename, uint32 PakOrder, const TCHAR* InPath /*= NULL*/)
|
|
{
|
|
bool bSuccess = false;
|
|
TSharedPtr<IFileHandle> PakHandle = MakeShareable(LowerLevel->OpenRead(InPakFilename));
|
|
if (PakHandle.IsValid())
|
|
{
|
|
FPakFile* Pak = new FPakFile(LowerLevel, InPakFilename, bSigned);
|
|
if (Pak->IsValid())
|
|
{
|
|
if (InPath != NULL)
|
|
{
|
|
Pak->SetMountPoint(InPath);
|
|
}
|
|
FString PakFilename = InPakFilename;
|
|
if ( PakFilename.EndsWith(TEXT("_P.pak")) )
|
|
{
|
|
PakOrder += 100;
|
|
}
|
|
{
|
|
// Add new pak file
|
|
FScopeLock ScopedLock(&PakListCritical);
|
|
FPakListEntry Entry;
|
|
Entry.ReadOrder = PakOrder;
|
|
Entry.PakFile = Pak;
|
|
PakFiles.Add(Entry);
|
|
PakFiles.StableSort();
|
|
}
|
|
bSuccess = true;
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogPakFile, Warning, TEXT("Failed to mount pak \"%s\", pak is invalid."), InPakFilename);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogPakFile, Warning, TEXT("Pak \"%s\" does not exist!"), InPakFilename);
|
|
}
|
|
return bSuccess;
|
|
}
|
|
|
|
bool FPakPlatformFile::Unmount(const TCHAR* InPakFilename)
|
|
{
|
|
{
|
|
FScopeLock ScopedLock(&PakListCritical);
|
|
|
|
for (int32 PakIndex = 0; PakIndex < PakFiles.Num(); PakIndex++)
|
|
{
|
|
if (PakFiles[PakIndex].PakFile->GetFilename() == InPakFilename)
|
|
{
|
|
delete PakFiles[PakIndex].PakFile;
|
|
PakFiles.RemoveAt(PakIndex);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
IFileHandle* FPakPlatformFile::CreatePakFileHandle(const TCHAR* Filename, FPakFile* PakFile, const FPakEntry* FileEntry)
|
|
{
|
|
IFileHandle* Result = NULL;
|
|
FArchive* PakReader = PakFile->GetSharedReader(LowerLevel);
|
|
|
|
// Create the handle.
|
|
if (FileEntry->CompressionMethod != COMPRESS_None && PakFile->GetInfo().Version >= FPakInfo::PakFile_Version_CompressionEncryption)
|
|
{
|
|
if (FileEntry->bEncrypted)
|
|
{
|
|
Result = new FPakFileHandle< FPakCompressedReaderPolicy<FPakSimpleEncryption> >(*PakFile, *FileEntry, PakReader, true);
|
|
}
|
|
else
|
|
{
|
|
Result = new FPakFileHandle< FPakCompressedReaderPolicy<> >(*PakFile, *FileEntry, PakReader, true);
|
|
}
|
|
}
|
|
else if (FileEntry->bEncrypted)
|
|
{
|
|
Result = new FPakFileHandle< FPakReaderPolicy<FPakSimpleEncryption> >(*PakFile, *FileEntry, PakReader, true);
|
|
}
|
|
else
|
|
{
|
|
Result = new FPakFileHandle<>(*PakFile, *FileEntry, PakReader, true);
|
|
}
|
|
|
|
return Result;
|
|
}
|
|
|
|
bool FPakPlatformFile::HandleMountPakDelegate(const FString& PakFilePath, uint32 PakOrder, IPlatformFile::FDirectoryVisitor* Visitor)
|
|
{
|
|
bool bReturn = Mount(*PakFilePath, PakOrder);
|
|
if (bReturn && Visitor != nullptr)
|
|
{
|
|
TArray<FPakListEntry> Paks;
|
|
GetMountedPaks(Paks);
|
|
// Find the single pak we just mounted
|
|
for (auto Pak : Paks)
|
|
{
|
|
if (PakFilePath == Pak.PakFile->GetFilename())
|
|
{
|
|
// Get a list of all of the files in the pak
|
|
for (FPakFile::FFileIterator It(*Pak.PakFile); It; ++It)
|
|
{
|
|
Visitor->Visit(*It.Filename(), false);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return bReturn;
|
|
}
|
|
|
|
bool FPakPlatformFile::HandleUnmountPakDelegate(const FString& PakFilePath)
|
|
{
|
|
return Unmount(*PakFilePath);
|
|
}
|
|
|
|
IFileHandle* FPakPlatformFile::OpenRead(const TCHAR* Filename, bool bAllowWrite)
|
|
{
|
|
IFileHandle* Result = NULL;
|
|
FPakFile* PakFile = NULL;
|
|
const FPakEntry* FileEntry = FindFileInPakFiles(Filename, &PakFile);
|
|
if (FileEntry != NULL)
|
|
{
|
|
Result = CreatePakFileHandle(Filename, PakFile, FileEntry);
|
|
}
|
|
#if !USING_SIGNED_CONTENT
|
|
else
|
|
{
|
|
if (IsFilenameAllowed(Filename))
|
|
{
|
|
// Default to wrapped file
|
|
Result = LowerLevel->OpenRead(Filename, bAllowWrite);
|
|
}
|
|
}
|
|
#endif
|
|
return Result;
|
|
}
|
|
|
|
bool FPakPlatformFile::BufferedCopyFile(IFileHandle& Dest, IFileHandle& Source, const int64 FileSize, uint8* Buffer, const int64 BufferSize) const
|
|
{
|
|
int64 RemainingSizeToCopy = FileSize;
|
|
// Continue copying chunks using the buffer
|
|
while (RemainingSizeToCopy > 0)
|
|
{
|
|
const int64 SizeToCopy = FMath::Min(BufferSize, RemainingSizeToCopy);
|
|
if (Source.Read(Buffer, SizeToCopy) == false)
|
|
{
|
|
return false;
|
|
}
|
|
if (Dest.Write(Buffer, SizeToCopy) == false)
|
|
{
|
|
return false;
|
|
}
|
|
RemainingSizeToCopy -= SizeToCopy;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool FPakPlatformFile::CopyFile(const TCHAR* To, const TCHAR* From)
|
|
{
|
|
bool Result = false;
|
|
FPakFile* PakFile = NULL;
|
|
const FPakEntry* FileEntry = FindFileInPakFiles(From, &PakFile);
|
|
if (FileEntry != NULL)
|
|
{
|
|
// Copy from pak to LowerLevel->
|
|
// Create handles both files.
|
|
TAutoPtr<IFileHandle> DestHandle(LowerLevel->OpenWrite(To));
|
|
TAutoPtr<IFileHandle> SourceHandle(CreatePakFileHandle(From, PakFile, FileEntry));
|
|
|
|
if (DestHandle.IsValid() && SourceHandle.IsValid())
|
|
{
|
|
const int64 BufferSize = 64 * 1024; // Copy in 64K chunks.
|
|
uint8* Buffer = (uint8*)FMemory::Malloc(BufferSize);
|
|
Result = BufferedCopyFile(*DestHandle, *SourceHandle, SourceHandle->Size(), Buffer, BufferSize);
|
|
FMemory::Free(Buffer);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Result = LowerLevel->CopyFile(To, From);
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
uint32 ComputePakChunkHash(const uint8* InData, const int64 InDataSize)
|
|
{
|
|
return FCrc::MemCrc32(InData, InDataSize);
|
|
}
|
|
|
|
/**
|
|
* Module for the pak file
|
|
*/
|
|
class FPakFileModule : public IPlatformFileModule
|
|
{
|
|
public:
|
|
virtual IPlatformFile* GetPlatformFile() override
|
|
{
|
|
static TScopedPointer<IPlatformFile> AutoDestroySingleton(new FPakPlatformFile());
|
|
return AutoDestroySingleton.GetOwnedPointer();
|
|
}
|
|
};
|
|
|
|
IMPLEMENT_MODULE(FPakFileModule, PakFile);
|