Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Compression.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Map.h"
6#include "Containers/UnrealString.h"
7#include "CoreTypes.h"
8#include "HAL/CriticalSection.h"
9#include "Misc/CompressionFlags.h"
10#include "Templates/Atomic.h"
11#include "UObject/NameTypes.h"
12
14template <typename T> class TAtomic;
15
16// Define global current platform default to current platform.
17// DEPRECATED, USE NAME_Zlib
18#define COMPRESS_Default COMPRESS_ZLIB
19
20/**
21 * Chunk size serialization code splits data into. The loading value CANNOT be changed without resaving all
22 * compressed data which is why they are split into two separate defines.
23 */
24#define LOADING_COMPRESSION_CHUNK_SIZE_PRE_369 32768
25#define LOADING_COMPRESSION_CHUNK_SIZE 131072
26#define SAVING_COMPRESSION_CHUNK_SIZE LOADING_COMPRESSION_CHUNK_SIZE
27
29{
30 /** Time spent compressing data in cycles. */
31 static TAtomic<uint64> CompressorTimeCycles;
32 /** Number of bytes before compression. */
33 static TAtomic<uint64> CompressorSrcBytes;
34 /** Number of bytes after compression. */
35 static TAtomic<uint64> CompressorDstBytes;
36
37
38 /**
39 * Returns a version number for a specified format
40 *
41 * @param FormatName Compressor format name (eg NAME_Zlib)
42 * @return An interpretation of an internal version number for a specific format (different formats will have different layouts) this should change if a version is updated
43 */
44 static uint32 GetCompressorVersion(FName FormatName);
45
46 /**
47 * Thread-safe abstract compression routine to query memory requirements for a compression operation.
48 * This is the minimize size to allocate the buffer for CompressMemory (encoding).
49 * Use GetMaximumCompressedSize at decode to know how large a compressed buffer may be.
50 *
51 * @param FormatName Name of the compression format
52 * @param UncompressedSize Size of uncompressed data in bytes
53 * @param Flags Flags to control what method to use and optionally control memory vs speed
54 * @param CompressionData Additional compression parameter (specifies BitWindow value for ZLIB compression format)
55 * @return The maximum possible bytes needed for compression of data buffer of size UncompressedSize
56 */
57 static int32 CompressMemoryBound(FName FormatName, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
58
59 /**
60 * Thread-safe abstract compression routine to query maximum compressed size that could be made.
61 * CompressMemoryBound is strictly greater equal GetMaximumCompressedSize.
62 *
63 * @param FormatName Name of the compression format
64 * @param UncompressedSize Size of uncompressed data in bytes
65 * @param Flags Flags to control what method to use and optionally control memory vs speed
66 * @param CompressionData Additional compression parameter (specifies BitWindow value for ZLIB compression format)
67 * @return The maximum possible size of valid compressed data made by this format
68 */
69 static int32 GetMaximumCompressedSize(FName FormatName, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
70
71 /**
72 * Thread-safe abstract compression routine. Compresses memory from uncompressed buffer and writes it to compressed
73 * buffer. Updates CompressedSize with size of compressed data. Compression controlled by the passed in flags.
74 * CompressMemory is expected to return true and write valid data even if it expanded bytes.
75 * Always check CompressedSize >= UncompressedSize and fall back to uncompressed, or use CompressMemoryIfWorthDecompressing
76 *
77 * @param FormatName Name of the compression format
78 * @param CompressedBuffer Buffer compressed data is going to be written to
79 * @param CompressedSize [in/out] Size of CompressedBuffer, at exit will be size of compressed data
80 * @param UncompressedBuffer Buffer containing uncompressed data
81 * @param UncompressedSize Size of uncompressed data in bytes
82 * @param Flags Flags to control what method to use and optionally control memory vs speed
83 * @param CompressionData Additional compression parameter (specifies BitWindow value for ZLIB compression format)
84 * @return true if compression succeeds, false if it fails because CompressedBuffer was too small or other reasons
85 */
86 static bool CompressMemory(FName FormatName, void* CompressedBuffer, int32& CompressedSize, const void* UncompressedBuffer, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
87
88 /**
89 * Same as CompressMemory but evaluates if the compression gain is worth the runtime decode time
90 * returns false if the size saving is not worth it (also if CompressedSize >= UncompressedSize)
91 * if false is returned, send the data uncompressed instead
92 *
93 * @param FormatName Name of the compression format
94 * @param MinBytesSaved Minimum amount of bytes which should be saved when performing compression, otherwise false is returned
95 * @param MinPercentSaved Minimum percentage of the buffer which should be saved when performing compression, otherwise false is returned
96 * @param CompressedBuffer Buffer compressed data is going to be written to
97 * @param CompressedSize [in/out] Size of CompressedBuffer, at exit will be size of compressed data
98 * @param UncompressedBuffer Buffer containing uncompressed data
99 * @param UncompressedSize Size of uncompressed data in bytes
100 * @param Flags Flags to control what method to use and optionally control memory vs speed
101 * @param CompressionData Additional compression parameter (specifies BitWindow value for ZLIB compression format)
102 * @return true if compression succeeds, false if it fails because CompressedBuffer was too small or other reasons
103 */
104 static bool CompressMemoryIfWorthDecompressing(FName FormatName, int32 MinBytesSaved, int32 MinPercentSaved, void* CompressedBuffer, int32& CompressedSize, const void* UncompressedBuffer, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
105
106 /**
107 * Thread-safe abstract decompression routine. Uncompresses memory from compressed buffer and writes it to uncompressed
108 * buffer. UncompressedSize is expected to be the exact size of the data after decompression.
109 *
110 * @param FormatName Name of the compression format
111 * @param UncompressedBuffer Buffer containing uncompressed data
112 * @param UncompressedSize Size of uncompressed data in bytes
113 * @param CompressedBuffer Buffer compressed data is going to be read from
114 * @param CompressedSize Size of CompressedBuffer data in bytes
115 * @param Flags Flags to control what method to use to decompress
116 * @param CompressionData Additional decompression parameter (specifies BitWindow value for ZLIB compression format)
117 * @return true if compression succeeds, false if it fails because CompressedBuffer was too small or other reasons
118 */
119 static bool UncompressMemory(FName FormatName, void* UncompressedBuffer, int32 UncompressedSize, const void* CompressedBuffer, int32 CompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0);
120
121 static bool UncompressMemoryStream(FName FormatName, void* UncompressedBuffer, int32 UncompressedSize, IMemoryReadStream* Stream, int64 StreamOffset, int32 CompressedSize, ECompressionFlags Flags = COMPRESS_NoFlags, int32 CompressionData = 0);
122 /**
123 * Returns a string which can be used to identify if a format has become out of date
124 *
125 * @param FormatName name of the format to retrieve the DDC suffix for
126 * @return unique DDC key string which will be different when the format is changed / updated
127 */
129
130
131 /**
132 * Checks to see if a format will be usable, so that a fallback can be used
133 * @param FormatName The name of the format to test
134 */
135 static bool IsFormatValid(FName FormatName);
136
137 /**
138 * Verifies if the passed in value represents valid compression flags
139 * @param InCompressionFlags Value to test
140 */
141 static bool VerifyCompressionFlagsValid(int32 InCompressionFlags);
142
144
145private:
146
147 /**
148 * Find a compression format module by name, returning nullptr if no module found
149 */
150 static struct ICompressionFormat* GetCompressionFormat(FName Method, bool bErrorOnFailure=true);
151
152 /** Mapping of Compression FNames to their compressor objects */
155
156};
#define LOADING_COMPRESSION_CHUNK_SIZE
Definition Compression.h:25
ECompressionFlags
@ COMPRESS_NoFlags
FWindowsCriticalSection FCriticalSection
static bool UncompressMemoryStream(FName FormatName, void *UncompressedBuffer, int32 UncompressedSize, IMemoryReadStream *Stream, int64 StreamOffset, int32 CompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0)
static FName GetCompressionFormatFromDeprecatedFlags(ECompressionFlags DeprecatedFlags)
static struct ICompressionFormat * GetCompressionFormat(FName Method, bool bErrorOnFailure=true)
static int32 GetMaximumCompressedSize(FName FormatName, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0)
static int32 CompressMemoryBound(FName FormatName, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0)
static bool VerifyCompressionFlagsValid(int32 InCompressionFlags)
static TAtomic< uint64 > CompressorDstBytes
Definition Compression.h:35
static FString GetCompressorDDCSuffix(FName FormatName)
static bool UncompressMemory(FName FormatName, void *UncompressedBuffer, int32 UncompressedSize, const void *CompressedBuffer, int32 CompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0)
static FCriticalSection CompressionFormatsCriticalSection
static bool IsFormatValid(FName FormatName)
static TAtomic< uint64 > CompressorTimeCycles
Definition Compression.h:31
static bool CompressMemoryIfWorthDecompressing(FName FormatName, int32 MinBytesSaved, int32 MinPercentSaved, void *CompressedBuffer, int32 &CompressedSize, const void *UncompressedBuffer, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0)
static TAtomic< uint64 > CompressorSrcBytes
Definition Compression.h:33
static uint32 GetCompressorVersion(FName FormatName)
static TMap< FName, struct ICompressionFormat * > CompressionFormats
static bool CompressMemory(FName FormatName, void *CompressedBuffer, int32 &CompressedSize, const void *UncompressedBuffer, int32 UncompressedSize, ECompressionFlags Flags=COMPRESS_NoFlags, int32 CompressionData=0)