Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
BufferWriter.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 "Serialization/Archive.h"
10#include "Containers/UnrealString.h"
11#include "Logging/LogMacros.h"
12#include "Misc/EnumClassFlags.h"
13#include "CoreGlobals.h"
14
16{
17 None = 0x0, // No Flags
18 TakeOwnership = 0x1, // Archive will take ownership of the passed-in memory and free it on destruction
19 AllowResize = 0x2, // Allow overflow by resizing buffer
20};
21
23
24/**
25* Similar to FMemoryWriter, but able to internally
26* manage the memory for the buffer.
27*/
28class FBufferWriter final : public FArchive
29{
30public:
31 /**
32 * Constructor
33 *
34 * @param Data Buffer to use as the source data to read from
35 * @param Size Size of Data
36 * @param InFlags Additional settings
37 */
39 : WriterData(Data)
40 , WriterPos(0)
41 , WriterSize(Size)
44 {
45 this->SetIsSaving(true);
46 }
47
49 {
50 Close();
51 }
52 bool Close()
53 {
54 if (bFreeOnClose)
55 {
57 WriterData = nullptr;
58 }
59 return !IsError();
60 }
61 void Serialize(void* Data, int64 Num)
62 {
63 const int64 NumBytesToAdd = WriterPos + Num - WriterSize;
64 if (NumBytesToAdd > 0)
65 {
66 if (bAllowResize)
67 {
68 const int64 NewArrayCount = WriterSize + NumBytesToAdd;
69 if (NewArrayCount >= MAX_int32)
70 {
71 UE_LOG(LogSerialization, Fatal, TEXT("FBufferWriter does not support data larger than 2GB. Archive name: %s."), *GetArchiveName());
72 }
73
75 WriterSize = NewArrayCount;
76 }
77 else
78 {
79 UE_LOG(LogSerialization, Fatal, TEXT("FBufferWriter overflowed. Archive name: %s."), *GetArchiveName());
80 }
81 }
82
83 check(WriterPos >= 0);
84 check((WriterPos + Num) <= WriterSize);
86 WriterPos += Num;
87 }
88 int64 Tell()
89 {
90 return WriterPos;
91 }
92 int64 TotalSize()
93 {
94 return WriterSize;
95 }
96 void Seek(int64 InPos)
97 {
98 check(InPos >= 0);
99 check(InPos <= WriterSize);
100 WriterPos = InPos;
101 }
102 bool AtEnd()
103 {
104 return WriterPos >= WriterSize;
105 }
106 /**
107 * Returns the name of the Archive. Useful for getting the name of the package a struct or object
108 * is in when a loading error occurs.
109 *
110 * This is overridden for the specific Archive Types
111 **/
112 virtual FString GetArchiveName() const { return TEXT("FBufferWriter"); }
113
115 {
116 return WriterData;
117 }
118protected:
124};
#define check(expr)
#define ENUM_CLASS_FLAGS(Enum)
EBufferWriterFlags
Definition Enums.h:17847
#define MAX_int32
#define TEXT(x)
Definition Platform.h:1108
int64 TotalSize()
virtual FString GetArchiveName() const
void * GetWriterData()
void Serialize(void *Data, int64 Num)
FBufferWriter(void *Data, int64 Size, EBufferWriterFlags InFlags=EBufferWriterFlags::None)
void Seek(int64 InPos)
FORCEINLINE bool IsError() const
Definition Archive.h:337
virtual void SetIsSaving(bool bInIsSaving)
static void Free(void *Original)
static void * Realloc(void *Original, SIZE_T Size, uint32 Alignment=DEFAULT_ALIGNMENT)
static FORCEINLINE void * Memcpy(void *Dest, const void *Src, SIZE_T Count)