ue4-数据二进制存取

  • 之前不知道ue4里面自带了那么好用的数据存取的工具,所以之前把cocos项目中的二进制存取移到ue4中 传送门
  • ue4中还可以二进制数据再压缩一下,使其变得更小(不过数据文件再怎么大也大不到那里,此举有点多余,对图片来说就不一样了)

压缩存取数据

  • 测试代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    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);
    }
  • 结果
    这里写图片描述

    这里写图片描述


未压缩存取数据

  • 测试代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    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);
    }
  • 结果
    这里写图片描述


参考资料