2014-04-23 17:52:54 -04:00
# include "UnrealVersionSelector.h"
# include "RequiredProgramMainCPPInclude.h"
# include "DesktopPlatformModule.h"
# include "PlatformInstallation.h"
IMPLEMENT_APPLICATION ( UnrealVersionSelector , " UnrealVersionSelector " )
2014-05-14 14:53:19 -04:00
bool GenerateProjectFiles ( const FString & ProjectFileName ) ;
2014-04-23 17:52:54 -04:00
bool RegisterCurrentEngineDirectory ( )
{
2014-04-23 18:46:48 -04:00
// Prompt for registering this directory
if ( FPlatformMisc : : MessageBoxExt ( EAppMsgType : : YesNo , TEXT ( " Configure this directory as an Unreal Engine installation? " ) , TEXT ( " Question " ) ) ! = EAppReturnType : : Yes )
2014-04-23 18:07:04 -04:00
{
return false ;
}
2014-04-23 18:46:48 -04:00
// Get the current engine directory.
FString EngineRootDir = FPlatformProcess : : BaseDir ( ) ;
FPlatformInstallation : : NormalizeEngineRootDir ( EngineRootDir ) ;
// Get any existing tag name or register a new one
FString Identifier ;
if ( ! FDesktopPlatformModule : : Get ( ) - > GetEngineIdentifierFromRootDir ( EngineRootDir , Identifier ) )
{
2014-05-06 10:27:35 -04:00
FPlatformMisc : : MessageBoxExt ( EAppMsgType : : Ok , TEXT ( " Couldn't add engine installation. " ) , TEXT ( " Error " ) ) ;
return false ;
2014-04-23 18:46:48 -04:00
}
// If the launcher isn't installed, set up the file associations
if ( ! FDesktopPlatformModule : : Get ( ) - > VerifyFileAssociations ( ) )
{
// Relaunch as administrator
FString ExecutableFileName = FString ( FPlatformProcess : : BaseDir ( ) ) / FString ( FPlatformProcess : : ExecutableName ( false ) ) ;
int32 ExitCode ;
if ( ! FPlatformProcess : : ExecElevatedProcess ( * ExecutableFileName , TEXT ( " /fileassociations " ) , & ExitCode ) | | ExitCode ! = 0 )
{
return false ;
}
}
2014-04-23 18:07:04 -04:00
// Notify the user that everything is awesome.
FPlatformMisc : : MessageBoxExt ( EAppMsgType : : Ok , TEXT ( " Registration successful. " ) , TEXT ( " Success " ) ) ;
return true ;
2014-04-23 17:52:54 -04:00
}
2014-04-23 18:46:48 -04:00
bool UpdateFileAssociations ( )
2014-04-23 17:52:54 -04:00
{
// Update everything
2014-04-23 18:46:48 -04:00
if ( ! FDesktopPlatformModule : : Get ( ) - > UpdateFileAssociations ( ) )
2014-04-23 17:52:54 -04:00
{
2014-04-23 18:46:48 -04:00
FPlatformMisc : : MessageBoxExt ( EAppMsgType : : Ok , TEXT ( " Couldn't update file associations. " ) , TEXT ( " Error " ) ) ;
2014-04-23 17:52:54 -04:00
return false ;
}
return true ;
}
2014-05-14 14:53:19 -04:00
bool SwitchVersion ( const FString & ProjectFileName )
2014-04-23 17:52:54 -04:00
{
2014-04-23 18:46:48 -04:00
// Get the current identifier
FString Identifier ;
FDesktopPlatformModule : : Get ( ) - > GetEngineIdentifierForProject ( ProjectFileName , Identifier ) ;
// Select the new association
if ( ! FPlatformInstallation : : SelectEngineInstallation ( Identifier ) )
2014-04-23 17:52:54 -04:00
{
return false ;
}
2014-04-23 18:46:48 -04:00
// Update the project file
if ( ! FDesktopPlatformModule : : Get ( ) - > SetEngineIdentifierForProject ( ProjectFileName , Identifier ) )
2014-04-23 17:52:54 -04:00
{
FPlatformMisc : : MessageBoxExt ( EAppMsgType : : Ok , TEXT ( " Couldn't set association for project. Check the file is writeable. " ) , TEXT ( " Error " ) ) ;
return false ;
}
2014-04-23 18:32:52 -04:00
2014-05-15 17:03:50 -04:00
// If it's a content-only project, we're done
FProjectStatus ProjectStatus ;
if ( IProjectManager : : Get ( ) . QueryStatusForProject ( ProjectFileName , ProjectStatus ) & & ! ProjectStatus . bCodeBasedProject )
{
return true ;
}
2014-05-14 14:53:19 -04:00
// Generate project files
return GenerateProjectFiles ( ProjectFileName ) ;
2014-04-23 17:52:54 -04:00
}
2014-05-14 14:53:19 -04:00
bool GetEngineRootDirForProject ( const FString & ProjectFileName , FString & OutRootDir )
2014-04-23 17:52:54 -04:00
{
2014-04-23 18:46:48 -04:00
FString Identifier ;
return FDesktopPlatformModule : : Get ( ) - > GetEngineIdentifierForProject ( ProjectFileName , Identifier ) & & FDesktopPlatformModule : : Get ( ) - > GetEngineRootDirFromIdentifier ( Identifier , OutRootDir ) ;
2014-04-23 17:52:54 -04:00
}
2014-05-14 14:53:19 -04:00
bool GetValidatedEngineRootDir ( const FString & ProjectFileName , FString & OutRootDir )
2014-04-23 17:52:54 -04:00
{
// Get the engine directory for this project
if ( ! GetEngineRootDirForProject ( ProjectFileName , OutRootDir ) )
{
// Try to set an association
2014-04-23 18:46:48 -04:00
if ( ! SwitchVersion ( ProjectFileName ) )
2014-04-23 17:52:54 -04:00
{
return false ;
}
// See if it's valid now
if ( ! GetEngineRootDirForProject ( ProjectFileName , OutRootDir ) )
{
FPlatformMisc : : MessageBoxExt ( EAppMsgType : : Ok , TEXT ( " Error retrieving project root directory " ) , TEXT ( " Error " ) ) ;
return false ;
}
}
return true ;
}
2014-05-14 14:53:19 -04:00
bool LaunchEditor ( const FString & ProjectFileName , const FString & Arguments )
2014-04-23 17:52:54 -04:00
{
// Get the engine root directory
FString RootDir ;
if ( ! GetValidatedEngineRootDir ( ProjectFileName , RootDir ) )
{
return false ;
}
// Launch the editor
if ( ! FPlatformInstallation : : LaunchEditor ( RootDir , FString : : Printf ( TEXT ( " \" %s \" %s " ) , * ProjectFileName , * Arguments ) ) )
{
FPlatformMisc : : MessageBoxExt ( EAppMsgType : : Ok , TEXT ( " Failed to launch editor " ) , TEXT ( " Error " ) ) ;
return false ;
}
return true ;
}
2014-05-14 14:53:19 -04:00
bool GenerateProjectFiles ( const FString & ProjectFileName )
2014-04-23 17:52:54 -04:00
{
2014-05-14 14:53:19 -04:00
IDesktopPlatform * DesktopPlatform = FDesktopPlatformModule : : Get ( ) ;
2014-04-23 17:52:54 -04:00
// Get the engine root directory
FString RootDir ;
if ( ! GetValidatedEngineRootDir ( ProjectFileName , RootDir ) )
{
return false ;
}
// Build the argument list
2014-04-23 18:28:06 -04:00
FString Arguments = TEXT ( " -game " ) ;
2014-04-23 18:46:48 -04:00
if ( FDesktopPlatformModule : : Get ( ) - > IsSourceDistribution ( RootDir ) )
2014-04-23 17:52:54 -04:00
{
Arguments + = TEXT ( " -engine " ) ;
}
2014-05-14 14:53:19 -04:00
// Start capturing the log output
FStringOutputDevice LogCapture ;
LogCapture . SetAutoEmitLineTerminator ( true ) ;
GLog - > AddOutputDevice ( & LogCapture ) ;
// Generate project files
FFeedbackContext * Warn = DesktopPlatform - > GetNativeFeedbackContext ( ) ;
bool bResult = DesktopPlatform - > GenerateProjectFiles ( RootDir , ProjectFileName , Warn ) ;
GLog - > RemoveOutputDevice ( & LogCapture ) ;
// Display an error dialog if we failed
if ( ! bResult )
2014-04-23 17:52:54 -04:00
{
2014-05-14 14:53:19 -04:00
FPlatformInstallation : : ErrorDialog ( TEXT ( " Failed to generate project files. " ) , LogCapture ) ;
2014-04-23 17:52:54 -04:00
return false ;
}
return true ;
}
2014-05-14 14:53:19 -04:00
int Main ( const TArray < FString > & Arguments )
2014-04-23 17:52:54 -04:00
{
bool bRes = false ;
if ( Arguments . Num ( ) = = 0 )
{
// Add the current directory to the list of installations
bRes = RegisterCurrentEngineDirectory ( ) ;
}
2014-04-23 18:46:48 -04:00
else if ( Arguments . Num ( ) = = 1 & & Arguments [ 0 ] = = TEXT ( " /fileassociations " ) )
2014-04-23 17:52:54 -04:00
{
// Update all the settings.
2014-04-23 18:46:48 -04:00
bRes = UpdateFileAssociations ( ) ;
2014-04-23 17:52:54 -04:00
}
2014-04-23 18:46:48 -04:00
else if ( Arguments . Num ( ) = = 2 & & Arguments [ 0 ] = = TEXT ( " /switchversion " ) )
2014-04-23 17:52:54 -04:00
{
// Associate with an engine label
2014-04-23 18:46:48 -04:00
bRes = SwitchVersion ( Arguments [ 1 ] ) ;
2014-04-23 17:52:54 -04:00
}
else if ( Arguments . Num ( ) = = 2 & & Arguments [ 0 ] = = TEXT ( " /editor " ) )
{
// Open a project with the editor
bRes = LaunchEditor ( Arguments [ 1 ] , L " " ) ;
}
else if ( Arguments . Num ( ) = = 2 & & Arguments [ 0 ] = = TEXT ( " /game " ) )
{
// Play a game using the editor executable
2014-04-25 13:30:17 -04:00
bRes = LaunchEditor ( Arguments [ 1 ] , L " -game " ) ;
2014-04-23 17:52:54 -04:00
}
2014-04-23 18:28:06 -04:00
else if ( Arguments . Num ( ) = = 2 & & Arguments [ 0 ] = = TEXT ( " /projectfiles " ) )
2014-04-23 17:52:54 -04:00
{
// Generate Visual Studio project files
bRes = GenerateProjectFiles ( Arguments [ 1 ] ) ;
}
else
{
// Invalid command line
FPlatformMisc : : MessageBoxExt ( EAppMsgType : : Ok , TEXT ( " Invalid command line " ) , NULL ) ;
}
return bRes ? 0 : 1 ;
}
# if PLATFORM_WINDOWS
# include "AllowWindowsPlatformTypes.h"
# include <Shellapi.h>
int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int ShowCmd )
{
int ArgC ;
2014-05-14 14:53:19 -04:00
LPWSTR * ArgV = : : CommandLineToArgvW ( GetCommandLine ( ) , & ArgC ) ;
2014-04-23 17:52:54 -04:00
FCommandLine : : Set ( TEXT ( " " ) ) ;
TArray < FString > Arguments ;
for ( int Idx = 1 ; Idx < ArgC ; Idx + + )
{
Arguments . Add ( ArgV [ Idx ] ) ;
}
return Main ( Arguments ) ;
}
# include "HideWindowsPlatformTypes.h"
2014-04-23 18:02:49 -04:00
# else
2014-05-14 14:53:19 -04:00
int main ( int ArgC , const char * ArgV [ ] )
2014-04-23 18:02:49 -04:00
{
FCommandLine : : Set ( TEXT ( " " ) ) ;
TArray < FString > Arguments ;
for ( int Idx = 1 ; Idx < ArgC ; Idx + + )
{
Arguments . Add ( ArgV [ Idx ] ) ;
}
return Main ( Arguments ) ;
}
2014-04-23 17:52:54 -04:00
# endif