Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
MappedName.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "Containers/ArrayView.h"
7#include "Containers/ContainersFwd.h"
8#include "CoreMinimal.h"
9#include "HAL/Platform.h"
10#include "Math/NumericLimits.h"
11#include "Misc/AssertionMacros.h"
12#include "UObject/NameBatchSerialization.h"
13#include "UObject/NameTypes.h"
14#include "UObject/UnrealNames.h"
15
16class FArchive;
17
18/**
19 * Index and name number into a name map.
20 */
22{
23 static constexpr uint32 InvalidIndex = ~uint32(0);
24 static constexpr uint32 IndexBits = 30u;
25 static constexpr uint32 IndexMask = (1u << IndexBits) - 1u;
26 static constexpr uint32 TypeMask = ~IndexMask;
27 static constexpr uint32 TypeShift = IndexBits;
28
29public:
30 enum class EType
31 {
32 Package,
34 Global
35 };
36
37 inline FMappedName() = default;
38
39 static inline FMappedName Create(const uint32 InIndex, const uint32 InNumber, EType InType)
40 {
41 check(InIndex <= MAX_int32);
42 return FMappedName((uint32(InType) << TypeShift) | InIndex, InNumber);
43 }
44
45 static inline FMappedName FromMinimalName(const FMinimalName& MinimalName)
46 {
47 return *reinterpret_cast<const FMappedName*>(&MinimalName);
48 }
49
50 static inline bool IsResolvedToMinimalName(const FMinimalName& MinimalName)
51 {
52 // Not completely safe, relies on that no FName will have its Index and Number equal to Max_uint32
53 const FMappedName MappedName = FromMinimalName(MinimalName);
54 return MappedName.IsValid();
55 }
56
57 static inline FName SafeMinimalNameToName(const FMinimalName& MinimalName)
58 {
59 return IsResolvedToMinimalName(MinimalName) ? MinimalNameToName(MinimalName) : NAME_None;
60 }
61
63 {
64 return *reinterpret_cast<const FMinimalName*>(this);
65 }
66
67 inline bool IsValid() const
68 {
69 return Index != InvalidIndex && Number != InvalidIndex;
70 }
71
72 inline EType GetType() const
73 {
74 return static_cast<EType>(uint32((Index & TypeMask) >> TypeShift));
75 }
76
77 inline bool IsGlobal() const
78 {
79 return ((Index & TypeMask) >> TypeShift) != 0;
80 }
81
82 inline uint32 GetIndex() const
83 {
84 return Index & IndexMask;
85 }
86
87 inline uint32 GetNumber() const
88 {
89 return Number;
90 }
91
92 inline bool operator!=(FMappedName Other) const
93 {
94 return Index != Other.Index || Number != Other.Number;
95 }
96
97 FName ResolveName(TConstArrayView<FDisplayNameEntryId> Names) const
98 {
99 return Names[GetIndex()].ToName(GetNumber());
100 }
101
102 friend FArchive& operator<<(FArchive& Ar, FMappedName& MappedName);
103
104private:
105 inline FMappedName(const uint32 InIndex, const uint32 InNumber)
106 : Index(InIndex)
107 , Number(InNumber) { }
108
111};
112
113/*
114 * Maps serialized name entries to names.
115 */
117{
118public:
119 inline int32 Num() const
120 {
121 return NameEntries.Num();
122 }
123
124 void Load(FArchive& Ar, FMappedName::EType NameMapType);
125
126 FName GetName(const FMappedName& MappedName) const
127 {
128 check(MappedName.GetType() == NameMapType);
129 check(MappedName.GetIndex() < uint32(NameEntries.Num()));
130
131 return NameEntries[MappedName.GetIndex()].ToName(MappedName.GetNumber());
132 }
133
134 bool TryGetName(const FMappedName& MappedName, FName& OutName) const
135 {
136 check(MappedName.GetType() == NameMapType);
137
138 uint32 Index = MappedName.GetIndex();
139 if (Index < uint32(NameEntries.Num()))
140 {
141 OutName = NameEntries[MappedName.GetIndex()].ToName(MappedName.GetNumber());
142
143 return true;
144 }
145
146 return false;
147 }
148
149 FMinimalName GetMinimalName(const FMappedName& MappedName) const
150 {
151 // Wasteful, looks up display name then discards it
152 return NameToMinimalName(GetName(MappedName));
153 }
154
155private:
158};
#define check(expr)
FORCEINLINE FName MinimalNameToName(FMinimalName InName)
Definition NameTypes.h:1648
#define MAX_int32
static constexpr uint32 TypeMask
Definition MappedName.h:26
bool operator!=(FMappedName Other) const
Definition MappedName.h:92
static FMappedName FromMinimalName(const FMinimalName &MinimalName)
Definition MappedName.h:45
static constexpr uint32 TypeShift
Definition MappedName.h:27
FMappedName(const uint32 InIndex, const uint32 InNumber)
Definition MappedName.h:105
static bool IsResolvedToMinimalName(const FMinimalName &MinimalName)
Definition MappedName.h:50
FMinimalName ToUnresolvedMinimalName() const
Definition MappedName.h:62
FMappedName()=default
static FName SafeMinimalNameToName(const FMinimalName &MinimalName)
Definition MappedName.h:57
FName ResolveName(TConstArrayView< FDisplayNameEntryId > Names) const
Definition MappedName.h:97
uint32 Number
Definition MappedName.h:110
bool IsGlobal() const
Definition MappedName.h:77
static constexpr uint32 InvalidIndex
Definition MappedName.h:23
static constexpr uint32 IndexBits
Definition MappedName.h:24
uint32 Index
Definition MappedName.h:109
EType GetType() const
Definition MappedName.h:72
bool IsValid() const
Definition MappedName.h:67
uint32 GetNumber() const
Definition MappedName.h:87
static FMappedName Create(const uint32 InIndex, const uint32 InNumber, EType InType)
Definition MappedName.h:39
uint32 GetIndex() const
Definition MappedName.h:82
static constexpr uint32 IndexMask
Definition MappedName.h:25
friend FMinimalName NameToMinimalName(FName InName)
Definition NameTypes.h:1658
FMinimalName GetMinimalName(const FMappedName &MappedName) const
Definition MappedName.h:149
FName GetName(const FMappedName &MappedName) const
Definition MappedName.h:126
int32 Num() const
Definition MappedName.h:119
void Load(FArchive &Ar, FMappedName::EType NameMapType)
bool TryGetName(const FMappedName &MappedName, FName &OutName) const
Definition MappedName.h:134
TArray< FDisplayNameEntryId > NameEntries
Definition MappedName.h:156
FMappedName::EType NameMapType
Definition MappedName.h:157