/
ST_WorldSubsystem.cpp
258 lines (214 loc) · 7.18 KB
/
ST_WorldSubsystem.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) James Baxter. All Rights Reserved.
#include "ST_WorldSubsystem.h"
// Engine
#include "Engine/World.h"
#include "GameMapsSettings.h"
#if WITH_EDITOR
#include "Editor.h"
#include "Engine/NetDriver.h"
#endif
/////////////////////////
///// Tick Function /////
/////////////////////////
void FST_WorldSubsystemTickFunction::ExecuteTick(float DeltaTime, ELevelTick TickType, ENamedThreads::Type CurrentThread, const FGraphEventRef& MyCompletionGraphEvent)
{
check(Target);
Target->TickSubsystem(DeltaTime);
}
FString FST_WorldSubsystemTickFunction::DiagnosticMessage()
{
return TEXT("ST_WorldSubsystem::Tick()");
}
FName FST_WorldSubsystemTickFunction::DiagnosticContext(bool bDetailed)
{
check(Target);
return Target->GetClass()->GetFName();
}
///////////////////////
///// Constructor /////
///////////////////////
UST_WorldSubsystem::UST_WorldSubsystem()
: UWorldSubsystem()
{
SubsystemTickFunction.bCanEverTick = false;
SubsystemTickFunction.bStartWithTickEnabled = false;
SubsystemTickFunction.bAllowTickOnDedicatedServer = true;
SubsystemTickFunction.TickGroup = ETickingGroup::TG_PrePhysics;
// Skip 'Entry' and 'MainMenu' levels by default..
LevelBlocklist.Add("UM_Entry");
LevelBlocklist.Add("UM_MainMenu");
bEnableInUntitledLevel = false;
bEnableInTransitionLevel = false;
// Initialise in all network domains by default
InitialisationNetModeMask |= (uint8)1 << static_cast<uint8>(ENetMode::NM_Standalone);
InitialisationNetModeMask |= (uint8)1 << static_cast<uint8>(ENetMode::NM_DedicatedServer);
InitialisationNetModeMask |= (uint8)1 << static_cast<uint8>(ENetMode::NM_ListenServer);
InitialisationNetModeMask |= (uint8)1 << static_cast<uint8>(ENetMode::NM_Client);
}
/////////////////////
///// Lifecycle /////
/////////////////////
bool UST_WorldSubsystem::ShouldCreateSubsystem(UObject* InOuter) const
{
if (Super::ShouldCreateSubsystem(InOuter))
{
const UWorld* lWorld = Cast<UWorld>(InOuter);
if (lWorld && lWorld->IsGameWorld())
{
#if WITH_EDITOR
// PIE creates temporary worlds for Net Clients which should really never initialize subsystems.
if (lWorld->IsPlayInEditor())
{
const UPackage* OuterPackage = CastChecked<UPackage>(lWorld->GetOuter());
if (!OuterPackage || OuterPackage->GetPIEInstanceID() == INDEX_NONE)
{
return false;
}
}
#endif
if (CheckNetMode(lWorld) && CheckLevelName(lWorld))
{
return true;
}
}
}
return false;
}
/////////////////////////
///// World Manager /////
/////////////////////////
void UST_WorldSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
const UWorld* lWorld = GetWorld();
check(lWorld && lWorld->IsGameWorld() && lWorld->PersistentLevel != nullptr);
// Can't actually do safe initialisation until much later...
PostInitWorldDelegateHandle = FCoreUObjectDelegates::PostLoadMapWithWorld.AddUObject(this, &UST_WorldSubsystem::PostInitWorldInternal);
#if WITH_EDITOR
// Editor environment is added fun
PostInitWorldPIEDelegateHandle = FEditorDelegates::PostPIEStarted.AddUObject(this, &UST_WorldSubsystem::PostInitWorldPIEInternal);
#endif
}
void UST_WorldSubsystem::Deinitialize()
{
SubsystemTickFunction.UnRegisterTickFunction();
FCoreUObjectDelegates::PostLoadMapWithWorld.Remove(PostInitWorldDelegateHandle);
#if WITH_EDITOR
FEditorDelegates::PostPIEStarted.Remove(PostInitWorldPIEDelegateHandle);
#endif
Super::Deinitialize();
}
bool UST_WorldSubsystem::DoesSupportWorldType(const EWorldType::Type WorldType) const
{
return WorldType == EWorldType::Game || WorldType == EWorldType::PIE;
}
void UST_WorldSubsystem::PostInitWorldInternal(UWorld* NewWorld)
{
const UWorld* lWorld = GetWorld();
if (lWorld && lWorld == NewWorld && !bHasPostWorldInitialized)
{
bHasPostWorldInitialized = true;
FCoreUObjectDelegates::PostLoadMapWithWorld.Remove(PostInitWorldDelegateHandle);
#if WITH_EDITOR
FEditorDelegates::PostPIEStarted.Remove(PostInitWorldPIEDelegateHandle);
#endif
// Handle Tick Registration now.
if (!SubsystemTickFunction.IsTickFunctionRegistered() && SubsystemTickFunction.bCanEverTick)
{
SubsystemTickFunction.Target = this;
SubsystemTickFunction.SetTickFunctionEnable(SubsystemTickFunction.bStartWithTickEnabled || SubsystemTickFunction.IsTickFunctionEnabled());
SubsystemTickFunction.RegisterTickFunction(lWorld->PersistentLevel);
}
OnWorldInitialized();
}
}
#if WITH_EDITOR
void UST_WorldSubsystem::PostInitWorldPIEInternal(bool bSimulating)
{
PostInitWorldInternal(GetWorld());
}
#endif
/////////////////////////////////
///// Initialization Checks /////
/////////////////////////////////
bool UST_WorldSubsystem::CheckNetMode(const UWorld* lWorld) const
{
ENetMode MyNetMode = ENetMode::NM_MAX;
if (GetSafeNetMode(MyNetMode, lWorld))
{
const uint8 NetModeMask = (uint8)1 << (uint8)MyNetMode;
return (InitialisationNetModeMask & NetModeMask) != 0;
}
return false;
}
bool UST_WorldSubsystem::CheckLevelName(const UWorld* lWorld) const
{
checkSlow(lWorld);
// Check Level Names
FString LevelName = lWorld->GetMapName();
LevelName.RemoveFromStart(lWorld->StreamingLevelsPrefix);
// Optionally Block in the 'Untitled' Level
// These are special levels the engine sometimes creates for intermediate UWorlds.
if (!bEnableInUntitledLevel && LevelName.Contains(TEXT("Untitled")))
{
return false;
}
// Optionally Block in the 'Transition' Level
// Read the Transition Level from project settings.
const UGameMapsSettings* MapSettings = GetDefault<UGameMapsSettings>();
check(MapSettings);
if (!bEnableInTransitionLevel && !MapSettings->TransitionMap.IsNull() && MapSettings->TransitionMap.GetAssetName() == LevelName)
{
return false;
}
if (LevelAllowlist.Num() != 0 && !LevelAllowlist.Contains(LevelName))
{
return false;
}
if (LevelBlocklist.Contains(LevelName))
{
return false;
}
return true;
}
/////////////////////
///// Utilities /////
/////////////////////
bool UST_WorldSubsystem::GetSafeNetMode(ENetMode& OutMode, const UWorld* OverrideWorld /*= nullptr*/) const
{
const UWorld* lWorld = OverrideWorld ? OverrideWorld : GetWorld();
check(lWorld);
OutMode = lWorld->GetNetMode();
#if WITH_EDITOR
// PIE creates some obscure UWorld lifetimes that make NetMode access more difficult.
// See https://udn.unrealengine.com/s/question/0D54z00007DW1CQCA1/why-are-uworldsubsystems-initialized-before-uworld-is-initialized
if (lWorld->IsPlayInEditor())
{
UEditorEngine* const EditorEngine = CastChecked<UEditorEngine>(GEngine);
const UPackage* Package = Cast<UPackage>(lWorld->GetOuter());
if (!Package || Package->GetPIEInstanceID() == INDEX_NONE)
{
// This is a Transient UPackage used for PIE Client worlds.
OutMode = ENetMode::NM_MAX;
return false;
}
else
{
// We can get the UGameInstance/UNetDriver from here, *before* the UWorld properties are set
const FWorldContext* PIEWC = EditorEngine->GetWorldContextFromPIEInstance(Package->GetPIEInstanceID());
if (ensureAlways(PIEWC))
{
if (!PIEWC->PendingNetGame)
{
OutMode = PIEWC->RunAsDedicated ? ENetMode::NM_DedicatedServer : ENetMode::NM_Standalone;
}
else if (ensureAlways(PIEWC->PendingNetGame->NetDriver))
{
OutMode = PIEWC->PendingNetGame->NetDriver->GetNetMode();
}
}
}
}
#endif
return true;
}