From dfa922de911fd93c0e932cbddce135f1e8211283 Mon Sep 17 00:00:00 2001 From: Pavel Penev Date: Wed, 8 Jan 2025 00:20:43 +0200 Subject: [PATCH] Pulled out shared code from documentation commands --- .../Documentation/DocumentationHelpers.cs | 59 +++++++++++++++ .../ExtractSpriteSequenceDocsCommand.cs | 19 +---- .../Documentation/ExtractTraitDocsCommand.cs | 74 +++++-------------- .../Documentation/ExtractWeaponDocsCommand.cs | 70 ++++-------------- 4 files changed, 97 insertions(+), 125 deletions(-) create mode 100644 OpenRA.Mods.Common/UtilityCommands/Documentation/DocumentationHelpers.cs diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/DocumentationHelpers.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/DocumentationHelpers.cs new file mode 100644 index 0000000000..1f0c3f83ce --- /dev/null +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/DocumentationHelpers.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects; + +namespace OpenRA.Mods.Common.UtilityCommands.Documentation +{ + public static class DocumentationHelpers + { + public static IEnumerable GetClassFieldInfos(Type type, IEnumerable fields, + HashSet relatedEnumTypes, ObjectCreator objectCreator) + { + return fields + .Select(fi => + { + if (fi.Field.FieldType.IsEnum) + relatedEnumTypes.Add(fi.Field.FieldType); + + return new ExtractedClassFieldInfo + { + PropertyName = fi.YamlName, + DefaultValue = FieldSaver.SaveField(objectCreator.CreateBasic(type), fi.Field.Name).Value.Value, + InternalType = Util.InternalTypeName(fi.Field.FieldType), + UserFriendlyType = Util.FriendlyTypeName(fi.Field.FieldType), + Description = string.Join(" ", Utility.GetCustomAttributes(fi.Field, true).SelectMany(d => d.Lines)), + OtherAttributes = fi.Field.CustomAttributes + .Where(a => a.AttributeType.Name != nameof(DescAttribute) && a.AttributeType.Name != nameof(FieldLoader.LoadUsingAttribute)) + .Select(a => + { + var name = a.AttributeType.Name; + name = name.EndsWith("Attribute", StringComparison.Ordinal) ? name[..^9] : name; + + return new ExtractedClassFieldAttributeInfo + { + Name = name, + Parameters = a.Constructor.GetParameters() + .Select(pi => new ExtractedClassFieldAttributeInfo.Parameter + { + Name = pi.Name, + Value = Util.GetAttributeParameterValue(a.ConstructorArguments[pi.Position]) + }) + }; + }) + }; + }); + } + + public static IEnumerable GetRelatedEnumInfos(HashSet relatedEnumTypes) + { + return relatedEnumTypes.OrderBy(t => t.Name).Select(type => new ExtractedEnumInfo + { + Namespace = type.Namespace, + Name = type.Name, + Values = Enum.GetNames(type).ToDictionary(x => Convert.ToInt32(Enum.Parse(type, x), NumberFormatInfo.InvariantInfo), y => y) + }); + } + } +} diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs index 319a66ca5a..863d43f8c5 100644 --- a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs @@ -11,7 +11,6 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Reflection; using Newtonsoft.Json; @@ -26,10 +25,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation { string IUtilityCommand.Name => "--sprite-sequence-docs"; - bool IUtilityCommand.ValidateArguments(string[] args) - { - return true; - } + bool IUtilityCommand.ValidateArguments(string[] args) => true; [Desc("[VERSION]", "Generate sprite sequence documentation in JSON format.")] void IUtilityCommand.Run(Utility utility, string[] args) @@ -59,8 +55,8 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation { Namespace = type.Namespace, Name = type.Name, - Description = string.Join(" ", Utility.GetCustomAttributes(type, false).SelectMany(d => d.Lines)), Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache), + Description = string.Join(" ", type.GetCustomAttributes(false).SelectMany(d => d.Lines)), InheritedTypes = type.BaseTypes() .Select(y => y.Name) .Where(y => y != type.Name && y != "Object"), @@ -68,7 +64,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation .Where(fi => fi.FieldType.IsGenericType && fi.FieldType.GetGenericTypeDefinition() == typeof(SpriteSequenceField<>)) .Select(fi => { - var description = string.Join(" ", Utility.GetCustomAttributes(fi, false) + var description = string.Join(" ", fi.GetCustomAttributes(false) .SelectMany(d => d.Lines)); var valueType = fi.FieldType.GetGenericArguments()[0]; @@ -94,18 +90,11 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation }) }); - var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new ExtractedEnumInfo - { - Namespace = type.Namespace, - Name = type.Name, - Values = Enum.GetNames(type).ToDictionary(x => Convert.ToInt32(Enum.Parse(type, x), NumberFormatInfo.InvariantInfo), y => y) - }); - var result = new { Version = version, SpriteSequenceTypes = sequenceTypesInfo, - RelatedEnums = relatedEnums + RelatedEnums = DocumentationHelpers.GetRelatedEnumInfos(relatedEnumTypes) }; return JsonConvert.SerializeObject(result); diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs index 1cdb27e9f5..ef47fb4724 100644 --- a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs @@ -11,7 +11,6 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using Newtonsoft.Json; using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects; @@ -24,10 +23,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation { string IUtilityCommand.Name => "--docs"; - bool IUtilityCommand.ValidateArguments(string[] args) - { - return true; - } + bool IUtilityCommand.ValidateArguments(string[] args) => true; [Desc("[VERSION]", "Generate trait documentation in JSON format.")] void IUtilityCommand.Run(Utility utility, string[] args) @@ -53,65 +49,31 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation var traitTypesInfo = traitTypes .Where(x => !x.ContainsGenericParameters && !x.IsAbstract) - .Select(type => new ExtractedTraitInfo + .Select(type => { - Namespace = type.Namespace, - Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name, - Description = string.Join(" ", Utility.GetCustomAttributes(type, false).SelectMany(d => d.Lines)), - Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache), - RequiresTraits = RequiredTraitTypes(type) - .Select(y => y.Name), - InheritedTypes = type.BaseTypes() - .Select(y => y.Name) - .Where(y => y != type.Name && y != $"{type.Name}Info" && y != "Object" && y != "TraitInfo`1"), // HACK: This is the simplest way to exclude TraitInfo, which doesn't serialize well. - Properties = FieldLoader.GetTypeLoadInfo(type) - .Where(fi => fi.Field.IsPublic && fi.Field.IsInitOnly && !fi.Field.IsStatic) - .Select(fi => - { - if (fi.Field.FieldType.IsEnum) - relatedEnumTypes.Add(fi.Field.FieldType); + var fields = FieldLoader.GetTypeLoadInfo(type) + .Where(fi => fi.Field.IsPublic && fi.Field.IsInitOnly && !fi.Field.IsStatic); - return new ExtractedClassFieldInfo - { - PropertyName = fi.YamlName, - DefaultValue = FieldSaver.SaveField(objectCreator.CreateBasic(type), fi.Field.Name).Value.Value, - InternalType = Util.InternalTypeName(fi.Field.FieldType), - UserFriendlyType = Util.FriendlyTypeName(fi.Field.FieldType), - Description = string.Join(" ", Utility.GetCustomAttributes(fi.Field, true).SelectMany(d => d.Lines)), - OtherAttributes = fi.Field.CustomAttributes - .Where(a => a.AttributeType.Name != nameof(DescAttribute) && a.AttributeType.Name != nameof(FieldLoader.LoadUsingAttribute)) - .Select(a => - { - var name = a.AttributeType.Name; - name = name.EndsWith("Attribute", StringComparison.Ordinal) ? name[..^9] : name; - - return new ExtractedClassFieldAttributeInfo - { - Name = name, - Parameters = a.Constructor.GetParameters() - .Select(pi => new ExtractedClassFieldAttributeInfo.Parameter - { - Name = pi.Name, - Value = Util.GetAttributeParameterValue(a.ConstructorArguments[pi.Position]) - }) - }; - }) - }; - }) + return new ExtractedTraitInfo + { + Namespace = type.Namespace, + Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name, + Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache), + Description = string.Join(" ", type.GetCustomAttributes(false).SelectMany(d => d.Lines)), + RequiresTraits = RequiredTraitTypes(type) + .Select(y => y.Name), + InheritedTypes = type.BaseTypes() + .Select(y => y.Name) + .Where(y => y != type.Name && y != $"{type.Name}Info" && y != "Object" && y != "TraitInfo`1"), // HACK: This is the simplest way to exclude TraitInfo, which doesn't serialize well. + Properties = DocumentationHelpers.GetClassFieldInfos(type, fields, relatedEnumTypes, objectCreator) + }; }); - var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new ExtractedEnumInfo - { - Namespace = type.Namespace, - Name = type.Name, - Values = Enum.GetNames(type).ToDictionary(x => Convert.ToInt32(Enum.Parse(type, x), NumberFormatInfo.InvariantInfo), y => y) - }); - var result = new { Version = version, TraitInfos = traitTypesInfo, - RelatedEnums = relatedEnums + RelatedEnums = DocumentationHelpers.GetRelatedEnumInfos(relatedEnumTypes) }; return JsonConvert.SerializeObject(result); diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs index b4e43a0fd3..7dac303a56 100644 --- a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs @@ -11,7 +11,6 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using Newtonsoft.Json; using OpenRA.GameRules; @@ -25,10 +24,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation { string IUtilityCommand.Name => "--weapon-docs"; - bool IUtilityCommand.ValidateArguments(string[] args) - { - return true; - } + bool IUtilityCommand.ValidateArguments(string[] args) => true; [Desc("[VERSION]", "Generate weaponry documentation in JSON format.")] void IUtilityCommand.Run(Utility utility, string[] args) @@ -58,63 +54,29 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation var weaponTypesInfo = weaponTypes .Where(x => !x.ContainsGenericParameters && !x.IsAbstract) - .Select(type => new ExtractedClassInfo + .Select(type => { - Namespace = type.Namespace, - Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name, - Description = string.Join(" ", Utility.GetCustomAttributes(type, false).SelectMany(d => d.Lines)), - Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache), - InheritedTypes = type.BaseTypes() - .Select(y => y.Name) - .Where(y => y != type.Name && y != $"{type.Name}Info" && y != "Object"), - Properties = FieldLoader.GetTypeLoadInfo(type) - .Where(fi => fi.Field.IsPublic && fi.Field.IsInitOnly && !fi.Field.IsStatic) - .Select(fi => - { - if (fi.Field.FieldType.IsEnum) - relatedEnumTypes.Add(fi.Field.FieldType); + var fields = FieldLoader.GetTypeLoadInfo(type) + .Where(fi => fi.Field.IsPublic && fi.Field.IsInitOnly && !fi.Field.IsStatic); - return new ExtractedClassFieldInfo - { - PropertyName = fi.YamlName, - DefaultValue = FieldSaver.SaveField(objectCreator.CreateBasic(type), fi.Field.Name).Value.Value, - InternalType = Util.InternalTypeName(fi.Field.FieldType), - UserFriendlyType = Util.FriendlyTypeName(fi.Field.FieldType), - Description = string.Join(" ", Utility.GetCustomAttributes(fi.Field, true).SelectMany(d => d.Lines)), - OtherAttributes = fi.Field.CustomAttributes - .Where(a => a.AttributeType.Name != nameof(DescAttribute) && a.AttributeType.Name != nameof(FieldLoader.LoadUsingAttribute)) - .Select(a => - { - var name = a.AttributeType.Name; - name = name.EndsWith("Attribute", StringComparison.Ordinal) ? name[..^9] : name; - - return new ExtractedClassFieldAttributeInfo - { - Name = name, - Parameters = a.Constructor.GetParameters() - .Select(pi => new ExtractedClassFieldAttributeInfo.Parameter - { - Name = pi.Name, - Value = Util.GetAttributeParameterValue(a.ConstructorArguments[pi.Position]) - }) - }; - }) - }; - }) + return new ExtractedClassInfo + { + Namespace = type.Namespace, + Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name, + Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache), + Description = string.Join(" ", type.GetCustomAttributes(false).SelectMany(d => d.Lines)), + InheritedTypes = type.BaseTypes() + .Select(y => y.Name) + .Where(y => y != type.Name && y != $"{type.Name}Info" && y != "Object"), + Properties = DocumentationHelpers.GetClassFieldInfos(type, fields, relatedEnumTypes, objectCreator) + }; }); - var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new ExtractedEnumInfo - { - Namespace = type.Namespace, - Name = type.Name, - Values = Enum.GetNames(type).ToDictionary(x => Convert.ToInt32(Enum.Parse(type, x), NumberFormatInfo.InvariantInfo), y => y) - }); - var result = new { Version = version, WeaponTypes = weaponTypesInfo, - RelatedEnums = relatedEnums + RelatedEnums = DocumentationHelpers.GetRelatedEnumInfos(relatedEnumTypes) }; return JsonConvert.SerializeObject(result);