Advertisement
Guest User

Untitled

a guest
Jul 14th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "ServerGameSession.h"
  4. #include "Blueprint/UserWidget.h"
  5. #include "Widgets/Login.h"
  6. #include "Widgets/LoginWidget.h"
  7. #include "Engine/Engine.h"
  8. #include "OnlineSessionSettings.h"
  9.  
  10. const static FName SESSION_NAME = TEXT("");
  11. const static FName SERVER_NAME_SETTINGS_KEY = TEXT("ServerName");
  12.  
  13. void AServerGameSession::RegisterServer()
  14. {
  15.  
  16. IOnlineSubsystem* SubSystem = IOnlineSubsystem::Get();
  17. if (SubSystem != nullptr) {
  18. UE_LOG(LogTemp, Warning, TEXT("Found Substem %s"), *SubSystem->GetSubsystemName().ToString());
  19. SessionInterface = SubSystem->GetSessionInterface();
  20. if (SessionInterface.IsValid())
  21. {
  22.  
  23. SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &AServerGameSession::OnCreateSessionComplete);
  24. SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &AServerGameSession::OnFindSessionComplete);
  25. // SessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &AServerGameSession::OnCreateSessionComplete);
  26.  
  27.  
  28. }
  29. //Host("dsd");
  30.  
  31. }
  32. else {
  33. UE_LOG(LogTemp, Warning, TEXT("Found no Substem"));
  34. }
  35. }
  36.  
  37.  
  38. void AServerGameSession::OnFindSessionComplete(bool Sucess)
  39. {
  40.  
  41.  
  42. if (Sucess && SessionSearch.IsValid() && loginMenu != nullptr)
  43. {
  44. UE_LOG(LogTemp, Warning, TEXT("Finished Find Session"));
  45.  
  46. TArray<FServerData> ServerName;
  47.  
  48. for (const FOnlineSessionSearchResult& SearchResult : SessionSearch->SearchResults) {
  49. UE_LOG(LogTemp, Warning, TEXT("Found Session Name %s"), *SearchResult.GetSessionIdStr());
  50. FServerData Data;
  51.  
  52. Data.MaxPlayers = SearchResult.Session.SessionSettings.NumPublicConnections;
  53. Data.CurrentPlayers = Data.MaxPlayers - SearchResult.Session.NumOpenPublicConnections;
  54. Data.HostUserName = SearchResult.Session.OwningUserName;
  55. FString ServerNames;
  56. if (SearchResult.Session.SessionSettings.Get(SERVER_NAME_SETTINGS_KEY, ServerNames))
  57. {
  58. Data.Name = ServerNames;
  59.  
  60. }
  61. else
  62. {
  63. Data.Name = "Could Not Find the Server";
  64.  
  65. }
  66. ServerName.Add(Data);
  67. }
  68. loginMenu->SetServerList(ServerName);
  69. }
  70.  
  71. }
  72. void AServerGameSession::OnCreateSessionComplete(FName SessionName, bool Sucess) {
  73.  
  74. if (!Sucess)
  75. {
  76. UE_LOG(LogTemp, Warning, TEXT("Could not Create Session"));
  77. return;
  78. }
  79. if (loginMenu != nullptr)
  80. {
  81. loginMenu->TearDown();
  82. }
  83. UWorld* World = GetWorld();
  84. if (!ensure(World != nullptr)) return;
  85. World->ServerTravel("/Game/Assets/Maps/Lobby?listen");
  86. }
  87.  
  88. void AServerGameSession::OndestroySessionComplete(FName SessionName, bool Sucess)
  89. {
  90. if (Sucess) {
  91. CreateSession();
  92. }
  93. }
  94. void AServerGameSession::Host(FString Servername)
  95. {
  96. DiresedServername = Servername;
  97. if (SessionInterface.IsValid()) {
  98.  
  99. auto ExistingSession = SessionInterface->GetNamedSession(GameSessionName);
  100. if (ExistingSession != nullptr)
  101. {
  102. SessionInterface->DestroySession(GameSessionName);
  103. }
  104. else
  105. {
  106. CreateSession();
  107.  
  108. }
  109.  
  110. }
  111.  
  112. }
  113.  
  114. void AServerGameSession::CreateSession()
  115. {
  116. if (SessionInterface.IsValid()) {
  117. FOnlineSessionSettings SessionSettings;
  118.  
  119. if (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL") {
  120. SessionSettings.bIsLANMatch = true;
  121. }
  122. else {
  123. SessionSettings.bIsLANMatch = false;
  124. }
  125.  
  126. SessionSettings.NumPublicConnections = 5;
  127. SessionSettings.bShouldAdvertise = true;
  128. SessionSettings.bUsesPresence = false;
  129. SessionSettings.bIsDedicated = true;
  130. SessionSettings.Set(SERVER_NAME_SETTINGS_KEY, DiresedServername, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);
  131.  
  132. SessionInterface->CreateSession(0, GameSessionName, SessionSettings);
  133.  
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement