This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShooterGameInstance.h
400 lines (284 loc) · 14.1 KB
/
ShooterGameInstance.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "ShooterGame.h"
#include "OnlineIdentityInterface.h"
#include "OnlineSessionInterface.h"
#include "OnlineGameActivityInterface.h"
#include "Engine/GameInstance.h"
#include "Engine/NetworkDelegates.h"
#include "ShooterGameInstance.generated.h"
class FVariantData;
class FShooterMainMenu;
class FShooterWelcomeMenu;
class FShooterMessageMenu;
class AShooterGameSession;
namespace ShooterGameInstanceState
{
extern const FName None;
extern const FName PendingInvite;
extern const FName WelcomeScreen;
extern const FName MainMenu;
extern const FName MessageMenu;
extern const FName Playing;
}
/** This class holds the value of what message to display when we are in the "MessageMenu" state */
class FShooterPendingMessage
{
public:
FText DisplayString; // This is the display message in the main message body
FText OKButtonString; // This is the ok button text
FText CancelButtonString; // If this is not empty, it will be the cancel button text
FName NextState; // Final destination state once message is discarded
TWeakObjectPtr< ULocalPlayer > PlayerOwner; // Owner of dialog who will have focus (can be NULL)
};
class FShooterPendingInvite
{
public:
FShooterPendingInvite() : ControllerId(-1), UserId(nullptr), bPrivilegesCheckedAndAllowed(false) {}
int32 ControllerId;
TSharedPtr< const FUniqueNetId > UserId;
FOnlineSessionSearchResult InviteResult;
bool bPrivilegesCheckedAndAllowed;
};
class SShooterWaitDialog : public SCompoundWidget
{
public:
SLATE_BEGIN_ARGS(SShooterWaitDialog)
{}
SLATE_ARGUMENT(FText, MessageText)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
private:
/** our curve sequence and the related handles */
FCurveSequence WidgetAnimation;
/** used for animating the text color. */
FCurveHandle TextColorCurve;
/** Gets the animated text color */
FSlateColor GetTextColor() const;
};
UENUM()
enum class EOnlineMode : uint8
{
Offline,
LAN,
Online
};
UCLASS(config=Game)
class UShooterGameInstance : public UGameInstance
{
public:
GENERATED_UCLASS_BODY()
public:
bool Tick(float DeltaSeconds);
AShooterGameSession* GetGameSession() const;
virtual void Init() override;
virtual void Shutdown() override;
virtual void StartGameInstance() override;
#if WITH_EDITOR
virtual FGameInstancePIEResult StartPlayInEditorGameInstance(ULocalPlayer* LocalPlayer, const FGameInstancePIEParameters& Params) override;
#endif // WITH_EDITOR
virtual void ReceivedNetworkEncryptionToken(const FString& EncryptionToken, const FOnEncryptionKeyResponse& Delegate) override;
virtual void ReceivedNetworkEncryptionAck(const FOnEncryptionKeyResponse& Delegate) override;
bool HostGame(ULocalPlayer* LocalPlayer, const FString& GameType, const FString& InTravelURL);
bool JoinSession(ULocalPlayer* LocalPlayer, int32 SessionIndexInSearchResults);
bool JoinSession(ULocalPlayer* LocalPlayer, const FOnlineSessionSearchResult& SearchResult);
void SetPendingInvite(const FShooterPendingInvite& InPendingInvite);
bool PlayDemo(ULocalPlayer* LocalPlayer, const FString& DemoName);
/** Travel directly to the named session */
void TravelToSession(const FName& SessionName);
/** Directly connect to an address */
void DirectConnectToSession(const FString& Address);
/** Get the Travel URL for a quick match */
static FString GetQuickMatchUrl();
/** Begin a hosted quick match */
void BeginHostingQuickMatch();
/** Initiates the session searching */
bool FindSessions(ULocalPlayer* PlayerOwner, bool bIsDedicatedServer, bool bLANMatch);
/** Sends the game to the specified state. */
void GotoState(FName NewState);
/** Obtains the initial welcome state, which can be different based on platform */
FName GetInitialState();
/** Sends the game to the initial startup/frontend state */
void GotoInitialState();
/** Gets the current state of the GameInstance */
const FName GetCurrentState() const;
/**
* Creates the message menu, clears other menus and sets the KingState to Message.
*
* @param Message Main message body
* @param OKButtonString String to use for 'OK' button
* @param CancelButtonString String to use for 'Cancel' button
* @param NewState Final state to go to when message is discarded
*/
void ShowMessageThenGotoState( const FText& Message, const FText& OKButtonString, const FText& CancelButtonString, const FName& NewState, const bool OverrideExisting = true, TWeakObjectPtr< ULocalPlayer > PlayerOwner = nullptr );
void RemoveExistingLocalPlayer(ULocalPlayer* ExistingPlayer);
void RemoveSplitScreenPlayers();
TSharedPtr< const FUniqueNetId > GetUniqueNetIdFromControllerId( const int ControllerId );
/** Returns true if the game is in online mode */
EOnlineMode GetOnlineMode() const { return OnlineMode; }
/** Sets the online mode of the game */
void SetOnlineMode(EOnlineMode InOnlineMode);
/** Updates the status of using multiplayer features */
void UpdateUsingMultiplayerFeatures(bool bIsUsingMultiplayerFeatures);
/** Sets the controller to ignore for pairing changes. Useful when we are showing external UI for manual profile switching. */
void SetIgnorePairingChangeForControllerId( const int32 ControllerId );
/** Returns true if the passed in local player is signed in and online */
bool IsLocalPlayerOnline(ULocalPlayer* LocalPlayer);
/** Returns true if the passed in local player is signed in*/
bool IsLocalPlayerSignedIn(ULocalPlayer* LocalPlayer);
/** Returns true if owning player is online. Displays proper messaging if the user can't play */
bool ValidatePlayerForOnlinePlay(ULocalPlayer* LocalPlayer);
/** Returns true if owning player is signed in. Displays proper messaging if the user can't play */
bool ValidatePlayerIsSignedIn(ULocalPlayer* LocalPlayer);
/** Shuts down the session, and frees any net driver */
void CleanupSessionOnReturnToMenu();
/** Flag the local player when they quit the game */
void LabelPlayerAsQuitter(ULocalPlayer* LocalPlayer) const;
// Generic confirmation handling (just hide the dialog)
FReply OnConfirmGeneric();
bool HasLicense() const { return bIsLicensed; }
/** Start task to get user privileges. */
void StartOnlinePrivilegeTask(const IOnlineIdentity::FOnGetUserPrivilegeCompleteDelegate& Delegate, EUserPrivileges::Type Privilege, TSharedPtr< const FUniqueNetId > UserId);
/** Common cleanup code for any Privilege task delegate */
void CleanupOnlinePrivilegeTask();
/** Show approved dialogs for various privileges failures */
void DisplayOnlinePrivilegeFailureDialogs(const FUniqueNetId& UserId, EUserPrivileges::Type Privilege, uint32 PrivilegeResults);
/** @return OnlineSession class to use for this player */
TSubclassOf<class UOnlineSession> GetOnlineSessionClass() override;
/** Create a session with the default map and game-type with the selected online settings */
bool HostQuickSession(ULocalPlayer& LocalPlayer, const FOnlineSessionSettings& SessionSettings);
/** Sets a rich presence string for all local players. */
void SetPresenceForLocalPlayers(const FString& StatusStr, const FVariantData& PresenceData);
/** Handle game activity requests */
void OnGameActivityActivationRequestComplete(const FUniqueNetId& PlayerId, const FString& ActivityId, const FOnlineSessionSearchResult* SessionInfo);
private:
UPROPERTY(config)
FString WelcomeScreenMap;
UPROPERTY(config)
FString MainMenuMap;
UPROPERTY(config)
FString MatchmakerEndpoint;
FName CurrentState;
FName PendingState;
FShooterPendingMessage PendingMessage;
FShooterPendingInvite PendingInvite;
/** URL to travel to after pending network operations */
FString TravelURL;
/** Current online mode of the game (offline, LAN, or online) */
EOnlineMode OnlineMode;
/** If true, enable splitscreen when map starts loading */
bool bPendingEnableSplitscreen;
/** Whether the user has an active license to play the game */
bool bIsLicensed;
/** Main menu UI */
TSharedPtr<FShooterMainMenu> MainMenuUI;
/** Message menu (Shown in the even of errors - unable to connect etc) */
TSharedPtr<FShooterMessageMenu> MessageMenuUI;
/** Welcome menu UI (for consoles) */
TSharedPtr<FShooterWelcomeMenu> WelcomeMenuUI;
/** Dialog widget to show non-interactive waiting messages for network timeouts and such. */
TSharedPtr<SShooterWaitDialog> WaitMessageWidget;
/** Controller to ignore for pairing changes. -1 to skip ignore. */
int32 IgnorePairingChangeForControllerId;
/** Last connection status that was passed into the HandleNetworkConnectionStatusChanged hander */
EOnlineServerConnectionStatus::Type CurrentConnectionStatus;
/** Delegate for callbacks to Tick */
FTickerDelegate TickDelegate;
/** Handle to various registered delegates */
FDelegateHandle TickDelegateHandle;
FDelegateHandle TravelLocalSessionFailureDelegateHandle;
FDelegateHandle OnJoinSessionCompleteDelegateHandle;
FDelegateHandle OnSearchSessionsCompleteDelegateHandle;
FDelegateHandle OnStartSessionCompleteDelegateHandle;
FDelegateHandle OnEndSessionCompleteDelegateHandle;
FDelegateHandle OnDestroySessionCompleteDelegateHandle;
FDelegateHandle OnCreatePresenceSessionCompleteDelegateHandle;
FDelegateHandle OnGameActivityActivationRequestedDelegateHandle;
FOnGameActivityActivationRequestedDelegate OnGameActivityActivationRequestedDelegate;
/** Local player login status when the system is suspended */
TArray<ELoginStatus::Type> LocalPlayerOnlineStatus;
/** A hard-coded encryption key used to try out the encryption code. This is NOT SECURE, do not use this technique in production! */
TArray<uint8> DebugTestEncryptionKey;
void HandleNetworkConnectionStatusChanged(const FString& ServiceName, EOnlineServerConnectionStatus::Type LastConnectionStatus, EOnlineServerConnectionStatus::Type ConnectionStatus );
void HandleSessionFailure( const FUniqueNetId& NetId, ESessionFailure::Type FailureType );
void OnPreLoadMap(const FString& MapName);
void OnPostLoadMap(UWorld*);
void OnPostDemoPlay();
virtual void HandleDemoPlaybackFailure( EDemoPlayFailure::Type FailureType, const FString& ErrorString ) override;
/** Delegate function executed after checking privileges for starting quick match */
void OnUserCanPlayInvite(const FUniqueNetId& UserId, EUserPrivileges::Type Privilege, uint32 PrivilegeResults);
/** Delegate for ending a session */
FOnEndSessionCompleteDelegate OnEndSessionCompleteDelegate;
void OnEndSessionComplete( FName SessionName, bool bWasSuccessful );
void MaybeChangeState();
void EndCurrentState(FName NextState);
void BeginNewState(FName NewState, FName PrevState);
void BeginPendingInviteState();
void BeginWelcomeScreenState();
void BeginMainMenuState();
void BeginMessageMenuState();
void BeginPlayingState();
void EndPendingInviteState();
void EndWelcomeScreenState();
void EndMainMenuState();
void EndMessageMenuState();
void EndPlayingState();
void ShowLoadingScreen();
void AddNetworkFailureHandlers();
void RemoveNetworkFailureHandlers();
/** Called when there is an error trying to travel to a local session */
void TravelLocalSessionFailure(UWorld *World, ETravelFailure::Type FailureType, const FString& ErrorString);
/** Callback which is intended to be called upon joining session */
void OnJoinSessionComplete(EOnJoinSessionCompleteResult::Type Result);
/** Callback which is intended to be called upon session creation */
void OnCreatePresenceSessionComplete(FName SessionName, bool bWasSuccessful);
/** Callback which is called after adding local users to a session */
void OnRegisterLocalPlayerComplete(const FUniqueNetId& PlayerId, EOnJoinSessionCompleteResult::Type Result);
/** Called after all the local players are registered */
void FinishSessionCreation(EOnJoinSessionCompleteResult::Type Result);
/** Callback which is called after adding local users to a session we're joining */
void OnRegisterJoiningLocalPlayerComplete(const FUniqueNetId& PlayerId, EOnJoinSessionCompleteResult::Type Result);
/** Called after all the local players are registered in a session we're joining */
void FinishJoinSession(EOnJoinSessionCompleteResult::Type Result);
/**
* Creates the message menu, clears other menus and sets the KingState to Message.
*
* @param Message Main message body
* @param OKButtonString String to use for 'OK' button
* @param CancelButtonString String to use for 'Cancel' button
*/
void ShowMessageThenGoMain(const FText& Message, const FText& OKButtonString, const FText& CancelButtonString);
/** Callback which is intended to be called upon finding sessions */
void OnSearchSessionsComplete(bool bWasSuccessful);
bool LoadFrontEndMap(const FString& MapName);
/** Travel directly to the named session */
void InternalTravelToSession(const FName& SessionName);
/** Show messaging and punt to welcome screen */
void HandleSignInChangeMessaging();
// OSS delegates to handle
void HandleUserLoginChanged(int32 GameUserIndex, ELoginStatus::Type PreviousLoginStatus, ELoginStatus::Type LoginStatus, const FUniqueNetId& UserId);
// Callback to pause the game when the OS has constrained our app.
void HandleAppWillDeactivate();
// Callback occurs when game being suspended
void HandleAppSuspend();
// Callback occurs when game resuming
void HandleAppResume();
// Callback to process game licensing change notifications.
void HandleAppLicenseUpdate();
// Callback to handle safe frame size changes.
void HandleSafeFrameChanged();
// Callback to handle controller connection changes.
void HandleControllerConnectionChange(bool bIsConnection, FPlatformUserId Unused, int32 GameUserIndex);
// Callback to handle controller pairing changes.
FReply OnPairingUsePreviousProfile();
// Callback to handle controller pairing changes.
FReply OnPairingUseNewProfile();
// Callback to handle controller pairing changes.
void HandleControllerPairingChanged(int GameUserIndex, FControllerPairingChangedUserInfo PreviousUserInfo, FControllerPairingChangedUserInfo NewUserInfo);
// Handle confirming the controller disconnected dialog.
FReply OnControllerReconnectConfirm();
protected:
bool HandleOpenCommand(const TCHAR* Cmd, FOutputDevice& Ar, UWorld* InWorld);
bool HandleDisconnectCommand(const TCHAR* Cmd, FOutputDevice& Ar, UWorld* InWorld);
bool HandleTravelCommand(const TCHAR* Cmd, FOutputDevice& Ar, UWorld* InWorld);
};