Files
UnrealEngineUWP/Engine/Source/Programs/UnrealBuildTool/System/XmlConfig.cs
Ben Marsh 7c77078af1 Copying //UE4/Dev-Core to //UE4/Dev-Main (Source: //UE4/Dev-Core @ 3345728)
#lockdown Nick.Penwarden

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

Change 3315219 on 2017/02/21 by Steve.Robb

	Fix for FObjectAndNameAsStringProxyArchive when serializing a TWeakObjectPtr.

Change 3315285 on 2017/02/21 by Steve.Robb

	Explicitly pass string builder into code generation functions.

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

	UAT: Clean up some formatting in StreamCopyDescription output - remove #fyi lines, exclude merge commits, and remove some blank lines.

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

	Fix shared resource files not being rebuilt if the version header changes.

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

	UAT: Use a class derived from AutomationException to return information specific to commandlets failing, rather than putting it in the base class.

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

	UAT: Move Distiller class from general use in UAT; FileFilter provides a much safer and fully featured implementation of the same concepts.

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

	UBT: Remove the StripBaseDirectory() and MakeRerootedFilePath() utility functions from UBT. These operations can now be done more safely with FileReference objects.

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

	UBT: Convert FileFilter to use FileReference and DirectoryReference arguments everywhere.

Change 3316236 on 2017/02/22 by Maciej.Mroz

	#jira UE-42045

	Nativization
	Fixed Warning: TEnumAsByte is not intended for use with enum

Change 3316253 on 2017/02/22 by Robert.Manuszewski

	Fixes for the async log file writer hangs and crashes.

	- potential fix for the logging system hang when running out of disk space while flushing log
	- fix for unexpected concurrency assert when flushing the log buffer to disk

Change 3316293 on 2017/02/22 by Steve.Robb

	GetTypeHash and lexicographical comparison operators (operator<() etc.) for TTuple.

Change 3316342 on 2017/02/22 by Maciej.Mroz

	Nativization: Wrappers (stubs) required only by other wrappers are properly generated.

	#codereview: Mike.Beach

Change 3316344 on 2017/02/22 by Maciej.Mroz

	Fixed crash in nativized Odin
	Async loading properly handles nativized structs.

Change 3316359 on 2017/02/22 by Steve.Robb

	GitHub #3287 : Ignore #pragma in USTRUCTs

	#jira UE-42248

Change 3316389 on 2017/02/22 by Matthew.Griffin

	Switched Installed Engine Filters to multiline properties to make them more readable
	Added Oodle to list of excluded plugins
	#jira UE-42030

Change 3316392 on 2017/02/22 by Ben.Marsh

	UBT: Split out FileReference/DirectoryReference classes into their own file.

Change 3316394 on 2017/02/22 by Ben.Marsh

	UBT: Move FileReference/DirectoryReference extension methods into the appropriate file.

Change 3316411 on 2017/02/22 by Ben.Marsh

	UAT: Remove file functions that take multiple arguments. There's not really a compelling use case for these to exist over looping from the calling code.

Change 3316446 on 2017/02/22 by Ben.Marsh

	UAT: Try disabling function name prefix to log output from UAT, to see if it improves readability. Function names are still included in the log file for debugging.

Change 3316575 on 2017/02/22 by Ben.Marsh

	UAT: Remove unused functionality for dealing with labels, and output a more human readable list of P4 settings at startup.

Change 3318481 on 2017/02/22 by Steve.Robb

	Use of FMath::IsPowerOfTwo in check.
	Static assert to ensure that an inline set allocator will have a hash size of a power of two.

Change 3318496 on 2017/02/22 by Steve.Robb

	Fix for TSet visualizers.

Change 3318919 on 2017/02/23 by Steve.Robb

	Fix for hot reloading UScriptStruct-derived objects in a module, where the CDOs of these objects haven't had PrepareCppStructOps() called on them.

	#jira UE-42178

Change 3318942 on 2017/02/23 by Steve.Robb

	Removal of a redundant insertion which can cause problems on reallocation of the map.

Change 3319010 on 2017/02/23 by Ben.Marsh

	UBT: Fix exception when a file that was previously part of the working set is deleted.

Change 3319134 on 2017/02/23 by Robert.Manuszewski

	Better fix for a deadlock when flushing log while it's already being flushed due to flush timer on the async log writer thread.

Change 3319249 on 2017/02/23 by Matthew.Griffin

	Added a function to check if running with debug game libs instead of checking command line in multiple places
	Added -RunConfig parameter, which has equivalent result to -debug if value of parameter starts with 'debug'
	Added -RunConfig=$(Configuration) as a default commandline argument for Mac so that editor can use debug game libs
	Removed -Shipping argument from VCProject generation as it's not used anymore

Change 3319253 on 2017/02/23 by Maciej.Mroz

	#jira UE-41846

	New mechanism to gather modules necessary for Nativized Assets. The modules are listed based on included headers. Previously the dependencies was gathered only in FBlueprintNativeCodeGenManifest::GatherModuleDependencies.

Change 3319591 on 2017/02/23 by Ben.Marsh

	Don't strip prefixes beginning with WARNING: or ERROR: using the Postp filter.

