|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel.DataAnnotations; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using YamlDotNet.Core; |
| 8 | +using YamlDotNet.Core.Events; |
| 9 | +using YamlDotNet.Serialization; |
| 10 | + |
| 11 | +namespace EatPdb { |
| 12 | +#nullable enable |
| 13 | + public class Config { |
| 14 | + public interface IFilter { |
| 15 | + public bool Filter(bool isCode, string name); |
| 16 | + } |
| 17 | + public interface IFilterData : IFilter { } |
| 18 | + public class IsCodeFilter : IFilterData { |
| 19 | + public IsCodeFilter(bool isCode) => IsCode = isCode; |
| 20 | + |
| 21 | + public bool IsCode { get; set; } |
| 22 | + |
| 23 | + public bool Filter(bool isCode, string name) => isCode == IsCode; |
| 24 | + } |
| 25 | + public class FullNameFilter : IFilterData { |
| 26 | + public FullNameFilter(string name) => Name = name; |
| 27 | + |
| 28 | + public string Name { get; set; } = ""; |
| 29 | + |
| 30 | + public bool Filter(bool isCode, string name) => name == Name; |
| 31 | + } |
| 32 | + public class PrefixFilter : IFilterData { |
| 33 | + public PrefixFilter(string prefix) => Prefix = prefix; |
| 34 | + |
| 35 | + public string Prefix { get; set; } = ""; |
| 36 | + public bool Filter(bool isCode, string name) => name.StartsWith(Prefix); |
| 37 | + } |
| 38 | + public class RegexFilter : IFilterData { |
| 39 | + public RegexFilter(string pattern) => regex = new Regex(pattern, RegexOptions.Compiled); |
| 40 | + |
| 41 | + public Regex regex; |
| 42 | + public bool Filter(bool isCode, string name) => regex.IsMatch(name); |
| 43 | + } |
| 44 | + public class WhitelistFilter : IFilter { |
| 45 | + public WhitelistFilter(IEnumerable<IFilterData>? filters) => Filters = filters; |
| 46 | + |
| 47 | + public IEnumerable<IFilterData>? Filters { get; set; } |
| 48 | + public bool Filter(bool isCode, string name) => Filters == null ? false : Filters.Select(filter => filter.Filter(isCode, name)).Contains(true); |
| 49 | + } |
| 50 | + public class BlacklistFilter : IFilter { |
| 51 | + public BlacklistFilter(IEnumerable<IFilterData>? filters) => Filters = filters; |
| 52 | + |
| 53 | + public IEnumerable<IFilterData>? Filters { get; set; } |
| 54 | + public bool Filter(bool isCode, string name) => Filters == null ? true : !Filters.Select(filter => filter.Filter(isCode, name)).Contains(true); |
| 55 | + } |
| 56 | + public class YamlTypeConverter : IYamlTypeConverter { |
| 57 | + public bool Accepts(Type type) => type == typeof(WhitelistFilter) || type == typeof(BlacklistFilter); |
| 58 | + public object? ReadYaml(IParser parser, Type type) { |
| 59 | + var data = new List<IFilterData>(); |
| 60 | + parser.Consume<SequenceStart>(); |
| 61 | + while (!parser.TryConsume<SequenceEnd>(out var _)) { |
| 62 | + if (parser.TryConsume<Scalar>(out var scalar)) { |
| 63 | + data.Add(new FullNameFilter(scalar.Value)); |
| 64 | + } else if (parser.TryConsume<MappingStart>(out var _)) { |
| 65 | + if (parser.TryConsume<Scalar>(out var key)) { |
| 66 | + var value = parser.Consume<Scalar>(); |
| 67 | + switch (key.Value) { |
| 68 | + case "is_code": |
| 69 | + data.Add(new IsCodeFilter(bool.Parse(value.Value))); |
| 70 | + break; |
| 71 | + case "full": |
| 72 | + case "name": |
| 73 | + data.Add(new FullNameFilter(value.Value)); |
| 74 | + break; |
| 75 | + case "prefix": |
| 76 | + data.Add(new PrefixFilter(value.Value)); |
| 77 | + break; |
| 78 | + case "regex": |
| 79 | + case "pattern": |
| 80 | + data.Add(new RegexFilter(value.Value)); |
| 81 | + break; |
| 82 | + default: |
| 83 | + throw new NotImplementedException("Unknown key: " + value.Value); |
| 84 | + } |
| 85 | + } |
| 86 | + parser.Consume<MappingEnd>(); |
| 87 | + } else { |
| 88 | + throw new NotImplementedException("Unknown list"); |
| 89 | + } |
| 90 | + } |
| 91 | + if (type == typeof(WhitelistFilter)) { |
| 92 | + return new WhitelistFilter(data); |
| 93 | + } else if (type == typeof(BlacklistFilter)) { |
| 94 | + return new BlacklistFilter(data); |
| 95 | + } |
| 96 | + throw new NotImplementedException(); |
| 97 | + } |
| 98 | + public void WriteYaml(IEmitter emitter, object? value, Type type) => throw new NotImplementedException(); |
| 99 | + } |
| 100 | + [Required, YamlMember(Alias = "in")] |
| 101 | + public string InputFile { get; set; } = ""; |
| 102 | + [Required, YamlMember(Alias = "out")] |
| 103 | + public string OutputFile { get; set; } = ""; |
| 104 | + [YamlMember(Alias = "pdb")] |
| 105 | + public string PdbFile { get; set; } = ""; |
| 106 | + [YamlMember(Alias = "def")] |
| 107 | + public string DefFile { get; set; } = ""; |
| 108 | + [YamlMember(Alias = "dll_name")] |
| 109 | + public string DllName { get; set; } = ""; |
| 110 | + [YamlMember(Alias = "filter")] |
| 111 | + public IFilter? Filter { get; set; } |
| 112 | + |
| 113 | + public void ApplyDefault() { |
| 114 | + if (PdbFile == "") |
| 115 | + PdbFile = Path.Join(Path.GetDirectoryName(InputFile), Path.GetFileNameWithoutExtension(InputFile) + ".pdb"); |
| 116 | + if (DefFile == "") |
| 117 | + DefFile = Path.Join(Path.GetDirectoryName(OutputFile), Path.GetFileNameWithoutExtension(OutputFile) + ".def"); |
| 118 | + if (DllName == "") |
| 119 | + DllName = Path.GetFileName(OutputFile); |
| 120 | + } |
| 121 | + } |
| 122 | +#nullable restore |
| 123 | +} |
0 commit comments