-
Notifications
You must be signed in to change notification settings - Fork 142
/
UnrealMetadata.h
128 lines (102 loc) · 3.42 KB
/
UnrealMetadata.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Improbable Worlds Ltd, All Rights Reserved
#pragma once
#include "GameFramework/Actor.h"
#include "Interop/SpatialClassInfoManager.h"
#include "Schema/Component.h"
#include "Schema/UnrealObjectRef.h"
#include "SpatialConstants.h"
#include "UObject/Package.h"
#include "UObject/UObjectHash.h"
#include "Utils/SchemaUtils.h"
#include <WorkerSDK/improbable/c_schema.h>
#include <WorkerSDK/improbable/c_worker.h>
using SubobjectToOffsetMap = TMap<UObject*, uint32>;
namespace SpatialGDK
{
struct UnrealMetadata : Component
{
static const Worker_ComponentId ComponentId = SpatialConstants::UNREAL_METADATA_COMPONENT_ID;
UnrealMetadata() = default;
UnrealMetadata(const TSchemaOption<FUnrealObjectRef>& InStablyNamedRef, const FString& InOwnerWorkerAttribute, const FString& InClassPath, const TSchemaOption<bool>& InbNetStartup)
: StablyNamedRef(InStablyNamedRef), OwnerWorkerAttribute(InOwnerWorkerAttribute), ClassPath(InClassPath), bNetStartup(InbNetStartup) {}
UnrealMetadata(const Worker_ComponentData& Data)
{
Schema_Object* ComponentObject = Schema_GetComponentDataFields(Data.schema_type);
if (Schema_GetObjectCount(ComponentObject, 1) == 1)
{
StablyNamedRef = GetObjectRefFromSchema(ComponentObject, 1);
}
OwnerWorkerAttribute = GetStringFromSchema(ComponentObject, 2);
ClassPath = GetStringFromSchema(ComponentObject, 3);
if (Schema_GetBoolCount(ComponentObject, 4) == 1)
{
bNetStartup = GetBoolFromSchema(ComponentObject, 4);
}
}
Worker_ComponentData CreateUnrealMetadataData()
{
Worker_ComponentData Data = {};
Data.component_id = ComponentId;
Data.schema_type = Schema_CreateComponentData(ComponentId);
Schema_Object* ComponentObject = Schema_GetComponentDataFields(Data.schema_type);
if (StablyNamedRef.IsSet())
{
AddObjectRefToSchema(ComponentObject, 1, StablyNamedRef.GetValue());
}
AddStringToSchema(ComponentObject, 2, OwnerWorkerAttribute);
AddStringToSchema(ComponentObject, 3, ClassPath);
if (bNetStartup.IsSet())
{
Schema_AddBool(ComponentObject, 4, bNetStartup.GetValue());
}
return Data;
}
FORCEINLINE UClass* GetNativeEntityClass()
{
if (NativeClass.IsValid())
{
return NativeClass.Get();
}
#if !UE_BUILD_SHIPPING
if (NativeClass.IsStale())
{
UE_LOG(LogSpatialClassInfoManager, Warning, TEXT("UnrealMetadata native class %s unloaded whilst entity in view."), *ClassPath);
}
#endif
UClass* Class = nullptr;
if (StablyNamedRef.IsSet())
{
Class = FindObject<UClass>(nullptr, *ClassPath, false);
}
else
{
Class = LoadObject<UClass>(nullptr, *ClassPath);
}
if (Class != nullptr && Class->IsChildOf<AActor>())
{
NativeClass = Class;
return Class;
}
return nullptr;
}
TSchemaOption<FUnrealObjectRef> StablyNamedRef;
FString OwnerWorkerAttribute;
FString ClassPath;
TSchemaOption<bool> bNetStartup;
TWeakObjectPtr<UClass> NativeClass;
};
FORCEINLINE SubobjectToOffsetMap CreateOffsetMapFromActor(AActor* Actor, const FClassInfo& Info)
{
SubobjectToOffsetMap SubobjectNameToOffset;
for (auto& SubobjectInfoPair : Info.SubobjectInfo)
{
UObject* Subobject = StaticFindObjectFast(UObject::StaticClass(), Actor, SubobjectInfoPair.Value->SubobjectName);
uint32 Offset = SubobjectInfoPair.Key;
if (Subobject != nullptr && Subobject->IsPendingKill() == false && Subobject->IsSupportedForNetworking())
{
SubobjectNameToOffset.Add(Subobject, Offset);
}
}
return SubobjectNameToOffset;
}
} // namespace SpatialGDK