- 之前不知道ue4里面自带了那么好用的数据存取的工具,所以之前把cocos项目中的二进制存取移到ue4中 传送门。
- ue4中还可以二进制数据再压缩一下,使其变得更小(不过数据文件再怎么大也大不到那里,此举有点多余,对图片来说就不一样了)
压缩存取数据
测试代码
void UMyBpFuncLib::TestFileWriteCompressed(FString _path) { //FPlatformFileManager::Get().SetPlatformFile(*CurrentPlatformFile); //Step 1: Variable Data -> Binary uint8 b = 1; int32 num = 123; float height = 456.789; FString dataStr = FString::Printf(TEXT("--- dataStr")); FVector tmp(11.11, 22.22, 33.33); TArray<FRotator> SaveDataRotatorArray; for (int32 i = 0; i < 10; i++) SaveDataRotatorArray.Push(FRotator(i*10, i*10, i*10)); FBufferArchive ToBinary; ToBinary << b; ToBinary << num; ToBinary << height; ToBinary << dataStr; ToBinary << tmp; //save player location to hard disk ToBinary << SaveDataRotatorArray; //Save data //FString SavePath = "C:\\mysavefileCp.save"; //No Data if (ToBinary.Num() <= 0) return; // Compress File //tmp compressed data array TArray<uint8> CompressedData; FArchiveSaveCompressedProxy Compressor = FArchiveSaveCompressedProxy(CompressedData, ECompressionFlags::COMPRESS_ZLIB); //Send entire binary array/archive to compressor Compressor << ToBinary; //send archive serialized data to binary array Compressor.Flush(); //test size FString str = FString::Printf(TEXT("--- befor Compresse:%d, after Compresse:%d"), ToBinary.Num(), CompressedData.Num()); GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Green, str); Step 2: Binary to Hard Disk - compress //vibes to file, return successful or not if (FFileHelper::SaveArrayToFile(CompressedData, *_path)) { // Free Binary Arrays Compressor.FlushCache(); CompressedData.Empty(); ToBinary.FlushCache(); ToBinary.Empty(); // Close Buffer ToBinary.Close(); return; } else { // Free Binary Arrays Compressor.FlushCache(); CompressedData.Empty(); ToBinary.FlushCache(); ToBinary.Empty(); // Close Buffer ToBinary.Close(); return; } } void UMyBpFuncLib::TestFileReadCompressed(FString _path) { //Load the Compressed data array TArray<uint8> CompressedData; if (!FFileHelper::LoadFileToArray(CompressedData, *_path)) { FString str = FString::Printf(TEXT("--- FFILEHELPER:>> Invalid File")); GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Green, str); return; } // Decompress File FArchiveLoadCompressedProxy Decompressor = FArchiveLoadCompressedProxy(CompressedData, ECompressionFlags::COMPRESS_ZLIB); //Decompression Error? if (Decompressor.GetError()) { FString str = FString::Printf(TEXT("--- FArchiveLoadCompressedProxy>> ERROR : File Was Not Compressed")); GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Green, str); return; } //Decompress FBufferArchive DecompressedBinaryArray; Decompressor << DecompressedBinaryArray; uint8 b = 0; int32 num = 0; float height = 0.f; FString dataStr(TEXT("")); FVector location; TArray<FRotator> rotatorArr; //Read the Data Retrieved by GFileManager FMemoryReader FromBinary = FMemoryReader(DecompressedBinaryArray, true); //true, free data after done FromBinary.Seek(0); FromBinary << b; FromBinary << num; FromBinary << height; FromBinary << dataStr; FromBinary << location; FromBinary << rotatorArr; CompressedData.Empty(); Decompressor.FlushCache(); FromBinary.FlushCache(); // Empty & Close Buffer DecompressedBinaryArray.Empty(); DecompressedBinaryArray.Close(); //print FString str2 = FString::Printf(TEXT("--- b:%d, num:%d, height:%f, dataStr:%s\n"), b, num, height, *dataStr); FString str3 = FString::Printf(TEXT("--- location x:%f, y:%f, z:%f\n"), location.X, location.Y, location.Z); FString str4(""); for (auto rot : rotatorArr) { FString tmp = FString::Printf(TEXT("--- rotator Pitch:%f, Yaw:%f, Roll:%f\n"), rot.Pitch, rot.Yaw, rot.Roll); str4.Append(tmp); } GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Yellow, str2 + str3 + str4); }
结果
未压缩存取数据
测试代码
void UMyBpFuncLib::TestFileWriteUnCompressed(FString _path) { //FPlatformFileManager::Get().SetPlatformFile(*CurrentPlatformFile); //Step 1: Variable Data -> Binary FBufferArchive ToBinary; uint8 b = 1; int32 num = 123; float height = 456.789; FString dataStr = FString::Printf(TEXT("--- dataStr")); FVector tmp(11.11, 22.22, 33.33); TArray<FRotator> SaveDataRotatorArray; for (int32 i = 0; i < 10; i++) SaveDataRotatorArray.Push(FRotator(i * 10, i * 10, i * 10)); ToBinary << b; ToBinary << num; ToBinary << height; ToBinary << dataStr; ToBinary << tmp; //save player location to hard disk ToBinary << SaveDataRotatorArray; //Save data //FString SavePath = "C:\\mysavefileUnCp.save"; //No Data if (ToBinary.Num() <= 0) return; //Step 2: Binary to Hard Disk if (FFileHelper::SaveArrayToFile(ToBinary, *_path)) { // Free Binary Array ToBinary.FlushCache(); ToBinary.Empty(); FString str = FString::Printf(TEXT("--- SaveFile Success")); GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Green, str); return; } // Free Binary Array ToBinary.FlushCache(); ToBinary.Empty(); } void UMyBpFuncLib::TestFileReadUnCompressed(FString _path) { //Load the data array, // you do not need to pre-initialize this array, // UE4 C++ is awesome and fills it // with whatever contents of file are, // and however many bytes that is TArray<uint8> TheBinaryArray; if (!FFileHelper::LoadFileToArray(TheBinaryArray, *_path)) { FString str = FString::Printf(TEXT("--- LoadFileToArray Fail")); GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Green, str); return; } //Testing FString str = FString::Printf(TEXT("--- Loaded File Size:%d"), TheBinaryArray.Num()); GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Green, str); //File Load Error if (TheBinaryArray.Num() <= 0) return; uint8 b = 0; int32 num = 0; float height = 0.f; FString dataStr(TEXT("")); FVector location; TArray<FRotator> rotatorArr; //Read the Data Retrieved by GFileManager FMemoryReader FromBinary = FMemoryReader(TheBinaryArray, true); //true, free data after done FromBinary.Seek(0); FromBinary << b; FromBinary << num; FromBinary << height; FromBinary << dataStr; FromBinary << location; FromBinary << rotatorArr; //Clean up FromBinary.FlushCache(); FromBinary.Close(); // Empty & Close Buffer TheBinaryArray.Empty(); //print FString str2 = FString::Printf(TEXT("--- b:%d, num:%d, height:%f, dataStr:%s\n"), b, num, height, *dataStr); FString str3 = FString::Printf(TEXT("--- location x:%f, y:%f, z:%f\n"), location.X, location.Y, location.Z); FString str4(""); for (auto rot : rotatorArr) { FString tmp = FString::Printf(TEXT("--- rotator Pitch:%f, Yaw:%f, Roll:%f\n"), rot.Pitch, rot.Yaw, rot.Roll); str4.Append(tmp); } GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Yellow, str2 + str3 + str4); }
结果