A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
Serializer.h
Go to the documentation of this file.
1// Copyright(c) PixoVR, LLC. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Serialization/MemoryReader.h"
7#include "Serialization/ObjectAndNameAsStringProxyArchive.h"
8#include "Async/Async.h"
9#include "Serialization/ArchiveLoadCompressedProxy.h"
10#include "Serialization/ArchiveSaveCompressedProxy.h"
11#include "Serializer.generated.h"
12
13USTRUCT()
14struct PIXOCORE_API FSerializedModel
15{
16 GENERATED_BODY()
17
18 UPROPERTY() UClass* Class;
19 UPROPERTY() TArray<uint8> Data;
20 UPROPERTY() bool bCompressed = false;
21};
23
24namespace
26 namespace BinarySerializer
27 {
30 {
31 FSerializedModel Model;
32 Model.Class = Object->GetClass();
33 FMemoryWriter MemoryWriter(Model.Data, true);
34 FObjectAndNameAsStringProxyArchive Ar(MemoryWriter, true);
35 Object->Serialize(Ar);
36
37 return Model;
38 }
39
40 FSerializedModel SerializeCompressed(UObject* Object)
41 {
42 FSerializedModel Model = Serialize(Object);
43 TArray<uint8> SerializedData = MoveTemp(Model.Data);
44 FArchiveSaveCompressedProxy Compressor(Model.Data, NAME_LZ4, COMPRESS_BiasSpeed);
45 Compressor << SerializedData;
46 Compressor.Flush();
47 Model.bCompressed = true;
49 return Model;
50 }
51
52 template<typename T, typename TCheck = typename std::enable_if<std::is_base_of<UObject, T>::value, void*>::type>
53 T* Deserialize(const FSerializedModel& Model, UObject* Outer)
54 {
55 T* Instance = NewObject<T>(Outer, Model.Class);
56 FMemoryReader MemoryReader(Model.Data, true);
57 FObjectAndNameAsStringProxyArchive Ar(MemoryReader, true);
58 Instance->Serialize(Ar);
59
60 return Instance;
61 }
62
63 template<typename T, typename TCheck = typename std::enable_if<std::is_base_of<UObject, T>::value, void*>::type>
64 T* DeserializeCompressed(const FSerializedModel& Model, UObject* Outer)
65 {
66 TArray<uint8> DecompressedData;
67 FArchiveLoadCompressedProxy MemoryReader(Model.Data, NAME_LZ4);
68 MemoryReader << DecompressedData;
69 MemoryReader.Flush();
70
71 const FSerializedModel DecompressedModel = { Model.Class, MoveTemp(DecompressedData) };
73 return Deserialize<T>(DecompressedModel, Outer);
74 }
75
76 void SerializeAsync(UObject* Object, TFunction<void(FSerializedModel&)> Callback, bool Compress = false)
77 {
78 Async(EAsyncExecution::TaskGraph, [Object, Callback, Compress]()
79 {
80 FSerializedModel Model = Compress ? SerializeCompressed(Object) : Serialize(Object);
81 Async(EAsyncExecution::TaskGraphMainThread,[Callback, Model = MoveTemp(Model)]() mutable
82 {
83 Callback(Model);
84 });
85 });
86 }
87
88 template<typename T, typename TCheck = typename std::enable_if<std::is_base_of<UObject, T>::value, void*>::type>
89 void DeserializeAsync(const FSerializedModel& Model, UObject* Outer, TFunction<void(T*)> Callback)
90 {
91 Async(EAsyncExecution::TaskGraph, [Model, Outer, Callback]()
92 {
93 T* Instance = Model.bCompressed ? DeserializeCompressed<T>(Model, Outer) : Deserialize<T>(Model, Outer);
94 Async(EAsyncExecution::TaskGraphMainThread, [Callback, Instance]()
95 {
96 Callback(Instance);
97 Instance->ClearInternalFlags(EInternalObjectFlags::Async);
98 });
99 });
100 }
101 };
102};
void SerializeAsync(UObject *Object, TFunction< void(FSerializedModel &)> Callback, bool Compress=false)
Definition Serializer.h:84
T * Deserialize(const FSerializedModel &Model, UObject *Outer)
Definition Serializer.h:61
T * DeserializeCompressed(const FSerializedModel &Model, UObject *Outer)
Definition Serializer.h:72
FSerializedModel SerializeCompressed(UObject *Object)
Definition Serializer.h:48
void DeserializeAsync(const FSerializedModel &Model, UObject *Outer, TFunction< void(T *)> Callback)
Definition Serializer.h:97
FSerializedModel Serialize(UObject *Object)
Definition Serializer.h:37
TArray< uint8 > Data
UPROPERTY()
Definition Serializer.h:25
UClass * Class
UPROPERTY()
Definition Serializer.h:22
bool bCompressed
UPROPERTY()
Definition Serializer.h:28