Files
UnrealEngineUWP/Engine/Source/Programs/UnrealBuildTool/System/FileSystemReference.cs
Ben Marsh 111ec7adc5 Copying //UE4/Dev-Core to //UE4/Dev-Main (Source: //UE4/Dev-Core @ 3314870)
#lockdown Nick.Penwarden

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

Change 3284872 on 2017/02/03 by Graeme.Thornton

	Seperate pak cache granularity from pak signing chunk size

Change 3285765 on 2017/02/03 by Graeme.Thornton

	Fix stats warnings because each slate new loading screen thread has the same stat name, but is assigned to a different thread

	#jira UE-41478

Change 3286913 on 2017/02/04 by Ben.Marsh

	IncludeTool: Merging fixes.

	* Don't remove existing forward declarations unless explicitly instructed to do so. Files are optimized with these declarations in place, so removing them can cause output files to fail to build. It can be a useful separate step though, so expose it as a command-line option instead.
	* Add a specific option for which files should be output by the tool. Any files which are excluded from this list are treated specially when generating output files, so as to prevent them from causing files to be omitted from other files that include them. Also add an option to force this mode for all headers, for use when testing formatting/include path generation.

Change 3287100 on 2017/02/05 by Ben.Marsh

	UBT: Move platform settings into platform-specific TargetRules objects.

Change 3287106 on 2017/02/05 by Ben.Marsh

	Merge UEBuildPlatformContext into UEBuildPlatform. Now that targets can have platform-specific settings, there is no need to separate a platform class which contains target-specific information.

Change 3287398 on 2017/02/06 by Steve.Robb

	Fix for UHT failing when -WarningsAsErrors and -Verbose are specified together.

Change 3287399 on 2017/02/06 by Steve.Robb

	Log verbosities made more readable in the debugger.

Change 3287410 on 2017/02/06 by Steve.Robb

	Fix for TStructOpsTypeTraits where WithCopy gives a different result between specializing the traits and not providing WithCopy and not specializing the traits at all.

	#fyi marc.audy

Change 3288020 on 2017/02/06 by Ben.Marsh

	Prevent forward declaration of the ITextData class. We need to include the header for the debugger visualizers to work correctly.

Change 3291817 on 2017/02/08 by Steve.Robb

	New EBlueprintCompileReinstancerFlags used to construct FBlueprintCompileReinstancer, instead of lots of bools.

Change 3292090 on 2017/02/08 by Graeme.Thornton

	Crash fix - don't update font engine services if it was never created

	#jira UE-33953

Change 3292993 on 2017/02/08 by Ben.Marsh

	Add an option to disable force-including PCHs for files in the non-unity working set. (bAdaptiveUnityDisablesPCH)

Change 3293231 on 2017/02/08 by Ben.Marsh

	BuildGraph: Allow overriding the changelist that a badge should be displayed for (with the Change="" attribute on the Badge declaration in XML), so the code changelist can be used if necessary. Also link to the failed step if only one has failed.

Change 3294213 on 2017/02/09 by Ben.Marsh

	EC: Allow setting a property on frequent CI jobs that allows us to exclude it from job searches for generating the dashboard. Filtering on the client side is causing dashboard pages to be almost empty.

Change 3294753 on 2017/02/09 by Ben.Zeigler

	#jira UE-41151 Fix UObjectLibrary::RemoveObject to remove from the correct array, and add comment mentioning that the dynamic use of Object Library is semi-deprecated

Change 3296070 on 2017/02/09 by Ben.Zeigler

	Explicitly turn off Copy for a struct that has a linked list internally. I think turning Copy on by default for all non POD Types is pretty risky and is likely to crash for other games. In this case it was being copied for network replication, and it didn't have one defined so the default C++ one copied the linked list and crashed on destruction.

Change 3296420 on 2017/02/10 by Graeme.Thornton

	Remove remaining references to AES_KEY, instead using the encryption key delegates to access the key where needed
	Refactored encryption and signing key access in unrealpak to make it easier to use

Change 3296609 on 2017/02/10 by Ben.Marsh

	BuildGraph: Fix error running the <Copy> task with an empty "From" argument.

	* FileSystemReference.IsUnderDirectory() was not correctly handling cases where the directory was a root directory (and has to end in a path separator)
	* FilePattern.AsDirectoryReference() with an empty token would append a path separator to an empty string, resulting in it referencing the root directory rather than the given base directory.

Change 3297440 on 2017/02/10 by Ben.Marsh

	UBT: Move the FileFilter class into UnrealBuildTool.

Change 3297725 on 2017/02/10 by Ben.Zeigler

	#jira UE-39199 Fix issue with enum value redirects using the wrong short or long name, it now fully supports both.
	Clean up a lot of confusingly named and broken functions on UEnum:
	#jira UE-41348 Deprecate FindEnumIndex, GetEnum, GetEnumName, replace with GetIndexByName, GetNameByIndex, and GetNameStringByIndex and clean up warnings
	#jira UE-38187 Deprecate GetDisplayNameText and GetEnumText, replaced both with GetDisplayNameTextAtIndex which is now callable outside the editor and has a better comment
	Deprecate FindEnumRedirects and replace with GetIndexByNameString. Fix code to not check the redirects array 5 times per enum lookup
	Fix GetValueAsString to actually act on a value, not an index. This matches common usage and the function's name
	While fixing deprecation warnings on internal games, fixed dozens of cases where it was using Index functions when it should have been using Value functions
	Delete some now redundant enum editor code and pipe everything through UEnum

Change 3297979 on 2017/02/10 by Ben.Zeigler

	Fix issues parsing Enums that are literally the string "None", which is allowed but leads to some odd behavior

