bool FBuildPatchAppManifest::SaveToFile(const FString& Filename, bool bUseBinary) { bool bSuccess = false; FArchive* FileOut = IFileManager::Get().CreateFileWriter(*Filename); if (FileOut) { if (bUseBinary) { Data->ManifestFileVersion = EBuildPatchAppManifestVersion::GetLatestVersion(); FManifestWriter ManifestData; Serialize(ManifestData); ManifestData.Finalize(); if (!ManifestData.IsError()) { int32 DataSize = ManifestData.TotalSize(); TArray<uint8> TempCompressed; TempCompressed.AddUninitialized(DataSize); int32 CompressedSize = DataSize; bool bDataIsCompressed = FCompression::CompressMemory( static_cast<ECompressionFlags>(COMPRESS_ZLIB | COMPRESS_BiasMemory), TempCompressed.GetData(), CompressedSize, ManifestData.GetBytes().GetData(), DataSize); TempCompressed.SetNum(CompressedSize); TArray<uint8>& FileData = bDataIsCompressed ? TempCompressed : ManifestData.GetBytes(); FManifestFileHeader Header; *FileOut << Header; Header.HeaderSize = FileOut->Tell(); Header.StoredAs = bDataIsCompressed ? EManifestFileHeader::STORED_COMPRESSED : EManifestFileHeader::STORED_RAW; Header.DataSize = DataSize; Header.CompressedSize = bDataIsCompressed ? CompressedSize : 0; FSHA1::HashBuffer(FileData.GetData(), FileData.Num(), Header.SHAHash.Hash); FileOut->Seek(0); *FileOut << Header; FileOut->Serialize(FileData.GetData(), FileData.Num()); bSuccess = !FileOut->IsError(); } } else { Data->ManifestFileVersion = EBuildPatchAppManifestVersion::GetLatestJsonVersion(); FString JSONOutput; SerializeToJSON(JSONOutput); FTCHARToUTF8 JsonUTF8(*JSONOutput); FileOut->Serialize((UTF8CHAR*)JsonUTF8.Get(), JsonUTF8.Length() * sizeof(UTF8CHAR)); } FileOut->Close(); delete FileOut; FileOut = nullptr; } return bSuccess; }
static void WriteRiffHeader(FArchive &Ar, int FileLength) { assert(!Ar.IsLoading); static const char *RIFF = "RIFF"; Ar.Serialize((char*)RIFF, 4); Ar << FileLength; static const char *WAVE = "WAVE"; Ar.Serialize((char*)WAVE, 4); }
static void SaveSound(const UObject *Obj, void *Data, int DataSize, const char *DefExt) { // check for enough place for header if (DataSize < 16) { appPrintf("... empty sound %s ?\n", Obj->Name); return; } const char *ext = DefExt; if (!memcmp(Data, "OggS", 4)) ext = "ogg"; else if (!memcmp(Data, "RIFF", 4)) ext = "wav"; else if (!memcmp(Data, "FSB4", 4)) ext = "fsb"; // FMOD sound bank else if (!memcmp(Data, "MSFC", 4)) ext = "mp3"; // PS3 MP3 codec FArchive *Ar = CreateExportArchive(Obj, "%s.%s", Obj->Name, ext); if (Ar) { Ar->Serialize(Data, DataSize); delete Ar; } }
void FNetworkFileServerClientConnection::ProcessWriteFile( FArchive& In, FArchive& Out ) { // Get Handle ID uint64 HandleId = 0; In << HandleId; int64 BytesWritten = 0; IFileHandle* File = FindOpenFile(HandleId); if (File) { int64 BytesToWrite = 0; In << BytesToWrite; uint8* Source = (uint8*)FMemory::Malloc(BytesToWrite); In.Serialize(Source, BytesToWrite); if (File->Write(Source, BytesToWrite)) { BytesWritten = BytesToWrite; } FMemory::Free(Source); } Out << BytesWritten; }
/** * Load a text file to an FString. * Supports all combination of ANSI/Unicode files and platforms. * @param Result string representation of the loaded file * @param Filename name of the file to load * @param VerifyFlags flags controlling the hash verification behavior ( see EHashOptions ) */ bool FFileHelper::LoadFileToString( FString& Result, const TCHAR* Filename, uint32 VerifyFlags ) { FArchive* Reader = IFileManager::Get().CreateFileReader( Filename ); if( !Reader ) { return 0; } int32 Size = Reader->TotalSize(); uint8* Ch = (uint8*)FMemory::Malloc(Size); Reader->Serialize( Ch, Size ); bool Success = Reader->Close(); delete Reader; BufferToString( Result, Ch, Size ); // handle SHA verify of the file if( (VerifyFlags & EHashOptions::EnableVerify) && ( (VerifyFlags & EHashOptions::ErrorMissingHash) || FSHA1::GetFileSHAHash(Filename, NULL) ) ) { // kick off SHA verify task. this frees the buffer on close FBufferReaderWithSHA Ar( Ch, Size, true, Filename, false, true ); } else { // free manually since not running SHA task FMemory::Free(Ch); } return Success; }
byte *FindXprData(const char *Name, int *DataSize) { // scan xprs static bool ready = false; if (!ready) { ready = true; appEnumGameFiles(ReadXprFile, "xpr"); } // find a file for (int i = 0; i < xprFiles.Num(); i++) { XprInfo *Info = &xprFiles[i]; for (int j = 0; j < Info->Items.Num(); j++) { XprEntry *File = &Info->Items[j]; if (strcmp(File->Name, Name) == 0) { // found appPrintf("Loading stream %s from %s (%d bytes)\n", Name, Info->File->RelativeName, File->DataSize); FArchive *Reader = appCreateFileReader(Info->File); Reader->Seek(File->DataOffset); byte *buf = (byte*)appMalloc(File->DataSize); Reader->Serialize(buf, File->DataSize); delete Reader; if (DataSize) *DataSize = File->DataSize; return buf; } } } appPrintf("WARNING: external stream %s was not found\n", Name); if (DataSize) *DataSize = 0; return NULL; }
bool GenerateHashForFile( FString Filename, uint8 FileHash[16]) { FArchive* File = IFileManager::Get().CreateFileReader(*Filename); if ( File == NULL ) return false; uint64 TotalSize = File->TotalSize(); uint8* ByteBuffer = new uint8[TotalSize]; File->Serialize(ByteBuffer, TotalSize); delete File; File = NULL; FMD5 FileHasher; FileHasher.Update(ByteBuffer, TotalSize); delete[] ByteBuffer; FileHasher.Final(FileHash); return true; // uint8 DestFileHash[20]; }
void FNetworkFileServerClientConnection::ProcessReadFile( FArchive& In, FArchive& Out ) { // Get Handle ID uint64 HandleId = 0; In << HandleId; int64 BytesToRead = 0; In << BytesToRead; int64 BytesRead = 0; IFileHandle* File = FindOpenFile(HandleId); if (File) { uint8* Dest = (uint8*)FMemory::Malloc(BytesToRead); if (File->Read(Dest, BytesToRead)) { BytesRead = BytesToRead; Out << BytesRead; Out.Serialize(Dest, BytesRead); } else { Out << BytesRead; } FMemory::Free(Dest); } else { Out << BytesRead; } }
void UChildActorComponent::Serialize(FArchive& Ar) { Super::Serialize(Ar); if (Ar.HasAllPortFlags(PPF_DuplicateForPIE)) { // PIE duplication should just work normally Ar << ChildActorTemplate; } else if (Ar.HasAllPortFlags(PPF_Duplicate)) { if (GIsEditor && Ar.IsLoading() && !IsTemplate()) { // If we're not a template then we do not want the duplicate so serialize manually and destroy the template that was created for us Ar.Serialize(&ChildActorTemplate, sizeof(UObject*)); if (AActor* UnwantedDuplicate = static_cast<AActor*>(FindObjectWithOuter(this, AActor::StaticClass()))) { UnwantedDuplicate->MarkPendingKill(); } } else if (!GIsEditor && !Ar.IsLoading() && !GIsDuplicatingClassForReinstancing) { // Avoid the archiver in the duplicate writer case because we want to avoid the duplicate being created Ar.Serialize(&ChildActorTemplate, sizeof(UObject*)); } else { // When we're loading outside of the editor we won't have created the duplicate, so its fine to just use the normal path // When we're loading a template then we want the duplicate, so it is fine to use normal archiver // When we're saving in the editor we'll create the duplicate, but on loading decide whether to take it or not Ar << ChildActorTemplate; } } #if WITH_EDITOR // Since we sometimes serialize properties in instead of using duplication if we are a template // and are not pointing at a component we own we'll need to fix that if (ChildActorTemplate && ChildActorTemplate->GetOuter() != this && IsTemplate()) { const FString TemplateName = FString::Printf(TEXT("%s_%s_CAT"), *GetName(), *ChildActorClass->GetName()); ChildActorTemplate = CastChecked<AActor>(StaticDuplicateObject(ChildActorTemplate, this, *TemplateName)); } #endif }
static byte *FindBioTexture(const UTexture *Tex) { int needSize = Tex->CachedBulkDataSize & 0xFFFFFFFF; #if DEBUG_BIO_BULK appPrintf("Search for ... %s (size=%X)\n", Tex->Name, needSize); #endif BioReadBulkCatalog(); for (int i = 0; i < bioCatalog.Num(); i++) { BioBulkCatalog &Cat = bioCatalog[i]; for (int j = 0; j < Cat.Files.Num(); j++) { const BioBulkCatalogFile &File = Cat.Files[j]; for (int k = 0; k < File.Items.Num(); k++) { const BioBulkCatalogItem &Item = File.Items[k]; if (!strcmp(Tex->Name, Item.ObjectName)) { if (abs(needSize - Item.DataSize) > 0x4000) // differs in 16k { #if DEBUG_BIO_BULK appPrintf("... Found %s in %s with wrong BulkDataSize %X (need %X)\n", Tex->Name, *File.Filename, Item.DataSize, needSize); #endif continue; } #if DEBUG_BIO_BULK appPrintf("... Found %s in %s at %X size %X (%dx%d fmt=%d bpp=%g strip:%d mips:%d)\n", Tex->Name, *File.Filename, Item.DataOffset, Item.DataSize, Tex->USize, Tex->VSize, Tex->Format, (float)Item.DataSize / (Tex->USize * Tex->VSize), Tex->HasBeenStripped, Tex->StrippedNumMips); #endif // found const CGameFileInfo *bulkFile = appFindGameFile(File.Filename); if (!bulkFile) { // no bulk file appPrintf("Decompressing %s: %s is missing\n", Tex->Name, *File.Filename); return NULL; } appPrintf("Reading %s mip level %d (%dx%d) from %s\n", Tex->Name, 0, Tex->USize, Tex->VSize, bulkFile->RelativeName); FArchive *Reader = appCreateFileReader(bulkFile); Reader->Seek(Item.DataOffset); byte *buf = (byte*)appMalloc(max(Item.DataSize, needSize)); Reader->Serialize(buf, Item.DataSize); delete Reader; return buf; } } } } #if DEBUG_BIO_BULK appPrintf("... Bulk for %s was not found\n", Tex->Name); #endif return NULL; }
/** * Save a binary array to a file. */ bool FFileHelper::SaveArrayToFile( const TArray<uint8>& Array, const TCHAR* Filename, IFileManager* FileManager /*= &IFileManager::Get()*/ ) { FArchive* Ar = FileManager->CreateFileWriter( Filename, 0 ); if( !Ar ) { return 0; } Ar->Serialize(const_cast<uint8*>(Array.GetData()), Array.Num()); delete Ar; return true; }
bool AHeatmapDataCollector::SaveArrayToFile(const TArray<int16>& Array, const TCHAR * Filename, IFileManager * FileManager, uint32 WriteFlags) const { FArchive* Ar = FileManager->CreateFileWriter(Filename, WriteFlags); if (!Ar) { return 0; } Ar->Serialize(const_cast<int16*>(Array.GetData()), Array.Num()); delete Ar; return true; }
bool UJavascriptLibrary::WriteFile(UObject* Object, FString Filename) { FArchive* Writer = IFileManager::Get().CreateFileWriter(*Filename); if (!Writer) { return false; } Writer->Serialize(FArrayBufferAccessor::GetData(), FArrayBufferAccessor::GetSize()); return Writer->Close(); }
bool BufferedCopyFile(FArchive& Dest, FArchive& Source, const FPakEntry& Entry, void* Buffer, int64 BufferSize) { // Align down BufferSize = BufferSize & ~(FAES::AESBlockSize-1); int64 RemainingSizeToCopy = Entry.Size; while (RemainingSizeToCopy > 0) { const int64 SizeToCopy = FMath::Min(BufferSize, RemainingSizeToCopy); // If file is encrypted so we need to account for padding int64 SizeToRead = Entry.bEncrypted ? Align(SizeToCopy,FAES::AESBlockSize) : SizeToCopy; Source.Serialize(Buffer,SizeToRead); if (Entry.bEncrypted) { FAES::DecryptData((uint8*)Buffer,SizeToRead); } Dest.Serialize(Buffer, SizeToCopy); RemainingSizeToCopy -= SizeToRead; } return true; }
bool UncompressCopyFile(FArchive& Dest, FArchive& Source, const FPakEntry& Entry, uint8*& PersistentBuffer, int64& BufferSize) { if (Entry.UncompressedSize == 0) { return false; } int64 WorkingSize = Entry.CompressionBlockSize; int32 MaxCompressionBlockSize = FCompression::CompressMemoryBound((ECompressionFlags)Entry.CompressionMethod, WorkingSize); WorkingSize += MaxCompressionBlockSize; if (BufferSize < WorkingSize) { PersistentBuffer = (uint8*)FMemory::Realloc(PersistentBuffer, WorkingSize); BufferSize = WorkingSize; } uint8* UncompressedBuffer = PersistentBuffer+MaxCompressionBlockSize; for (uint32 BlockIndex=0, BlockIndexNum=Entry.CompressionBlocks.Num(); BlockIndex < BlockIndexNum; ++BlockIndex) { uint32 CompressedBlockSize = Entry.CompressionBlocks[BlockIndex].CompressedEnd - Entry.CompressionBlocks[BlockIndex].CompressedStart; uint32 UncompressedBlockSize = (uint32)FMath::Min<int64>(Entry.UncompressedSize - Entry.CompressionBlockSize*BlockIndex, Entry.CompressionBlockSize); Source.Seek(Entry.CompressionBlocks[BlockIndex].CompressedStart); uint32 SizeToRead = Entry.bEncrypted ? Align(CompressedBlockSize, FAES::AESBlockSize) : CompressedBlockSize; Source.Serialize(PersistentBuffer, SizeToRead); if (Entry.bEncrypted) { FAES::DecryptData(PersistentBuffer, SizeToRead); } if(!FCompression::UncompressMemory((ECompressionFlags)Entry.CompressionMethod,UncompressedBuffer,UncompressedBlockSize,PersistentBuffer,CompressedBlockSize)) { return false; } Dest.Serialize(UncompressedBuffer,UncompressedBlockSize); } return true; }
uint8 FBuildPatchUtils::VerifyFile(const FString& FileToVerify, const FSHAHashData& Hash1, const FSHAHashData& Hash2, FBuildPatchFloatDelegate ProgressDelegate, FBuildPatchBoolRetDelegate ShouldPauseDelegate, double& TimeSpentPaused) { uint8 ReturnValue = 0; FArchive* FileReader = IFileManager::Get().CreateFileReader(*FileToVerify); ProgressDelegate.ExecuteIfBound(0.0f); if (FileReader != NULL) { FSHA1 HashState; FSHAHashData HashValue; const int64 FileSize = FileReader->TotalSize(); uint8* FileReadBuffer = new uint8[FileBufferSize]; while (!FileReader->AtEnd() && !FBuildPatchInstallError::HasFatalError()) { // Pause if necessary const double PrePauseTime = FPlatformTime::Seconds(); double PostPauseTime = PrePauseTime; bool bShouldPause = ShouldPauseDelegate.IsBound() && ShouldPauseDelegate.Execute(); while (bShouldPause && !FBuildPatchInstallError::HasFatalError()) { FPlatformProcess::Sleep(0.1f); bShouldPause = ShouldPauseDelegate.Execute(); PostPauseTime = FPlatformTime::Seconds(); } // Count up pause time TimeSpentPaused += PostPauseTime - PrePauseTime; // Read file and update hash state const int64 SizeLeft = FileSize - FileReader->Tell(); const uint32 ReadLen = FMath::Min< int64 >(FileBufferSize, SizeLeft); FileReader->Serialize(FileReadBuffer, ReadLen); HashState.Update(FileReadBuffer, ReadLen); const double FileSizeTemp = FileSize; const float Progress = 1.0f - ((SizeLeft - ReadLen) / FileSizeTemp); ProgressDelegate.ExecuteIfBound(Progress); } delete[] FileReadBuffer; HashState.Final(); HashState.GetHash(HashValue.Hash); ReturnValue = (HashValue == Hash1) ? 1 : (HashValue == Hash2) ? 2 : 0; if (ReturnValue == 0) { GLog->Logf(TEXT("BuildDataGenerator: Verify failed on %s"), *FPaths::GetCleanFilename(FileToVerify)); } FileReader->Close(); delete FileReader; } else { GLog->Logf(TEXT("BuildDataGenerator: ERROR VerifyFile cannot open %s"), *FileToVerify); } ProgressDelegate.ExecuteIfBound(1.0f); return ReturnValue; }
/** * Creates (fake so far) minidump */ void GenerateMinidump(const FString & Path) { FArchive* ReportFile = IFileManager::Get().CreateFileWriter(*Path); if (ReportFile != NULL) { // write BOM static uint32 Garbage = 0xDEADBEEF; ReportFile->Serialize(&Garbage, sizeof(Garbage)); ReportFile->Close(); delete ReportFile; } }
bool CopyFileToPak(FArchive& InPak, const FString& InMountPoint, const FPakInputPair& InFile, uint8*& InOutPersistentBuffer, int64& InOutBufferSize, FPakEntryPair& OutNewEntry) { TAutoPtr<FArchive> FileHandle(IFileManager::Get().CreateFileReader(*InFile.Source)); bool bFileExists = FileHandle.IsValid(); if (bFileExists) { const int64 FileSize = FileHandle->TotalSize(); const int64 PaddedEncryptedFileSize = Align(FileSize, FAES::AESBlockSize); OutNewEntry.Filename = InFile.Dest.Mid(InMountPoint.Len()); OutNewEntry.Info.Offset = 0; // Don't serialize offsets here. OutNewEntry.Info.Size = FileSize; OutNewEntry.Info.UncompressedSize = FileSize; OutNewEntry.Info.CompressionMethod = COMPRESS_None; OutNewEntry.Info.bEncrypted = InFile.bNeedEncryption; if (InOutBufferSize < PaddedEncryptedFileSize) { InOutPersistentBuffer = (uint8*)FMemory::Realloc(InOutPersistentBuffer, PaddedEncryptedFileSize); InOutBufferSize = FileSize; } // Load to buffer FileHandle->Serialize(InOutPersistentBuffer, FileSize); { int64 SizeToWrite = FileSize; if (InFile.bNeedEncryption) { for(int64 FillIndex=FileSize; FillIndex < PaddedEncryptedFileSize && InFile.bNeedEncryption; ++FillIndex) { // Fill the trailing buffer with random bytes from file InOutPersistentBuffer[FillIndex] = InOutPersistentBuffer[rand()%FileSize]; } //Encrypt the buffer before writing it to disk FAES::EncryptData(InOutPersistentBuffer, PaddedEncryptedFileSize); // Update the size to be written SizeToWrite = PaddedEncryptedFileSize; OutNewEntry.Info.bEncrypted = true; } // Calculate the buffer hash value FSHA1::HashBuffer(InOutPersistentBuffer,FileSize,OutNewEntry.Info.Hash); // Write to file OutNewEntry.Info.Serialize(InPak,FPakInfo::PakFile_Version_Latest); InPak.Serialize(InOutPersistentBuffer,SizeToWrite); } } return bFileExists; }
bool FRuntimeAssetCacheBackend::PutCachedData(const FName Bucket, const TCHAR* CacheKey, TArray<uint8>& InData, FCacheEntryMetadata* Metadata) { bool bResult = false; FArchive* Ar = CreateWriteArchive(Bucket, CacheKey); if (!Ar) { return bResult; } *Ar << *Metadata; Ar->Serialize(InData.GetData(), InData.Num()); bResult = Ar->Close(); delete Ar; return bResult; }
bool UJavascriptLibrary::ReadFile(UObject* Object, FString Filename) { FArchive* Reader = IFileManager::Get().CreateFileReader(*Filename); if (!Reader) { return false; } int32 Size = Reader->TotalSize(); if (Size != FArrayBufferAccessor::GetSize()) { return false; } Reader->Serialize(FArrayBufferAccessor::GetData(), Size); return Reader->Close(); }
bool FBuildPatchFileConstructor::InsertChunkData(const FChunkPartData& ChunkPart, FArchive& DestinationFile, FSHA1& HashState) { uint8* Data; uint8* DataStart; FChunkFile* ChunkFile = FBuildPatchChunkCache::Get().GetChunkFile( ChunkPart.Guid ); if( ChunkFile != NULL && !FBuildPatchInstallError::HasFatalError() ) { ChunkFile->GetDataLock( &Data, NULL ); DataStart = &Data[ ChunkPart.Offset ]; HashState.Update( DataStart, ChunkPart.Size ); DestinationFile.Serialize( DataStart, ChunkPart.Size ); ChunkFile->Dereference(); ChunkFile->ReleaseDataLock(); return true; } return false; }
/** * Load a binary file to a dynamic array. */ bool FFileHelper::LoadFileToArray( TArray<uint8>& Result, const TCHAR* Filename, uint32 Flags ) { FArchive* Reader = IFileManager::Get().CreateFileReader( Filename, Flags ); if( !Reader ) { if (!(Flags & FILEREAD_Silent)) { UE_LOG(LogStreaming,Warning,TEXT("Failed to read file '%s' error."),Filename); } return 0; } Result.Reset(); Result.AddUninitialized( Reader->TotalSize() ); Reader->Serialize(Result.GetData(), Result.Num()); bool Success = Reader->Close(); delete Reader; return Success; }
bool CopyCompressedFileToPak(FArchive& InPak, const FString& InMountPoint, const FPakInputPair& InFile, const FCompressedFileBuffer& CompressedFile, FPakEntryPair& OutNewEntry) { if (CompressedFile.TotalCompressedSize == 0) { return false; } int64 HeaderTell = InPak.Tell(); OutNewEntry.Info.CompressionMethod = CompressedFile.FileCompressionMethod; OutNewEntry.Info.CompressionBlocks.AddUninitialized(CompressedFile.CompressedBlocks.Num()); int64 TellPos = InPak.Tell() + OutNewEntry.Info.GetSerializedSize(FPakInfo::PakFile_Version_Latest); const TArray<FPakCompressedBlock>& Blocks = CompressedFile.CompressedBlocks; for (int32 BlockIndex = 0, BlockCount = CompressedFile.CompressedBlocks.Num(); BlockIndex < BlockCount; ++BlockIndex) { OutNewEntry.Info.CompressionBlocks[BlockIndex].CompressedStart = Blocks[BlockIndex].CompressedStart + TellPos; OutNewEntry.Info.CompressionBlocks[BlockIndex].CompressedEnd = Blocks[BlockIndex].CompressedEnd + TellPos; } if (InFile.bNeedEncryption) { FAES::EncryptData(CompressedFile.CompressedBuffer.Get(), CompressedFile.TotalCompressedSize); } //Hash the final buffer thats written FSHA1 Hash; Hash.Update(CompressedFile.CompressedBuffer.Get(), CompressedFile.TotalCompressedSize); Hash.Final(); // Update file size & Hash OutNewEntry.Info.CompressionBlockSize = CompressedFile.FileCompressionBlockSize; OutNewEntry.Info.UncompressedSize = CompressedFile.OriginalSize; OutNewEntry.Info.Size = CompressedFile.TotalCompressedSize; Hash.GetHash(OutNewEntry.Info.Hash); // Write the header, then the data OutNewEntry.Filename = InFile.Dest.Mid(InMountPoint.Len()); OutNewEntry.Info.Offset = 0; // Don't serialize offsets here. OutNewEntry.Info.bEncrypted = InFile.bNeedEncryption; OutNewEntry.Info.Serialize(InPak,FPakInfo::PakFile_Version_Latest); InPak.Serialize(CompressedFile.CompressedBuffer.Get(), CompressedFile.TotalCompressedSize); return true; }
bool FPerfCounters::ReportUnplayableCondition(const FString& ConditionDescription) { FString UnplayableConditionFile(FPaths::Combine(*FPaths::GameSavedDir(), *FString::Printf(TEXT("UnplayableConditionForPid_%d.txt"), FPlatformProcess::GetCurrentProcessId()))); FArchive* ReportFile = IFileManager::Get().CreateFileWriter(*UnplayableConditionFile); if (UNLIKELY(ReportFile == nullptr)) { return false; } // include description for debugging FTCHARToUTF8 Converter(*FString::Printf(TEXT("Unplayable condition encountered: %s\n"), *ConditionDescription)); ReportFile->Serialize(reinterpret_cast<void *>(const_cast<char *>(Converter.Get())), Converter.Length()); ReportFile->Close(); delete ReportFile; return true; }
bool FHighResScreenshotConfig::SaveImage(const FString& File, const TArray<TPixelType>& Bitmap, const FIntPoint& BitmapSize, FString* OutFilename) const { typedef FPixelTypeTraits<TPixelType> Traits; static_assert(ARE_TYPES_EQUAL(TPixelType, FFloat16Color) || ARE_TYPES_EQUAL(TPixelType, FColor), "Source format must be either FColor or FFloat16Color"); const int32 x = BitmapSize.X; const int32 y = BitmapSize.Y; check(Bitmap.Num() == x * y); const bool bIsWritingHDRImage = Traits::IsWritingHDRImage(bCaptureHDR); IFileManager* FileManager = &IFileManager::Get(); const size_t BitsPerPixel = (sizeof(TPixelType) / 4) * 8; TSharedPtr<class IImageWrapper> ImageCompressor = bIsWritingHDRImage ? ImageCompressorHDR : ImageCompressorLDR; // here we require the input file name to have an extension FString NewExtension = bIsWritingHDRImage ? TEXT(".exr") : TEXT(".png"); FString Filename = FPaths::GetBaseFilename(File, false) + NewExtension; if (OutFilename != nullptr) { *OutFilename = Filename; } if (ImageCompressor.IsValid() && ImageCompressor->SetRaw((void*)&Bitmap[0], sizeof(TPixelType)* x * y, x, y, Traits::SourceChannelLayout, BitsPerPixel)) { FArchive* Ar = FileManager->CreateFileWriter(Filename.GetCharArray().GetData()); if (Ar != nullptr) { const TArray<uint8>& CompressedData = ImageCompressor->GetCompressed(); int32 CompressedSize = CompressedData.Num(); Ar->Serialize((void*)CompressedData.GetData(), CompressedSize); delete Ar; } else { return false; } } return true; }
void FNetworkFileServerClientConnection::PackageFile( FString& Filename, FArchive& Out ) { // get file timestamp and send it to client FDateTime ServerTimeStamp = Sandbox->GetTimeStamp(*Filename); TArray<uint8> Contents; // open file IFileHandle* File = Sandbox->OpenRead(*Filename); if (!File) { ServerTimeStamp = FDateTime::MinValue(); // if this was a directory, this will make sure it is not confused with a zero byte file UE_LOG(LogFileServer, Warning, TEXT("Request for missing file %s."), *Filename ); } else { if (!File->Size()) { UE_LOG(LogFileServer, Warning, TEXT("Sending empty file %s...."), *Filename); } else { // read it Contents.AddUninitialized(File->Size()); File->Read(Contents.GetData(), Contents.Num()); } // close it delete File; UE_LOG(LogFileServer, Display, TEXT("Read %s, %d bytes"), *Filename, Contents.Num()); } Out << Filename; Out << ServerTimeStamp; uint64 FileSize = Contents.Num(); Out << FileSize; Out.Serialize(Contents.GetData(), FileSize); }
FCacheEntryMetadata* FRuntimeAssetCacheBackend::GetCachedData(const FName Bucket, const TCHAR* CacheKey, TArray<uint8>& OutData) { FCacheEntryMetadata* Result = nullptr; FArchive* Ar = CreateReadArchive(Bucket, CacheKey); if (!Ar) { return Result; } Result = PreloadMetadata(Ar); int64 TotalSize = Ar->TotalSize(); int64 CurrentPosition = Ar->Tell(); int64 NumberOfBytesToSerialize = TotalSize - CurrentPosition; OutData.Reset(); OutData.AddUninitialized(NumberOfBytesToSerialize); Ar->Serialize(OutData.GetData(), NumberOfBytesToSerialize); Ar->Close(); delete Ar; return Result; }
virtual void UpdateADBPath() override { FScopeLock PathUpdateLock(&ADBPathCheckLock); TCHAR AndroidDirectory[32768] = { 0 }; FPlatformMisc::GetEnvironmentVariable(TEXT("ANDROID_HOME"), AndroidDirectory, 32768); FString ADBPath; #if PLATFORM_MAC if (AndroidDirectory[0] == 0) { // didn't find ANDROID_HOME, so parse the .bash_profile file on MAC FArchive* FileReader = IFileManager::Get().CreateFileReader(*FString([@"~/.bash_profile" stringByExpandingTildeInPath])); if (FileReader) { const int64 FileSize = FileReader->TotalSize(); ANSICHAR* AnsiContents = (ANSICHAR*)FMemory::Malloc(FileSize); FileReader->Serialize(AnsiContents, FileSize); FileReader->Close(); delete FileReader; TArray<FString> Lines; FString(ANSI_TO_TCHAR(AnsiContents)).ParseIntoArrayLines(&Lines); FMemory::Free(AnsiContents); for (int32 Index = 0; Index < Lines.Num(); Index++) { if (AndroidDirectory[0] == 0 && Lines[Index].StartsWith(TEXT("export ANDROID_HOME="))) { FString Directory; Lines[Index].Split(TEXT("="), NULL, &Directory); Directory = Directory.Replace(TEXT("\""), TEXT("")); FCString::Strcpy(AndroidDirectory, *Directory); setenv("ANDROID_HOME", TCHAR_TO_ANSI(AndroidDirectory), 1); } } } }
bool FBuildPatchUtils::VerifyChunkFile( FArchive& ChunkFileData, bool bQuickCheck ) { const int64 FileSize = ChunkFileData.TotalSize(); bool bSuccess = ChunkFileData.IsLoading(); if ( !bSuccess ) { GLog->Logf( TEXT( "BuildPatchServices: ERROR: VerifyChunkFile expected readonly archive" ) ); } else { // Read the header FChunkHeader Header; ChunkFileData << Header; // Check header magic if ( !Header.IsValidMagic() ) { bSuccess = false; GLog->Logf( TEXT( "BuildPatchServices: ERROR: VerifyChunkFile corrupt header" ) ); } // Check Header and data size if ( bSuccess && ( Header.HeaderSize + Header.DataSize ) != FileSize ) { bSuccess = false; GLog->Logf( TEXT( "BuildPatchServices: ERROR: VerifyChunkFile header info does not match file size" ) ); } if( bSuccess && !bQuickCheck ) { // Hashes for checking data FSHA1 SHAHasher; FSHAHashData SHAHash; uint64 CycPoly64Hash = 0; // Load the data to check uint8* FileReadBuffer = new uint8[ FileBufferSize ]; int64 DataOffset = 0; switch ( Header.StoredAs ) { case FChunkHeader::STORED_RAW: while( !ChunkFileData.AtEnd() ) { const int64 SizeLeft = FileSize - ChunkFileData.Tell(); const uint32 ReadLen = FMath::Min< int64 >( FileBufferSize, SizeLeft ); ChunkFileData.Serialize( FileReadBuffer, ReadLen ); switch ( Header.HashType ) { case FChunkHeader::HASH_ROLLING: CycPoly64Hash = FCycPoly64Hash::GetHashForDataSet(FileReadBuffer, ReadLen, CycPoly64Hash); break; case FChunkHeader::HASH_SHA1: SHAHasher.Update( FileReadBuffer, ReadLen ); break; default: check( false ); // @TODO LSwift: Implement other storage methods! bSuccess = false; break; } DataOffset += ReadLen; } if( bSuccess ) { switch ( Header.HashType ) { case FChunkHeader::HASH_ROLLING: bSuccess = Header.RollingHash == CycPoly64Hash; break; case FChunkHeader::HASH_SHA1: SHAHasher.Final(); SHAHasher.GetHash( SHAHash.Hash ); bSuccess = SHAHash == Header.SHAHash; break; } if (!bSuccess) { GLog->Logf(TEXT("BuildPatchServices: ERROR: VerifyChunkFile file hashcheck failed")); } } break; default: GLog->Logf( TEXT( "BuildPatchServices: ERROR: VerifyChunkFile failed, unknown storage type" ) ); bSuccess = false; break; } delete[] FileReadBuffer; } } return bSuccess; }
/** * Mimics Windows WER format */ void GenerateWindowsErrorReport(const FString & WERPath, bool bReportingNonCrash) { FArchive* ReportFile = IFileManager::Get().CreateFileWriter(*WERPath); if (ReportFile != NULL) { // write BOM static uint16 ByteOrderMarker = 0xFEFF; ReportFile->Serialize(&ByteOrderMarker, sizeof(ByteOrderMarker)); WriteLine(ReportFile, TEXT("<?xml version=\"1.0\" encoding=\"UTF-16\"?>")); WriteLine(ReportFile, TEXT("<WERReportMetadata>")); WriteLine(ReportFile, TEXT("\t<OSVersionInformation>")); WriteLine(ReportFile, TEXT("\t\t<WindowsNTVersion>0.0</WindowsNTVersion>")); WriteLine(ReportFile, TEXT("\t\t<Build>No Build</Build>")); WriteLine(ReportFile, TEXT("\t\t<Product>Linux</Product>")); WriteLine(ReportFile, TEXT("\t\t<Edition>No Edition</Edition>")); WriteLine(ReportFile, TEXT("\t\t<BuildString>No BuildString</BuildString>")); WriteLine(ReportFile, TEXT("\t\t<Revision>0</Revision>")); WriteLine(ReportFile, TEXT("\t\t<Flavor>No Flavor</Flavor>")); WriteLine(ReportFile, TEXT("\t\t<Architecture>Unknown Architecture</Architecture>")); WriteLine(ReportFile, TEXT("\t\t<LCID>0</LCID>")); WriteLine(ReportFile, TEXT("\t</OSVersionInformation>")); WriteLine(ReportFile, TEXT("\t<ParentProcessInformation>")); WriteLine(ReportFile, *FString::Printf(TEXT("\t\t<ParentProcessId>%d</ParentProcessId>"), getppid())); WriteLine(ReportFile, TEXT("\t\t<ParentProcessPath>C:\\Windows\\explorer.exe</ParentProcessPath>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<ParentProcessCmdLine>C:\\Windows\\Explorer.EXE</ParentProcessCmdLine>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t</ParentProcessInformation>")); WriteLine(ReportFile, TEXT("\t<ProblemSignatures>")); WriteLine(ReportFile, TEXT("\t\t<EventType>APPCRASH</EventType>")); WriteLine(ReportFile, *FString::Printf(TEXT("\t\t<Parameter0>UE4-%s</Parameter0>"), FApp::GetGameName())); WriteLine(ReportFile, *FString::Printf(TEXT("\t\t<Parameter1>%d.%d.%d</Parameter1>"), FEngineVersion::Current().GetMajor(), FEngineVersion::Current().GetMinor(), FEngineVersion::Current().GetPatch())); WriteLine(ReportFile, TEXT("\t\t<Parameter2>0</Parameter2>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<Parameter3>Unknown Fault Module</Parameter3>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<Parameter4>0.0.0.0</Parameter4>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<Parameter5>00000000</Parameter5>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<Parameter6>00000000</Parameter6>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<Parameter7>0000000000000000</Parameter7>")); // FIXME: supply valid? WriteLine(ReportFile, *FString::Printf(TEXT("\t\t<Parameter8>!%s!</Parameter8>"), FCommandLine::Get())); // FIXME: supply valid? Only partially valid WriteLine(ReportFile, *FString::Printf(TEXT("\t\t<Parameter9>%s!%s!%s!%d</Parameter9>"), *FApp::GetBranchName(), FPlatformProcess::BaseDir(), FPlatformMisc::GetEngineMode(), FEngineVersion::Current().GetChangelist())); WriteLine(ReportFile, TEXT("\t</ProblemSignatures>")); WriteLine(ReportFile, TEXT("\t<DynamicSignatures>")); WriteLine(ReportFile, TEXT("\t\t<Parameter1>6.1.7601.2.1.0.256.48</Parameter1>")); WriteLine(ReportFile, TEXT("\t\t<Parameter2>1033</Parameter2>")); WriteLine(ReportFile, *FString::Printf(TEXT("\t\t<DeploymentName>%s</DeploymentName>"), FApp::GetDeploymentName())); WriteLine(ReportFile, *FString::Printf(TEXT("\t\t<IsEnsure>%s</IsEnsure>"), bReportingNonCrash ? TEXT("1") : TEXT("0"))); WriteLine(ReportFile, TEXT("\t</DynamicSignatures>")); WriteLine(ReportFile, TEXT("\t<SystemInformation>")); WriteLine(ReportFile, TEXT("\t\t<MID>11111111-2222-3333-4444-555555555555</MID>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<SystemManufacturer>Unknown.</SystemManufacturer>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<SystemProductName>Linux machine</SystemProductName>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t\t<BIOSVersion>A02</BIOSVersion>")); // FIXME: supply valid? WriteLine(ReportFile, TEXT("\t</SystemInformation>")); WriteLine(ReportFile, TEXT("</WERReportMetadata>")); ReportFile->Close(); delete ReportFile; } }