From c66f63805a6d286841419fdc405861014df80b5d Mon Sep 17 00:00:00 2001 From: Pavel Penev Date: Tue, 7 Jan 2025 23:29:28 +0200 Subject: [PATCH] Added types for Utility documentation output --- .../ExtractSpriteSequenceDocsCommand.cs | 21 ++++++------- .../Documentation/ExtractTraitDocsCommand.cs | 25 +++++++--------- .../Documentation/ExtractWeaponDocsCommand.cs | 25 +++++++--------- .../ExtractedClassFieldAttributeInfo.cs | 29 ++++++++++++++++++ .../Objects/ExtractedClassFieldInfo.cs | 30 +++++++++++++++++++ .../Objects/ExtractedClassInfo.cs | 30 +++++++++++++++++++ .../Objects/ExtractedEnumInfo.cs | 24 +++++++++++++++ .../Objects/ExtractedTraitInfo.cs | 20 +++++++++++++ 8 files changed, 164 insertions(+), 40 deletions(-) create mode 100644 OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldAttributeInfo.cs create mode 100644 OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldInfo.cs create mode 100644 OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassInfo.cs create mode 100644 OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedEnumInfo.cs create mode 100644 OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedTraitInfo.cs diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs index 9edeabf4ed..319a66ca5a 100644 --- a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs @@ -17,6 +17,7 @@ using System.Reflection; using Newtonsoft.Json; using OpenRA.Graphics; using OpenRA.Mods.Common.Graphics; +using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects; using OpenRA.Primitives; namespace OpenRA.Mods.Common.UtilityCommands.Documentation @@ -54,10 +55,10 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation var sequenceTypesInfo = sequenceTypes .Where(x => !x.ContainsGenericParameters && !x.IsAbstract) - .Select(type => new + .Select(type => new ExtractedClassInfo { - type.Namespace, - type.Name, + Namespace = type.Namespace, + Name = type.Name, Description = string.Join(" ", Utility.GetCustomAttributes(type, false).SelectMany(d => d.Lines)), Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache), InheritedTypes = type.BaseTypes() @@ -82,7 +83,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation if (defaultValueField != null && defaultValueField.FieldType.IsEnum) relatedEnumTypes.Add(defaultValueField.FieldType); - return new + return new ExtractedClassFieldInfo { PropertyName = key, DefaultValue = defaultValue?.ToString(), @@ -93,15 +94,11 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation }) }); - var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new + var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new ExtractedEnumInfo { - type.Namespace, - type.Name, - Values = Enum.GetNames(type).Select(x => new - { - Key = Convert.ToInt32(Enum.Parse(type, x), NumberFormatInfo.InvariantInfo), - Value = x - }) + 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 diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs index 16562ea2e1..1cdb27e9f5 100644 --- a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractTraitDocsCommand.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using Newtonsoft.Json; +using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects; using OpenRA.Primitives; using OpenRA.Traits; @@ -52,9 +53,9 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation var traitTypesInfo = traitTypes .Where(x => !x.ContainsGenericParameters && !x.IsAbstract) - .Select(type => new + .Select(type => new ExtractedTraitInfo { - type.Namespace, + 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), @@ -70,7 +71,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation if (fi.Field.FieldType.IsEnum) relatedEnumTypes.Add(fi.Field.FieldType); - return new + return new ExtractedClassFieldInfo { PropertyName = fi.YamlName, DefaultValue = FieldSaver.SaveField(objectCreator.CreateBasic(type), fi.Field.Name).Value.Value, @@ -84,13 +85,13 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation var name = a.AttributeType.Name; name = name.EndsWith("Attribute", StringComparison.Ordinal) ? name[..^9] : name; - return new + return new ExtractedClassFieldAttributeInfo { Name = name, Parameters = a.Constructor.GetParameters() - .Select(pi => new + .Select(pi => new ExtractedClassFieldAttributeInfo.Parameter { - pi.Name, + Name = pi.Name, Value = Util.GetAttributeParameterValue(a.ConstructorArguments[pi.Position]) }) }; @@ -99,15 +100,11 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation }) }); - var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new + var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new ExtractedEnumInfo { - type.Namespace, - type.Name, - Values = Enum.GetNames(type).Select(x => new - { - Key = Convert.ToInt32(Enum.Parse(type, x), NumberFormatInfo.InvariantInfo), - Value = x - }) + 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 diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs index 925444c261..b4e43a0fd3 100644 --- a/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractWeaponDocsCommand.cs @@ -15,6 +15,7 @@ using System.Globalization; using System.Linq; using Newtonsoft.Json; using OpenRA.GameRules; +using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects; using OpenRA.Primitives; using OpenRA.Traits; @@ -57,9 +58,9 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation var weaponTypesInfo = weaponTypes .Where(x => !x.ContainsGenericParameters && !x.IsAbstract) - .Select(type => new + .Select(type => new ExtractedClassInfo { - type.Namespace, + 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), @@ -73,7 +74,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation if (fi.Field.FieldType.IsEnum) relatedEnumTypes.Add(fi.Field.FieldType); - return new + return new ExtractedClassFieldInfo { PropertyName = fi.YamlName, DefaultValue = FieldSaver.SaveField(objectCreator.CreateBasic(type), fi.Field.Name).Value.Value, @@ -87,13 +88,13 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation var name = a.AttributeType.Name; name = name.EndsWith("Attribute", StringComparison.Ordinal) ? name[..^9] : name; - return new + return new ExtractedClassFieldAttributeInfo { Name = name, Parameters = a.Constructor.GetParameters() - .Select(pi => new + .Select(pi => new ExtractedClassFieldAttributeInfo.Parameter { - pi.Name, + Name = pi.Name, Value = Util.GetAttributeParameterValue(a.ConstructorArguments[pi.Position]) }) }; @@ -102,15 +103,11 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation }) }); - var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new + var relatedEnums = relatedEnumTypes.OrderBy(t => t.Name).Select(type => new ExtractedEnumInfo { - type.Namespace, - type.Name, - Values = Enum.GetNames(type).Select(x => new - { - Key = Convert.ToInt32(Enum.Parse(type, x), NumberFormatInfo.InvariantInfo), - Value = x - }) + 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 diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldAttributeInfo.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldAttributeInfo.cs new file mode 100644 index 0000000000..a3f6306fb9 --- /dev/null +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldAttributeInfo.cs @@ -0,0 +1,29 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Collections.Generic; + +namespace OpenRA.Mods.Common.UtilityCommands.Documentation.Objects +{ + public class ExtractedClassFieldAttributeInfo + { + public string Name { get; set; } + + public IEnumerable Parameters { get; set; } + + public class Parameter + { + public string Name { get; set; } + + public string Value { get; set; } + } + } +} diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldInfo.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldInfo.cs new file mode 100644 index 0000000000..1cb3892458 --- /dev/null +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassFieldInfo.cs @@ -0,0 +1,30 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Collections.Generic; + +namespace OpenRA.Mods.Common.UtilityCommands.Documentation.Objects +{ + public class ExtractedClassFieldInfo + { + public string PropertyName { get; set; } + + public string DefaultValue { get; set; } + + public string InternalType { get; set; } + + public string UserFriendlyType { get; set; } + + public string Description { get; set; } + + public IEnumerable OtherAttributes { get; set; } + } +} diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassInfo.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassInfo.cs new file mode 100644 index 0000000000..979c3a0c14 --- /dev/null +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedClassInfo.cs @@ -0,0 +1,30 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Collections.Generic; + +namespace OpenRA.Mods.Common.UtilityCommands.Documentation.Objects +{ + public class ExtractedClassInfo + { + public string Namespace { get; set; } + + public string Name { get; set; } + + public string Filename { get; set; } + + public string Description { get; set; } + + public IEnumerable InheritedTypes { get; set; } + + public IEnumerable Properties { get; set; } + } +} diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedEnumInfo.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedEnumInfo.cs new file mode 100644 index 0000000000..0d79a5fa5b --- /dev/null +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedEnumInfo.cs @@ -0,0 +1,24 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Collections.Generic; + +namespace OpenRA.Mods.Common.UtilityCommands.Documentation.Objects +{ + public record ExtractedEnumInfo + { + public string Namespace { get; set; } + + public string Name { get; set; } + + public IDictionary Values { get; set; } + } +} diff --git a/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedTraitInfo.cs b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedTraitInfo.cs new file mode 100644 index 0000000000..758260bfeb --- /dev/null +++ b/OpenRA.Mods.Common/UtilityCommands/Documentation/Objects/ExtractedTraitInfo.cs @@ -0,0 +1,20 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Collections.Generic; + +namespace OpenRA.Mods.Common.UtilityCommands.Documentation.Objects +{ + public class ExtractedTraitInfo : ExtractedClassInfo + { + public IEnumerable RequiresTraits { get; set; } + } +}