Change 3298299 on 2017/02/10 by Steve.Robb

	TTuple improvements:
	- equality comparable
	- serializable
	- in the correct folder

	2-tuples are specialized to be syntactically compatible with both TPair and TTuple.
	TPair is now an alias for a 2-tuple and is no longer bound to TPairInitializer.

	#fyi robert.manuszewski,ben.marsh

Change 3298460 on 2017/02/11 by Ben.Marsh

	UGS: Set the correct result from running custom tasks.

Change 3298462 on 2017/02/11 by Ben.Marsh

	UBT: Fix some deprecated messages that have the wrong release version, and add a better message for how ModuleRules constructors need to be updated.

Change 3299447 on 2017/02/13 by Graeme.Thornton

	Fix AES and pak signing key embedding for content only projects
	 - Force temp target when any keys are specified by project config

Change 3299649 on 2017/02/13 by Steve.Robb

	PLATFORM_HAS_DEFAULTED_OPERATORS fixed.
	Other obsolete compiler switches removed.

Change 3299787 on 2017/02/13 by Steve.Robb

	IsAbstract() for testing if a reflected native type contains pure virtual functions.  Needed for BP nativization.

	#fyi robert.manuszewski

Change 3300576 on 2017/02/13 by Ben.Marsh

	EC: Add support for starting builds on any agent type. Mapping from agent types to resource pools is stored in an EC property sheet (/Generated/<Stream>/AgentTypes), allowing EC procedures to map it to a resource pool from a parameter.

Change 3300600 on 2017/02/13 by Ben.Marsh

	EC: Add the -ClearHistory argument to UAT run to export BuildGraph settings, to allow running on incremental workspaces.

Change 3300624 on 2017/02/13 by Ben.Marsh

	Switch incremental builds for all streams to start up on the incremental agent.

Change 3302134 on 2017/02/14 by Steve.Robb

	UnrealCodeAnalyzer removed.

	#fyi ben.marsh,robert.manuszewski

Change 3302639 on 2017/02/14 by Ben.Zeigler

	Fix crash cooking odin with default command line
	#jira UE-41952 Delete StealthTeleport map that crashes on load, and update default cook list that gets used if nothing specified

Change 3303002 on 2017/02/14 by Ben.Zeigler

	#jira UE-41061 Fix it so editor only filtering on savepackage is uniformly applied regardless of if it's at package or object level
	#jira UE-41880 Rewrite editor/client/server only filtering logic in SavePackage to fix various bugs. It now does all of the filtering up front, and won't process any filtered objects for imports or exports
	Rename NotForEditorGame to NotAlwaysLoadedForEditorGame and improve comments, this flag says that the asset should be loaded EVEN IF it is editor only, it does not affect loading for normal objects
	Change the non-map cook flags to RF_Public instead of RF_Standalone. Blueprint classes aren't RF_Standalone so were only being cooked before due to an accident of the dependency checker
	Change it so anything with a Transient outer is marked transient at save time. These objects would not save out properly anyway
	Fix it so -cooksinglepackage works properly again and excludes localization and startup packages
	Tested with Fortnite and Odin, Odin works but with lots of warnings with nativization on which I need to investigate

Change 3303084 on 2017/02/14 by Ben.Zeigler

	Attempt to get Nativization and EDL working without warnings

	Change 3305153 on 2017/02/15 by Ben.Zeigler

	Fix Fortnite and Orion cook, I don't understand why this passed my local testing
	Fix the CDO subobject finder to actually return things instead of doing nothing, and fix a shadow variable warning

Change 3305959 on 2017/02/16 by Gil.Gribb

	UE4 - Tweaked out the EDL loader for the switch with benefits to all platforms.

Change 3306159 on 2017/02/16 by Ben.Marsh

	Fix path to target binaries when building non-monolithic in a unique build environment.

Change 3306584 on 2017/02/16 by Steve.Robb

	UEnum internal functions renamed from Index to Value.
	GetValueAsString_Internal() parameter now takes an int64, as is expected for enum values.

	#fyi ben.zeigler

Change 3307836 on 2017/02/16 by Ben.Zeigler

	#jira UE-42055 Load very old redirects in cooked builds. Matinee has no way of resaving redirects, so as long as matinee exists we need to keep them around forever, or fix matinee manually
	Fixes lighting in Infiltrator demo

Change 3307929 on 2017/02/16 by Ben.Zeigler

	#jira UE-42055 Second half of matinee redirector fix

Change 3308840 on 2017/02/17 by Matthew.Griffin

	Reimplementing CL#3305808 from 4.15

		Changed QA label build process so that it only allows version with 3 components (we always add the .0 for initial releases)

Change 3309115 on 2017/02/17 by Ben.Marsh

	Windows: Fix the GetModulesDirectory() function always returning the engine binaries directory. It's possible to build non-monolithic targets which output all engine binaries to the game binaries directory - a requirement to being able to set game-specific defines or build settings, because we don't want shared engine binaries to be tainted with them. The module manager needs to be able to operate early on,  before many of the game settings have been initialized, so just return the directory containing the Core module instead.

Change 3309120 on 2017/02/17 by Ben.Marsh

	Fix support for creating modular builds which don't use the shared build environment.

Change 3309125 on 2017/02/17 by Ben.Marsh

	Require that -CookDir arguments are specified separately on the command line. '+' is a valid path character (and common in build versions), so we shouldn't treat it as an argument separator.

Change 3309128 on 2017/02/17 by Ben.Marsh

	Fix UnrealPak failures when enumerating all files from a source directory, if that directory happens to contain spaces.

Change 3309131 on 2017/02/17 by Ben.Marsh

	Fix list of discovered assets being cleared by second call to FindFilesRecursive() when building DDC. Disable the -cookdir parameter again.

