Pulled out shared code from documentation commands
This commit is contained in:
@@ -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<ExtractedClassFieldInfo> GetClassFieldInfos(Type type, IEnumerable<FieldLoader.FieldLoadInfo> fields,
|
||||||
|
HashSet<Type> 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<DescAttribute>(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<ExtractedEnumInfo> GetRelatedEnumInfos(HashSet<Type> 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)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@@ -26,10 +25,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation
|
|||||||
{
|
{
|
||||||
string IUtilityCommand.Name => "--sprite-sequence-docs";
|
string IUtilityCommand.Name => "--sprite-sequence-docs";
|
||||||
|
|
||||||
bool IUtilityCommand.ValidateArguments(string[] args)
|
bool IUtilityCommand.ValidateArguments(string[] args) => true;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Desc("[VERSION]", "Generate sprite sequence documentation in JSON format.")]
|
[Desc("[VERSION]", "Generate sprite sequence documentation in JSON format.")]
|
||||||
void IUtilityCommand.Run(Utility utility, string[] args)
|
void IUtilityCommand.Run(Utility utility, string[] args)
|
||||||
@@ -59,8 +55,8 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation
|
|||||||
{
|
{
|
||||||
Namespace = type.Namespace,
|
Namespace = type.Namespace,
|
||||||
Name = type.Name,
|
Name = type.Name,
|
||||||
Description = string.Join(" ", Utility.GetCustomAttributes<DescAttribute>(type, false).SelectMany(d => d.Lines)),
|
|
||||||
Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache),
|
Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache),
|
||||||
|
Description = string.Join(" ", type.GetCustomAttributes<DescAttribute>(false).SelectMany(d => d.Lines)),
|
||||||
InheritedTypes = type.BaseTypes()
|
InheritedTypes = type.BaseTypes()
|
||||||
.Select(y => y.Name)
|
.Select(y => y.Name)
|
||||||
.Where(y => y != type.Name && y != "Object"),
|
.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<>))
|
.Where(fi => fi.FieldType.IsGenericType && fi.FieldType.GetGenericTypeDefinition() == typeof(SpriteSequenceField<>))
|
||||||
.Select(fi =>
|
.Select(fi =>
|
||||||
{
|
{
|
||||||
var description = string.Join(" ", Utility.GetCustomAttributes<DescAttribute>(fi, false)
|
var description = string.Join(" ", fi.GetCustomAttributes<DescAttribute>(false)
|
||||||
.SelectMany(d => d.Lines));
|
.SelectMany(d => d.Lines));
|
||||||
|
|
||||||
var valueType = fi.FieldType.GetGenericArguments()[0];
|
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
|
var result = new
|
||||||
{
|
{
|
||||||
Version = version,
|
Version = version,
|
||||||
SpriteSequenceTypes = sequenceTypesInfo,
|
SpriteSequenceTypes = sequenceTypesInfo,
|
||||||
RelatedEnums = relatedEnums
|
RelatedEnums = DocumentationHelpers.GetRelatedEnumInfos(relatedEnumTypes)
|
||||||
};
|
};
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(result);
|
return JsonConvert.SerializeObject(result);
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects;
|
using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects;
|
||||||
@@ -24,10 +23,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation
|
|||||||
{
|
{
|
||||||
string IUtilityCommand.Name => "--docs";
|
string IUtilityCommand.Name => "--docs";
|
||||||
|
|
||||||
bool IUtilityCommand.ValidateArguments(string[] args)
|
bool IUtilityCommand.ValidateArguments(string[] args) => true;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Desc("[VERSION]", "Generate trait documentation in JSON format.")]
|
[Desc("[VERSION]", "Generate trait documentation in JSON format.")]
|
||||||
void IUtilityCommand.Run(Utility utility, string[] args)
|
void IUtilityCommand.Run(Utility utility, string[] args)
|
||||||
@@ -53,65 +49,31 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation
|
|||||||
|
|
||||||
var traitTypesInfo = traitTypes
|
var traitTypesInfo = traitTypes
|
||||||
.Where(x => !x.ContainsGenericParameters && !x.IsAbstract)
|
.Where(x => !x.ContainsGenericParameters && !x.IsAbstract)
|
||||||
.Select(type => new ExtractedTraitInfo
|
.Select(type =>
|
||||||
{
|
{
|
||||||
Namespace = type.Namespace,
|
var fields = FieldLoader.GetTypeLoadInfo(type)
|
||||||
Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name,
|
.Where(fi => fi.Field.IsPublic && fi.Field.IsInitOnly && !fi.Field.IsStatic);
|
||||||
Description = string.Join(" ", Utility.GetCustomAttributes<DescAttribute>(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<T>, 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);
|
|
||||||
|
|
||||||
return new ExtractedClassFieldInfo
|
return new ExtractedTraitInfo
|
||||||
{
|
{
|
||||||
PropertyName = fi.YamlName,
|
Namespace = type.Namespace,
|
||||||
DefaultValue = FieldSaver.SaveField(objectCreator.CreateBasic(type), fi.Field.Name).Value.Value,
|
Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name,
|
||||||
InternalType = Util.InternalTypeName(fi.Field.FieldType),
|
Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache),
|
||||||
UserFriendlyType = Util.FriendlyTypeName(fi.Field.FieldType),
|
Description = string.Join(" ", type.GetCustomAttributes<DescAttribute>(false).SelectMany(d => d.Lines)),
|
||||||
Description = string.Join(" ", Utility.GetCustomAttributes<DescAttribute>(fi.Field, true).SelectMany(d => d.Lines)),
|
RequiresTraits = RequiredTraitTypes(type)
|
||||||
OtherAttributes = fi.Field.CustomAttributes
|
.Select(y => y.Name),
|
||||||
.Where(a => a.AttributeType.Name != nameof(DescAttribute) && a.AttributeType.Name != nameof(FieldLoader.LoadUsingAttribute))
|
InheritedTypes = type.BaseTypes()
|
||||||
.Select(a =>
|
.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<T>, which doesn't serialize well.
|
||||||
var name = a.AttributeType.Name;
|
Properties = DocumentationHelpers.GetClassFieldInfos(type, fields, relatedEnumTypes, objectCreator)
|
||||||
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])
|
|
||||||
})
|
|
||||||
};
|
|
||||||
})
|
|
||||||
};
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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
|
var result = new
|
||||||
{
|
{
|
||||||
Version = version,
|
Version = version,
|
||||||
TraitInfos = traitTypesInfo,
|
TraitInfos = traitTypesInfo,
|
||||||
RelatedEnums = relatedEnums
|
RelatedEnums = DocumentationHelpers.GetRelatedEnumInfos(relatedEnumTypes)
|
||||||
};
|
};
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(result);
|
return JsonConvert.SerializeObject(result);
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using OpenRA.GameRules;
|
using OpenRA.GameRules;
|
||||||
@@ -25,10 +24,7 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation
|
|||||||
{
|
{
|
||||||
string IUtilityCommand.Name => "--weapon-docs";
|
string IUtilityCommand.Name => "--weapon-docs";
|
||||||
|
|
||||||
bool IUtilityCommand.ValidateArguments(string[] args)
|
bool IUtilityCommand.ValidateArguments(string[] args) => true;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Desc("[VERSION]", "Generate weaponry documentation in JSON format.")]
|
[Desc("[VERSION]", "Generate weaponry documentation in JSON format.")]
|
||||||
void IUtilityCommand.Run(Utility utility, string[] args)
|
void IUtilityCommand.Run(Utility utility, string[] args)
|
||||||
@@ -58,63 +54,29 @@ namespace OpenRA.Mods.Common.UtilityCommands.Documentation
|
|||||||
|
|
||||||
var weaponTypesInfo = weaponTypes
|
var weaponTypesInfo = weaponTypes
|
||||||
.Where(x => !x.ContainsGenericParameters && !x.IsAbstract)
|
.Where(x => !x.ContainsGenericParameters && !x.IsAbstract)
|
||||||
.Select(type => new ExtractedClassInfo
|
.Select(type =>
|
||||||
{
|
{
|
||||||
Namespace = type.Namespace,
|
var fields = FieldLoader.GetTypeLoadInfo(type)
|
||||||
Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name,
|
.Where(fi => fi.Field.IsPublic && fi.Field.IsInitOnly && !fi.Field.IsStatic);
|
||||||
Description = string.Join(" ", Utility.GetCustomAttributes<DescAttribute>(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);
|
|
||||||
|
|
||||||
return new ExtractedClassFieldInfo
|
return new ExtractedClassInfo
|
||||||
{
|
{
|
||||||
PropertyName = fi.YamlName,
|
Namespace = type.Namespace,
|
||||||
DefaultValue = FieldSaver.SaveField(objectCreator.CreateBasic(type), fi.Field.Name).Value.Value,
|
Name = type.Name.EndsWith("Info", StringComparison.Ordinal) ? type.Name[..^4] : type.Name,
|
||||||
InternalType = Util.InternalTypeName(fi.Field.FieldType),
|
Filename = Utilities.GetSourceFilenameFromPdb(type, pdbReaderCache),
|
||||||
UserFriendlyType = Util.FriendlyTypeName(fi.Field.FieldType),
|
Description = string.Join(" ", type.GetCustomAttributes<DescAttribute>(false).SelectMany(d => d.Lines)),
|
||||||
Description = string.Join(" ", Utility.GetCustomAttributes<DescAttribute>(fi.Field, true).SelectMany(d => d.Lines)),
|
InheritedTypes = type.BaseTypes()
|
||||||
OtherAttributes = fi.Field.CustomAttributes
|
.Select(y => y.Name)
|
||||||
.Where(a => a.AttributeType.Name != nameof(DescAttribute) && a.AttributeType.Name != nameof(FieldLoader.LoadUsingAttribute))
|
.Where(y => y != type.Name && y != $"{type.Name}Info" && y != "Object"),
|
||||||
.Select(a =>
|
Properties = DocumentationHelpers.GetClassFieldInfos(type, fields, relatedEnumTypes, objectCreator)
|
||||||
{
|
};
|
||||||
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])
|
|
||||||
})
|
|
||||||
};
|
|
||||||
})
|
|
||||||
};
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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
|
var result = new
|
||||||
{
|
{
|
||||||
Version = version,
|
Version = version,
|
||||||
WeaponTypes = weaponTypesInfo,
|
WeaponTypes = weaponTypesInfo,
|
||||||
RelatedEnums = relatedEnums
|
RelatedEnums = DocumentationHelpers.GetRelatedEnumInfos(relatedEnumTypes)
|
||||||
};
|
};
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(result);
|
return JsonConvert.SerializeObject(result);
|
||||||
|
|||||||
Reference in New Issue
Block a user