Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SAD.h"
- #include "SADGameInstance.h"
- #include "Personality/SADGameState.h"
- #include "SocketSubsystem.h"
- #include "Engine/Engine.h"
- #include "Engine/LatentActionManager.h"
- #include "OnlineSubsystem.h"
- #include "OnlineSessionInterface.h"
- #include "GameFramework/OnlineSession.h"
- #include "GameFramework/PlayerState.h"
- USADGameInstance::USADGameInstance(const FObjectInitializer& ObjectInitializer)
- : Super(ObjectInitializer)
- {
- }
- void USADGameInstance::Init()
- {
- // todo Возможно UGUIManager не нужен на дедике
- GUIManager = NewObject<UGUIManager>(this, GUIManagerType);
- GUIManager->Init(this);
- InitUDPMessaging();
- ServerManager = NewObject<UServerManager>(this, UServerManager::StaticClass());
- ServerManager->SetOwner(nullptr); // Owner is not assigned yet
- Super::Init();
- PRINT("USADGameInstance Init");
- if (IsRunningDedicatedServer())
- {
- PRINT("USADGameInstance OpenLevel TestLevel");
- UGameplayStatics::OpenLevel(GetWorld(), "TestLevel");
- }
- }
- void USADGameInstance::ServerRegistration()
- {
- bIsQuickStarted = false;
- FString username, password;
- GConfig->GetString(
- TEXT("DedicatedServer.Config"),
- TEXT("Login"),
- username,
- GGameIni
- );
- GConfig->GetString(
- TEXT("DedicatedServer.Config"),
- TEXT("Password"),
- password,
- GGameIni
- );
- PRINT("USADGameInstance::ServerRegistration login: %s, pass: %s", *username, *password);
- await(this, ServerManager->login(username, password),
- [=] (bool success, FString access_token, EAccessLevel access_level)
- {
- PRINT("USADGameInstance::Login sucess %s", BSTR(success));
- if (success && IsRunningDedicatedServer())
- OnSessionStart();
- else
- ERROR_MSG("Setrver authorization is unsuccessful!");
- },
- [=]() { WARN_MSG("!!!+++ There is login TIMEOUT +++!!!"); }
- );
- }
- void USADGameInstance::OnSessionStart()
- {
- PRINT("USADGameInstance::OnSessionStart call of RegisterWorld");
- bool canBind = false;
- TSharedRef<FInternetAddr> localIp = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLocalHostAddr(*GLog, canBind);
- PRINT("OnSessionStart - %s: I am -- (%s)", *localIp->ToString(true) );
- ServerManager->RegisterWorld(ServerManager->AccessToken, *localIp->ToString(true));
- ServerManager->RequestItemsCatalog();
- PRINT("USADGameInstance::OnSessionStart call of RequestItemsCatalog");
- }
- void USADGameInstance::StartGameInstance()
- {
- PRINT("--++USADGameInstance::StartGameInstance IsRunningDedicatedServer(): %d", IsRunningDedicatedServer());
- FURL URL = FURL();
- if (BrowseMap(URL) && IsRunningDedicatedServer())
- ServerRegistration();
- }
- bool USADGameInstance::BrowseMap(const FURL& URL)
- {
- PRINT("USADGameInstance::BrowseMap URL: %s", *URL.ToString());
- if (URL.Valid)
- {
- UEngine* const Engine = GetEngine();
- FString Error;
- const EBrowseReturnVal::Type BrowseRet = Engine->Browse(*WorldContext, URL, Error);
- if (BrowseRet == EBrowseReturnVal::Success)
- {
- PRINT("USADGameInstance::BrowseMap EBrowseReturnVal::Success");
- return true;
- }
- else if (BrowseRet == EBrowseReturnVal::Pending)
- {
- WARN_MSG("USADGameInstance::BrowseMap EBrowseReturnVal::Pending");
- }
- }
- WARN_MSG("USADGameInstance::BrowseMap URL for Browse is invalid! URL: %s", *URL.ToString());
- return false;
- }
- void USADGameInstance::InitUDPMessaging()
- {
- int32 SMPort = -1;
- GConfig->GetInt(
- TEXT("ServerManager.Config"),
- TEXT("Port"),
- SMPort,
- GGameIni
- );
- FVector4 Host;
- GConfig->GetVector4(
- TEXT("ServerManager.Config"),
- TEXT("Host"),
- Host,
- GGameIni
- );
- FIPv4Address SMHost = FIPv4Address(int(Host.X), int(Host.Y), int(Host.Z), int(Host.W));
- PRINT("SMPort %d", SMPort);
- PRINT("SMHost %d.%d.%d.%d", int(Host.X), int(Host.Y), int(Host.Z), int(Host.W));
- UDPMessager = NewObject<UUDPMessaging>(this);
- UDPMessager->Connect(SMHost, SMPort);
- }
Advertisement
Add Comment
Please, Sign In to add comment