You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.8 KiB

  1. // Copyright (c) 2022 Tom Englert
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. #nullable enable
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Reflection.Metadata;
  23. using ICSharpCode.Decompiler.IL;
  24. using ICSharpCode.Decompiler.Metadata;
  25. namespace ICSharpCode.Decompiler.Disassembler
  26. {
  27. public class SortByNameProcessor : IEntityProcessor
  28. {
  29. public IReadOnlyCollection<InterfaceImplementationHandle> Process(MetadataFile module,
  30. IReadOnlyCollection<InterfaceImplementationHandle> items)
  31. {
  32. return items.OrderBy(item => GetSortKey(item, module)).ToArray();
  33. }
  34. public IReadOnlyCollection<TypeDefinitionHandle> Process(MetadataFile module,
  35. IReadOnlyCollection<TypeDefinitionHandle> items)
  36. {
  37. return items.OrderBy(item => GetSortKey(item, module)).ToArray();
  38. }
  39. public IReadOnlyCollection<MethodDefinitionHandle> Process(MetadataFile module,
  40. IReadOnlyCollection<MethodDefinitionHandle> items)
  41. {
  42. return items.OrderBy(item => GetSortKey(item, module)).ToArray();
  43. }
  44. public IReadOnlyCollection<PropertyDefinitionHandle> Process(MetadataFile module,
  45. IReadOnlyCollection<PropertyDefinitionHandle> items)
  46. {
  47. return items.OrderBy(item => GetSortKey(item, module)).ToArray();
  48. }
  49. public IReadOnlyCollection<EventDefinitionHandle> Process(MetadataFile module,
  50. IReadOnlyCollection<EventDefinitionHandle> items)
  51. {
  52. return items.OrderBy(item => GetSortKey(item, module)).ToArray();
  53. }
  54. public IReadOnlyCollection<FieldDefinitionHandle> Process(MetadataFile module,
  55. IReadOnlyCollection<FieldDefinitionHandle> items)
  56. {
  57. return items.OrderBy(item => GetSortKey(item, module)).ToArray();
  58. }
  59. public IReadOnlyCollection<CustomAttributeHandle> Process(MetadataFile module,
  60. IReadOnlyCollection<CustomAttributeHandle> items)
  61. {
  62. return items.OrderBy(item => GetSortKey(item, module)).ToArray();
  63. }
  64. private static string GetSortKey(TypeDefinitionHandle handle, MetadataFile module) =>
  65. handle.GetFullTypeName(module.Metadata).ToILNameString();
  66. private static string GetSortKey(MethodDefinitionHandle handle, MetadataFile module)
  67. {
  68. PlainTextOutput output = new PlainTextOutput();
  69. MethodDefinition definition = module.Metadata.GetMethodDefinition(handle);
  70. // Start with the methods name, skip return type
  71. output.Write(module.Metadata.GetString(definition.Name));
  72. DisassemblerSignatureTypeProvider signatureProvider = new DisassemblerSignatureTypeProvider(module, output);
  73. MethodSignature<Action<ILNameSyntax>> signature =
  74. definition.DecodeSignature(signatureProvider, new MetadataGenericContext(handle, module));
  75. if (signature.GenericParameterCount > 0)
  76. {
  77. output.Write($"`{signature.GenericParameterCount}");
  78. }
  79. InstructionOutputExtensions.WriteParameterList(output, signature);
  80. return output.ToString();
  81. }
  82. private static string GetSortKey(InterfaceImplementationHandle handle, MetadataFile module) =>
  83. module.Metadata.GetInterfaceImplementation(handle)
  84. .Interface
  85. .GetFullTypeName(module.Metadata)
  86. .ToILNameString();
  87. private static string GetSortKey(FieldDefinitionHandle handle, MetadataFile module) =>
  88. module.Metadata.GetString(module.Metadata.GetFieldDefinition(handle).Name);
  89. private static string GetSortKey(PropertyDefinitionHandle handle, MetadataFile module) =>
  90. module.Metadata.GetString(module.Metadata.GetPropertyDefinition(handle).Name);
  91. private static string GetSortKey(EventDefinitionHandle handle, MetadataFile module) =>
  92. module.Metadata.GetString(module.Metadata.GetEventDefinition(handle).Name);
  93. private static string GetSortKey(CustomAttributeHandle handle, MetadataFile module) =>
  94. module.Metadata.GetCustomAttribute(handle)
  95. .Constructor
  96. .GetDeclaringType(module.Metadata)
  97. .GetFullTypeName(module.Metadata)
  98. .ToILNameString();
  99. }
  100. }