Change 3320357 on 2017/02/23 by Steven.Hutton

	Slight changes to Add Crash method - Returning select fields instead of entity objects in queries for perf reasons.

Change 3320361 on 2017/02/23 by Steven.Hutton

	Performance improvements subsequent to the recent database changes.

Change 3320446 on 2017/02/23 by Steven.Hutton

	adding my temporary performance tracker class - reports to a private slack channel with add crash performance data.

Change 3320479 on 2017/02/23 by Ben.Marsh

	Fix CIS errors.

Change 3320576 on 2017/02/23 by Jin.Zhang

	Update CrashReporter to use AWS

Change 3320742 on 2017/02/23 by Jin.Zhang

	Merging crash caching

Change 3321119 on 2017/02/24 by Robert.Manuszewski

	DLL injection protection support for non-monolithic builds

Change 3323308 on 2017/02/27 by Matthew.Griffin

	Moved compilation of SwarmInterface after its dependencies so that we will see a build failure immediately if they change version in future

Change 3323423 on 2017/02/27 by Chad.Garyet

	Adding a script to check and warn about csproj targeted .net versions being mismatched

	#JIRA UE-39624

Change 3323442 on 2017/02/27 by Ben.Marsh

	UBT: Output an error if an engine module references a game module.

Change 3323743 on 2017/02/27 by Ben.Marsh

	PR #3303: Resolved PVS scan issues (Contributed by projectgheist)

Change 3323748 on 2017/02/27 by Ben.Marsh

	Convert whitespace to tabs.

Change 3324851 on 2017/02/28 by Chris.Wood

	Add Odin symbol locations to engine config for MDD on CR server.

	NotForLicensees

Change 3324979 on 2017/02/28 by Gil.Gribb

	Fixed bad merge of priority change in the EDL.

Change 3326889 on 2017/03/01 by Steven.Hutton

	Update to buggs controller to generate faster queries.

Change 3326910 on 2017/03/01 by Robert.Manuszewski

	Removing legacy #if from PackageFileSummary.

Change 3327118 on 2017/03/01 by Gil.Gribb

	UE4 - Fixed race that resulted in a memory leak when reading compressed paks.

Change 3327633 on 2017/03/01 by Gil.Gribb

	UE4 - Added a cvar to control the pak precacher thottle.

Change 3327674 on 2017/03/01 by Steve.Robb

	Unified boilerplate between all generated code files.

Change 3328544 on 2017/03/01 by Chris.Wood

	CrashReportProcess.config update (CRP v1.2.17)

	Tweaks to a few values.
	Update website URL to explicitly point to old, non-cloud site on devweb-02.

Change 3328714 on 2017/03/01 by Chris.Wood

	Correct CRP config regression. Point website at new cloud site. Still v1.2.17

Change 3329192 on 2017/03/02 by Matthew.Griffin

	Added Shared Build Id file to the list of Precompiled Build Dependencies in a target receipt so that it's brought into an installed build

Change 3329285 on 2017/03/02 by Ben.Marsh

	UGS: Allow a project to specify a filters for the streams that should be displayed for fast-switching to. The QuickSelectStreamList seting in the [Options] section of the project settings references a depot path containing a list of strings used to filter the stream list. An option is shown to switch back to showing all available streams, if desired.

Change 3330636 on 2017/03/02 by Ben.Marsh

	UBT: Bump version number of C++ include cache to force it to be rebuilt with additional include information for the default RC files.

Change 3331262 on 2017/03/03 by Robert.Manuszewski

	Merging Dev-LoadTimes to Dev-Core (Garbage Collection performance improvements)

	- Improved GC multithreading
	- Improved BeginDestroy performance
	- Introduced ULevelActorCluster for StaticMeshActor and ReflectionCapture actor clustering (can be toggled through project settings or console command gc.ActorClusterEnabled)
	- A few improvements to AddReferencedObjects functions
	- Misc improvements to GC code
	- Garbage Collector now properly handles clusters which had their objects marked as pending kill
	- Blueprints can now create clusters too (can be toggled through project settings or console command gc.BlueprintClusteringEnabled, defaults to disabled)

Change 3331285 on 2017/03/03 by Robert.Manuszewski

	A few fixes for the previous check-in.

Change 3332001 on 2017/03/03 by Ben.Marsh

	UBT: Add support for generating a UDN file containing the valid settings for BuildConfiguration.xml. Pass -configdoc=<filename> on the command line to generate such a file.

Change 3332022 on 2017/03/03 by Ben.Marsh

	Update documentation for where to find the BuildConfiguration settings.

Change 3332031 on 2017/03/03 by Ben.Marsh

	Remove documentation for Windows XP support; it has been removed in the 4.16 release.

Change 3332256 on 2017/03/03 by Ben.Marsh

	UBT: Add support for generating a UDN page containing module and target settings.

Change 3332458 on 2017/03/03 by Ben.Marsh

	UBT: Improvements to generated documentation.

Change 3332459 on 2017/03/03 by Ben.Marsh

	Add generated documentation for .target.cs files, .build.cs files, and BuildConfiguration.xml files.

