Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
MemoryWriter.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Misc/AssertionMacros.h"
7#include "HAL/UnrealMemory.h"
8#include "Math/NumericLimits.h"
9#include "Containers/UnrealString.h"
10#include "UObject/NameTypes.h"
11#include "Logging/LogMacros.h"
12#include "CoreGlobals.h"
13#include "Serialization/MemoryArchive.h"
14
15/**
16 * Archive for storing arbitrary data to the specified memory location
17 */
18template <int IndexSize>
20{
21 static_assert(IndexSize == 32 || IndexSize == 64, "Only 32-bit and 64-bit index sizes supported");
22 using IndexSizeType = typename TBitsToSizeType<IndexSize>::Type;
23
24public:
25 TMemoryWriter( TArray<uint8, TSizedDefaultAllocator<IndexSize>>& InBytes, bool bIsPersistent = false, bool bSetOffset = false, const FName InArchiveName = NAME_None )
27 , Bytes(InBytes)
29 {
30 this->SetIsSaving(true);
32 if (bSetOffset)
33 {
34 Offset = InBytes.Num();
35 }
36 }
37
38 virtual void Serialize(void* Data, int64 Num) override
39 {
40 const int64 NumBytesToAdd = Offset + Num - Bytes.Num();
41 if( NumBytesToAdd > 0 )
42 {
44 if constexpr (IndexSize == 32)
45 {
47 {
48 UE_LOG(LogSerialization, Fatal, TEXT("FMemoryWriter does not support data larger than 2GB. Archive name: %s."), *ArchiveName.ToString());
49 }
50 }
51
53 }
54
55 check((Offset + Num) <= Bytes.Num());
56
57 if( Num )
58 {
60 Offset += Num;
61 }
62 }
63 /**
64 * Returns the name of the Archive. Useful for getting the name of the package a struct or object
65 * is in when a loading error occurs.
66 *
67 * This is overridden for the specific Archive Types
68 **/
69 virtual FString GetArchiveName() const override
70 {
72 {
73 return ArchiveName.ToString();
74 }
75
76 if constexpr (IndexSize == 64)
77 {
78 return TEXT("FMemoryWriter64");
79 }
80 else
81 {
82 return TEXT("FMemoryWriter");
83 }
84 }
85
86 int64 TotalSize() override
87 {
88 return Bytes.Num();
89 }
90
91protected:
92
93 TArray<uint8, TSizedDefaultAllocator<IndexSize>>& Bytes;
94
95 /** Archive name, used to debugging, by default set to NAME_None. */
97};
98
99
100// FMemoryWriter and FMemoryWriter64 are implemented as derived classes rather than aliases
101// so that forward declarations will work.
102
103class FMemoryWriter : public TMemoryWriter<32>
104{
105 using Super = TMemoryWriter<32>;
106
107public:
108 using Super::Super;
109};
110
112{
113 using Super = TMemoryWriter<64>;
114
115public:
116 using Super::Super;
117};
#define check(expr)
#define MAX_int32
#define TEXT(x)
Definition Platform.h:1108
const FName ArchiveName
int64 TotalSize() override
TArray< uint8, TSizedDefaultAllocator< IndexSize > > & Bytes
virtual FString GetArchiveName() const override
virtual void Serialize(void *Data, int64 Num) override
TMemoryWriter(TArray< uint8, TSizedDefaultAllocator< IndexSize > > &InBytes, bool bIsPersistent=false, bool bSetOffset=false, const FName InArchiveName=NAME_None)