-
Notifications
You must be signed in to change notification settings - Fork 0
/
SilentRedGameInstance.cpp
247 lines (190 loc) · 6.76 KB
/
SilentRedGameInstance.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
// Fill out your copyright notice in the Description page of Project Settings.
#include "SilentRedGameInstance.h"
#include "Engine/Engine.h"
#include "UObject/ConstructorHelpers.h"
#include "Blueprint/UserWidget.h"
#include "MenuSystem/MainMenu.h"
#include "MenuSystem/MenuWidget.h"
#include "OnlineSessionSettings.h"
const static FName SESSION_NAME = TEXT("Game");
const static FName SERVER_NAME_SETTINGS_KEY = TEXT("Server Name");
USilentRedGameInstance::USilentRedGameInstance(const FObjectInitializer & ObjectInitializer)
{
ConstructorHelpers::FClassFinder<UUserWidget>MenuBPClass(TEXT("/Game/UI/MainMenu"));
if (!ensure(MenuBPClass.Class != nullptr)) return;
MenuClass = MenuBPClass.Class;
ConstructorHelpers::FClassFinder<UUserWidget>InGameMenuBPClass(TEXT("/Game/UI/WBP_InGameMenu"));
if (!ensure(InGameMenuBPClass.Class != nullptr)) return;
InGameMenuClass = InGameMenuBPClass.Class;
}
void USilentRedGameInstance::Init()
{
IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get();
if (Subsystem != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("Found Subsystem %s"), *Subsystem->GetSubsystemName().ToString());
SessionInterface = Subsystem->GetSessionInterface();
if (SessionInterface.IsValid())
{
SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &USilentRedGameInstance::OnCreateSessionComplete);
SessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &USilentRedGameInstance::OnDestroySessionComplete);
SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &USilentRedGameInstance::OnFindSessionComplete);
SessionInterface->OnJoinSessionCompleteDelegates.AddUObject(this, &USilentRedGameInstance::OnJoinSessionComplete);
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Found no Subsystem"));
}
}
void USilentRedGameInstance::LoadMenuWidget()
{
if (!ensure(MenuClass != nullptr)) return;
Menu = CreateWidget<UMainMenu>(this, MenuClass);
if (!ensure(Menu != nullptr)) return;
Menu->Setup();
Menu->SetMenuInterface(this);
}
void USilentRedGameInstance::InGameLoadMenu()
{
if (!ensure(InGameMenuClass != nullptr)) return;
UMenuWidget* Menu = CreateWidget<UMenuWidget>(this, InGameMenuClass);
if (!ensure(Menu != nullptr)) return;
Menu->Setup();
Menu->SetMenuInterface(this);
}
void USilentRedGameInstance::Host(FString ServerName)
{
DesiredServerName = ServerName;
if (SessionInterface.IsValid())
{
auto ExistingSession = SessionInterface->GetNamedSession(SESSION_NAME);
if (ExistingSession != nullptr)
{
FOnlineSearchSettings SessionSettings;
SessionInterface->DestroySession(SESSION_NAME);
}
else
{
CreateSession();
}
}
}
void USilentRedGameInstance::OnDestroySessionComplete(FName SessionName, bool Success)
{
if (Success)
{
CreateSession();
}
}
void USilentRedGameInstance::CreateSession()
{
if (SessionInterface.IsValid())
{
FOnlineSessionSettings SessionSettings;
if (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL")
{
SessionSettings.bIsLANMatch = true;
}
else
{
SessionSettings.bIsLANMatch = false;
}
SessionSettings.NumPublicConnections = 25;
SessionSettings.bShouldAdvertise = true;
SessionSettings.bAllowJoinInProgress = true;
SessionSettings.bUsesPresence = true;
SessionSettings.Set(SERVER_NAME_SETTINGS_KEY, DesiredServerName, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);
SessionInterface->CreateSession(0, SESSION_NAME, SessionSettings);
}
}
void USilentRedGameInstance::OnCreateSessionComplete(FName SessionName, bool Success)
{
if (!Success)
{
UE_LOG(LogTemp, Warning, TEXT("Could not Create Session"));
return;
}
if (Menu != nullptr)
{
Menu->Teardown();
}
UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;
Engine->AddOnScreenDebugMessage(0, 2, FColor::Green, TEXT("Hosting Server"));
UWorld* World = GetWorld();
if (!ensure(World != nullptr)) return;
World->ServerTravel("/Game/Maps/Alphamap?listen");
}
void USilentRedGameInstance::RefreshServerList()
{
SessionSearch = MakeShareable(new FOnlineSessionSearch());
if (SessionSearch.IsValid())
{
//SessionSearch->bIsLanQuery = true;
SessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals);
UE_LOG(LogTemp, Warning, TEXT("Starting Find Session"));
SessionSearch->MaxSearchResults = 100;
SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
}
}
void USilentRedGameInstance::OnFindSessionComplete(bool Success)
{
if (Success && SessionSearch.IsValid() && Menu != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("Finished Find Session"));
TArray<FServerData> ServerNames;
for (const FOnlineSessionSearchResult& SearchResult : SessionSearch->SearchResults)
{
UE_LOG(LogTemp, Warning, TEXT("Found Session Nams: %s"), *SearchResult.GetSessionIdStr());
FServerData Data;
Data.Name = SearchResult.GetSessionIdStr();
Data.MaxPlayers = SearchResult.Session.SessionSettings.NumPublicConnections;
Data.CurrentPlayers = Data.MaxPlayers - SearchResult.Session.NumOpenPublicConnections;
Data.HostUserName = SearchResult.Session.OwningUserName;
FString ServerName;
if (SearchResult.Session.SessionSettings.Get(SERVER_NAME_SETTINGS_KEY, ServerName))
{
Data.Name = ServerName;
}
else
{
Data.Name = "Could not find server name";
}
ServerNames.Add(Data);
}
Menu->SetServerList(ServerNames);
}
}
void USilentRedGameInstance::Join(uint32 Index)
{
if (!SessionInterface.IsValid()) return;
if (!SessionSearch.IsValid()) return;
if (Menu != nullptr)
{
Menu->Teardown();
}
SessionInterface->JoinSession(0, SESSION_NAME, SessionSearch->SearchResults[Index]);
}
void USilentRedGameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
if (!SessionInterface.IsValid()) return;
FString Address;
if (!SessionInterface->GetResolvedConnectString(SessionName, Address))
{
UE_LOG(LogTemp, Warning, TEXT("Could not Get Connect String."));
return;
}
UEngine* Engine = GetEngine();
if (!ensure(Engine != nullptr)) return;
Engine->AddOnScreenDebugMessage(0, 2, FColor::Green, FString::Printf(TEXT("Joining %s"), *Address));
APlayerController* PlayerController = GetFirstLocalPlayerController();
if (!ensure(PlayerController != nullptr)) return;
PlayerController->ClientTravel(Address, ETravelType::TRAVEL_Absolute);
}
void USilentRedGameInstance::LoadMainMenu()
{
APlayerController* PlayerController = GetFirstLocalPlayerController();
if (!ensure(PlayerController != nullptr)) return;
PlayerController->ClientTravel("/Game/Maps/MenuLevel", ETravelType::TRAVEL_Absolute);
}