UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
EditorBulkDataWriter.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6
7#if WITH_EDITORONLY_DATA
8
9namespace UE::Serialization
10{
11
13{
14public:
15 FEditorBulkDataWriter(FEditorBulkData& InBulkData, bool bIsPersistent = false)
17 {
18 SetIsSaving(true);
19 SetIsPersistent(bIsPersistent);
20
21 FSharedBuffer Payload = InBulkData.GetPayload().Get();
22
23 if (Payload)
24 {
25 const int64 CurrentDataLength = Payload.GetSize();
26
27 // Clone the payload so that we have a local copy that we can
28 // append additional data to.
29 Buffer = FMemory::Malloc(CurrentDataLength, DEFAULT_ALIGNMENT);
31
32 BufferLength = CurrentDataLength;
33
34 // Start at the end of the existing data
35 CurPos = CurrentDataLength;
36 DataLength = CurrentDataLength;
37 }
38 else
39 {
40 Buffer = nullptr;
41 BufferLength = 0;
42
43 CurPos = 0;
44 DataLength = 0;
45 }
46 }
47
49 {
50 // Remove the slack from the allocated bulk data
51 Buffer = FMemory::Realloc(Buffer, DataLength, DEFAULT_ALIGNMENT);
52 BulkData.UpdatePayload(FSharedBuffer::TakeOwnership(Buffer, DataLength, FMemory::Free));
53 }
54
56 bool IsValid() const
57 {
58 return Buffer != nullptr;
59 }
60
61 virtual void Serialize(void* Data, int64 Num)
62 {
63 // Determine if we need to reallocate the buffer to fit the next item
64 const int64 NewPos = CurPos + Num;
65 checkf(NewPos >= CurPos, TEXT("Serialization has somehow gone backwards"));
66
67 if (NewPos > BufferLength)
68 {
69 // If so, resize to the new size + 3/8 additional slack
70 const int64 NewLength = NewPos + 3 * NewPos / 8 + 16;
71 Buffer = FMemory::Realloc(Buffer, NewLength, DEFAULT_ALIGNMENT);
72 BufferLength = NewLength;
73 }
74
75 FMemory::Memcpy(static_cast<unsigned char*>(Buffer) + CurPos, Data, Num);
76
77 CurPos += Num;
78 DataLength = FMath::Max(DataLength, CurPos);
79 }
80
81 using FArchive::operator<<; // For visibility of the overloads we don't override
82
83 virtual FArchive& operator<<(class FName& Name) override
84 {
85 // FNames are serialized as strings in BulkData
86 FString StringName = Name.ToString();
87 *this << StringName;
88 return *this;
89 }
90
91 virtual int64 Tell() { return CurPos; }
92 virtual int64 TotalSize() { return DataLength; }
93
94 virtual void Seek(int64 InPos)
95 {
96 check(InPos >= 0);
97 check(InPos <= DataLength);
98 CurPos = InPos;
99 }
100
101 virtual bool AtEnd()
102 {
103 return CurPos >= DataLength;
104 }
105
106 virtual FString GetArchiveName() const
107 {
108 return TEXT("FEditorBulkDataWriter");
109 }
110
111protected:
113 FEditorBulkData& BulkData;
114
116 void* Buffer;
118 int64 BufferLength;
119
121 int64 CurPos;
122
124 int64 DataLength;
125};
126
127} // namespace UE::Serialization
128
129#endif //WITH_EDITORONLY_DATA
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
FArchive & operator<<(FArchive &Ar, FEnvQueryDebugProfileData::FStep &Data)
Definition EnvQueryTypes.cpp:489
@ DEFAULT_ALIGNMENT
Definition MemoryBase.h:24
@ Num
Definition MetalRHIPrivate.h:234
Definition Archive.h:1208
Definition NameTypes.h:617
Definition SharedBuffer.h:341
uint64 GetSize() const
Definition SharedBuffer.h:388
const void * GetData() const
Definition SharedBuffer.h:385
static FSharedBuffer TakeOwnership(const void *Data, uint64 Size, DeleteFunctionType &&DeleteFunction)
Definition SharedBuffer.h:569
Definition StructuredLog.cpp:42
static FORCENOINLINE CORE_API void Free(void *Original)
Definition UnrealMemory.cpp:685
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160