Change 3332460 on 2017/03/03 by Ben.Marsh

	UBT: Make LinkTypePrivate actually private, so it doesn't show up in the docs.

Change 3332899 on 2017/03/06 by Robert.Manuszewski

	Making sure actor clustering is not used in the editor (fix for actors being deleted when GC runs in the editor)

	#jira UE-42548

Change 3332955 on 2017/03/06 by Maciej.Mroz

	Nativization distinguishes client and server platform:
	- Separated lists on additional assets, additional modules, excluded assets, excluded modules, excluded paths (in config)
	- Context (compilation options, nativization options and platform) is deliveren to BPCOmpilerCppBackend in FCompilerNativizationOptions struct.
	- Wrappers (for unconverted BPs) are created only when they are directly called.

	- Fortnite dedicated server can be nativized

Change 3332990 on 2017/03/06 by Ben.Marsh

	UBT: Add more comprehensive wrapper methods for System.IO.File and System.IO.Directory to FileReference and DirectoryReference.

Change 3333032 on 2017/03/06 by Ben.Marsh

	Documentation for build tools

Change 3333037 on 2017/03/06 by Ben.Marsh

	Add a build step to extract UAT and UBT documentation from XML comments.

Change 3333089 on 2017/03/06 by Ben.Marsh

	UAT: Re-enable logging the calling function to the console in UAT. Needs a pass for readability first.

Change 3333651 on 2017/03/06 by Gil.Gribb

	UE4 - Fix a werid recursive situation where StaticLoadObject could return an object that has not finished loading. Also produces a fatal error if this sometimes happens. EDL only.

Change 3335236 on 2017/03/07 by Ben.Marsh

	UGS: Set the sync changelist separately to the compatibility changelist.

Change 3335261 on 2017/03/07 by Gil.Gribb

	UE4 - Fixed batched render fences when BeginDestroy calls FlushRenderingCommands.

Change 3335740 on 2017/03/07 by Gil.Gribb

	maybe fix static analysis warning

Change 3335945 on 2017/03/07 by Steve.Robb

	Move FFindInstancedReferenceSubobjectHelper code out of header.
	Add map/set property support to allow instanced members of these container types to be handled during CPFUO.

	https://udn.unrealengine.com/questions/349232/tmap-with-instanced-object-as-value-gets-cleared-o.html

Change 3336693 on 2017/03/07 by Ben.Marsh

	UBT: Use shared PCHs for game plugins by default, to reduce time spent generating individual PCHs.

Change 3336694 on 2017/03/07 by Steve.Robb

	Static assert added to TMap to prevent the use of keys which don't implement a GetTypeHash.
	Fixes to types which relied on implicit conversions when calling GetTypeHash.
	Workaround in SAssetView.h and PropertyEditorModule.h for an apparent VC bug where the compiler wrongly instantiates TPointerIsConvertibleFromTo for certain forward-declared types, causing future TSharedPtr conversions to fail.

	#jira UE-42441

Change 3336698 on 2017/03/07 by Steve.Robb

	Hardcoded endpoint handling replaced with a generic string.
	Obsolete .proto and .java code generation removed.

Change 3336811 on 2017/03/07 by Wes.Hunt

	Add a game blacklist to the crash report processor. Fixed a syntax error in Config.cs, added a XML comment to shut up a warning.

Change 3336973 on 2017/03/08 by Steve.Robb

	Fix for missing GetTypeHash in a plugin.

Change 3336996 on 2017/03/08 by Steve.Robb

	Significant refactor of code generation, to try and make data flow more apparent.

Change 3337571 on 2017/03/08 by Steve.Robb

	CIS fixes for missing GetTypeHash functions.
	Non-unity fix.

Change 3337588 on 2017/03/08 by Gil.Gribb

	UE4 - Fixed obscure check with flushing rhi resources.

Change 3337620 on 2017/03/08 by Steve.Robb

	WITH_HOT_RELOAD_CTORS macros removed.
	UseVTableConstructors config option removed.

Change 3339369 on 2017/03/09 by Steve.Robb

	GetTypeHash overload for nn::account::Uid.

Change 3339464 on 2017/03/09 by Daniel.Lamb

	Fixed assert in 4.15 to do with trying to gather dependency info from invalid packages.
	#jira UE-42583
	#test Editor + Cook + Run shootergame

Change 3339465 on 2017/03/09 by Maciej.Mroz

	Fixed serialization issue, after UserDefinedEnum was used in EnumProperty.

Change 3339469 on 2017/03/09 by Maciej.Mroz

	Fixed Nativization problem, when default value is passed as non-const reference.

Change 3340178 on 2017/03/09 by Daniel.Lamb

	Added support for in memory only packages.  The Cooker ignores these and added core functions to recognize these packages.
	Other systems will need to add support where nessisary.

Change 3341002 on 2017/03/10 by Maciej.Mroz

	Nativization: Fixed FFindHeadersToInclude. Headers necessary for owners of subobjects are properly included.

Change 3341076 on 2017/03/10 by Steve.Robb

	Fix for FBakedTextureSourceInfo move semantics.

	#jira UE-42658

