Example #1
0
void UGameEngine::Init(IEngineLoop* InEngineLoop)
{
	DECLARE_SCOPE_CYCLE_COUNTER(TEXT("UGameEngine Init"), STAT_GameEngineStartup, STATGROUP_LoadTime);

	// Call base.
	UEngine::Init(InEngineLoop);

#if USE_NETWORK_PROFILER
	FString NetworkProfilerTag;
	if( FParse::Value(FCommandLine::Get(), TEXT("NETWORKPROFILER="), NetworkProfilerTag ) )
	{
		GNetworkProfiler.EnableTracking(true);
	}
#endif

	// Load all of the engine modules that we need at startup that are not editor-related
	UGameEngine::LoadRuntimeEngineStartupModules();

	// Load and apply user game settings
	GetGameUserSettings()->LoadSettings();
	GetGameUserSettings()->ApplyNonResolutionSettings();

	// Create game instance.  For GameEngine, this should be the only GameInstance that ever gets created.
	{
		FStringClassReference GameInstanceClassName = GetDefault<UGameMapsSettings>()->GameInstanceClass;
		UClass* GameInstanceClass = (GameInstanceClassName.IsValid() ? LoadObject<UClass>(NULL, *GameInstanceClassName.ToString()) : UGameInstance::StaticClass());
		
		GameInstance = NewObject<UGameInstance>(this, GameInstanceClass);

		GameInstance->InitializeStandalone();
	}
 
//  	// Creates the initial world context. For GameEngine, this should be the only WorldContext that ever gets created.
//  	FWorldContext& InitialWorldContext = CreateNewWorldContext(EWorldType::Game);

	// Initialize the viewport client.
	UGameViewportClient* ViewportClient = NULL;
	if(GIsClient)
	{
		ViewportClient = NewObject<UGameViewportClient>(this, GameViewportClientClass);
		ViewportClient->Init(*GameInstance->GetWorldContext(), GameInstance);
		GameViewport = ViewportClient;
		GameInstance->GetWorldContext()->GameViewport = ViewportClient;
	}

	bCheckForMovieCapture = true;

	// Attach the viewport client to a new viewport.
	if(ViewportClient)
	{
		// This must be created before any gameplay code adds widgets
		bool bWindowAlreadyExists = GameViewportWindow.IsValid();
		if (!bWindowAlreadyExists)
		{
			GameViewportWindow = CreateGameWindow();
		}

		CreateGameViewport( ViewportClient );

		if( !bWindowAlreadyExists )
		{
			SwitchGameWindowToUseGameViewport();
		}

		FString Error;
		if(ViewportClient->SetupInitialLocalPlayer(Error) == NULL)
		{
			UE_LOG(LogEngine, Fatal,TEXT("%s"),*Error);
		}

		UGameViewportClient::OnViewportCreated().Broadcast();
	}

	GameInstance->StartGameInstance();


	UE_LOG(LogInit, Display, TEXT("Game Engine Initialized.") );

	// for IsInitialized()
	bIsInitialized = true;
}
int32 UUnitTestCommandlet::Main(const FString& Params)
{
	// @todo #JohnBLowPri: Fix StandaloneRenderer support for static builds
#if IS_MONOLITHIC
	UE_LOG(LogUnitTest, Log, TEXT("NetcodeUnitTest commandlet not currently supported in static/monolithic builds"));

#else
	GIsRequestingExit = false;

	// @todo #JohnBLowPri: Steam detection doesn't seem to work this early on, but does work further down the line;
	//				try to find a way, to detect it as early as possible

	// NetcodeUnitTest is not compatible with Steam; if Steam is running/detected, abort immediately
	// @todo #JohnBLowPri: Add support for Steam
	if (NUTNet::IsSteamNetDriverAvailable())
	{
		UE_LOG(LogUnitTest, Log, TEXT("NetcodeUnitTest does not currently support Steam. Close Steam before running."));
		GIsRequestingExit = true;
	}

	if (!GIsRequestingExit)
	{
		GIsRunning = true;

		// Hack-set the engine GameViewport, so that setting GIsClient, doesn't cause an auto-exit
		// @todo JohnB: If you later remove the GIsClient setting code below, remove this as well
		if (GEngine->GameViewport == NULL)
		{
			UGameEngine* GameEngine = Cast<UGameEngine>(GEngine);

			// GameInstace = GameEngine->GameInstance;
			UGameInstance* GameInstance = GET_PRIVATE(UGameEngine, GameEngine, GameInstance);

			if (GameEngine != NULL)
			{
				UGameViewportClient* NewViewport = NewObject<UGameViewportClient>(GameEngine);
				FWorldContext* CurContext = GameInstance->GetWorldContext();

				GameEngine->GameViewport = NewViewport;
				NewViewport->Init(*CurContext, GameInstance);

				// Set the internal FViewport, for the new game viewport, to avoid another bit of auto-exit code
				NewViewport->Viewport = new FDummyViewport(NewViewport);

				// Set the main engine world context game viewport, to match the newly created viewport, in order to prevent crashes
				CurContext->GameViewport = NewViewport;
			}
		}


		// Now, after init stages are done, enable GIsClient for netcode and such
		GIsClient = true;


		// Before running any unit tests, create a world object, which will contain the unit tests manager etc.
		//	(otherwise, when the last unit test world is cleaned up, the unit test manager stops functioning)
		UWorld* UnitTestWorld = NUTNet::CreateUnitTestWorld(false);


		const TCHAR* ParamsRef = *Params;
		FString UnitTestParam = TEXT("");
		FString UnitCmd = TEXT("UnitTest ");


		if (FParse::Value(ParamsRef, TEXT("UnitTest="), UnitTestParam))
		{
			UnitCmd += UnitTestParam;
		}
		else
		{
			UnitCmd += TEXT("all");
		}

		GEngine->Exec(UnitTestWorld, *UnitCmd);


		// NOTE: This main loop is partly based off of FileServerCommandlet
		while (GIsRunning && !GIsRequestingExit)
		{
			GEngine->UpdateTimeAndHandleMaxTickRate();
			GEngine->Tick(FApp::GetDeltaTime(), false);

			if (FSlateApplication::IsInitialized())
			{
				FSlateApplication::Get().PumpMessages();
				FSlateApplication::Get().Tick();
			}


			// Execute deferred commands
			for (int32 DeferredCommandsIndex=0; DeferredCommandsIndex<GEngine->DeferredCommands.Num(); DeferredCommandsIndex++)
			{
				GEngine->Exec(UnitTestWorld, *GEngine->DeferredCommands[DeferredCommandsIndex], *GLog);
			}

			GEngine->DeferredCommands.Empty();


			FPlatformProcess::Sleep(0);


			// When the unit tests complete, open an exit-confirmation window, and when that closes, exit the main loop
			// NOTE: This will not execute, if the last open slate window is closed (such as when clicking 'yes' to 'abort all' dialog);
			//			in that circumstance, GIsRequestingExit gets set, by the internal engine code
			if (GUnitTestManager == NULL || !GUnitTestManager->IsRunningUnitTests())
			{
				if (bConfirmedExit || FApp::IsUnattended())
				{
					// Wait until the status window is closed, if it is still open, before exiting
					if (GUnitTestManager == NULL || !GUnitTestManager->StatusWindow.IsValid())
					{
						GIsRequestingExit = true;
					}
				}
				else if (!ConfirmDialog.IsValid())
				{
					FText CompleteMsg = FText::FromString(TEXT("Unit test commandlet complete."));
					FText CompleteTitle = FText::FromString(TEXT("Unit tests complete"));

					FOnLogDialogResult DialogCallback = FOnLogDialogResult::CreateLambda(
						[&](const TSharedRef<SWindow>& DialogWindow, EAppReturnType::Type Result, bool bNoResult)
						{
							if (DialogWindow == ConfirmDialog)
							{
								bConfirmedExit = true;
							}
						});

					ConfirmDialog = OpenLogDialog_NonModal(EAppMsgType::Ok, CompleteMsg, CompleteTitle, DialogCallback);


					// If the 'abort all' dialog was open, close it now as it is redundant;
					// can't close it before opening above dialog though, otherwise no slate windows, triggers an early exit
					if (GUnitTestManager != NULL && GUnitTestManager->AbortAllDialog.IsValid())
					{
						GUnitTestManager->AbortAllDialog->RequestDestroyWindow();

						GUnitTestManager->AbortAllDialog.Reset();
					}
				}
			}
		}

		// Cleanup the unit test world
		NUTNet::MarkUnitTestWorldForCleanup(UnitTestWorld, true);


		GIsRunning = false;
	}
#endif

	return (GWarn->Errors.Num() == 0 ? 0 : 1);
}
Example #3
0
void UGameEngine::Init(IEngineLoop* InEngineLoop)
{
	DECLARE_SCOPE_CYCLE_COUNTER(TEXT("UGameEngine Init"), STAT_GameEngineStartup, STATGROUP_LoadTime);

	// Call base.
	UEngine::Init(InEngineLoop);

#if USE_NETWORK_PROFILER
	FString NetworkProfilerTag;
	if( FParse::Value(FCommandLine::Get(), TEXT("NETWORKPROFILER="), NetworkProfilerTag ) )
	{
		GNetworkProfiler.EnableTracking(true);
	}
#endif

	// Load and apply user game settings
	GetGameUserSettings()->LoadSettings();
	GetGameUserSettings()->ApplySettings();

	// Creates the initial world context. For GameEngine, this should be the only WorldContext that ever gets created.
	FWorldContext &InitialWorldContext = CreateNewWorldContext(EWorldType::Game);

	// Initialize the viewport client.
	UGameViewportClient* ViewportClient = NULL;
	if(GIsClient)
	{
		ViewportClient = ConstructObject<UGameViewportClient>(GameViewportClientClass,this);
		ViewportClient->SetReferenceToWorldContext(InitialWorldContext);
		GameViewport = ViewportClient;
		InitialWorldContext.GameViewport = ViewportClient;
	}

	bCheckForMovieCapture = true;

	// Attach the viewport client to a new viewport.
	if(ViewportClient)
	{
		// This must be created before any gameplay code adds widgets
		bool bWindowAlreadyExists = GameViewportWindow.IsValid();
		if (!bWindowAlreadyExists)
		{
			GameViewportWindow = CreateGameWindow();
		}

		CreateGameViewport( ViewportClient );

		if( !bWindowAlreadyExists )
		{
			SwitchGameWindowToUseGameViewport();
		}
		FString Error;
		if(!ViewportClient->Init(Error))
		{
			UE_LOG(LogEngine, Fatal,TEXT("%s"),*Error);
		}
	}

	// Create default URL.
	// @note:  if we change how we determine the valid start up map update LaunchEngineLoop's GetStartupMap()
	FURL DefaultURL;
	DefaultURL.LoadURLConfig( TEXT("DefaultPlayer"), GGameIni );

	// Enter initial world.
	EBrowseReturnVal::Type BrowseRet = EBrowseReturnVal::Failure;
	FString Error;
	TCHAR Parm[4096]=TEXT("");
	const TCHAR* Tmp = FCommandLine::Get();

#if UE_BUILD_SHIPPING
	// In shipping don't allow an override
	Tmp = TEXT("");
#endif // UE_BUILD_SHIPPING

	const UGameMapsSettings* GameMapsSettings = GetDefault<UGameMapsSettings>();
	const FString& DefaultMap = GameMapsSettings->GetGameDefaultMap();
	if (!FParse::Token(Tmp, Parm, ARRAY_COUNT(Parm), 0) || Parm[0] == '-')
	{
		FCString::Strcpy(Parm, *(DefaultMap + GameMapsSettings->LocalMapOptions));
	}
	FURL URL( &DefaultURL, Parm, TRAVEL_Partial );
	if( URL.Valid )
	{
		BrowseRet = Browse(InitialWorldContext, URL, Error );
	}

	// If waiting for a network connection, go into the starting level.
	if (BrowseRet != EBrowseReturnVal::Success && FCString::Stricmp(Parm, *DefaultMap) != 0)
	{
		const FText Message = FText::Format( NSLOCTEXT("Engine", "MapNotFound", "The map specified on the commandline '{0}' could not be found. Would you like to load the default map instead?"), FText::FromString( URL.Map ) );

		// the map specified on the command-line couldn't be loaded.  ask the user if we should load the default map instead
		if ( FCString::Stricmp(*URL.Map, *DefaultMap) != 0 &&
			FMessageDialog::Open( EAppMsgType::OkCancel, Message ) != EAppReturnType::Ok)
		{
			// user canceled (maybe a typo while attempting to run a commandlet)
			FPlatformMisc::RequestExit( false );
			return;
		}
		else
		{
			BrowseRet = Browse(InitialWorldContext, FURL(&DefaultURL, *(DefaultMap + GameMapsSettings->LocalMapOptions), TRAVEL_Partial), Error);
		}
	}

	// Handle failure.
	if( BrowseRet != EBrowseReturnVal::Success )
	{
		UE_LOG(LogLoad, Fatal, TEXT("%s"), *FString::Printf( TEXT("Failed to enter %s: %s. Please check the log for errors."), Parm, *Error) );
	}
	UE_LOG(LogInit, Display, TEXT("Game Engine Initialized.") );

	// for IsInitialized()
	bIsInitialized = true;
}