UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ArchiveReplaceObjectAndStructPropertyRef.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5// HEADER_UNIT_SKIP - Internal
6
7#include "CoreMinimal.h"
9
10/*----------------------------------------------------------------------------
11 FArchiveReplaceObjectAndStructPropertyRef.
12----------------------------------------------------------------------------*/
18template <class T>
20{
21public:
31 {
32 // Note: We intentionally add the 'DelayStart' flag above; otherwise the base class ctor will use the base archive type for serialization, and our overrides won't get called.
33 // Here we're looking for whether or not the *caller* of this ctor has asked for a delayed start; in that case, we defer to the caller as to when our serialization will start.
35 {
37 }
38 }
39
40 UE_DEPRECATED(5.0, "Use version that supplies flags via enum.")
42 (
44 const TMap<T*, T*>& InReplacementMap,
46 bool bIgnoreOuterRef,
48 bool bDelayStart = false,
50 )
52 {
53 // Note: We intentionally request a delayed start above; otherwise the base class ctor will use the base archive type for serialization, and our overrides won't get called.
54 // Here we're looking for whether or not the *caller* of this ctor has asked for a delayed start; in that case, we defer to the caller as to when our serialization will start.
55 if (!bDelayStart)
56 {
58 }
59 }
60
64 virtual FArchive& operator<<(FField*& InField) override
65 {
66 if (InField)
67 {
68 // Some structs (like UFunctions in their bytecode) reference properties of another UStructs.
69 // In this case we need to inspect their owner and if it's one of the objects we want to replace,
70 // replace the entire property with the one matching on the struct we want to replace it with
71 UStruct* OldOwnerStruct = InField->GetOwner<UStruct>();
73 {
74 T* const* ReplaceWith = (T* const*)((const TMap<UObject*, UObject*>*)&this->ReplacementMap)->Find(OldOwnerStruct);
75 if (ReplaceWith)
76 {
77 // We want to replace the property's owner but since that would be even worse than replacing UObject's Outer
78 // we need to replace the entire property instead. We need to find the new property on the object we want to replace the Owner with
80 FField* ReplaceWithField = NewOwnerStruct ? NewOwnerStruct->FindPropertyByName(InField->GetFName()) : nullptr;
81 // Do we need to verify the existence of ReplaceWithField? Theoretically it could be missing on the new version
82 // of the owner struct and in this case we still don't want to keep the stale old property pointer around so it's safer to null it
85 {
86 this->ReplacedReferences.FindOrAdd(OldOwnerStruct).AddUnique(this->GetSerializedProperty());
87 }
88 this->Count++;
89 }
90 // A->IsIn(A) returns false, but we don't want to NULL that reference out, so extra check here.
91 else if (OldOwnerStruct == this->SearchObject || OldOwnerStruct->IsIn(this->SearchObject))
92 {
93 bool bAlreadyAdded = false;
94 this->SerializedObjects.Add(OldOwnerStruct, &bAlreadyAdded);
95 if (!bAlreadyAdded)
96 {
97 // No recursion
98 this->PendingSerializationObjects.Add(OldOwnerStruct);
99 }
100 }
101 else if (this->bNullPrivateReferences && !OldOwnerStruct->HasAnyFlags(RF_Public))
102 {
103 checkf(false, TEXT("Can't null a reference to %s on property %s as it would be equivalent to nulling UObject's Outer."),
104 *OldOwnerStruct->GetPathName(), *InField->GetName());
105 }
106 }
107 else
108 {
109 // Just serialize the field to find any UObjects it may be referencing that we want to replace
110 InField->Serialize(*this);
111 }
112 }
113 return *this;
114 }
115};
116
117
EArchiveReplaceObjectFlags
Definition ArchiveReplaceObjectRef.h:133
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
@ RF_Public
Object is visible outside its package.
Definition ObjectMacros.h:559
Definition ArchiveReplaceObjectAndStructPropertyRef.h:20
virtual FArchive & operator<<(FField *&InField) override
Definition ArchiveReplaceObjectAndStructPropertyRef.h:64
FArchiveReplaceObjectAndStructPropertyRef(UObject *InSearchObject, const TMap< T *, T * > &InReplacementMap, EArchiveReplaceObjectFlags Flags=EArchiveReplaceObjectFlags::None)
Definition ArchiveReplaceObjectAndStructPropertyRef.h:29
bool bTrackReplacedReferences
Definition ArchiveReplaceObjectRef.h:118
TMap< UObject *, TArray< FProperty * > > ReplacedReferences
Definition ArchiveReplaceObjectRef.h:115
TArray< UObject * > PendingSerializationObjects
Definition ArchiveReplaceObjectRef.h:112
TSet< UObject * > SerializedObjects
Definition ArchiveReplaceObjectRef.h:109
bool bNullPrivateReferences
Definition ArchiveReplaceObjectRef.h:124
UObject * SearchObject
Definition ArchiveReplaceObjectRef.h:100
int32 Count
Definition ArchiveReplaceObjectRef.h:106
Definition ArchiveReplaceObjectRef.h:169
const TMap< T *, T * > & ReplacementMap
Definition ArchiveReplaceObjectRef.h:319
void SerializeSearchObject()
Definition ArchiveReplaceObjectRef.h:222
Definition Archive.h:1208
UE_FORCEINLINE_HINT class FProperty * GetSerializedProperty() const
Definition Archive.h:748
Definition Field.h:556
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
Definition UnrealString.h.inl:34
COREUOBJECT_API bool IsIn(const UObject *SomeOuter) const
Definition UObjectBaseUtility.cpp:344
bool HasAnyFlags(EObjectFlags FlagsToCheck) const
Definition UObjectBaseUtility.h:93
Definition Object.h:95
Definition Class.h:480
@ false
Definition radaudio_common.h:23