Files
UnrealEngineUWP/Engine/Source/Developer/DesktopPlatform/Private/Linux/DesktopPlatformLinux.cpp
Ben Marsh 4ba423868f Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340)
#lockdown Nick.Penwarden
#rb none

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3209340 on 2016/11/23 by Ben.Marsh

	Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h.

	Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms.

	  * Every header now includes everything it needs to compile.
	        * There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first.
	        * There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h.
	  * Every .cpp file includes its matching .h file first.
	        * This helps validate that each header is including everything it needs to compile.
	  * No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more.
	        * You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there.
	        * There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible.
	  * No engine code explicitly includes a precompiled header any more.
	        * We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies.
	        * PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files.

	Tool used to generate this transform is at Engine\Source\Programs\IncludeTool.

[CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00

494 lines
16 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "Linux/DesktopPlatformLinux.h"
#include "HAL/FileManager.h"
#include "Misc/Paths.h"
#include "Misc/Guid.h"
#include "Misc/ConfigCacheIni.h"
#include "Misc/FeedbackContext.h"
#include "DesktopPlatformPrivate.h"
#include "Modules/ModuleManager.h"
#include "Linux/LinuxApplication.h"
#include "Misc/FeedbackContextMarkup.h"
#include "HAL/ThreadHeartBeat.h"
//#include "LinuxNativeFeedbackContext.h"
// custom dialogs
#if WITH_LINUX_NATIVE_DIALOGS
#include "UNativeDialogs.h"
#else
#include "ISlateFileDialogModule.h"
#endif // WITH_LINUX_NATIVE_DIALOGS
#define LOCTEXT_NAMESPACE "DesktopPlatform"
#define MAX_FILETYPES_STR 4096
#define MAX_FILENAME_STR 65536
FDesktopPlatformLinux::FDesktopPlatformLinux()
: FDesktopPlatformBase()
{
#if WITH_LINUX_NATIVE_DIALOGS
bool bLNDInit = ULinuxNativeDialogs_Initialize();
if (bLNDInit)
{
UE_LOG(LogDesktopPlatform, Log, TEXT("LinuxNativeDialogs have been successfully initialized."));
}
else
{
UE_LOG(LogDesktopPlatform, Warning, TEXT("DesktopPlatformLinux could not initialize LinuxNativeDialogs - it will not work properly."));
}
#else
UE_LOG(LogDesktopPlatform, Log, TEXT("DesktopPlatformLinux is not using LinuxNativeDialogs."));
#endif // WITH_LINUX_NATIVE_DIALOGS
}
FDesktopPlatformLinux::~FDesktopPlatformLinux()
{
#if WITH_LINUX_NATIVE_DIALOGS
ULinuxNativeDialogs_Shutdown();
#endif
}
bool FDesktopPlatformLinux::OpenFileDialog(const void* ParentWindowHandle, const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray<FString>& OutFilenames, int32& OutFilterIndex)
{
#if WITH_LINUX_NATIVE_DIALOGS
return FileDialogShared(false, ParentWindowHandle, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames, OutFilterIndex);
#else
if (!FModuleManager::Get().IsModuleLoaded("SlateFileDialogs"))
{
FModuleManager::Get().LoadModule("SlateFileDialogs");
}
ISlateFileDialogsModule *FileDialog = FModuleManager::GetModulePtr<ISlateFileDialogsModule>("SlateFileDialogs");
if (FileDialog)
{
return FileDialog->OpenFileDialog(ParentWindowHandle, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames, OutFilterIndex);
}
return false;
#endif // WITH_LINUX_NATIVE_DIALOGS
}
bool FDesktopPlatformLinux::OpenFileDialog(const void* ParentWindowHandle, const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray<FString>& OutFilenames)
{
#if WITH_LINUX_NATIVE_DIALOGS
int32 DummyFilterIndex;
return FileDialogShared(false, ParentWindowHandle, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames, DummyFilterIndex);
#else
if (!FModuleManager::Get().IsModuleLoaded("SlateFileDialogs"))
{
FModuleManager::Get().LoadModule("SlateFileDialogs");
}
ISlateFileDialogsModule *FileDialog = FModuleManager::GetModulePtr<ISlateFileDialogsModule>("SlateFileDialogs");
if (FileDialog)
{
return FileDialog->OpenFileDialog(ParentWindowHandle, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames);
}
return false;
#endif // WITH_LINUX_NATIVE_DIALOGS
}
bool FDesktopPlatformLinux::SaveFileDialog(const void* ParentWindowHandle, const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray<FString>& OutFilenames)
{
#if WITH_LINUX_NATIVE_DIALOGS
int32 DummyFilterIndex = 0;
return FileDialogShared(true, ParentWindowHandle, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames, DummyFilterIndex);
#else
if (!FModuleManager::Get().IsModuleLoaded("SlateFileDialogs"))
{
FModuleManager::Get().LoadModule("SlateFileDialogs");
}
ISlateFileDialogsModule *FileDialog = FModuleManager::GetModulePtr<ISlateFileDialogsModule>("SlateFileDialogs");
if (FileDialog)
{
return FileDialog->SaveFileDialog(ParentWindowHandle, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames);
}
return false;
#endif // WITH_LINUX_NATIVE_DIALOGS
}
bool FDesktopPlatformLinux::OpenDirectoryDialog(const void* ParentWindowHandle, const FString& DialogTitle, const FString& DefaultPath, FString& OutFolderName)
{
#if WITH_LINUX_NATIVE_DIALOGS
bool bSuccess;
struct UFileDialogHints hints = DEFAULT_UFILEDIALOGHINTS;
LinuxApplication->SetCapture(NULL);
hints.Action = UFileDialogActionOpenDirectory;
hints.WindowTitle = TCHAR_TO_UTF8(*DialogTitle);
hints.InitialDirectory = TCHAR_TO_UTF8(*DefaultPath);
UFileDialog* dialog = UFileDialog_Create(&hints);
while(UFileDialog_ProcessEvents(dialog))
{
FPlatformMisc::PumpMessages(true); // pretend that we're the main loop
}
const UFileDialogResult* result = UFileDialog_Result(dialog);
if (result)
{
if (result->count == 1)
{
OutFolderName = UTF8_TO_TCHAR(result->selection[0]);
//OutFolderName = IFileManager::Get().ConvertToRelativePath(*OutFolderName); // @todo (amigo): converting to relative path ends up without ../...
FPaths::NormalizeFilename(OutFolderName);
bSuccess = true;
}
else
{
bSuccess = false;
}
// Todo like in Windows, normalize files here instead of above
}
else
{
bSuccess = false;
}
UFileDialog_Destroy(dialog);
return bSuccess;
#else
if (!FModuleManager::Get().IsModuleLoaded("SlateFileDialogs"))
{
FModuleManager::Get().LoadModule("SlateFileDialogs");
}
ISlateFileDialogsModule *FileDialog = FModuleManager::GetModulePtr<ISlateFileDialogsModule>("SlateFileDialogs");
if (FileDialog)
{
return FileDialog->OpenDirectoryDialog(ParentWindowHandle, DialogTitle, DefaultPath, OutFolderName);
}
return false;
#endif // WITH_LINUX_NATIVE_DIALOGS
}
bool FDesktopPlatformLinux::OpenFontDialog(const void* ParentWindowHandle, FString& OutFontName, float& OutHeight, EFontImportFlags& OutFlags)
{
unimplemented();
return false;
}
bool FDesktopPlatformLinux::CanOpenLauncher(bool Install)
{
// TODO: no launcher support at the moment
return false;
}
bool FDesktopPlatformLinux::OpenLauncher(const FOpenLauncherOptions& Options)
{
// TODO: support launcher for realz
return true;
}
bool FDesktopPlatformLinux::FileDialogShared(bool bSave, const void* ParentWindowHandle, const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray<FString>& OutFilenames, int32& OutFilterIndex)
{
#if WITH_LINUX_NATIVE_DIALOGS
FString Filename;
bool bSuccess;
LinuxApplication->SetCapture(NULL);
UE_LOG(LogDesktopPlatform, Warning, TEXT("FileDialogShared DialogTitle: %s, DefaultPath: %s, DefaultFile: %s, FileTypes: %s, Flags: %d"),
*DialogTitle, *DefaultPath, *DefaultFile, *FileTypes, Flags);
struct UFileDialogHints hints = DEFAULT_UFILEDIALOGHINTS;
hints.WindowTitle = TCHAR_TO_UTF8(*DialogTitle);
FString AllExtensionsSpaceDelim; // a string like "*.cpp *.h *.c"
// The strings will come in pairs, formatted as follows: Pair1String1|Pair1String2|Pair2String1|Pair2String2
// where the second string in the pair is the extension(s)
TArray<FString> Filters;
FileTypes.ParseIntoArray(Filters, TEXT("|"), true);
for( int32 ExtensionIndex = 1; ExtensionIndex < Filters.Num(); ExtensionIndex += 2)
{
const FString& Extensions = Filters[ExtensionIndex];
TArray<FString> ExtensionsArray;
// Extension can be either *.jpg or *.jpg;*.png -> in the latter case split at ';'
int32 UnusedIndex;
if (Extensions.FindChar(TEXT(';'), UnusedIndex))
{
Extensions.ParseIntoArray(ExtensionsArray, TEXT(";"), true);
}
else
{
ExtensionsArray.Add(Extensions); // just a single extension
}
for (const FString& Extension : ExtensionsArray)
{
if (AllExtensionsSpaceDelim.Find(Extension, ESearchCase::IgnoreCase) == INDEX_NONE)
{
AllExtensionsSpaceDelim += Extension;
AllExtensionsSpaceDelim += TEXT(" ");
}
}
}
FString AllExtensionsLumpedTogether(TEXT("All applicable ("));
AllExtensionsLumpedTogether += AllExtensionsSpaceDelim;
AllExtensionsLumpedTogether += TEXT(")");
char FileTypesBuf[MAX_FILETYPES_STR * 2] = {0,};
FTCHARToUTF8_Convert::Convert(FileTypesBuf, sizeof(FileTypesBuf), *AllExtensionsLumpedTogether, AllExtensionsLumpedTogether.Len());
hints.NameFilter = FileTypesBuf;
char DefPathBuf[MAX_FILENAME_STR * 2] = {0,};
FTCHARToUTF8_Convert::Convert(DefPathBuf, sizeof(DefPathBuf), *DefaultPath, DefaultPath.Len());
hints.InitialDirectory = DefPathBuf;
char DefFileBuf[MAX_FILENAME_STR * 2] = {0,};
FTCHARToUTF8_Convert::Convert(DefFileBuf, sizeof(DefFileBuf), *DefaultFile, DefaultFile.Len());
hints.DefaultFile = DefFileBuf;
if (bSave)
{
hints.Action = UFileDialogActionSave;
}
else
{
hints.Action = UFileDialogActionOpen;
}
UFileDialog* dialog = UFileDialog_Create(&hints);
while(UFileDialog_ProcessEvents(dialog))
{
FPlatformMisc::PumpMessages(true); // pretend that we're the main loop
}
const UFileDialogResult* result = UFileDialog_Result(dialog);
if (result)
{
if (result->count > 1)
{
// Todo better handling of multi-selects
UE_LOG(LogDesktopPlatform, Warning, TEXT("FileDialogShared Selected Files: %d"), result->count);
for(int i = 0;i < result->count;++i) {
//Filename = FUTF8ToTCHAR(result->selection[i], MAX_FILENAME_STR).Get();
Filename = UTF8_TO_TCHAR(result->selection[i]);
//new(OutFilenames) FString(Filename);
OutFilenames.Add(Filename);
Filename = IFileManager::Get().ConvertToRelativePath(*Filename);
FPaths::NormalizeFilename(Filename);
//UE_LOG(LogDesktopPlatform, Warning, TEXT("FileDialogShared File: %s"), *Filename);
}
bSuccess = true;
}
else if (result->count == 1)
{
//Filename = FUTF8ToTCHAR(result->selection[0], MAX_FILENAME_STR).Get();
Filename = UTF8_TO_TCHAR(result->selection[0]);
//new(OutFilenames) FString(Filename);
OutFilenames.Add(Filename);
Filename = IFileManager::Get().ConvertToRelativePath(*Filename);
FPaths::NormalizeFilename(Filename);
//UE_LOG(LogDesktopPlatform, Warning, TEXT("FileDialogShared File: %s"), *Filename);
bSuccess = true;
}
else
{
bSuccess = false;
}
// Todo like in Windows, normalize files here instead of above
}
else
{
bSuccess = false;
}
UFileDialog_Destroy(dialog);
return bSuccess;
#else
return false;
#endif // WITH_LINUX_NATIVE_DIALOGS
}
bool FDesktopPlatformLinux::RegisterEngineInstallation(const FString &RootDir, FString &OutIdentifier)
{
bool bRes = false;
if (IsValidRootDirectory(RootDir))
{
FConfigFile ConfigFile;
FString ConfigPath = FString(FPlatformProcess::ApplicationSettingsDir()) / FString(TEXT("UnrealEngine")) / FString(TEXT("Install.ini"));
ConfigFile.Read(ConfigPath);
FConfigSection &Section = ConfigFile.FindOrAdd(TEXT("Installations"));
OutIdentifier = FGuid::NewGuid().ToString(EGuidFormats::DigitsWithHyphensInBraces);
Section.AddUnique(*OutIdentifier, RootDir);
ConfigFile.Dirty = true;
ConfigFile.Write(ConfigPath);
}
return bRes;
}
void FDesktopPlatformLinux::EnumerateEngineInstallations(TMap<FString, FString> &OutInstallations)
{
EnumerateLauncherEngineInstallations(OutInstallations);
FString UProjectPath = FString(FPlatformProcess::ApplicationSettingsDir()) / "Unreal.uproject";
FArchive* File = IFileManager::Get().CreateFileWriter(*UProjectPath, FILEWRITE_EvenIfReadOnly);
if (File)
{
File->Close();
delete File;
}
else
{
FSlowHeartBeatScope SuspendHeartBeat;
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Unable to write to Settings Directory", TCHAR_TO_UTF8(*UProjectPath), NULL);
}
FConfigFile ConfigFile;
FString ConfigPath = FString(FPlatformProcess::ApplicationSettingsDir()) / FString(TEXT("UnrealEngine")) / FString(TEXT("Install.ini"));
ConfigFile.Read(ConfigPath);
FConfigSection &Section = ConfigFile.FindOrAdd(TEXT("Installations"));
// Remove invalid entries
// @todo The installations list might contain multiple keys for the same value. Do we have to remove them?
TArray<FName> KeysToRemove;
for (auto It : Section)
{
const FString& EngineDir = It.Value.GetValue();
// We remove entries pointing to a folder that doesn't exist or was using the wrong path.
if (EngineDir.Contains(FPaths::EngineDir()) || !IFileManager::Get().DirectoryExists(*EngineDir))
{
KeysToRemove.Add(It.Key);
ConfigFile.Dirty = true;
}
}
for (auto Key : KeysToRemove)
{
Section.Remove(Key);
}
// @todo: currently we can enumerate only this installation
FString EngineDir = FPaths::RootDir();
FPaths::NormalizeDirectoryName(EngineDir);
FPaths::CollapseRelativeDirectories(EngineDir);
FString EngineId;
const FName* Key = Section.FindKey(EngineDir);
if (Key)
{
FGuid IdGuid;
FGuid::Parse(Key->ToString(), IdGuid);
EngineId = IdGuid.ToString(EGuidFormats::DigitsWithHyphensInBraces);;
}
else
{
if (!OutInstallations.FindKey(EngineDir))
{
EngineId = FGuid::NewGuid().ToString(EGuidFormats::DigitsWithHyphensInBraces);
Section.AddUnique(*EngineId, EngineDir);
ConfigFile.Dirty = true;
}
}
if (!EngineId.IsEmpty() && !OutInstallations.Find(EngineId))
{
OutInstallations.Add(EngineId, EngineDir);
}
ConfigFile.Write(ConfigPath);
IFileManager::Get().Delete(*UProjectPath);
}
bool FDesktopPlatformLinux::IsSourceDistribution(const FString &RootDir)
{
// Check for the existence of a GenerateProjectFiles.sh file. This allows compatibility with the GitHub 4.0 release.
FString GenerateProjectFilesPath = RootDir / TEXT("GenerateProjectFiles.sh");
if (IFileManager::Get().FileSize(*GenerateProjectFilesPath) >= 0)
{
return true;
}
// Otherwise use the default test
return FDesktopPlatformBase::IsSourceDistribution(RootDir);
}
bool FDesktopPlatformLinux::VerifyFileAssociations()
{
STUBBED("FDesktopPlatformLinux::VerifyFileAssociationsg");
return true; // for now we are associated
}
bool FDesktopPlatformLinux::UpdateFileAssociations()
{
//unimplemented();
STUBBED("FDesktopPlatformLinux::UpdateFileAssociations");
return false;
}
bool FDesktopPlatformLinux::OpenProject(const FString &ProjectFileName)
{
// Get the project filename in a native format
FString PlatformProjectFileName = ProjectFileName;
FPaths::MakePlatformFilename(PlatformProjectFileName);
STUBBED("FDesktopPlatformLinux::OpenProject");
return false;
}
bool FDesktopPlatformLinux::RunUnrealBuildTool(const FText& Description, const FString& RootDir, const FString& Arguments, FFeedbackContext* Warn)
{
// Get the path to UBT
FString UnrealBuildToolPath = RootDir / TEXT("Engine/Binaries/DotNET/UnrealBuildTool.exe");
if(IFileManager::Get().FileSize(*UnrealBuildToolPath) < 0)
{
Warn->Logf(ELogVerbosity::Error, TEXT("Couldn't find UnrealBuildTool at '%s'"), *UnrealBuildToolPath);
return false;
}
// Write the output
Warn->Logf(TEXT("Running %s %s"), *UnrealBuildToolPath, *Arguments);
// launch UBT with Mono
FString ScriptPath = FPaths::ConvertRelativePathToFull(RootDir / TEXT("Engine/Build/BatchFiles/Linux/RunMono.sh"));
FString CmdLineParams = FString::Printf(TEXT("\"%s\" \"%s\" %s"), *ScriptPath, *UnrealBuildToolPath, *Arguments);
// Spawn it with bash (and not sh) because of pushd
int32 ExitCode = 0;
return FFeedbackContextMarkup::PipeProcessOutput(Description, TEXT("/bin/bash"), CmdLineParams, Warn, &ExitCode) && ExitCode == 0;
}
bool FDesktopPlatformLinux::IsUnrealBuildToolRunning()
{
// For now assume that if mono application is running, we're running UBT
// @todo: we need to get the commandline for the mono process and check if UBT.exe is in there.
return FPlatformProcess::IsApplicationRunning(TEXT("mono"));
}
FFeedbackContext* FDesktopPlatformLinux::GetNativeFeedbackContext()
{
//unimplemented();
STUBBED("FDesktopPlatformLinux::GetNativeFeedbackContext");
return GWarn;
}
FString FDesktopPlatformLinux::GetUserTempPath()
{
return FString(FPlatformProcess::UserTempDir());
}
#undef LOCTEXT_NAMESPACE