Change 3341160 on 2017/03/10 by Gil.Gribb

	UE4 - Fix hazard with SetMaterialUsage from a thread.

Change 3341409 on 2017/03/10 by Steve.Robb

	Reduction of the generated code size for StaticRegisterNatives functions.

Change 3341523 on 2017/03/10 by Steve.Robb

	Code generation simplified.

Change 3341800 on 2017/03/10 by Ben.Marsh

	UnrealVS: Fix UnrealVS compatibility with RTM version of Visual Studio 2017. 2017 toolchain for extensions is no longer able to build <= 2015 extensions due to validation of the VSIX manifest, so create a separate solution for it.

Change 3342034 on 2017/03/10 by Ben.Marsh

	Fix compiler setting not being loaded correctly into the Windows target settings dialog.

	#jira UE-42746

Change 3342041 on 2017/03/10 by Ben.Marsh

	Fix -ErrorOnEngineContentUse not being set in the cooker options correctly.

Change 3342094 on 2017/03/10 by Steve.Robb

	Fix to deteministic name order during code generation.

Change 3342251 on 2017/03/10 by Daniel.Lamb

	Integrate fix for resave lightmaps commandlet when upgrading from no mapbuilddatapackages to mapbuilddatapackages.
	#thanks Tim.Hagberg

	#test None

Change 3342961 on 2017/03/13 by Robert.Manuszewski

	Fixing memory leak when playing while running -nullrhi on the commandline in cooked games caused by shader resources not being destroyed.

	#jira FORT-38977

Change 3343022 on 2017/03/13 by Steve.Robb

	GetTypeHash fixes for FUniqueNetIdLive.

	#jira UE-42788

Change 3343448 on 2017/03/13 by Steve.Robb

	Compiled-in defer object order fixed.
	Debuggability of the deferred registration map improved.

	#jira UE-42828

[CL 3345747 by Ben Marsh in Main branch]
2017-03-14 15:48:33 -04:00

