Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
MemoryReader.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 "HAL/UnrealMemory.h"
7#include "Math/UnrealMathUtility.h"
8#include "Memory/MemoryView.h"
9#include "Containers/UnrealString.h"
10#include "Serialization/MemoryArchive.h"
11#include "Containers/ArrayView.h"
12
13/**
14 * Archive for reading arbitrary data from the specified memory location
15 */
17{
18public:
19 /**
20 * Returns the name of the Archive. Useful for getting the name of the package a struct or object
21 * is in when a loading error occurs.
22 *
23 * This is overridden for the specific Archive Types
24 **/
25 virtual FString GetArchiveName() const override
26 {
27 return TEXT("FMemoryReader");
28 }
29
30 virtual int64 TotalSize() override
31 {
32 return FMath::Min((int64)Bytes.Num(), LimitSize);
33 }
34
35 void Serialize( void* Data, int64 Num )
36 {
37 if (Num && !IsError())
38 {
39 // Only serialize if we have the requested amount of data
40 if (Offset + Num <= TotalSize())
41 {
42 FMemory::Memcpy( Data, &Bytes[(int32)Offset], Num );
43 Offset += Num;
44 }
45 else
46 {
47 SetError();
48 }
49 }
50 }
51
52 explicit FMemoryReader( const TArray<uint8>& InBytes, bool bIsPersistent = false )
53 : Bytes (InBytes)
54 , LimitSize(INT64_MAX)
55 {
56 this->SetIsLoading(true);
57 this->SetIsPersistent(bIsPersistent);
58 }
59
60 /** With this method it's possible to attach data behind some serialized data. */
61 void SetLimitSize(int64 NewLimitSize)
62 {
63 LimitSize = NewLimitSize;
64 }
65
66private:
68 int64 LimitSize;
69};
70
71/**
72 * Archive for reading arbitrary data from the specified memory view
73 */
75{
76public:
77 /**
78 * Returns the name of the Archive. Useful for getting the name of the package a struct or object
79 * is in when a loading error occurs.
80 *
81 * This is overridden for the specific Archive Types
82 **/
83 virtual FString GetArchiveName() const override
84 {
85 return TEXT("FMemoryReaderView");
86 }
87
88 virtual int64 TotalSize() override
89 {
90 return FMath::Min(static_cast<int64>(Bytes.GetSize()), LimitSize);
91 }
92
93 void Serialize(void* Data, int64 Num)
94 {
95 if (Num && !IsError())
96 {
97 // Only serialize if we have the requested amount of data
98 if (Offset + Num <= TotalSize())
99 {
100 FMutableMemoryView(Data, static_cast<uint64>(Num)).CopyFrom(Bytes.Mid(Offset, Num));
101 Offset += Num;
102 }
103 else
104 {
105 SetError();
106 }
107 }
108 }
109
110 explicit FMemoryReaderView(TArrayView<const uint8> InBytes, bool bIsPersistent = false)
112 {
113 }
114
115 explicit FMemoryReaderView(FMemoryView InBytes, bool bIsPersistent = false)
116 : Bytes(InBytes)
117 , LimitSize(INT64_MAX)
118 {
119 this->SetIsLoading(true);
120 this->SetIsPersistent(bIsPersistent);
121 }
122
123 /** With this method it's possible to attach data behind some serialized data. */
124 void SetLimitSize(int64 NewLimitSize)
125 {
126 LimitSize = NewLimitSize;
127 }
128
129private:
132};
#define TEXT(x)
Definition Platform.h:1108
void SetLimitSize(int64 NewLimitSize)
FMemoryReader(const TArray< uint8 > &InBytes, bool bIsPersistent=false)
virtual int64 TotalSize() override
void Serialize(void *Data, int64 Num)
const TArray< uint8 > & Bytes
virtual FString GetArchiveName() const override
virtual int64 TotalSize() override
void SetLimitSize(int64 NewLimitSize)
void Serialize(void *Data, int64 Num)
virtual FString GetArchiveName() const override
FMemoryReaderView(TArrayView< const uint8 > InBytes, bool bIsPersistent=false)
FMemoryView Bytes
virtual void SetIsPersistent(bool bInIsPersistent)
FORCEINLINE bool IsError() const
Definition Archive.h:337
virtual void SetIsLoading(bool bInIsLoading)