mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-03 08:14:28 -05:00 
			
		
		
		
	Fixed some crashes in response strings source generator, reorganized more submodules into their folders
This commit is contained in:
		@@ -1,7 +1,8 @@
 | 
				
			|||||||
using System.CodeDom.Compiler;
 | 
					#nullable enable
 | 
				
			||||||
using System.Collections;
 | 
					using System.CodeDom.Compiler;
 | 
				
			||||||
using System.Collections.Generic;
 | 
					using System.Collections.Generic;
 | 
				
			||||||
using System.Collections.Immutable;
 | 
					using System.Collections.Immutable;
 | 
				
			||||||
 | 
					using System.Collections.ObjectModel;
 | 
				
			||||||
using System.Diagnostics;
 | 
					using System.Diagnostics;
 | 
				
			||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
@@ -29,18 +30,34 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public class MethodModel
 | 
					    public class MethodModel
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public string Namespace { get; set; }
 | 
					        public string? Namespace { get; }
 | 
				
			||||||
        public IReadOnlyCollection<string> Classes { get; set; }
 | 
					        public IReadOnlyCollection<string> Classes { get; }
 | 
				
			||||||
        public string ReturnType { get; set; }
 | 
					        public string ReturnType { get; }
 | 
				
			||||||
        public string MethodName { get; set; }
 | 
					        public string MethodName { get; }
 | 
				
			||||||
        public IEnumerable<string> Params { get; set; }
 | 
					        public IEnumerable<string> Params { get; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public MethodModel(string? ns, IReadOnlyCollection<string> classes, string returnType, string methodName, IEnumerable<string> @params)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            Namespace = ns;
 | 
				
			||||||
 | 
					            Classes = classes;
 | 
				
			||||||
 | 
					            ReturnType = returnType;
 | 
				
			||||||
 | 
					            MethodName = methodName;
 | 
				
			||||||
 | 
					            Params = @params;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public class FileModel
 | 
					    public class FileModel
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public string Namespace { get; set; }
 | 
					        public string? Namespace { get; }
 | 
				
			||||||
        public IReadOnlyCollection<string> ClassHierarchy { get; set; }
 | 
					        public IReadOnlyCollection<string> ClassHierarchy { get; }
 | 
				
			||||||
        public IReadOnlyCollection<MethodModel> Methods { get; set; }
 | 
					        public IReadOnlyCollection<MethodModel> Methods { get; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public FileModel(string? ns, IReadOnlyCollection<string> classHierarchy, IReadOnlyCollection<MethodModel> methods)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            Namespace = ns;
 | 
				
			||||||
 | 
					            ClassHierarchy = classHierarchy;
 | 
				
			||||||
 | 
					            Methods = methods;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void Initialize(IncrementalGeneratorInitializationContext context)
 | 
					    public void Initialize(IncrementalGeneratorInitializationContext context)
 | 
				
			||||||
@@ -53,13 +70,13 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
            .CreateSyntaxProvider(
 | 
					            .CreateSyntaxProvider(
 | 
				
			||||||
                static (node, _) => node is MethodDeclarationSyntax { AttributeLists.Count: > 0 },
 | 
					                static (node, _) => node is MethodDeclarationSyntax { AttributeLists.Count: > 0 },
 | 
				
			||||||
                static (ctx, cancel) => Transform(ctx, cancel))
 | 
					                static (ctx, cancel) => Transform(ctx, cancel))
 | 
				
			||||||
            .Where(static m => m != default)
 | 
					            .Where(static m => m is not null)
 | 
				
			||||||
            .Where(static m => m.ChildTokens().Any(static x => x.IsKind(SyntaxKind.PublicKeyword)));
 | 
					            .Where(static m => m!.ChildTokens().Any(static x => x.IsKind(SyntaxKind.PublicKeyword)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var compilationMethods = context.CompilationProvider.Combine(methods.Collect());
 | 
					        var compilationMethods = context.CompilationProvider.Combine(methods.Collect());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        context.RegisterSourceOutput(compilationMethods,
 | 
					        context.RegisterSourceOutput(compilationMethods,
 | 
				
			||||||
            static (ctx, tuple) => RegisterAction(in ctx, tuple.Left, in tuple.Right));
 | 
					            static (ctx, tuple) => RegisterAction(in ctx, tuple.Left, in tuple.Right!));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private static void RegisterAction(in SourceProductionContext ctx,
 | 
					    private static void RegisterAction(in SourceProductionContext ctx,
 | 
				
			||||||
@@ -137,8 +154,13 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
        
 | 
					        
 | 
				
			||||||
        tw.WriteLine("// <AutoGenerated />");
 | 
					        tw.WriteLine("// <AutoGenerated />");
 | 
				
			||||||
        tw.WriteLine("#pragma warning disable CS1066");
 | 
					        tw.WriteLine("#pragma warning disable CS1066");
 | 
				
			||||||
        tw.WriteLine($"namespace {model.Namespace};");
 | 
					
 | 
				
			||||||
        
 | 
					        if (model.Namespace is not null)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            tw.WriteLine($"namespace {model.Namespace};");
 | 
				
			||||||
 | 
					            tw.WriteLine();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach (var className in model.ClassHierarchy)
 | 
					        foreach (var className in model.ClassHierarchy)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            tw.WriteLine($"public partial class {className}");
 | 
					            tw.WriteLine($"public partial class {className}");
 | 
				
			||||||
@@ -176,13 +198,15 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
        
 | 
					        
 | 
				
			||||||
        foreach (var group in groups)
 | 
					        foreach (var group in groups)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            if (cancel.IsCancellationRequested)
 | 
				
			||||||
 | 
					                return new Collection<FileModel>();
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
            var elems = group.ToList();
 | 
					            var elems = group.ToList();
 | 
				
			||||||
            var model = new FileModel()
 | 
					            var model = new FileModel(
 | 
				
			||||||
            {
 | 
					                methods: elems,
 | 
				
			||||||
                Methods = elems,
 | 
					                ns: elems[0].Namespace,
 | 
				
			||||||
                Namespace = elems[0].Namespace,
 | 
					                classHierarchy: elems[0].Classes
 | 
				
			||||||
                ClassHierarchy = elems[0].Classes
 | 
					            );
 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            models.Add(model);
 | 
					            models.Add(model);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -195,53 +219,53 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        // SpinWait.SpinUntil(static () => Debugger.IsAttached);
 | 
					        // SpinWait.SpinUntil(static () => Debugger.IsAttached);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var methodModel = new MethodModel();
 | 
					 | 
				
			||||||
        var semanticModel = comp.GetSemanticModel(decl.SyntaxTree);
 | 
					        var semanticModel = comp.GetSemanticModel(decl.SyntaxTree);
 | 
				
			||||||
        methodModel.Params = decl.ParameterList.Parameters
 | 
					        var methodModel = new MethodModel(
 | 
				
			||||||
            .Select(p =>
 | 
					            @params: decl.ParameterList.Parameters
 | 
				
			||||||
            {
 | 
					                .Select(p =>
 | 
				
			||||||
                var prefix = p.Modifiers.Any(static x => x.IsKind(SyntaxKind.ParamsKeyword))
 | 
					 | 
				
			||||||
                    ? "params "
 | 
					 | 
				
			||||||
                    : string.Empty;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                var type = semanticModel
 | 
					 | 
				
			||||||
                    .GetTypeInfo(p.Type!)
 | 
					 | 
				
			||||||
                    .Type!
 | 
					 | 
				
			||||||
                    .ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                var name = p.Identifier.Text;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                var suffix = string.Empty;
 | 
					 | 
				
			||||||
                if (p.Default is not null)
 | 
					 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    if (p.Default.Value is LiteralExpressionSyntax)
 | 
					                    var prefix = p.Modifiers.Any(static x => x.IsKind(SyntaxKind.ParamsKeyword))
 | 
				
			||||||
 | 
					                        ? "params "
 | 
				
			||||||
 | 
					                        : string.Empty;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    var type = semanticModel
 | 
				
			||||||
 | 
					                        .GetTypeInfo(p.Type!)
 | 
				
			||||||
 | 
					                        .Type!
 | 
				
			||||||
 | 
					                        .ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    var name = p.Identifier.Text;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    var suffix = string.Empty;
 | 
				
			||||||
 | 
					                    if (p.Default is not null)
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        suffix = " = " + p.Default.Value;
 | 
					                        if (p.Default.Value is LiteralExpressionSyntax)
 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    else if (p.Default.Value is MemberAccessExpressionSyntax maes)
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        var maesSemModel = comp.GetSemanticModel(maes.SyntaxTree);
 | 
					 | 
				
			||||||
                        var sym = maesSemModel.GetSymbolInfo(maes.Name);
 | 
					 | 
				
			||||||
                        if (sym.Symbol is null)
 | 
					 | 
				
			||||||
                        {
 | 
					                        {
 | 
				
			||||||
                            suffix = " = " + p.Default.Value;
 | 
					                            suffix = " = " + p.Default.Value;
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        else
 | 
					                        else if (p.Default.Value is MemberAccessExpressionSyntax maes)
 | 
				
			||||||
                        {
 | 
					                        {
 | 
				
			||||||
                            suffix = " = " + sym.Symbol.ToDisplayString();
 | 
					                            var maesSemModel = comp.GetSemanticModel(maes.SyntaxTree);
 | 
				
			||||||
 | 
					                            var sym = maesSemModel.GetSymbolInfo(maes.Name);
 | 
				
			||||||
 | 
					                            if (sym.Symbol is null)
 | 
				
			||||||
 | 
					                            {
 | 
				
			||||||
 | 
					                                suffix = " = " + p.Default.Value;
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					                            else
 | 
				
			||||||
 | 
					                            {
 | 
				
			||||||
 | 
					                                suffix = " = " + sym.Symbol.ToDisplayString();
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return $"{prefix}{type} {name}{suffix}";
 | 
					                    return $"{prefix}{type} {name}{suffix}";
 | 
				
			||||||
            })
 | 
					                })
 | 
				
			||||||
            .ToList();
 | 
					                .ToList(),
 | 
				
			||||||
 | 
					            methodName: decl.Identifier.Text,
 | 
				
			||||||
        methodModel.MethodName = decl.Identifier.Text;
 | 
					            returnType: decl.ReturnType.ToString(),
 | 
				
			||||||
        methodModel.ReturnType = decl.ReturnType.ToString();
 | 
					            ns: GetNamespace(decl),
 | 
				
			||||||
        methodModel.Namespace = GetNamespace(decl);
 | 
					            classes: GetClasses(decl)
 | 
				
			||||||
        methodModel.Classes = GetClasses(decl);
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return methodModel;
 | 
					        return methodModel;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -266,10 +290,10 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
    // }
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //https://github.com/andrewlock/NetEscapades.EnumGenerators/blob/main/src/NetEscapades.EnumGenerators/EnumGenerator.cs
 | 
					    //https://github.com/andrewlock/NetEscapades.EnumGenerators/blob/main/src/NetEscapades.EnumGenerators/EnumGenerator.cs
 | 
				
			||||||
    static string GetNamespace(MethodDeclarationSyntax declarationSyntax)
 | 
					    static string? GetNamespace(MethodDeclarationSyntax declarationSyntax)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // determine the namespace the class is declared in, if any
 | 
					        // determine the namespace the class is declared in, if any
 | 
				
			||||||
        var nameSpace = string.Empty;
 | 
					        string? nameSpace = null;
 | 
				
			||||||
        var parentOfInterest = declarationSyntax.Parent;
 | 
					        var parentOfInterest = declarationSyntax.Parent;
 | 
				
			||||||
        while (parentOfInterest is not null)
 | 
					        while (parentOfInterest is not null)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -294,7 +318,7 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
            
 | 
					            
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        return default;
 | 
					        return nameSpace;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    static IReadOnlyCollection<string> GetClasses(MethodDeclarationSyntax declarationSyntax)
 | 
					    static IReadOnlyCollection<string> GetClasses(MethodDeclarationSyntax declarationSyntax)
 | 
				
			||||||
@@ -317,7 +341,7 @@ public class CmdAttribute : System.Attribute
 | 
				
			|||||||
        return classes;
 | 
					        return classes;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private static MethodDeclarationSyntax Transform(GeneratorSyntaxContext ctx, CancellationToken cancel)
 | 
					    private static MethodDeclarationSyntax? Transform(GeneratorSyntaxContext ctx, CancellationToken cancel)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        var methodDecl = (MethodDeclarationSyntax)ctx.Node;
 | 
					        var methodDecl = (MethodDeclarationSyntax)ctx.Node;
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
using System;
 | 
					#nullable enable
 | 
				
			||||||
 | 
					using System;
 | 
				
			||||||
using System.CodeDom.Compiler;
 | 
					using System.CodeDom.Compiler;
 | 
				
			||||||
using System.Collections.Generic;
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Diagnostics;
 | 
				
			||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
using System.Text.RegularExpressions;
 | 
					using System.Text.RegularExpressions;
 | 
				
			||||||
@@ -9,10 +11,16 @@ using Newtonsoft.Json;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace NadekoBot.Generators
 | 
					namespace NadekoBot.Generators
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    internal class TranslationPair
 | 
					    internal readonly struct TranslationPair
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public string Name { get; set; }
 | 
					        public string Name { get; }
 | 
				
			||||||
        public string Value { get; set; }
 | 
					        public string Value { get; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public TranslationPair(string name, string value)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            Name = name;
 | 
				
			||||||
 | 
					            Value = value;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [Generator]
 | 
					    [Generator]
 | 
				
			||||||
@@ -48,8 +56,7 @@ namespace NadekoBot.Generators
 | 
				
			|||||||
            using (var sw = new IndentedTextWriter(stringWriter))
 | 
					            using (var sw = new IndentedTextWriter(stringWriter))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                sw.WriteLine("namespace NadekoBot");
 | 
					                sw.WriteLine("namespace NadekoBot");
 | 
				
			||||||
                sw.WriteLine("{");
 | 
					                sw.WriteLine();
 | 
				
			||||||
                sw.Indent++;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                sw.WriteLine("public static class strs");
 | 
					                sw.WriteLine("public static class strs");
 | 
				
			||||||
                sw.WriteLine("{");
 | 
					                sw.WriteLine("{");
 | 
				
			||||||
@@ -82,8 +89,6 @@ namespace NadekoBot.Generators
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                sw.Indent--;
 | 
					                sw.Indent--;
 | 
				
			||||||
                sw.WriteLine("}");
 | 
					                sw.WriteLine("}");
 | 
				
			||||||
                sw.Indent--;
 | 
					 | 
				
			||||||
                sw.WriteLine("}");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                sw.Flush();
 | 
					                sw.Flush();
 | 
				
			||||||
@@ -93,21 +98,33 @@ namespace NadekoBot.Generators
 | 
				
			|||||||
            context.AddSource("LocStr.g.cs", LOC_STR_SOURCE);
 | 
					            context.AddSource("LocStr.g.cs", LOC_STR_SOURCE);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private List<TranslationPair> GetFields(string dataText)
 | 
					        private List<TranslationPair> GetFields(string? dataText)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (string.IsNullOrWhiteSpace(dataText))
 | 
					            if (string.IsNullOrWhiteSpace(dataText))
 | 
				
			||||||
                throw new ArgumentNullException(nameof(dataText));
 | 
					                return new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(dataText);
 | 
					            Dictionary<string, string> data;
 | 
				
			||||||
 | 
					            try
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                var output = JsonConvert.DeserializeObject<Dictionary<string, string>>(dataText!);
 | 
				
			||||||
 | 
					                if (output is null)
 | 
				
			||||||
 | 
					                    return new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                data = output;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            catch
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                Debug.WriteLine("Failed parsing responses file.");
 | 
				
			||||||
 | 
					                return new();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var list = new List<TranslationPair>();
 | 
					            var list = new List<TranslationPair>();
 | 
				
			||||||
            foreach (var entry in data)
 | 
					            foreach (var entry in data)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                list.Add(new TranslationPair()
 | 
					                list.Add(new(
 | 
				
			||||||
                {
 | 
					                    entry.Key,
 | 
				
			||||||
                    Name = entry.Key,
 | 
					                    entry.Value
 | 
				
			||||||
                    Value = entry.Value
 | 
					                ));
 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return list;
 | 
					            return list;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -200,7 +200,7 @@ public partial class Gambling : GamblingModule<GamblingService>
 | 
				
			|||||||
        if (--page < 0)
 | 
					        if (--page < 0)
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var trs = new List<CurrencyTransaction>();
 | 
					        List<CurrencyTransaction> trs;
 | 
				
			||||||
        await using (var uow = _db.GetDbContext())
 | 
					        await using (var uow = _db.GetDbContext())
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            trs = uow.CurrencyTransactions.GetPageFor(userId, page);
 | 
					            trs = uow.CurrencyTransactions.GetPageFor(userId, page);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1 +0,0 @@
 | 
				
			|||||||
#nullable disable
 | 
					 | 
				
			||||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user