704 lines
24 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace UnrealBuildTool
{
/// <summary>
/// Functions for manipulating the XML config cache
/// </summary>
static class XmlConfig
{
/// <summary>
/// An input config file
/// </summary>
public class InputFile
{
/// <summary>
/// Location of the file
/// </summary>
public FileReference Location;
/// <summary>
/// Which folder to display the config file under in the generated project files
/// </summary>
public string FolderName;
}
/// <summary>
/// Parsed config values
/// </summary>
static XmlConfigData Values;
/// <summary>
/// Cached serializer for the XML schema
/// </summary>
static XmlSerializer CachedSchemaSerializer;
/// <summary>
/// Initialize the config system with the given types
/// </summary>
public static bool ReadConfigFiles()
{
// Find all the configurable types
List<Type> ConfigTypes = FindConfigurableTypes();
// Find all the input files
FileReference[] InputFiles = FindInputFiles().Select(x => x.Location).ToArray();
// Get the path to the cache file
FileReference CacheFile = FileReference.Combine(UnrealBuildTool.EngineDirectory, "Intermediate", "Build", "XmlConfigCache.bin");
FileReference SchemaFile = GetSchemaLocation();
// Try to read the existing cache from disk
XmlConfigData CachedValues;
if(IsCacheUpToDate(CacheFile, InputFiles) && FileReference.Exists(SchemaFile))
{
if(XmlConfigData.TryRead(CacheFile, ConfigTypes, out CachedValues) && Enumerable.SequenceEqual(InputFiles, CachedValues.InputFiles))
{
Values = CachedValues;
}
}
// If that failed, regenerate it
if(Values == null)
{
// Find all the configurable fields from the given types
Dictionary<string, Dictionary<string, FieldInfo>> CategoryToFields = new Dictionary<string, Dictionary<string, FieldInfo>>();
FindConfigurableFields(ConfigTypes, CategoryToFields);
// Create a schema for the config files
XmlSchema Schema = CreateSchema(CategoryToFields);
WriteSchema(Schema, SchemaFile);
// Read all the XML files and validate them against the schema
Dictionary<Type, Dictionary<FieldInfo, object>> TypeToValues = new Dictionary<Type, Dictionary<FieldInfo, object>>();
foreach(FileReference InputFile in InputFiles)
{
if(!TryReadFile(InputFile, CategoryToFields, TypeToValues, Schema))
{
return false;
}
}
// Create the new cache
Values = new XmlConfigData(InputFiles, TypeToValues.ToDictionary(x => x.Key, x => x.Value.ToArray()));
Values.Write(CacheFile);
}
// Apply all the static field values
foreach(KeyValuePair<Type, KeyValuePair<FieldInfo, object>[]> TypeValuesPair in Values.TypeToValues)
{
foreach(KeyValuePair<FieldInfo, object> FieldValuePair in TypeValuesPair.Value)
{
if(FieldValuePair.Key.IsStatic)
{
object Value = InstanceValue(FieldValuePair.Value, FieldValuePair.Key.FieldType);
FieldValuePair.Key.SetValue(null, Value);
}
}
}
return true;
}
/// <summary>
/// Find all the configurable types in the current assembly
/// </summary>
/// <returns>List of configurable types</returns>
static List<Type> FindConfigurableTypes()
{
List<Type> ConfigTypes = new List<Type>();
foreach(Type ConfigType in Assembly.GetExecutingAssembly().GetTypes())
{
if(HasXmlConfigFileAttribute(ConfigType))
{
ConfigTypes.Add(ConfigType);
}
}
return ConfigTypes;
}
/// <summary>
/// Determines whether the given type has a field with an XmlConfigFile attribute
/// </summary>
/// <param name="Type">The type to check</param>
/// <returns>True if the type has a field with the XmlConfigFile attribute</returns>
static bool HasXmlConfigFileAttribute(Type Type)
{
foreach(FieldInfo Field in Type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
foreach(CustomAttributeData CustomAttribute in Field.CustomAttributes)
{
if(CustomAttribute.AttributeType == typeof(XmlConfigFileAttribute))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Find the location of the XML config schema
/// </summary>
/// <returns>The location of the schema file</returns>
public static FileReference GetSchemaLocation()
{
return FileReference.Combine(UnrealBuildTool.EngineDirectory, "Saved", "UnrealBuildTool", "BuildConfiguration.Schema.xsd");
}
/// <summary>
/// Initialize the list of input files
/// </summary>
public static List<InputFile> FindInputFiles()
{
// Find all the input file locations
List<InputFile> InputFiles = new List<InputFile>();
// Check for the config file under /Engine/Programs/NotForLicensees/UnrealBuildTool
FileReference NotForLicenseesConfigLocation = FileReference.Combine(UnrealBuildTool.EngineDirectory, "Programs", "NotForLicensees", "UnrealBuildTool", "BuildConfiguration.xml");
if(FileReference.Exists(NotForLicenseesConfigLocation))
{
InputFiles.Add(new InputFile { Location = NotForLicenseesConfigLocation, FolderName = "NotForLicensees" });
}
// Check for the user config file under /Engine/Programs/NotForLicensees/UnrealBuildTool
FileReference UserConfigLocation = FileReference.Combine(UnrealBuildTool.EngineDirectory, "Saved", "UnrealBuildTool", "BuildConfiguration.xml");
if(!FileReference.Exists(UserConfigLocation))
{
CreateDefaultConfigFile(UserConfigLocation);
}
InputFiles.Add(new InputFile { Location = UserConfigLocation, FolderName = "User" });
// Check for the global config file under AppData/Unreal Engine/UnrealBuildTool
string AppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if(!String.IsNullOrEmpty(AppDataFolder))
{
FileReference AppDataConfigLocation = FileReference.Combine(new DirectoryReference(AppDataFolder), "Unreal Engine", "UnrealBuildTool", "BuildConfiguration.xml");
if(!FileReference.Exists(AppDataConfigLocation))
{
CreateDefaultConfigFile(AppDataConfigLocation);
}
InputFiles.Add(new InputFile { Location = AppDataConfigLocation, FolderName = "Global (AppData)" });
}
// Check for the global config file under My Documents/Unreal Engine/UnrealBuildTool
string PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
if(!String.IsNullOrEmpty(PersonalFolder))
{
FileReference PersonalConfigLocation = FileReference.Combine(new DirectoryReference(PersonalFolder), "Unreal Engine", "UnrealBuildTool", "BuildConfiguration.xml");
if(FileReference.Exists(PersonalConfigLocation))
{
InputFiles.Add(new InputFile { Location = PersonalConfigLocation, FolderName = "Global (Documents)" });
}
}
return InputFiles;
}
/// <summary>
/// Create a default config file at the given location
/// </summary>
/// <param name="Location">Location to read from</param>
static void CreateDefaultConfigFile(FileReference Location)
{
DirectoryReference.CreateDirectory(Location.Directory);
using (StreamWriter Writer = new StreamWriter(Location.FullName))
{
Writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
Writer.WriteLine("<Configuration xmlns=\"{0}\">", XmlConfigFile.SchemaNamespaceURI);
Writer.WriteLine("</Configuration>");
}
}
/// <summary>
/// Applies config values to the given object
/// </summary>
/// <param name="TargetObject">The object instance to be configured</param>
public static void ApplyTo(object TargetObject)
{
for(Type TargetType = TargetObject.GetType(); TargetType != null; TargetType = TargetType.BaseType)
{
KeyValuePair<FieldInfo, object>[] FieldValues;
if(Values.TypeToValues.TryGetValue(TargetType, out FieldValues))
{
foreach(KeyValuePair<FieldInfo, object> FieldValuePair in FieldValues)
{
if(!FieldValuePair.Key.IsStatic)
{
object ValueInstance = InstanceValue(FieldValuePair.Value, FieldValuePair.Key.FieldType);
FieldValuePair.Key.SetValue(TargetObject, ValueInstance);
}
}
}
}
}
/// <summary>
/// Instances a value for assignment to a target object
/// </summary>
/// <param name="Value">The value to instance</param>
/// <param name="ValueType">The type of value</param>
/// <returns>New instance of the given value, if necessary</returns>
static object InstanceValue(object Value, Type ValueType)
{
if(ValueType == typeof(string[]))
{
return ((string[])Value).Clone();
}
else
{
return Value;
}
}
/// <summary>
/// Gets a config value for a single value, without writing it to an instance of that class
/// </summary>
/// <param name="TargetType">Type to find config values for</param>
/// <param name="Name">Name of the field to receive</param>
/// <param name="Value">On success, receives the value of the field</param>
/// <returns>True if the value was read, false otherwise</returns>
public static bool TryGetValue(Type TargetType, string Name, out object Value)
{
// Find all the config values for this type
KeyValuePair<FieldInfo, object>[] FieldValues;
if(!Values.TypeToValues.TryGetValue(TargetType, out FieldValues))
{
Value = null;
return false;
}
// Find the value with the matching name
foreach(KeyValuePair<FieldInfo, object> FieldPair in FieldValues)
{
if(FieldPair.Key.Name == Name)
{
Value = FieldPair.Value;
return true;
}
}
// Not found
Value = null;
return false;
}
/// <summary>
/// Find all the configurable fields in the given types by searching for XmlConfigFile attributes.
/// </summary>
/// <param name="ConfigTypes">Array of types to search</param>
/// <param name="CategoryToFields">Dictionaries populated with category -> name -> field mappings on return</param>
static void FindConfigurableFields(IEnumerable<Type> ConfigTypes, Dictionary<string, Dictionary<string, FieldInfo>> CategoryToFields)
{
foreach(Type ConfigType in ConfigTypes)
{
foreach(FieldInfo FieldInfo in ConfigType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public | BindingFlags.NonPublic))
{
IEnumerable<XmlConfigFileAttribute> Attributes = FieldInfo.GetCustomAttributes<XmlConfigFileAttribute>();
foreach(XmlConfigFileAttribute Attribute in Attributes)
{
string CategoryName = Attribute.Category ?? ConfigType.Name;
Dictionary<string, FieldInfo> NameToField;
if(!CategoryToFields.TryGetValue(CategoryName, out NameToField))
{
NameToField = new Dictionary<string, FieldInfo>();
CategoryToFields.Add(CategoryName, NameToField);
}
NameToField[Attribute.Name ?? FieldInfo.Name] = FieldInfo;
}
}
}
}
/// <summary>
/// Creates a schema from attributes in the given types
/// </summary>
/// <param name="CategoryToFields">Lookup for all field settings</param>
/// <returns>New schema instance</returns>
static XmlSchema CreateSchema(Dictionary<string, Dictionary<string, FieldInfo>> CategoryToFields)
{
// Create elements for all the categories
XmlSchemaAll RootAll = new XmlSchemaAll();
foreach(KeyValuePair<string, Dictionary<string, FieldInfo>> CategoryPair in CategoryToFields)
{
string CategoryName = CategoryPair.Key;
XmlSchemaAll CategoryAll = new XmlSchemaAll();
foreach (KeyValuePair<string, FieldInfo> FieldPair in CategoryPair.Value)
{
XmlSchemaElement Element = CreateSchemaFieldElement(FieldPair.Key, FieldPair.Value.FieldType);
CategoryAll.Items.Add(Element);
}
XmlSchemaComplexType CategoryType = new XmlSchemaComplexType();
CategoryType.Particle = CategoryAll;
XmlSchemaElement CategoryElement = new XmlSchemaElement();
CategoryElement.Name = CategoryName;
CategoryElement.SchemaType = CategoryType;
CategoryElement.MinOccurs = 0;
CategoryElement.MaxOccurs = 1;
RootAll.Items.Add(CategoryElement);
}
// Create the root element and schema object
XmlSchemaComplexType RootType = new XmlSchemaComplexType();
RootType.Particle = RootAll;
XmlSchemaElement RootElement = new XmlSchemaElement();
RootElement.Name = XmlConfigFile.RootElementName;
RootElement.SchemaType = RootType;
XmlSchema Schema = new XmlSchema();
Schema.TargetNamespace = XmlConfigFile.SchemaNamespaceURI;
Schema.ElementFormDefault = XmlSchemaForm.Qualified;
Schema.Items.Add(RootElement);
// Finally compile it
XmlSchemaSet SchemaSet = new XmlSchemaSet();
SchemaSet.Add(Schema);
SchemaSet.Compile();
return SchemaSet.Schemas().OfType<XmlSchema>().First();
}
/// <summary>
/// Creates an XML schema element for reading a value of the given type
/// </summary>
/// <param name="Name">Name of the field</param>
/// <param name="Type">Type of the field</param>
/// <returns>New schema element representing the field</returns>
static XmlSchemaElement CreateSchemaFieldElement(string Name, Type Type)
{
XmlSchemaElement Element = new XmlSchemaElement();
Element.Name = Name;
Element.MinOccurs = 0;
Element.MaxOccurs = 1;
if(Type == typeof(string))
{
Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName;
}
else if(Type == typeof(bool))
{
Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Boolean).QualifiedName;
}
else if(Type == typeof(int))
{
Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Int).QualifiedName;
}
else if(Type == typeof(float))
{
Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Float).QualifiedName;
}
else if(Type == typeof(double))
{
Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Double).QualifiedName;
}
else if(Type.IsEnum)
{
XmlSchemaSimpleTypeRestriction Restriction = new XmlSchemaSimpleTypeRestriction();
Restriction.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName;
foreach(string EnumName in Enum.GetNames(Type))
{
XmlSchemaEnumerationFacet Facet = new XmlSchemaEnumerationFacet();
Facet.Value = EnumName;
Restriction.Facets.Add(Facet);
}
XmlSchemaSimpleType EnumType = new XmlSchemaSimpleType();
EnumType.Content = Restriction;
Element.SchemaType = EnumType;
}
else if(Type == typeof(string[]))
{
XmlSchemaElement ItemElement = new XmlSchemaElement();
ItemElement.Name = "Item";
ItemElement.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName;
ItemElement.MinOccurs = 0;
ItemElement.MaxOccursString = "unbounded";
XmlSchemaSequence Sequence = new XmlSchemaSequence();
Sequence.Items.Add(ItemElement);
XmlSchemaComplexType ArrayType = new XmlSchemaComplexType();
ArrayType.Particle = Sequence;
Element.SchemaType = ArrayType;
}
else
{
throw new Exception("Unsupported field type for XmlConfigFile attribute");
}
return Element;
}
/// <summary>
/// Writes a schema to the given location. Avoids writing it if the file is identical.
/// </summary>
/// <param name="Schema">The schema to be written</param>
/// <param name="Location">Location to write to</param>
static void WriteSchema(XmlSchema Schema, FileReference Location)
{
XmlWriterSettings Settings = new XmlWriterSettings();
Settings.Indent = true;
Settings.IndentChars = "\t";
Settings.NewLineChars = Environment.NewLine;
Settings.OmitXmlDeclaration = true;
if(CachedSchemaSerializer == null)
{
CachedSchemaSerializer = XmlSerializer.FromTypes(new Type[] { typeof(XmlSchema) })[0];
}
StringBuilder Output = new StringBuilder();
Output.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
using(XmlWriter Writer = XmlWriter.Create(Output, Settings))
{
XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces();
Namespaces.Add("", "http://www.w3.org/2001/XMLSchema");
CachedSchemaSerializer.Serialize(Writer, Schema, Namespaces);
}
string OutputText = Output.ToString();
if(!FileReference.Exists(Location) || File.ReadAllText(Location.FullName) != OutputText)
{
DirectoryReference.CreateDirectory(Location.Directory);
File.WriteAllText(Location.FullName, OutputText);
}
}
/// <summary>
/// Reads an XML config file and merges it to the given cache
/// </summary>
/// <param name="Location">Location to read from</param>
/// <param name="CategoryToFields">Lookup for configurable fields by category</param>
/// <param name="TypeToValues">Map of types to fields and their associated values</param>
/// <param name="Schema">Schema to validate against</param>
/// <returns>True if the file was read successfully</returns>
static bool TryReadFile(FileReference Location, Dictionary<string, Dictionary<string, FieldInfo>> CategoryToFields, Dictionary<Type, Dictionary<FieldInfo, object>> TypeToValues, XmlSchema Schema)
{
// Read the XML file, and validate it against the schema
XmlConfigFile ConfigFile;
if(!XmlConfigFile.TryRead(Location, Schema, out ConfigFile))
{
return false;
}
// Parse the document
foreach(XmlElement CategoryElement in ConfigFile.DocumentElement.ChildNodes.OfType<XmlElement>())
{
Dictionary<string, FieldInfo> NameToField = CategoryToFields[CategoryElement.Name];
foreach(XmlElement KeyElement in CategoryElement.ChildNodes.OfType<XmlElement>())
{
FieldInfo Field;
if(NameToField.TryGetValue(KeyElement.Name, out Field))
{
// Parse the corresponding value
object Value;
if(Field.FieldType == typeof(string[]))
{
Value = KeyElement.ChildNodes.OfType<XmlElement>().Where(x => x.Name == "Item").Select(x => x.InnerText).ToArray();
}
else
{
Value = ParseValue(Field.FieldType, KeyElement.InnerText);
}
// Add it to the set of values for the type containing this field
Dictionary<FieldInfo, object> FieldToValue;
if(!TypeToValues.TryGetValue(Field.DeclaringType, out FieldToValue))
{
FieldToValue = new Dictionary<FieldInfo, object>();
TypeToValues.Add(Field.DeclaringType, FieldToValue);
}
FieldToValue[Field] = Value;
}
}
}
return true;
}
/// <summary>
/// Parse the value for a field from its text based representation in an XML file
/// </summary>
/// <param name="FieldType">The type of field being read</param>
/// <param name="Text">Text to parse</param>
/// <returns>The object that was parsed</returns>
static object ParseValue(Type FieldType, string Text)
{
if(FieldType == typeof(string))
{
return Text;
}
else if(FieldType == typeof(bool))
{
return (Text == "1" || Text.Equals("true", StringComparison.InvariantCultureIgnoreCase));
}
else if(FieldType == typeof(int))
{
return Int32.Parse(Text);
}
else if(FieldType == typeof(float))
{
return Single.Parse(Text);
}
else if(FieldType == typeof(double))
{
return Double.Parse(Text);
}
else if(FieldType.IsEnum)
{
return Enum.Parse(FieldType, Text);
}
else
{
throw new Exception(String.Format("Unsupported config type '{0}'", FieldType.Name));
}
}
/// <summary>
/// Checks that the given cache file exists and is newer than the given input files, and attempts to read it. Verifies that the resulting cache was created
/// from the same input files in the same order.
/// </summary>
/// <param name="CacheFile">Path to the config cache file</param>
/// <param name="InputFiles">The expected set of input files in the cache</param>
/// <returns>True if the cache was valid and could be read, false otherwise.</returns>
static bool IsCacheUpToDate(FileReference CacheFile, FileReference[] InputFiles)
{
// Always rebuild if the cache doesn't exist
if(!FileReference.Exists(CacheFile))
{
return false;
}
// Get the timestamp for the cache
DateTime CacheWriteTime = File.GetLastWriteTimeUtc(CacheFile.FullName);
// Always rebuild if this executable is newer
if(File.GetLastWriteTimeUtc(Assembly.GetExecutingAssembly().Location) > CacheWriteTime)
{
return false;
}
// Check if any of the input files are newer than the cache
foreach(FileReference InputFile in InputFiles)
{
if(File.GetLastWriteTimeUtc(InputFile.FullName) > CacheWriteTime)
{
return false;
}
}
// Otherwise, it's up to date
return true;
}
/// <summary>
/// Generates documentation files for the available settings, by merging the XML documentation from the compiler.
/// </summary>
/// <param name="OutputFile">The documentation file to write</param>
public static void WriteDocumentation(FileReference OutputFile)
{
// Find all the configurable types
List<Type> ConfigTypes = FindConfigurableTypes();
// Find all the configurable fields from the given types
Dictionary<string, Dictionary<string, FieldInfo>> CategoryToFields = new Dictionary<string, Dictionary<string, FieldInfo>>();
FindConfigurableFields(ConfigTypes, CategoryToFields);
// Get the path to the XML documentation
FileReference InputDocumentationFile = new FileReference(Assembly.GetExecutingAssembly().Location).ChangeExtension(".xml");
if(!FileReference.Exists(InputDocumentationFile))
{
throw new BuildException("Generated assembly documentation not found at {0}.", InputDocumentationFile);
}
// Get the current engine version for versioning the page
BuildVersion Version;
if(!BuildVersion.TryRead(out Version))
{
throw new BuildException("Unable to read the current build version");
}
// Read the documentation
XmlDocument InputDocumentation = new XmlDocument();
InputDocumentation.Load(InputDocumentationFile.FullName);
// Make sure we can write to the output file
FileReference.MakeWriteable(OutputFile);
// Generate the UDN documentation file
using (StreamWriter Writer = new StreamWriter(OutputFile.FullName))
{
Writer.WriteLine("Availability: NoPublish");
Writer.WriteLine("Title: Build Configuration Properties Page");
Writer.WriteLine("Crumbs:");
Writer.WriteLine("Description: This is a procedurally generated markdown page.");
Writer.WriteLine("Version: {0}.{1}", Version.MajorVersion, Version.MinorVersion);
Writer.WriteLine("");
foreach(KeyValuePair<string, Dictionary<string, FieldInfo>> CategoryPair in CategoryToFields)
{
string CategoryName = CategoryPair.Key;
Writer.WriteLine("### {0}", CategoryName);
Writer.WriteLine();
Dictionary<string, FieldInfo> Fields = CategoryPair.Value;
foreach(KeyValuePair<string, FieldInfo> FieldPair in Fields)
{
string FieldName = FieldPair.Key;
FieldInfo Field = FieldPair.Value;
XmlNode Node = InputDocumentation.SelectSingleNode(String.Format("//member[@name='F:{0}.{1}']/summary", Field.DeclaringType.FullName, Field.Name));
if(Node != null)
{
// Reflow the comments into paragraphs, assuming that each paragraph will be separated by a blank line
List<string> Lines = new List<string>(Node.InnerText.Trim().Split('\n').Select(x => x.Trim()));
for(int Idx = Lines.Count - 1; Idx > 0; Idx--)
{
if(Lines[Idx - 1].Length > 0 && !Lines[Idx].StartsWith("*") && !Lines[Idx].StartsWith("-"))
{
Lines[Idx - 1] += " " + Lines[Idx];
Lines.RemoveAt(Idx);
}
}
// Write the result to the .udn file
if(Lines.Count > 0)
{
Writer.WriteLine("$ {0} : {1}", FieldName, Lines[0]);
for(int Idx = 1; Idx < Lines.Count; Idx++)
{
if(Lines[Idx].StartsWith("*") || Lines[Idx].StartsWith("-"))
{
Writer.WriteLine(" * {0}", Lines[Idx].Substring(1).TrimStart());
}
else
{
Writer.WriteLine(" * {0}", Lines[Idx]);
}
}
Writer.WriteLine();
}
}
}
}
}
// Success!
Log.TraceInformation("Written documentation to {0}.", OutputFile);
}
}
}