Files
UnrealEngineUWP/Engine/Source/Programs/UnrealBuildTool/System/ConfigCache.cs
Marc Audy 0c3be2b6ad Merge Release-Engine-Staging to Test @ CL# 18240298
[CL 18241953 by Marc Audy in ue5-release-engine-test branch]
2021-11-18 14:37:34 -05:00

457 lines
14 KiB
C#

// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using EpicGames.Core;
using UnrealBuildTool;
namespace UnrealBuildTool
{
/// <summary>
/// Caches config files and config file hierarchies
/// </summary>
public static class ConfigCache
{
/// <summary>
/// Delegate to add a value to an ICollection in a target object
/// </summary>
/// <param name="TargetObject">The object containing the field to be modified</param>
/// <param name="ValueObject">The value to add</param>
delegate void AddElementDelegate(object TargetObject, object? ValueObject);
/// <summary>
/// Caches information about a field with a [ConfigFile] attribute in a type
/// </summary>
class ConfigField
{
/// <summary>
/// The field with the config attribute
/// </summary>
public FieldInfo FieldInfo;
/// <summary>
/// The attribute instance
/// </summary>
public ConfigFileAttribute Attribute;
/// <summary>
/// For fields implementing ICollection, specifies the element type
/// </summary>
public Type? ElementType;
/// <summary>
/// For fields implementing ICollection, a callback to add an element type.
/// </summary>
public AddElementDelegate? AddElement;
/// <summary>
/// Constructor
/// </summary>
/// <param name="FieldInfo"></param>
/// <param name="Attribute"></param>
public ConfigField(FieldInfo FieldInfo, ConfigFileAttribute Attribute)
{
this.FieldInfo = FieldInfo;
this.Attribute = Attribute;
}
}
/// <summary>
/// Stores information identifying a unique config hierarchy
/// </summary>
class ConfigHierarchyKey
{
/// <summary>
/// The hierarchy type
/// </summary>
public ConfigHierarchyType Type;
/// <summary>
/// The project directory to read from
/// </summary>
public DirectoryReference? ProjectDir;
/// <summary>
/// Which platform-specific files to read
/// </summary>
public UnrealTargetPlatform Platform;
/// <summary>
/// Custom config subdirectory to load
/// </summary>
public string CustomConfig;
/// <summary>
/// Constructor
/// </summary>
/// <param name="Type">The hierarchy type</param>
/// <param name="ProjectDir">The project directory to read from</param>
/// <param name="Platform">Which platform-specific files to read</param>
/// <param name="CustomConfig">Custom config subdirectory to load</param>
public ConfigHierarchyKey(ConfigHierarchyType Type, DirectoryReference? ProjectDir, UnrealTargetPlatform Platform, string CustomConfig)
{
this.Type = Type;
this.ProjectDir = ProjectDir;
this.Platform = Platform;
this.CustomConfig = CustomConfig;
}
/// <summary>
/// Test whether this key is equal to another object
/// </summary>
/// <param name="Other">The object to compare against</param>
/// <returns>True if the objects match, false otherwise</returns>
public override bool Equals(object? Other)
{
ConfigHierarchyKey? OtherKey = Other as ConfigHierarchyKey;
return (OtherKey != null && OtherKey.Type == Type && OtherKey.ProjectDir == ProjectDir && OtherKey.Platform == Platform && OtherKey.CustomConfig == CustomConfig);
}
/// <summary>
/// Returns a stable hash of this object
/// </summary>
/// <returns>Hash value for this object</returns>
public override int GetHashCode()
{
return ((ProjectDir != null) ? ProjectDir.GetHashCode() : 0) + ((int)Type * 123) + (Platform.GetHashCode() * 345) + (CustomConfig.GetHashCode() * 789);
}
}
/// <summary>
/// Cache of individual config files
/// </summary>
static Dictionary<FileReference, ConfigFile> LocationToConfigFile = new Dictionary<FileReference, ConfigFile>();
/// <summary>
/// Cache of config hierarchies by project
/// </summary>
static Dictionary<ConfigHierarchyKey, ConfigHierarchy> HierarchyKeyToHierarchy = new Dictionary<ConfigHierarchyKey, ConfigHierarchy>();
/// <summary>
/// Cache of config fields by type
/// </summary>
static Dictionary<Type, List<ConfigField>> TypeToConfigFields = new Dictionary<Type, List<ConfigField>>();
/// <summary>
/// Attempts to read a config file (or retrieve it from the cache)
/// </summary>
/// <param name="Location">Location of the file to read</param>
/// <param name="ConfigFile">On success, receives the parsed config file</param>
/// <returns>True if the file exists and was read, false otherwise</returns>
internal static bool TryReadFile(FileReference Location, [NotNullWhen(true)] out ConfigFile? ConfigFile)
{
lock (LocationToConfigFile)
{
if (!LocationToConfigFile.TryGetValue(Location, out ConfigFile))
{
if (FileReference.Exists(Location))
{
ConfigFile = new ConfigFile(Location);
}
if (ConfigFile != null)
{
LocationToConfigFile.Add(Location, ConfigFile);
}
}
}
return ConfigFile != null;
}
/// <summary>
/// Reads a config hierarchy (or retrieve it from the cache)
/// </summary>
/// <param name="Type">The type of hierarchy to read</param>
/// <param name="ProjectDir">The project directory to read the hierarchy for</param>
/// <param name="Platform">Which platform to read platform-specific config files for</param>
/// <param name="CustomConfig">Optional override config directory to search, for support of multiple target types</param>
/// <returns>The requested config hierarchy</returns>
public static ConfigHierarchy ReadHierarchy(ConfigHierarchyType Type, DirectoryReference? ProjectDir, UnrealTargetPlatform Platform, string CustomConfig = "")
{
// Handle command line overrides
List<String> OverrideStrings = new List<String>();
string[] CmdLine = Environment.GetCommandLineArgs();
string IniConfigArgPrefix = "-ini:" + Enum.GetName(typeof(ConfigHierarchyType), Type) + ":";
string CustomConfigPrefix = "-CustomConfig=";
foreach (string CmdLineArg in CmdLine)
{
if (CmdLineArg.StartsWith(IniConfigArgPrefix, StringComparison.InvariantCultureIgnoreCase))
{
OverrideStrings.Add(CmdLineArg.Substring(IniConfigArgPrefix.Length));
}
if (CmdLineArg.StartsWith(CustomConfigPrefix, StringComparison.InvariantCultureIgnoreCase))
{
CustomConfig = CmdLineArg.Substring(CustomConfigPrefix.Length);
}
}
if (CustomConfig == null)
{
CustomConfig = String.Empty;
}
// Get the key to use for the cache. It cannot be null, so we use the engine directory if a project directory is not given.
ConfigHierarchyKey Key = new ConfigHierarchyKey(Type, ProjectDir, Platform, CustomConfig);
// Try to get the cached hierarchy with this key
ConfigHierarchy? Hierarchy;
lock (HierarchyKeyToHierarchy)
{
if (!HierarchyKeyToHierarchy.TryGetValue(Key, out Hierarchy))
{
// Find all the input files
List<ConfigFile> Files = new List<ConfigFile>();
foreach (FileReference IniFileName in ConfigHierarchy.EnumerateConfigFileLocations(Type, ProjectDir, Platform, CustomConfig))
{
ConfigFile? File;
if (TryReadFile(IniFileName, out File))
{
Files.Add(File);
}
}
foreach (string OverrideString in OverrideStrings)
{
ConfigFile OverrideFile = new ConfigFile(OverrideString);
Files.Add(OverrideFile);
}
// Create the hierarchy
Hierarchy = new ConfigHierarchy(Files);
HierarchyKeyToHierarchy.Add(Key, Hierarchy);
}
}
return Hierarchy;
}
/// <summary>
/// Gets a list of ConfigFields for the given type
/// </summary>
/// <param name="TargetObjectType">Type to get configurable fields for</param>
/// <returns>List of config fields for the given type</returns>
static List<ConfigField> FindConfigFieldsForType(Type TargetObjectType)
{
List<ConfigField>? Fields;
lock(TypeToConfigFields)
{
if (!TypeToConfigFields.TryGetValue(TargetObjectType, out Fields))
{
Fields = new List<ConfigField>();
if(TargetObjectType.BaseType != null)
{
Fields.AddRange(FindConfigFieldsForType(TargetObjectType.BaseType));
}
foreach (FieldInfo FieldInfo in TargetObjectType.GetFields(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
{
IEnumerable<ConfigFileAttribute> Attributes = FieldInfo.GetCustomAttributes<ConfigFileAttribute>();
foreach (ConfigFileAttribute Attribute in Attributes)
{
// Copy the field
ConfigField Setter = new ConfigField(FieldInfo, Attribute);
// Check if the field type implements ICollection<>. If so, we can take multiple values.
foreach (Type InterfaceType in FieldInfo.FieldType.GetInterfaces())
{
if (InterfaceType.IsGenericType && InterfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
{
MethodInfo MethodInfo = InterfaceType.GetRuntimeMethod("Add", new Type[] { InterfaceType.GenericTypeArguments[0] })!;
Setter.AddElement = (Target, Value) => { MethodInfo.Invoke(Setter.FieldInfo.GetValue(Target), new object?[] { Value }); };
Setter.ElementType = InterfaceType.GenericTypeArguments[0];
break;
}
}
// Add it to the output list
Fields.Add(Setter);
}
}
TypeToConfigFields.Add(TargetObjectType, Fields);
}
}
return Fields;
}
/// <summary>
/// Read config settings for the given object
/// </summary>
/// <param name="ProjectDir">Path to the project directory</param>
/// <param name="Platform">The platform being built</param>
/// <param name="TargetObject">Object to receive the settings</param>
public static void ReadSettings(DirectoryReference? ProjectDir, UnrealTargetPlatform Platform, object TargetObject)
{
ReadSettings(ProjectDir, Platform, TargetObject, null);
}
/// <summary>
/// Read config settings for the given object
/// </summary>
/// <param name="ProjectDir">Path to the project directory</param>
/// <param name="Platform">The platform being built</param>
/// <param name="TargetObject">Object to receive the settings</param>
/// <param name="ConfigValues">Will be populated with config values that were retrieved. May be null.</param>
internal static void ReadSettings(DirectoryReference? ProjectDir, UnrealTargetPlatform Platform, object TargetObject, Dictionary<ConfigDependencyKey, IReadOnlyList<string>?>? ConfigValues)
{
List<ConfigField> Fields = FindConfigFieldsForType(TargetObject.GetType());
foreach(ConfigField Field in Fields)
{
// Read the hierarchy listed
ConfigHierarchy Hierarchy = ReadHierarchy(Field.Attribute.ConfigType, ProjectDir, Platform);
// Get the key name
string KeyName = Field.Attribute.KeyName ?? Field.FieldInfo.Name;
// Get the value(s) associated with this key
IReadOnlyList<string>? Values;
Hierarchy.TryGetValues(Field.Attribute.SectionName, KeyName, out Values);
// Parse the values from the config files and update the target object
if (Field.AddElement == null)
{
if(Values != null && Values.Count == 1)
{
object? Value;
if(TryParseValue(Values[0], Field.FieldInfo.FieldType, out Value))
{
Field.FieldInfo.SetValue(TargetObject, Value);
}
}
}
else
{
if(Values != null)
{
foreach(string Item in Values)
{
object? Value;
if(TryParseValue(Item, Field.ElementType!, out Value))
{
Field.AddElement(TargetObject, Value);
}
}
}
}
// Save the dependency
if (ConfigValues != null)
{
ConfigDependencyKey Key = new ConfigDependencyKey(Field.Attribute.ConfigType, ProjectDir, Platform, Field.Attribute.SectionName, KeyName);
ConfigValues[Key] = Values;
}
}
}
/// <summary>
/// Attempts to parse the given text into an object which matches a specific field type
/// </summary>
/// <param name="Text">The text to parse</param>
/// <param name="FieldType">The type of field to parse</param>
/// <param name="Value">If successful, a value of type 'FieldType'</param>
/// <returns>True if the value could be parsed, false otherwise</returns>
public static bool TryParseValue(string Text, Type FieldType, out object? Value)
{
if(FieldType == typeof(string))
{
Value = Text;
return true;
}
else if(FieldType == typeof(bool))
{
bool BoolValue;
if(ConfigHierarchy.TryParse(Text, out BoolValue))
{
Value = BoolValue;
return true;
}
else
{
Value = null;
return false;
}
}
else if(FieldType == typeof(int))
{
int IntValue;
if(ConfigHierarchy.TryParse(Text, out IntValue))
{
Value = IntValue;
return true;
}
else
{
Value = null;
return false;
}
}
else if(FieldType == typeof(float))
{
float FloatValue;
if(ConfigHierarchy.TryParse(Text, out FloatValue))
{
Value = FloatValue;
return true;
}
else
{
Value = null;
return false;
}
}
else if(FieldType == typeof(double))
{
double DoubleValue;
if(ConfigHierarchy.TryParse(Text, out DoubleValue))
{
Value = DoubleValue;
return true;
}
else
{
Value = null;
return false;
}
}
else if(FieldType == typeof(Guid))
{
Guid GuidValue;
if(ConfigHierarchy.TryParse(Text, out GuidValue))
{
Value = GuidValue;
return true;
}
else
{
Value = null;
return false;
}
}
else if(FieldType.IsEnum)
{
try
{
Value = Enum.Parse(FieldType, Text);
return true;
}
catch
{
Value = null;
return false;
}
}
else if(FieldType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return TryParseValue(Text, FieldType.GetGenericArguments()[0], out Value);
}
else
{
throw new Exception("Unsupported type for [ConfigFile] attribute");
}
}
}
}