ue4-数据二进制存取

  • 之前不知道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);
    }
  • 结果
    这里写图片描述


参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝶泳奈何桥.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值