Change 3309140 on 2017/02/17 by Ben.Marsh

	UAT: Fix exception moving a file from one location to another if the target directory does not exist.

Change 3309212 on 2017/02/17 by Ben.Marsh

	Fixes/improvements for mod editor and code mods:

	* A separate top-level project is generated for each code mod in the Visual Studio solution.
	* Plugin descriptors now have a flag to identify themselves as mod as opposed to a regular game plugin, which prevents project plugins from getting their own VS project. New mods created with the mod editor will have this set by default, as do the three existing sample mods.
	* Cleaning and building code mods will never modify engine binaries. Presence of the Engine/Build/InstalledProjectBuild.txt file is used to indicate running in this environment. This flag also disables options to edit metadata for non-mod plugins in installed builds.
	* Plugin browser now includes a separate category for mods.
	* Mod editor now behaves as an "installed" program by default, and will use the user's home folder for storing settings.

Change 3309231 on 2017/02/17 by Steve.Robb

	Fix for Ar << bSomeBool where Ar is a derived class which overrides an operator<<.

	#jira UE-42052

Change 3309248 on 2017/02/17 by Ben.Marsh

	Add support for hot-reloading game plugin modules from Visual Studio, as long as their module returns IsGameModule() = true.

Change 3309257 on 2017/02/17 by Ben.Marsh

	Prevent game binaries from being renamed for hot reload when working with installed projects.

Change 3309355 on 2017/02/17 by Steven.Hutton

	Changes to make the website compatible with the new database changes.

Change 3309371 on 2017/02/17 by Ben.Marsh

	Fix exception on shutdown when running asset registry with threads disabled.

	#jira UE-41951

Change 3309389 on 2017/02/17 by Ben.Zeigler

	#jira UE-42051 Fix ensure and crash when loading a null asset ID via the LoadAsset BP node

Change 3309570 on 2017/02/17 by Gil.Gribb

	UE4 - Switch load time performace tweaks, plus abstracted the IO tracker and handle manager for other platforms and applied it to the PS4.

Change 3310039 on 2017/02/17 by Ben.Marsh

	BuildGraph: Prevent exception when trying to delete a file that does not exist.

Change 3311484 on 2017/02/20 by Chris.Wood

	CrashReportProcess crash add retry logic improvements (CRP v1.2.16)

Change 3311600 on 2017/02/20 by Matthew.Griffin

	Updated StripSymbols functions so that all platforms can deal with the source and target file being the same

Change 3311675 on 2017/02/20 by Steve.Robb

	FNativeClassHeaderGenerator::CurrentSourceFile stack replaced with C++ stack.

Change 3311893 on 2017/02/20 by Ben.Marsh

	UGS: Add support for notifying users if CIS steps fail for content changes. Badges which test content should be listed in the [Notifications] section of the project-specific INI file, through +ContentBadges= lines.

Change 3313966 on 2017/02/21 by Ben.Marsh

	Fix EC parsing of error messages output by the editor in the form "LogXYZ:Error:". Greedy optional subexpression in regex was matching everything until a space, so terminate a colon too.

Change 3314398 on 2017/02/21 by Ben.Zeigler

	#jira UE-42212 Fix shutdown of AnimGraph module to be safer

[CL 3315211 by Ben Marsh in Main branch]
2017-02-21 15:51:42 -05:00

1043 lines
34 KiB
C#

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnrealBuildTool
{
/// <summary>
/// Represents a case-insensitive name in the filesystem
/// </summary>
public class FileSystemName
{
/// <summary>
/// Name as it should be displayed
/// </summary>
public readonly string DisplayName;
/// <summary>
/// The canonical form of the name
/// </summary>
public readonly string CanonicalName;
/// <summary>
/// Constructor
/// </summary>
/// <param name="DisplayName">The display name</param>
public FileSystemName(string DisplayName)
{
this.DisplayName = DisplayName;
CanonicalName = DisplayName.ToLowerInvariant();
}
/// <summary>
/// Compares two filesystem object names for equality. Uses the canonical name representation, not the display name representation.
/// </summary>
/// <param name="A">First object to compare.</param>
/// <param name="B">Second object to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public static bool operator ==(FileSystemName A, FileSystemName B)
{
if ((object)A == null)
{
return (object)B == null;
}
else
{
return (object)B != null && A.CanonicalName == B.CanonicalName;
}
}
/// <summary>
/// Compares two filesystem object names for inequality. Uses the canonical name representation, not the display name representation.
/// </summary>
/// <param name="A">First object to compare.</param>
/// <param name="B">Second object to compare.</param>
/// <returns>False if the names represent the same object, true otherwise</returns>
public static bool operator !=(FileSystemName A, FileSystemName B)
{
return !(A == B);
}
/// <summary>
/// Compares against another object for equality.
/// </summary>
/// <param name="Obj">other instance to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public override bool Equals(object Obj)
{
return (Obj is FileSystemName) && ((FileSystemName)Obj) == this;
}
/// <summary>
/// Returns a hash code for this object
/// </summary>
/// <returns>Hash code for this object</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
/// <summary>
/// Returns the display name
/// </summary>
/// <returns>The display name</returns>
public override string ToString()
{
return DisplayName;
}
}
/// <summary>
/// Base class for file system objects (files or directories).
/// </summary>
[Serializable]
public abstract class FileSystemReference
{
/// <summary>
/// The path to this object. Stored as an absolute path, with O/S preferred separator characters, and no trailing slash for directories.
/// </summary>
public readonly string FullName;
/// <summary>
/// The canonical full name for this object.
/// </summary>
public readonly string CanonicalName;
/// <summary>
/// Direct constructor for a path
/// </summary>
protected FileSystemReference(string InFullName)
{
FullName = InFullName;
CanonicalName = InFullName.ToLowerInvariant();
}
/// <summary>
/// Direct constructor for a path
/// </summary>
protected FileSystemReference(string InFullName, string InCanonicalName)
{
FullName = InFullName;
CanonicalName = InCanonicalName;
}
/// <summary>
/// Create a full path by concatenating multiple strings
/// </summary>
/// <returns></returns>
static protected string CombineStrings(DirectoryReference BaseDirectory, params string[] Fragments)
{
// Get the initial string to append to, and strip any root directory suffix from it
StringBuilder NewFullName = new StringBuilder(BaseDirectory.FullName);
if (NewFullName.Length > 0 && NewFullName[NewFullName.Length - 1] == Path.DirectorySeparatorChar)
{
NewFullName.Remove(NewFullName.Length - 1, 1);
}
// Scan through the fragments to append, appending them to a string and updating the base length as we go
foreach (string Fragment in Fragments)
{
// Check if this fragment is an absolute path
if ((Fragment.Length >= 2 && Fragment[1] == ':') || (Fragment.Length >= 1 && (Fragment[0] == '\\' || Fragment[0] == '/')))
{
// It is. Reset the new name to the full version of this path.
NewFullName.Clear();
NewFullName.Append(Path.GetFullPath(Fragment).TrimEnd(Path.DirectorySeparatorChar));
}
else
{
// Append all the parts of this fragment to the end of the existing path.
int StartIdx = 0;
while (StartIdx < Fragment.Length)
{
// Find the end of this fragment. We may have been passed multiple paths in the same string.
int EndIdx = StartIdx;
while (EndIdx < Fragment.Length && Fragment[EndIdx] != '\\' && Fragment[EndIdx] != '/')
{
EndIdx++;
}
// Ignore any empty sections, like leading or trailing slashes, and '.' directory references.
int Length = EndIdx - StartIdx;
if (Length == 0)
{
// Multiple directory separators in a row; illegal.
throw new ArgumentException("Path fragment '{0}' contains invalid directory separators.");
}
else if (Length == 2 && Fragment[StartIdx] == '.' && Fragment[StartIdx + 1] == '.')
{
// Remove the last directory name
for (int SeparatorIdx = NewFullName.Length - 1; SeparatorIdx >= 0; SeparatorIdx--)
{
if (NewFullName[SeparatorIdx] == Path.DirectorySeparatorChar)
{
NewFullName.Remove(SeparatorIdx, NewFullName.Length - SeparatorIdx);
break;
}
}
}
else if (Length != 1 || Fragment[StartIdx] != '.')
{
// Append this fragment
NewFullName.Append(Path.DirectorySeparatorChar);
NewFullName.Append(Fragment, StartIdx, Length);
}
// Move to the next part
StartIdx = EndIdx + 1;
}
}
}
// Append the directory separator
if (NewFullName.Length == 0 || (NewFullName.Length == 2 && NewFullName[1] == ':'))
{
NewFullName.Append(Path.DirectorySeparatorChar);
}
// Set the new path variables
return NewFullName.ToString();
}
/// <summary>
/// Checks whether this name has the given extension.
/// </summary>
/// <param name="Extension">The extension to check</param>
/// <returns>True if this name has the given extension, false otherwise</returns>
public bool HasExtension(string Extension)
{
if (Extension.Length > 0 && Extension[0] != '.')
{
return HasExtension("." + Extension);
}
else
{
return CanonicalName.EndsWith(Extension.ToLowerInvariant());
}
}
/// <summary>
/// Determines if the given object is at or under the given directory
/// </summary>
/// <param name="Other">Directory to check against</param>
/// <returns>True if this path is under the given directory</returns>
public bool IsUnderDirectory(DirectoryReference Other)
{
return CanonicalName.StartsWith(Other.CanonicalName) && (CanonicalName.Length == Other.CanonicalName.Length || CanonicalName[Other.CanonicalName.Length] == Path.DirectorySeparatorChar || Other.IsRootDirectory());
}
/// <summary>
/// Searches the path fragments for the given name. Only complete fragments are considered a match.
/// </summary>
/// <param name="Name">Name to check for</param>
/// <param name="Offset">Offset within the string to start the search</param>
/// <returns>True if the given name is found within the path</returns>
public bool ContainsName(FileSystemName Name, int Offset)
{
return ContainsName(Name, Offset, FullName.Length - Offset);
}
/// <summary>
/// Searches the path fragments for the given name. Only complete fragments are considered a match.
/// </summary>
/// <param name="Name">Name to check for</param>
/// <param name="Offset">Offset within the string to start the search</param>
/// <param name="Length">Length of the substring to search</param>
/// <returns>True if the given name is found within the path</returns>
public bool ContainsName(FileSystemName Name, int Offset, int Length)
{
// Check the substring to search is at least long enough to contain a match
if(Length < Name.CanonicalName.Length)
{
return false;
}
// Find each occurence of the name within the remaining string, then test whether it's surrounded by directory separators
int MatchIdx = Offset;
for(;;)
{
// Find the next occurrence
MatchIdx = CanonicalName.IndexOf(Name.CanonicalName, MatchIdx, Offset + Length - MatchIdx);
if(MatchIdx == -1)
{
return false;
}
// Check if the substring is a directory
int MatchEndIdx = MatchIdx + Name.CanonicalName.Length;
if(CanonicalName[MatchIdx - 1] == Path.DirectorySeparatorChar && (MatchEndIdx == CanonicalName.Length || CanonicalName[MatchEndIdx] == Path.DirectorySeparatorChar))
{
return true;
}
// Move past the string that didn't match
MatchIdx += Name.CanonicalName.Length;
}
}
/// <summary>
/// Determines if the given object is under the given directory, within a subfolder of the given name. Useful for masking out directories by name.
/// </summary>
/// <param name="Name">Name of a subfolder to also check for</param>
/// <param name="BaseDir">Base directory to check against</param>
/// <returns>True if the path is under the given directory</returns>
public bool ContainsName(FileSystemName Name, DirectoryReference BaseDir)
{
// Check that this is under the base directory
if(!IsUnderDirectory(BaseDir))
{
return false;
}
else
{
return ContainsName(Name, BaseDir.FullName.Length);
}
}
/// <summary>
/// Determines if the given object is under the given directory, within a subfolder of the given name. Useful for masking out directories by name.
/// </summary>
/// <param name="Names">Names of subfolders to also check for</param>
/// <param name="BaseDir">Base directory to check against</param>
/// <returns>True if the path is under the given directory</returns>
public bool ContainsAnyNames(FileSystemName[] Names, DirectoryReference BaseDir)
{
// Check that this is under the base directory
if(!IsUnderDirectory(BaseDir))
{
return false;
}
else
{
return Names.Any(x => ContainsName(x, BaseDir.FullName.Length));
}
}
/// <summary>
/// Creates a relative path from the given base directory
/// </summary>
/// <param name="Directory">The directory to create a relative path from</param>
/// <returns>A relative path from the given directory</returns>
public string MakeRelativeTo(DirectoryReference Directory)
{
// Find how much of the path is common between the two paths. This length does not include a trailing directory separator character.
int CommonDirectoryLength = -1;
for (int Idx = 0; ; Idx++)
{
if (Idx == CanonicalName.Length)
{
// The two paths are identical. Just return the "." character.
if (Idx == Directory.CanonicalName.Length)
{
return ".";
}
// Check if we're finishing on a complete directory name
if (Directory.CanonicalName[Idx] == Path.DirectorySeparatorChar)
{
CommonDirectoryLength = Idx;
}
break;
}
else if (Idx == Directory.CanonicalName.Length)
{
// Check whether the end of the directory name coincides with a boundary for the current name.
if (CanonicalName[Idx] == Path.DirectorySeparatorChar)
{
CommonDirectoryLength = Idx;
}
break;
}
else
{
// Check the two paths match, and bail if they don't. Increase the common directory length if we've reached a separator.
if (CanonicalName[Idx] != Directory.CanonicalName[Idx])
{
break;
}
if (CanonicalName[Idx] == Path.DirectorySeparatorChar)
{
CommonDirectoryLength = Idx;
}
}
}
// If there's no relative path, just return the absolute path
if (CommonDirectoryLength == -1)
{
return FullName;
}
// Append all the '..' separators to get back to the common directory, then the rest of the string to reach the target item
StringBuilder Result = new StringBuilder();
for (int Idx = CommonDirectoryLength + 1; Idx < Directory.CanonicalName.Length; Idx++)
{
// Move up a directory
Result.Append("..");
Result.Append(Path.DirectorySeparatorChar);
// Scan to the next directory separator
while (Idx < Directory.CanonicalName.Length && Directory.CanonicalName[Idx] != Path.DirectorySeparatorChar)
{
Idx++;
}
}
if (CommonDirectoryLength + 1 < FullName.Length)
{
Result.Append(FullName, CommonDirectoryLength + 1, FullName.Length - CommonDirectoryLength - 1);
}
return Result.ToString();
}
/// <summary>
/// Returns a string representation of this filesystem object
/// </summary>
/// <returns>Full path to the object</returns>
public override string ToString()
{
return FullName;
}
}
/// <summary>
/// Representation of an absolute directory path. Allows fast hashing and comparisons.
/// </summary>
[Serializable]
public class DirectoryReference : FileSystemReference, IEquatable<DirectoryReference>
{
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="InPath">Path to this directory.</param>
public DirectoryReference(string InPath)
: base(FixTrailingPathSeparator(Path.GetFullPath(InPath)))
{
}
/// <summary>
/// Construct a DirectoryReference from a DirectoryInfo object.
/// </summary>
/// <param name="InInfo">Path to this file</param>
public DirectoryReference(DirectoryInfo InInfo)
: base(FixTrailingPathSeparator(InInfo.FullName))
{
}
/// <summary>
/// Constructor for creating a directory object directly from two strings.
/// </summary>
/// <param name="InFullName"></param>
/// <param name="InCanonicalName"></param>
protected DirectoryReference(string InFullName, string InCanonicalName)
: base(InFullName, InCanonicalName)
{
}
/// <summary>
/// Ensures that the correct trailing path separator is appended. On Windows, the root directory (eg. C:\) always has a trailing path separator, but no other
/// path does.
/// </summary>
/// <param name="DirName">Absolute path to the directory</param>
/// <returns>Path to the directory, with the correct trailing path separator</returns>
private static string FixTrailingPathSeparator(string DirName)
{
if(DirName.Length == 2 && DirName[1] == ':')
{
return DirName + Path.DirectorySeparatorChar;
}
else if(DirName.Length == 3 && DirName[1] == ':' && DirName[2] == Path.DirectorySeparatorChar)
{
return DirName;
}
else if(DirName.Length > 1 && DirName[DirName.Length - 1] == Path.DirectorySeparatorChar)
{
return DirName.TrimEnd(Path.DirectorySeparatorChar);
}
else
{
return DirName;
}
}
/// <summary>
/// Gets the top level directory name
/// </summary>
/// <returns>The name of the directory</returns>
public string GetDirectoryName()
{
return Path.GetFileName(FullName);
}
/// <summary>
/// Gets the directory containing this object
/// </summary>
/// <returns>A new directory object representing the directory containing this object</returns>
public DirectoryReference ParentDirectory
{
get
{
if (IsRootDirectory())
{
return null;
}
int ParentLength = CanonicalName.LastIndexOf(Path.DirectorySeparatorChar);
if (ParentLength == 2 && CanonicalName[1] == ':')
{
ParentLength++;
}
return new DirectoryReference(FullName.Substring(0, ParentLength), CanonicalName.Substring(0, ParentLength));
}
}
/// <summary>
/// Gets the parent directory for a file
/// </summary>
/// <param name="File">The file to get directory for</param>
/// <returns>The full directory name containing the given file</returns>
public static DirectoryReference GetParentDirectory(FileReference File)
{
int ParentLength = File.CanonicalName.LastIndexOf(Path.DirectorySeparatorChar);
return new DirectoryReference(File.FullName.Substring(0, ParentLength), File.CanonicalName.Substring(0, ParentLength));
}
/// <summary>
/// Contains the current directory
/// </summary>
public static DirectoryReference GetCurrentDirectory()
{
return new DirectoryReference(Directory.GetCurrentDirectory());
}
/// <summary>
/// Creates a directory
/// </summary>
public static void CreateDirectory(DirectoryReference Location)
{
Directory.CreateDirectory(Location.FullName);
}
/// <summary>
/// Deletes a directory
/// </summary>
public static void Delete(DirectoryReference Location, bool bRecursive)
{
Directory.Delete(Location.FullName, bRecursive);
}
/// <summary>
/// Checks whether the directory exists
/// </summary>
/// <returns>True if this directory exists</returns>
public static bool Exists(DirectoryReference Location)
{
return Directory.Exists(Location.FullName);
}
/// <summary>
/// Enumerate files from a given directory
/// </summary>
/// <returns>Sequence of file references</returns>
public static IEnumerable<FileReference> EnumerateFiles(DirectoryReference BaseDir)
{
foreach (string FileName in Directory.EnumerateFiles(BaseDir.FullName))
{
yield return FileReference.MakeFromNormalizedFullPath(FileName);
}
}
/// <summary>
/// Enumerate files from a given directory
/// </summary>
/// <returns>Sequence of file references</returns>
public static IEnumerable<FileReference> EnumerateFiles(DirectoryReference BaseDir, string Pattern)
{
foreach (string FileName in Directory.EnumerateFiles(BaseDir.FullName, Pattern))
{
yield return FileReference.MakeFromNormalizedFullPath(FileName);
}
}
/// <summary>
/// Enumerate files from a given directory
/// </summary>
/// <returns>Sequence of file references</returns>
public static IEnumerable<FileReference> EnumerateFiles(DirectoryReference BaseDir, string Pattern, SearchOption Option)
{
foreach (string FileName in Directory.EnumerateFiles(BaseDir.FullName, Pattern, Option))
{
yield return FileReference.MakeFromNormalizedFullPath(FileName);
}
}
/// <summary>
/// Enumerate subdirectories in a given directory
/// </summary>
/// <returns>Sequence of directory references</returns>
public static IEnumerable<DirectoryReference> EnumerateDirectories(DirectoryReference BaseDir)
{
foreach (string DirectoryName in Directory.EnumerateDirectories(BaseDir.FullName))
{
yield return DirectoryReference.MakeFromNormalizedFullPath(DirectoryName);
}
}
/// <summary>
/// Enumerate subdirectories in a given directory
/// </summary>
/// <returns>Sequence of directory references</returns>
public static IEnumerable<DirectoryReference> EnumerateDirectories(DirectoryReference BaseDir, string Pattern)
{
foreach (string DirectoryName in Directory.EnumerateDirectories(BaseDir.FullName, Pattern))
{
yield return DirectoryReference.MakeFromNormalizedFullPath(DirectoryName);
}
}
/// <summary>
/// Enumerate subdirectories in a given directory
/// </summary>
/// <returns>Sequence of directory references</returns>
public static IEnumerable<DirectoryReference> EnumerateDirectories(DirectoryReference BaseDir, string Pattern, SearchOption Option)
{
foreach (string DirectoryName in Directory.EnumerateDirectories(BaseDir.FullName, Pattern, Option))
{
yield return DirectoryReference.MakeFromNormalizedFullPath(DirectoryName);
}
}
/// <summary>
/// Determines whether this path represents a root directory in the filesystem
/// </summary>
/// <returns>True if this path is a root directory, false otherwise</returns>
public bool IsRootDirectory()
{
return CanonicalName[CanonicalName.Length - 1] == Path.DirectorySeparatorChar;
}
/// <summary>
/// Combine several fragments with a base directory, to form a new directory name
/// </summary>
/// <param name="BaseDirectory">The base directory</param>
/// <param name="Fragments">Fragments to combine with the base directory</param>
/// <returns>The new directory name</returns>
public static DirectoryReference Combine(DirectoryReference BaseDirectory, params string[] Fragments)
{
string FullName = FileSystemReference.CombineStrings(BaseDirectory, Fragments);
return new DirectoryReference(FullName, FullName.ToLowerInvariant());
}
/// <summary>
/// Compares two filesystem object names for equality. Uses the canonical name representation, not the display name representation.
/// </summary>
/// <param name="A">First object to compare.</param>
/// <param name="B">Second object to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public static bool operator ==(DirectoryReference A, DirectoryReference B)
{
if ((object)A == null)
{
return (object)B == null;
}
else
{
return (object)B != null && A.CanonicalName == B.CanonicalName;
}
}
/// <summary>
/// Compares two filesystem object names for inequality. Uses the canonical name representation, not the display name representation.
/// </summary>
/// <param name="A">First object to compare.</param>
/// <param name="B">Second object to compare.</param>
/// <returns>False if the names represent the same object, true otherwise</returns>
public static bool operator !=(DirectoryReference A, DirectoryReference B)
{
return !(A == B);
}
/// <summary>
/// Compares against another object for equality.
/// </summary>
/// <param name="Obj">other instance to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public override bool Equals(object Obj)
{
return (Obj is DirectoryReference) && ((DirectoryReference)Obj) == this;
}
/// <summary>
/// Compares against another object for equality.
/// </summary>
/// <param name="Obj">other instance to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public bool Equals(DirectoryReference Obj)
{
return Obj == this;
}
/// <summary>
/// Returns a hash code for this object
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return CanonicalName.GetHashCode();
}
/// <summary>
/// Helper function to create a remote directory reference. Unlike normal DirectoryReference objects, these aren't converted to a full path in the local filesystem.
/// </summary>
/// <param name="AbsolutePath">The absolute path in the remote file system</param>
/// <returns>New directory reference</returns>
public static DirectoryReference MakeRemote(string AbsolutePath)
{
return new DirectoryReference(AbsolutePath, AbsolutePath.ToLowerInvariant());
}
/// <summary>
/// Helper function to create a directory reference from a raw platform path. The path provided *MUST* be exactly the same as that returned by Path.GetFullPath().
/// </summary>
/// <param name="AbsolutePath">The absolute path in the file system</param>
/// <returns>New file reference</returns>
public static DirectoryReference MakeFromNormalizedFullPath(string AbsolutePath)
{
return new DirectoryReference(AbsolutePath, AbsolutePath.ToLowerInvariant());
}
/// <summary>
/// Gets the parent directory for a file, or returns null if it's null.
/// </summary>
/// <param name="File">The file to create a directory reference for</param>
/// <returns>The directory containing the file </returns>
public static DirectoryReference FromFile(FileReference File)
{
return (File == null)? null : File.Directory;
}
}
/// <summary>
/// Representation of an absolute file path. Allows fast hashing and comparisons.
/// </summary>
[Serializable]
public class FileReference : FileSystemReference, IEquatable<FileReference>
{
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="InPath">Path to this file</param>
public FileReference(string InPath)
: base(Path.GetFullPath(InPath))
{
if(FullName.EndsWith("\\") || FullName.EndsWith("/"))
{
throw new ArgumentException("File names may not be terminated by a path separator character");
}
}
/// <summary>
/// Construct a FileReference from a FileInfo object.
/// </summary>
/// <param name="InInfo">Path to this file</param>
public FileReference(FileInfo InInfo)
: base(InInfo.FullName)
{
}
/// <summary>
/// Default constructor.
/// </summary>
protected FileReference(string InFullName, string InCanonicalName)
: base(InFullName, InCanonicalName)
{
}
/// <summary>
/// Gets the file name without path information
/// </summary>
/// <returns>A string containing the file name</returns>
public string GetFileName()
{
return Path.GetFileName(FullName);
}
/// <summary>
/// Gets the file name without path information or an extension
/// </summary>
/// <returns>A string containing the file name without an extension</returns>
public string GetFileNameWithoutExtension()
{
return Path.GetFileNameWithoutExtension(FullName);
}
/// <summary>
/// Gets the file name without path or any extensions
/// </summary>
/// <returns>A string containing the file name without an extension</returns>
public string GetFileNameWithoutAnyExtensions()
{
int StartIdx = FullName.LastIndexOf(Path.DirectorySeparatorChar) + 1;
int EndIdx = FullName.IndexOf('.', StartIdx);
if (EndIdx < StartIdx)
{
return FullName.Substring(StartIdx);
}
else
{
return FullName.Substring(StartIdx, EndIdx - StartIdx);
}
}
/// <summary>
/// Gets the extension for this filename
/// </summary>
/// <returns>A string containing the extension of this filename</returns>
public string GetExtension()
{
return Path.GetExtension(FullName);
}
/// <summary>
/// Change the file's extension to something else
/// </summary>
/// <param name="Extension">The new extension</param>
/// <returns>A FileReference with the same path and name, but with the new extension</returns>
public FileReference ChangeExtension(string Extension)
{
string NewFullName = Path.ChangeExtension(FullName, Extension);
return new FileReference(NewFullName, NewFullName.ToLowerInvariant());
}
/// <summary>
/// Gets the directory containing this file
/// </summary>
/// <returns>A new directory object representing the directory containing this object</returns>
public DirectoryReference Directory
{
get { return DirectoryReference.GetParentDirectory(this); }
}
/// <summary>
/// Determines whether the given filename exists
/// </summary>
/// <returns>True if it exists, false otherwise</returns>
public static bool Exists(FileReference Location)
{
return File.Exists(Location.FullName);
}
/// <summary>
/// Deletes this file
/// </summary>
public static void Delete(FileReference Location)
{
File.Delete(Location.FullName);
}
/// <summary>
/// Combine several fragments with a base directory, to form a new filename
/// </summary>
/// <param name="BaseDirectory">The base directory</param>
/// <param name="Fragments">Fragments to combine with the base directory</param>
/// <returns>The new file name</returns>
public static FileReference Combine(DirectoryReference BaseDirectory, params string[] Fragments)
{
string FullName = FileSystemReference.CombineStrings(BaseDirectory, Fragments);
return new FileReference(FullName, FullName.ToLowerInvariant());
}
/// <summary>
/// Append a string to the end of a filename
/// </summary>
/// <param name="A">The base file reference</param>
/// <param name="B">Suffix to be appended</param>
/// <returns>The new file reference</returns>
public static FileReference operator +(FileReference A, string B)
{
return new FileReference(A.FullName + B, A.CanonicalName + B.ToLowerInvariant());
}
/// <summary>
/// Compares two filesystem object names for equality. Uses the canonical name representation, not the display name representation.
/// </summary>
/// <param name="A">First object to compare.</param>
/// <param name="B">Second object to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public static bool operator ==(FileReference A, FileReference B)
{
if ((object)A == null)
{
return (object)B == null;
}
else
{
return (object)B != null && A.CanonicalName == B.CanonicalName;
}
}
/// <summary>
/// Compares two filesystem object names for inequality. Uses the canonical name representation, not the display name representation.
/// </summary>
/// <param name="A">First object to compare.</param>
/// <param name="B">Second object to compare.</param>
/// <returns>False if the names represent the same object, true otherwise</returns>
public static bool operator !=(FileReference A, FileReference B)
{
return !(A == B);
}
/// <summary>
/// Compares against another object for equality.
/// </summary>
/// <param name="Obj">other instance to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public override bool Equals(object Obj)
{
return (Obj is FileReference) && ((FileReference)Obj) == this;
}
/// <summary>
/// Compares against another object for equality.
/// </summary>
/// <param name="Obj">other instance to compare.</param>
/// <returns>True if the names represent the same object, false otherwise</returns>
public bool Equals(FileReference Obj)
{
return Obj == this;
}
/// <summary>
/// Returns a hash code for this object
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return CanonicalName.GetHashCode();
}
/// <summary>
/// Helper function to create a remote file reference. Unlike normal FileReference objects, these aren't converted to a full path in the local filesystem, but are
/// left as they are passed in.
/// </summary>
/// <param name="AbsolutePath">The absolute path in the remote file system</param>
/// <returns>New file reference</returns>
public static FileReference MakeRemote(string AbsolutePath)
{
return new FileReference(AbsolutePath, AbsolutePath.ToLowerInvariant());
}
/// <summary>
/// Helper function to create a file reference from a raw platform path. The path provided *MUST* be exactly the same as that returned by Path.GetFullPath().
/// </summary>
/// <param name="AbsolutePath">The absolute path in the file system</param>
/// <returns>New file reference</returns>
public static FileReference MakeFromNormalizedFullPath(string AbsolutePath)
{
return new FileReference(AbsolutePath, AbsolutePath.ToLowerInvariant());
}
}
static class FileReferenceExtensionMethods
{
/// <summary>
/// Manually serialize a file reference to a binary stream.
/// </summary>
/// <param name="Writer">Binary writer to write to</param>
/// <param name="File">The file reference to write</param>
public static void Write(this BinaryWriter Writer, FileReference File)
{
Writer.Write((File == null) ? String.Empty : File.FullName);
}
/// <summary>
/// Manually serialize a file reference to a binary stream.
/// </summary>
/// <param name="Writer">Binary writer to write to</param>
/// <param name="Directory">The directory reference to write</param>
public static void Write(this BinaryWriter Writer, DirectoryReference Directory)
{
Writer.Write((Directory == null) ? String.Empty : Directory.FullName);
}
/// <summary>
/// Serializes a file reference, using a lookup table to avoid serializing the same name more than once.
/// </summary>
/// <param name="Writer">The writer to save this reference to</param>
/// <param name="File">A file reference to output; may be null</param>
/// <param name="FileToUniqueId">A lookup table that caches previous files that have been output, and maps them to unique id's.</param>
public static void Write(this BinaryWriter Writer, FileReference File, Dictionary<FileReference, int> FileToUniqueId)
{
int UniqueId;
if (File == null)
{
Writer.Write(-1);
}
else if (FileToUniqueId.TryGetValue(File, out UniqueId))
{
Writer.Write(UniqueId);
}
else
{
Writer.Write(FileToUniqueId.Count);
Writer.Write(File);
FileToUniqueId.Add(File, FileToUniqueId.Count);
}
}
/// <summary>
/// Manually deserialize a file reference from a binary stream.
/// </summary>
/// <param name="Reader">Binary reader to read from</param>
/// <returns>New FileReference object</returns>
public static FileReference ReadFileReference(this BinaryReader Reader)
{
string FullName = Reader.ReadString();
return (FullName.Length == 0) ? null : FileReference.MakeFromNormalizedFullPath(FullName);
}
/// <summary>
/// Manually deserialize a directory reference from a binary stream.
/// </summary>
/// <param name="Reader">Binary reader to read from</param>
/// <returns>New DirectoryReference object</returns>
public static DirectoryReference ReadDirectoryReference(this BinaryReader Reader)
{
string FullName = Reader.ReadString();
return (FullName.Length == 0) ? null : DirectoryReference.MakeFromNormalizedFullPath(FullName);
}
/// <summary>
/// Deserializes a file reference, using a lookup table to avoid writing the same name more than once.
/// </summary>
/// <param name="Reader">The source to read from</param>
/// <param name="UniqueFiles">List of previously read file references. The index into this array is used in place of subsequent ocurrences of the file.</param>
/// <returns>The file reference that was read</returns>
public static FileReference ReadFileReference(this BinaryReader Reader, List<FileReference> UniqueFiles)
{
int UniqueId = Reader.ReadInt32();
if (UniqueId == -1)
{
return null;
}
else if (UniqueId < UniqueFiles.Count)
{
return UniqueFiles[UniqueId];
}
else
{
FileReference Result = Reader.ReadFileReference();
UniqueFiles.Add(Result);
return Result;
}
}
}
}